Skip to content

Instantly share code, notes, and snippets.

@slior

slior/bots.js Secret

Created December 10, 2020 08:14
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/476e6f8f78267825528573bebc59e9af to your computer and use it in GitHub Desktop.
Save slior/476e6f8f78267825528573bebc59e9af to your computer and use it in GitHub Desktop.
Mancala - headless mode and more bots
...
program
.version('0.0.1')
.usage("Mancala Bot War: Run Mancala AI Experiment")
.option('-p1, --player1 <AI 1 type>', "The bot to use for player 1")
.option('-p2, --player2 <AI 2 type>', "The bot to use for player 2")
.option('-r, --rounds <rnds>', "Number of games to play")
.option('-o, --out <dir>', "output file")
.parse(process.argv)
...
range(1,program.rounds*1).forEach(round => {
dbg(`Round ${round}`)
let game = new MancalaGame(14,'',_ => {},gameMsg,{p1 : program.player1, p2:program.player2},results => {
dbg (`Round ${round} Results: ${results}`)
let line = []
line.push(results.player1StoneCount)
line.push(results.player2StoneCount)
line.push(results.winner || 0) //write the player who one or 0 for draw
fs.appendFileSync(program.out,line.join(',') + "\n",{flag :'a'})
})
game.start();
})
class MancalaGame
{
constructor(gameSize,cnvsELID,_updatePlayerCallback,_showMsgCallback,requestedAIPlayers,_gameOverCallback)
{
...
this.gameOverCallback = _gameOverCallback
...
if (cnvsELID)
{
this.boardUI = maybe(new BoardUI(cnvsELID,this.cellCount,this))
this.boardUI.ifPresent(bui => bui.initializeBoardDrawing());
}
else this.boardUI = None; //headless mode
}
_redraw()
{
this.boardUI.ifPresent(_ => _.drawBoardState(this.board,this));
}
_gameOver()
{
let player1StoneCount = this.board.player1StoneCount();
let player2StoneCount = this.board.player2StoneCount();
let results = { player1StoneCount : player1StoneCount, player2StoneCount : player2StoneCount}
switch (true)
{
case player1StoneCount > player2StoneCount : results.winner = 1; break;
case player2StoneCount > player1StoneCount : results.winner = 2; break;
default : results.isDraw = true; break;
}
this.gameOverCallback(results);
}
togglePlayer()
{
this.player = this.player.theOtherOne();
this.boardUI.ifPresent(_ => _.toggleHighlights(this.player.number));
return this.player;
}
}
<script>
function setup()
{
game = new main.MancalaGame(resolveGameSize(),'cnvsMain',updateCurrentPlayer,showMsg, resolveRequestedAI(),gameOver);
game.start();
}
function gameOver(results)
{
let a = ["Game Over","# Stones P1:" + results.player1StoneCount,"# Stones P2: " + results.player2StoneCount];
if (!results.isDraw)
a.push(`Player ${results.winner} Wins!`)
else
a.push('Draw!')
showMsg(a.join('<br/>'))
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment