Skip to content

Instantly share code, notes, and snippets.

@slior

slior/game.js Secret

Created November 17, 2020 14:08
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/727dbfe59b8afcee1b796bc2f97fc8f8 to your computer and use it in GitHub Desktop.
Save slior/727dbfe59b8afcee1b796bc2f97fc8f8 to your computer and use it in GitHub Desktop.
Mancala (db555b58544181ff4c4c34b3d951f8f77e91c833) - adding ability to change game size
class MancalaGame
{
constructor(gameSize,cnvsELID,_updatePlayerCallback,_showMsgCallback)
{
...
this.cellCount = gameSize;
...
this.board = new Board(this.cellCount);
}
_initializeBoardDrawing()
{
...
drawBoard(cnvs,this.cellCount);
...
}
}
<script>
const SIZE_PARAM_NAME = 'size';
const DEFAULT_GAME_SIZE = 14;
const GAME_SIZE_UPPER_BOUND = 28;
const GAME_SIZE_LOWER_BOUND = 6;
...
function setup()
{
game = new main.MancalaGame(resolveGameSize(),'cnvsMain',updateCurrentPlayer,showMsg);
}
function resolveGameSize()
{
let params = new URLSearchParams(window.location.search);
let size = (params.has(SIZE_PARAM_NAME) && !isNaN(params.get(SIZE_PARAM_NAME))) ?
params.get(SIZE_PARAM_NAME)*1 : DEFAULT_GAME_SIZE;
let ret = Math.max( //we bound the game size between the minimum and the upper bound
Math.min(size
,GAME_SIZE_UPPER_BOUND || DEFAULT_GAME_SIZE)
,GAME_SIZE_LOWER_BOUND);
if (ret % 2 != 0) ret -= 1; //we have to make sure it's a divisble by 2.
console.log("Game size: " + ret);
return ret;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment