Skip to content

Instantly share code, notes, and snippets.

@slior

slior/game.js Secret

Created November 8, 2020 13:07
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/2284ff2f41360ca559603ee2f01f4349 to your computer and use it in GitHub Desktop.
Save slior/2284ff2f41360ca559603ee2f01f4349 to your computer and use it in GitHub Desktop.
Mancala (4247ec9a7e36bb31710aa52fb1349ed710ac154c) - adding game over
class MancalaGame
{
constructor(cnvsELID,_updatePlayerCallback,_showMsgCallback)
{
...
this.gameDone = false;
...
}
handleCellClick(boardCell)
{ //todo: clean this up
if (this.gameDone)
{
dbg("Game over, get outta here");
return;
}
...
let currentPlayerHasNoMoreMoves = this.player1Playing(this.board.allPlayer1Cells(c => this.board.stonesIn(c) <= 0)) ||
this.player2Playing(this.board.allPlayer2Cells(c => this.board.stonesIn(c) <= 0))
if (currentPlayerHasNoMoreMoves)
this.gameOver();
this.canvas.ifPresent(cnvs => {
drawBoardState(cnvs,this.board,this)
})
}
}
gameOver()
{
let player1StoneCount = this.board.player1StoneCount();
let player2StoneCount = this.board.player2StoneCount();
let a = ["Game Over","# Stones P1:" + player1StoneCount,"# Stones P2: " + player2StoneCount];
switch (true)
{
case player1StoneCount > player2StoneCount : a.push("Player 1 Wins!"); break;
case player2StoneCount > player1StoneCount : a.push("Player 2 Wins!"); break;
default : a.push("Draw!"); break;
}
this.showMsg(a.join("<br/>"));
this.setGameOver();
}
setGameOver() { this.gameDone = true; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment