-
-
Save slior/476e6f8f78267825528573bebc59e9af to your computer and use it in GitHub Desktop.
Mancala - headless mode and more bots
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
... | |
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(); | |
}) |
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(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; | |
} | |
} |
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
<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