-
-
Save slior/e4a45132f016f8c01f018a6c3540dbd2 to your computer and use it in GitHub Desktop.
Mancala (d106b4b026eb74242c60e0e210579889a5688597) - cleanup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MancalaGame | |
{ | |
constructor(cnvsELID,_updatePlayerCallback,_showMsgCallback) | |
{ | |
... | |
this._initializeBoardDrawing(); | |
} | |
_initializeBoardDrawing() | |
{ | |
this.canvas.ifPresent(cnvs => { | |
... | |
}) | |
} | |
handleCellClick(boardCell) | |
{ | |
if (!this.gameDone) | |
{ | |
this._resetGameMessagePanel(); | |
if (this.isValidMove(boardCell)) | |
this._makeMove(boardCell) | |
else | |
this.showMsg("Invalid Move") | |
} | |
else dbg("Game over, stop procrastinating") | |
} | |
_makeMove(boardCell) | |
{ | |
let lastCell = this.playCell(boardCell); | |
this._togglePlayerOrExtraTurn(lastCell) | |
this._checkAndDeclareGameOverIfNecessary() | |
this._redraw(); | |
} | |
_resetGameMessagePanel() | |
{ | |
this.showMsg(" ") | |
} | |
_togglePlayerOrExtraTurn(lastCell) | |
{ | |
let lastCellIsHomeOfCurrentPlayer = this.player1Playing(this.board.isPlayer1Home(lastCell)) || | |
this.player2Playing(this.board.isPlayer2Home(lastCell)) | |
if (!lastCellIsHomeOfCurrentPlayer) | |
this.updatePlayerCallback(this.togglePlayer()); | |
else | |
this.showMsg("Extra Turn") | |
} | |
_checkAndDeclareGameOverIfNecessary() | |
{ | |
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(); | |
} | |
_redraw() | |
{ | |
this.canvas.ifPresent(cnvs => { | |
drawBoardState(cnvs,this.board,this) | |
}) | |
} | |
playCell(boardCell) | |
{ | |
let _ = this.board; | |
let targetCells = range(1,_.stonesIn(boardCell)).map(steps => _.cellFrom(boardCell,steps)) | |
let lastCell = targetCells[targetCells.length-1]; | |
let lastCellWasEmpty = _.stonesIn(lastCell) == 0; | |
targetCells.forEach(c => _.addStoneTo(c)); | |
this._checkAndCaptureIfNecessary(lastCell,lastCellWasEmpty); | |
_.setCellStoneCount(boardCell,0); | |
return lastCell; | |
} | |
_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; | |
dbg("Capturing stones from " + acrossCell + " to " + lastCell) | |
_.setCellStoneCount(lastCell,_.stonesIn(lastCell) + _.stonesIn(acrossCell)); | |
_.setCellStoneCount(acrossCell,0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment