Skip to content

Instantly share code, notes, and snippets.

@slior

slior/board.js Secret

Created November 17, 2020 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slior/716fd42081b028b5dc207f7264ee336b to your computer and use it in GitHub Desktop.
Save slior/716fd42081b028b5dc207f7264ee336b to your computer and use it in GitHub Desktop.
Mancala (044e2043a3987c33fd51047de9f1d9f399ad1875) -bug fix.
class Board
{
...
/**
* Retrieve the cell number for the home (Mancala) of the given player.
* @param {numer} player The number of the player whose Mancala we're seeking (1 or 2)
* @returns The cell number for the given player's Mancala ( either 0 or totalCellCount/2)
*/
homeOf(player)
{
requires(player == 1 || player == 2,"Player number can be either 1 or 2");
return player == 1 ? this.player1Home() : this.player2Home();
}
}
class MancalaGame
{
...
playCell(boardCell)
{
...
let targetCells = this._calculateTargetCellsForMove(boardCell);
...
}
_calculateTargetCellsForMove(fromCell)
{
let _ = this.board;
let stepCount = _.stonesIn(fromCell);
dbg("Playing " + stepCount + " stones from cell " + fromCell)
let targetCells = range(1,stepCount)
.map(steps => _.cellFrom(fromCell,steps,this.player.number))
.filter(c => c != _.homeOf(this.player.theOtherOne().number)) //remove, if applicable, the cell of the other player's mancala
while (targetCells.length < stepCount) //add any cells, until we reach a situation where we have enough holes to fill (per the stone count in the played cell)
{
let addedCell = _.cellFrom(targetCells[targetCells.length-1],1)
if (addedCell == _.homeOf(this.player.theOtherOne().number))
targetCells.push(_.cellFrom(addedCell,1))
else
targetCells.push(addedCell)
}
return targetCells;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment