Last active
January 2, 2022 03:36
-
-
Save fisherds/d6483551d4c6fa2bebd1af5cda702395 to your computer and use it in GitHub Desktop.
Given code for the Cant Stop model object
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
rhit.CantStopGame = class { | |
static NUM_DICE = 3; // Hardcoded but easy to change if making a different game. | |
static MAX_FIRST_ROLL_VALUE = 3; // Hardcoded but easy to change | |
static RoundState = { | |
PLAYER_1_ACTIVE: "Player 1's Turn", | |
PLAYER_1_BUST: "Player 1 Bust!", | |
PLAYER_2_ACTIVE: "Player 2's Turn", | |
PLAYER_2_BUST: "Player 2 Bust!", | |
} | |
/** constructor */ | |
constructor() { | |
this.roundState = rhit.CantStopGame.RoundState.PLAYER_1_ACTIVE; | |
this.player1Score = 0; | |
this.player2Score = 0; | |
this.currentRoundScore = 0; | |
this.diceValues = []; // Example [1, 5, 4] | |
for (let k = 0; k < rhit.CantStopGame.NUM_DICE; k++) { | |
this.diceValues.push(0); // Just creating an array the correct size. | |
} | |
this._initialRollDice(); | |
console.log(`Created CantStopGame with ${this.toString()}`); | |
} | |
// Returns a random value 1 to 6 (or maxValue if provided) | |
// Used internally as a static class method. | |
static getRandomDieValue(maxValue = 6) { | |
return Math.floor(Math.random() * maxValue) + 1; | |
} | |
// Public method - used from an active state | |
keepRolling() { | |
switch (this.roundState) { | |
case rhit.CantStopGame.RoundState.PLAYER_1_ACTIVE: | |
case rhit.CantStopGame.RoundState.PLAYER_2_ACTIVE: | |
this._rollDice(); | |
return; | |
case rhit.CantStopGame.RoundState.PLAYER_1_BUST: | |
case rhit.CantStopGame.RoundState.PLAYER_2_BUST: | |
console.log("This player has gone bust and cannot continue to roll. Check your code."); | |
return; | |
} | |
} | |
// Public method - used from an active state | |
stopRolling() { | |
switch (this.roundState) { | |
case rhit.CantStopGame.RoundState.PLAYER_1_ACTIVE: | |
this.player1Score += this.currentRoundScore; | |
this.nextPlayer(); | |
return; | |
case rhit.CantStopGame.RoundState.PLAYER_2_ACTIVE: | |
this.player2Score += this.currentRoundScore; | |
this.nextPlayer(); | |
return; | |
case rhit.CantStopGame.RoundState.PLAYER_1_BUST: | |
case rhit.CantStopGame.RoundState.PLAYER_2_BUST: | |
console.log("This player has gone bust and cannot stop. Check your code."); | |
return; | |
} | |
} | |
// Public method - to be used from the BUST state | |
nextPlayer() { | |
switch (this.roundState) { | |
case rhit.CantStopGame.RoundState.PLAYER_1_ACTIVE: | |
case rhit.CantStopGame.RoundState.PLAYER_1_BUST: | |
this.roundState = rhit.CantStopGame.RoundState.PLAYER_2_ACTIVE; | |
break; | |
case rhit.CantStopGame.RoundState.PLAYER_2_ACTIVE: | |
case rhit.CantStopGame.RoundState.PLAYER_2_BUST: | |
this.roundState = rhit.CantStopGame.RoundState.PLAYER_1_ACTIVE; | |
break; | |
} | |
this._initialRollDice(); | |
} | |
// Private methods - You should not call or use these method. | |
_initialRollDice() { | |
this.currentRoundScore = 0; | |
this.diceValues.forEach((diceValue, index) => { | |
const randValue = rhit.CantStopGame.getRandomDieValue(rhit.CantStopGame.MAX_FIRST_ROLL_VALUE); | |
this.currentRoundScore += randValue; | |
this.diceValues[index] = randValue; | |
}); | |
} | |
_rollDice() { | |
let numSixes = 0; | |
let total = 0; | |
this.diceValues.forEach((diceValue, index) => { | |
const randValue = rhit.CantStopGame.getRandomDieValue(); | |
if (randValue == 6) { | |
numSixes++; | |
} | |
total += randValue; | |
this.diceValues[index] = randValue; | |
}); | |
switch (this.roundState) { | |
case rhit.CantStopGame.RoundState.PLAYER_1_ACTIVE: | |
if (numSixes == 1) { | |
// Player 1 has gone bust! | |
this.roundState = rhit.CantStopGame.RoundState.PLAYER_1_BUST; | |
this.currentRoundScore = 0; | |
} else { | |
this.currentRoundScore += total; | |
} | |
return; | |
case rhit.CantStopGame.RoundState.PLAYER_2_ACTIVE: | |
if (numSixes == 1) { | |
// Player 2 has gone bust! | |
this.roundState = rhit.CantStopGame.RoundState.PLAYER_2_BUST; | |
this.currentRoundScore = 0; | |
} else { | |
this.currentRoundScore += total; | |
} | |
return; | |
case rhit.CantStopGame.RoundState.PLAYER_1_BUST: | |
case rhit.CantStopGame.RoundState.PLAYER_2_BUST: | |
console.log("Error! A bust player just rolled! Check your code."); | |
return; | |
} | |
} | |
// Getter - Optional helper to get values from the array | |
getDieValueAtIndex(dieIndex) { | |
return this.diceValues[dieIndex]; | |
} | |
// Optional method used for debugging. It prints the model object if you call this method. | |
toString() { | |
return `Dice: ${this.diceValues.join(' ')} Scores: ${this.player1Score}|${this.player2Score}|${this.currentRoundScore} RoundState: ${this.roundState}`; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment