-
Notifications
You must be signed in to change notification settings - Fork 660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Basics-Beat-That-Submission: Ying Ling #575
base: main
Are you sure you want to change the base?
Changes from all commits
2db3a29
4da8b3e
f6dc2bb
e9248d6
68f0524
606198a
eb3526d
229a9d9
35c83d5
cbfe117
3cb1e5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,162 @@ | ||
// Global Variables | ||
var GAME_STATE_DICE_ROLL = "GAME_STATE_DICE_ROLL"; | ||
var GAME_STATE_CHOOSE_DICE_ORDER = "GAME_STATE_CHOOSE_DICE_ORDER"; | ||
var GAME_STATE_COMPARE_SCORES = "GAME_STATE_COMPARE_SCORES"; | ||
var gameState = GAME_STATE_DICE_ROLL; | ||
|
||
var currentPlayerRolls = []; | ||
|
||
var currentPlayer = 1; | ||
var allPlayersScore = []; | ||
|
||
// Helper Function | ||
var rollDice = function () { | ||
console.log("Control flow: start of rollDice()"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. console logs usually are internal checks for your use only, it usually is omitted for final code submissions. |
||
// Random decimal between 0 and 6 | ||
var randomDecimal = Math.random() * 6; | ||
// Random integer from 1 to 6 | ||
var randomInteger = Math.floor(randomDecimal) + 1; | ||
|
||
console.log("rollDice output, randomInteger: ", randomInteger); | ||
return randomInteger; | ||
}; | ||
|
||
var rollDiceForPlayer = function () { | ||
console.log("Control flow: start of rollDiceForPlayer()"); | ||
var counter = 0; | ||
while (counter < 2) { | ||
currentPlayerRolls.push(rollDice()); | ||
counter = counter + 1; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in this instance it might be better to use a for loop as opposed to a while loop. You already know how many times the loop will run! |
||
|
||
console.log( | ||
"rollDiceForPlayer changes, currentPlayerRolls: ", | ||
currentPlayerRolls | ||
); | ||
return ( | ||
"Welcome, Player " + | ||
currentPlayer + | ||
"<br><br>You rolled:<br>Dice 1: " + | ||
currentPlayerRolls[0] + | ||
" | Dice 2: " + | ||
currentPlayerRolls[1] + | ||
".<br><br>Now, please input either '1' or '2' to choose the corresponding dice to be used as the first digit of your final value." | ||
); | ||
}; | ||
|
||
var getPlayerScore = function (playerInput) { | ||
var playerScore; | ||
// input validation | ||
if (playerInput != 1 && playerInput != 2) { | ||
console.log( | ||
"Control flow: input validation, invalid inout... NOT 1 AND NOT 2" | ||
); | ||
return ( | ||
"Error! Please only input '1' or '' to choose which dice to use as the first digit.<br><br>Your dice rolls are:<br>Dice 1: " + | ||
currentPlayerRolls[0] + | ||
" | Dice 2: " + | ||
currentPlayerRolls[1] + | ||
"." | ||
); | ||
} | ||
// input == 1 | ||
if (playerInput == 1) { | ||
console.log("Control flow: input == 1"); | ||
playerScore = Number( | ||
String(currentPlayerRolls[0]) + String(currentPlayerRolls[1]) | ||
); | ||
} | ||
// input == 2 | ||
else if (playerInput == 2) { | ||
console.log("Control flow: input == 2"); | ||
playerScore = Number( | ||
String(currentPlayerRolls[1]) + String(currentPlayerRolls[0]) | ||
); | ||
} | ||
// Store playerScore in array | ||
allPlayersScore.push(playerScore); | ||
|
||
// clear current player rolls array | ||
currentPlayerRolls = []; | ||
return "Player" + currentPlayer + ", your chosen value is: " + playerScore; | ||
}; | ||
|
||
var comparePlayerScore = function () { | ||
var compareMessage = | ||
"Player 1 score: " + | ||
allPlayersScore[0] + | ||
"<br>Player 2 score: " + | ||
allPlayersScore[1]; | ||
|
||
// player 1 wins | ||
if (allPlayersScore[0] > allPlayersScore[1]) { | ||
compareMessage = compareMessage + "<br><br>Player 1 wins!"; | ||
} | ||
// player 2 wins | ||
if (allPlayersScore[0] < allPlayersScore[1]) { | ||
compareMessage = compareMessage + "<br><br>Player 2 wins!"; | ||
} | ||
// tie | ||
if (allPlayersScore[0] == allPlayersScore[1]) { | ||
compareMessage = compareMessage + "<br><br>It's a tie!"; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. slight nitpick here, it might be better to use:
if/else if/else is better than consecutive ifs here. |
||
|
||
return compareMessage; | ||
}; | ||
|
||
var resetGame = function () { | ||
currentPlayer = 1; | ||
gameState = GAME_STATE_DICE_ROLL; | ||
allPlayersScore = []; | ||
}; | ||
|
||
var main = function (input) { | ||
var myOutputValue = 'hello world'; | ||
return myOutputValue; | ||
console.log("Checking game state on submit click: ", gameState); | ||
console.log("Checking currentPlayer on submit click: ", currentPlayer); | ||
var outputMessage = ""; | ||
|
||
if (gameState == GAME_STATE_DICE_ROLL) { | ||
console.log("Control flow: gameState == GAME_STATE_DICE_ROLL"); | ||
|
||
// Display dice rolled as output message | ||
outputMessage = rollDiceForPlayer(); | ||
|
||
// Change the game state | ||
gameState = GAME_STATE_CHOOSE_DICE_ORDER; | ||
return outputMessage; | ||
} | ||
if (gameState == GAME_STATE_CHOOSE_DICE_ORDER) { | ||
console.log("Control flow: gameState == GAME_STATE_CHOOSE_DICE_ORDER"); | ||
|
||
// Call playerScore function | ||
outputMessage = getPlayerScore(input); | ||
|
||
if (currentPlayer == 1) { | ||
console.log("Control flow: end of player 1's turn, now player 2's turn"); | ||
currentPlayer = 2; | ||
gameState = GAME_STATE_DICE_ROLL; | ||
return outputMessage + "<br><br>It is now player 2's turn!"; | ||
} | ||
if (currentPlayer == 2) { | ||
console.log( | ||
"Control flow: end of player 2's turn, Next submit click will calculate score" | ||
); | ||
gameState = GAME_STATE_COMPARE_SCORES; | ||
|
||
return outputMessage + "<br><br>Press submit to calculate scores!"; | ||
} | ||
} | ||
|
||
if (gameState == GAME_STATE_COMPARE_SCORES) { | ||
console.log("Control flow: gameState == GAME_STATE_COMPARE_SCORES"); | ||
|
||
outputMessage = comparePlayerScore(); | ||
|
||
resetGame(); | ||
console.log("Current player after reset:", currentPlayer); | ||
console.log("Game state after reset:", gameState); | ||
console.log("allPlayersScore array:", allPlayersScore); | ||
|
||
return outputMessage; | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great usage of constants here!