Skip to content

Instantly share code, notes, and snippets.

@slior

slior/game.js Secret

Created November 11, 2020 13:19
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/66a9102debbfab4613b420db81eab2ea to your computer and use it in GitHub Desktop.
Save slior/66a9102debbfab4613b420db81eab2ea to your computer and use it in GitHub Desktop.
Mancala (38feec5b410f3e30e2701ed8889b1624f8457df0) - fixing a bug in the capturing
_checkAndCaptureIfNecessary(lastCell,lastCellWasEmpty)
{
let _ = this.board;
let isLastCellAHomeCell = _.isPlayer1Home(lastCell) || _.isPlayer2Home(lastCell);
let lastCellBelongsToCurrentPlayer = this.player1Playing(_.isPlayer1Cell(lastCell)) ||
this.player2Playing(_.isPlayer2Cell(lastCell))
if (lastCellWasEmpty && !isLastCellAHomeCell && lastCellBelongsToCurrentPlayer)
{ //capture the stones from the other player
let acrossCell = _.totalCellCount() - lastCell;
let targetHome = this.player == PLAYER.one ? _.player1Home() : _.player2Home();
let totalCapturedStones = _.stonesIn(lastCell) + _.stonesIn(acrossCell);
dbg("Capturing stones from " + acrossCell + " and " + lastCell + " to " + targetHome + ". Total: " + totalCapturedStones )
_.setCellStoneCount(targetHome,_.stonesIn(targetHome) + totalCapturedStones);
_.setCellStoneCount(acrossCell,0);
_.setCellStoneCount(lastCell,0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment