JP Greed Island simulator
by Tim Ko
JavaScript
class Game {
constructor({numPlayers = 5, priceToPlay = 1, consecutiveWins = 10, possibleChoices = 2}) {
this.numPlayers = numPlayers;
this.priceToPlay = priceToPlay;
this.consecutiveWins = consecutiveWins;
this.possibleChoices = possibleChoices;
this.resetGame();
}
resetGame() {
this.players = {};
for (let i = 0; i < this.numPlayers; i++) {
this.players[i] = 0;
}
}
runSimulation() {
//console.log(`Playing game with ${_.size(this.players)} players, $${this.priceToPlay} entry fee, and ${this.consecutiveWins} consecutive wins!`);
let numberOfRoundsPlayed = 0;
let amountOfMoneyMade = 0;
//for (let x = 0; x < 10; x++) {
while (!_.values(this.players).includes(this.consecutiveWins)) {
//console.log(`Round ${numberOfRoundsPlayed + 1}:`);
amountOfMoneyMade += _.values(this.players).filter(value => value === 0).length * this.priceToPlay;
Object.keys(this.players).forEach(playerNumber => {
const score = this.players[playerNumber];
const didWin = Math.floor(Math.random() * this.possibleChoices) === 1;
//console.log(`Player ${parseInt(playerNumber) + 1} ${didWin ? 'won' : 'lost'}`);
this.players[playerNumber] = didWin ? score + 1 : 0;
});
//console.log(`Scoreboard: ${JSON.stringify(this.players)}`);
//console.log(`The pot now has $${amountOfMoneyMade}`);
//console.log();
numberOfRoundsPlayed++;
}
//console.log(`It took ${numberOfRoundsPlayed} rounds for someone to win!`);
//console.log(`The pot has $${amountOfMoneyMade}. That's a LOT of money!`);
this.resetGame();
return {
numberOfRoundsPlayed,
amountOfMoneyMade,
};
}
}
let options = {
numberOfPlayers: 5,
costToPlay: 1,
roundsToWin: 10,
possibleChoices: 2, // i.e. 1/possibleChoices chance to move on to the next round
}
let numberOfSimulations = 1000;
let game = new Game(options);
let roundsData = {
min:...