From 52dbeaa81a8c224279e057254e5da94bb4ffa00b Mon Sep 17 00:00:00 2001 From: franticenigma Date: Mon, 5 Feb 2024 17:43:14 +0800 Subject: [PATCH 01/13] version 1 --- script.js | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/script.js b/script.js index bbe8a293..2739d8b8 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,88 @@ +// === REQUIREMENTS // +// there are 2 players and players take turns +// when a player clicks submit, the game rolls 2 dice and shows the dice roll, for example 3 and 6 +// the player picks the order of the dice they want. For example, if they wanted the number 63, they would specify that the 2nd dice goes first. next player would complete all the steps +// after both players have rolled and chosen dice order, the player with the higher combined number wins + +// == problem breakdown and planning === // +// ver. 1 rolls 2 dice and turns the output for 1 payer. then player chooses the dice order and get the correct return output. final chosen value will reflect in the outpux box +// ver. 2 refactor the code to work with 2 players +//ver 3. implement comparing dice scores and declare winner +// ver 4. reset the game so that the players can play continually without refreshing the browser page + +//declare global variable; all caps to remind that these will never change +var GAME_STATE_DICE_ROLL = "GAME_STATE_DICE_ROLL"; +var GAME_STATE_CHOOSE_DICE_ORDER = "GAME_STATE_CHOOSE_DICE_ORDER"; +//first step of the game +var gameState = GAME_STATE_DICE_ROLL; + +// create an array that will store player values +var playerRolls = []; + +// helper function = rollDice +var rollDice = function () { + console.log(`Control flow: start of rollDice()`); + // random decimal between 0 and 6 + var randomDecimal = Math.random() * 6; + // random integer from 1-6 + var randomInteger = Math.floor(randomDecimal) + 1; + + console.log("rollDice output, randomInteger: ", randomInteger); + return randomInteger; +}; + +// helper function to roll dice for player +var rollDiceForPlayer = function () { + console.log(`Control flow: start of rollDiceForPlayer()`); + var counter = 0; + while (counter < 2) { + playerRolls.push(rollDice()); + counter = counter + 1; + } + + console.log(`rollDiceForPlayer changes, playerRolls: `, playerRolls); + return `Welcome

You rolled:
Dice 1: ${playerRolls[0]} | Dice 2: ${playerRolls[1]}

Now, please input either "1" or "2" to choose corresponding dice to be used as the first digit of your final value.
`; +}; + +var getPlayerScore = function (playerInput) { + // input validation + if (playerInput != 1 && playerInput != 2) { + console.log( + `Control flow: input validation, invalid input... NOT 1 AND NOT 2` + ); + return `Error! Please only input "1" or "" to choose which dice to use as the first digit.

Your dice rolls are:
Dice 1: ${playerRolls[0]} | Dice 2: ${playerRolls[1]}.`; + } + // input == 1 + if (playerInput == 1) { + console.log(`Control flow: input == 1`); + var playerScore = Number(String(playerRolls[0]) + String(playerRolls[1])); + return `Your chosen value is: ${playerScore}`; + } + + // input == 2 + if (playerInput == 2) { + console.log(`Control flow: input == 2`); + var playerScore = Number(String(playerRolls[1]) + String(playerRolls[0])); + return `Your chose value is: ${playerScore}`; + } +}; + var main = function (input) { - var myOutputValue = 'hello world'; - return myOutputValue; + console.log(`Checking game state on submit click: `, gameState); + 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`); + // Caller playerScore function + outputMessage = getPlayerScore(input); + return outputMessage; + } }; From 2d6f2cf78bfc6bdb777e64546393d7e221ed5f29 Mon Sep 17 00:00:00 2001 From: franticenigma Date: Mon, 5 Feb 2024 18:10:00 +0800 Subject: [PATCH 02/13] 2 players --- script.js | 55 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/script.js b/script.js index 2739d8b8..b2fedba6 100644 --- a/script.js +++ b/script.js @@ -7,17 +7,25 @@ // == problem breakdown and planning === // // ver. 1 rolls 2 dice and turns the output for 1 payer. then player chooses the dice order and get the correct return output. final chosen value will reflect in the outpux box // ver. 2 refactor the code to work with 2 players +// - global variables fort currentPlayer; and an array to store the score of all players allPlayerScore +// - refactor outputMessages to interact with each player, 1 and 2 +// - write logic for player 1 to go first then player 2, and finally point towards comparing score //ver 3. implement comparing dice scores and declare winner // ver 4. reset the game so that the players can play continually without refreshing the browser page //declare global variable; all caps to remind that these will never change +//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"; //first step of the game var gameState = GAME_STATE_DICE_ROLL; +var currentPlayerRolls = []; +var currentPlayer = 1; +var allPlayersScore = []; + // create an array that will store player values -var playerRolls = []; // helper function = rollDice var rollDice = function () { @@ -36,39 +44,50 @@ var rollDiceForPlayer = function () { console.log(`Control flow: start of rollDiceForPlayer()`); var counter = 0; while (counter < 2) { - playerRolls.push(rollDice()); + currentPlayerRolls.push(rollDice()); counter = counter + 1; } - console.log(`rollDiceForPlayer changes, playerRolls: `, playerRolls); - return `Welcome

You rolled:
Dice 1: ${playerRolls[0]} | Dice 2: ${playerRolls[1]}

Now, please input either "1" or "2" to choose corresponding dice to be used as the first digit of your final value.
`; + console.log(`rollDiceForPlayer changes, playerRolls: `, currentPlayerRolls); + return `Welcome, Player ${currentPlayer}

You rolled:
Dice 1: ${currentPlayerRolls[0]} | Dice 2: ${currentPlayerRolls[1]}

Now, please input either "1" or "2" to choose 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 input... NOT 1 AND NOT 2` ); - return `Error! Please only input "1" or "" to choose which dice to use as the first digit.

Your dice rolls are:
Dice 1: ${playerRolls[0]} | Dice 2: ${playerRolls[1]}.`; + return `Error! Please only input "1" or "" to choose which dice to use as the first digit.

Your dice rolls are:
Dice 1: ${currentPlayerRolls[0]} | Dice 2: ${currentPlayerRolls[1]}.`; } // input == 1 if (playerInput == 1) { console.log(`Control flow: input == 1`); - var playerScore = Number(String(playerRolls[0]) + String(playerRolls[1])); - return `Your chosen value is: ${playerScore}`; + playerScore = Number( + String(currentPlayerRolls[0]) + String(currentPlayerRolls[1]) + ); } // input == 2 if (playerInput == 2) { console.log(`Control flow: input == 2`); - var playerScore = Number(String(playerRolls[1]) + String(playerRolls[0])); - return `Your chose value is: ${playerScore}`; + 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 main = function (input) { 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`); @@ -83,6 +102,22 @@ var main = function (input) { console.log(`Control flow: gameState == GAME_STATE_CHOOSE_DICE_ORDER`); // Caller playerScore function outputMessage = getPlayerScore(input); - return outputMessage; + + if (currentPlayer == 1) { + console.log( + `Control flow: end of player 1's turn, now it's player 2's turn` + ); + currentPlayer = 2; + gameState = GAME_STATE_DICE_ROLL; + return `${outputMessage}

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 scores` + ); + gameState = GAME_STATE_COMPARE_SCORES; + return `${outputMessage} Press submit to calculate scores!`; + } } }; From d789485860cd9f0c5ed6cfbceb04ac1262b02dc9 Mon Sep 17 00:00:00 2001 From: franticenigma Date: Wed, 7 Feb 2024 14:49:56 +0800 Subject: [PATCH 03/13] version 3: compare player scores --- script.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/script.js b/script.js index b2fedba6..e7a954ce 100644 --- a/script.js +++ b/script.js @@ -85,6 +85,23 @@ var getPlayerScore = function (playerInput) { return `Player ${currentPlayer}, your chosen value is: ${playerScore}`; }; +var comparePlayersScores = function () { + var compareMessage = `Player 1 score: ${allPlayersScore[0]}
Player 2 score: ${allPlayersScore[1]}`; + // player 1 wins + if (allPlayersScore[0] > allPlayersScore[1]) { + compareMessage = `${compareMessage}

Player 1 wins!`; + } + // player 2 wins + if (allPlayersScore[0] < allPlayersScore[1]) { + compareMessage = `${compareMessage}

Player 2 wins!`; + } + // tie + if (allPlayersScore[0] == allPlayersScore[1]) { + compareMessage = `${compareMessage}

It's a tie!`; + } + return compareMessage; +}; + var main = function (input) { console.log(`Checking game state on submit click: `, gameState); console.log(`Checking currentPlayer on submit click: `, currentPlayer); @@ -120,4 +137,10 @@ var main = function (input) { return `${outputMessage} Press submit to calculate scores!`; } } + if (gameState == GAME_STATE_COMPARE_SCORES) { + console.log(`Control flow: gameState == GAME_STATE_COMPARE_SCORES`); + + outputMessage = comparePlayersScores(); + return outputMessage; + } }; From e5af22893e3eb3977480b6be52c1607cbd46565f Mon Sep 17 00:00:00 2001 From: franticenigma Date: Wed, 7 Feb 2024 15:04:09 +0800 Subject: [PATCH 04/13] version 4: reset and restart game without refreshing browser --- script.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/script.js b/script.js index e7a954ce..098f78e0 100644 --- a/script.js +++ b/script.js @@ -101,6 +101,11 @@ var comparePlayersScores = function () { } return compareMessage; }; +var resetGame = function () { + currentPlayer = 1; + gameState = GAME_STATE_DICE_ROLL; + allPlayersScore = []; +}; var main = function (input) { console.log(`Checking game state on submit click: `, gameState); @@ -141,6 +146,11 @@ var main = function (input) { console.log(`Control flow: gameState == GAME_STATE_COMPARE_SCORES`); outputMessage = comparePlayersScores(); + + resetGame(); + console.log(`Current player after reset: ${currentPlayer}`); + console.log(`Game state after reset: ${gameState}`); + console.log(`allPlayersScoreArray: ${allPlayersScore}`); return outputMessage; } }; From 3568cd876d23853c75c5db21fcb13fa824a79ada Mon Sep 17 00:00:00 2001 From: franticenigma Date: Wed, 7 Feb 2024 22:32:05 +0800 Subject: [PATCH 05/13] added function to add player scores for first player --- script.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/script.js b/script.js index 098f78e0..8df5c3f8 100644 --- a/script.js +++ b/script.js @@ -24,8 +24,7 @@ var gameState = GAME_STATE_DICE_ROLL; var currentPlayerRolls = []; var currentPlayer = 1; var allPlayersScore = []; - -// create an array that will store player values +var overallPlayersScore = []; // helper function = rollDice var rollDice = function () { @@ -80,6 +79,9 @@ var getPlayerScore = function (playerInput) { // Store playerScore in array allPlayersScore.push(playerScore); + // store overall player score until browser is refreshed + overallPlayersScore.push(playerScore); + // clear current player rolls array currentPlayerRolls = []; return `Player ${currentPlayer}, your chosen value is: ${playerScore}`; @@ -101,6 +103,19 @@ var comparePlayersScores = function () { } return compareMessage; }; + +// need a function that will calculate the sum of numbers based on when/where they were pushed in the array +var runningSumOfNumbers = function () { + console.log(`adding player scores`); + var runningPlayerScore = 0; + var runningMessage = ``; + for (var i = 0; i < overallPlayersScore.length; i += 2) { + runningPlayerScore += overallPlayersScore[i]; + runningMessage = `Player 1 running score is: ${runningPlayerScore}`; + } + return runningMessage; +}; + var resetGame = function () { currentPlayer = 1; gameState = GAME_STATE_DICE_ROLL; @@ -111,6 +126,7 @@ var main = function (input) { console.log(`Checking game state on submit click: `, gameState); console.log(`Checking currentPlayer on submit click: `, currentPlayer); var outputMessage = ``; + var runningScoreMessage = ``; if (gameState == GAME_STATE_DICE_ROLL) { console.log(`Control flow: gameState == GAME_STATE_DICE_ROLL`); // Display dice rolled as output message @@ -127,11 +143,11 @@ var main = function (input) { if (currentPlayer == 1) { console.log( - `Control flow: end of player 1's turn, now it's player 2's turn` + `Control flow: end of player 1's turn, now it's player 2's turn.` ); currentPlayer = 2; gameState = GAME_STATE_DICE_ROLL; - return `${outputMessage}

It is now player 2's turn!`; + return `${outputMessage}

It is now player 2's turn!
Please click Submit again to roll the dice.`; } if (currentPlayer == 2) { @@ -146,11 +162,12 @@ var main = function (input) { console.log(`Control flow: gameState == GAME_STATE_COMPARE_SCORES`); outputMessage = comparePlayersScores(); - resetGame(); console.log(`Current player after reset: ${currentPlayer}`); console.log(`Game state after reset: ${gameState}`); console.log(`allPlayersScoreArray: ${allPlayersScore}`); - return outputMessage; + console.log(`overallPlayersScoreArray: ${overallPlayersScore}`); + runningScoreMessage = runningSumOfNumbers(); + return `${outputMessage}
${runningScoreMessage}`; } }; From 393ac63da40300755d15da2a1b51e94a66db95ef Mon Sep 17 00:00:00 2001 From: franticenigma Date: Wed, 7 Feb 2024 22:45:21 +0800 Subject: [PATCH 06/13] updated function to add total scores for both players --- script.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/script.js b/script.js index 8df5c3f8..b0eb5d32 100644 --- a/script.js +++ b/script.js @@ -107,12 +107,18 @@ var comparePlayersScores = function () { // need a function that will calculate the sum of numbers based on when/where they were pushed in the array var runningSumOfNumbers = function () { console.log(`adding player scores`); - var runningPlayerScore = 0; + var runningPlayerScore1 = 0; + var runningPlayerScore2 = 0; var runningMessage = ``; for (var i = 0; i < overallPlayersScore.length; i += 2) { - runningPlayerScore += overallPlayersScore[i]; - runningMessage = `Player 1 running score is: ${runningPlayerScore}`; + runningPlayerScore1 += overallPlayersScore[i]; } + for (var j = 1; j < overallPlayersScore.length; j += 2) { + runningPlayerScore2 += overallPlayersScore[j]; + } + runningMessage = `
Overall total score:
+ Player 1 is: ${runningPlayerScore1}
+ Player 2 is: ${runningPlayerScore2}`; return runningMessage; }; From 201db98c888117e174720e4b3357205174e0f669 Mon Sep 17 00:00:00 2001 From: franticenigma Date: Wed, 7 Feb 2024 22:48:38 +0800 Subject: [PATCH 07/13] finished Score feature --- script.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index b0eb5d32..c3aaa00b 100644 --- a/script.js +++ b/script.js @@ -167,7 +167,8 @@ var main = function (input) { if (gameState == GAME_STATE_COMPARE_SCORES) { console.log(`Control flow: gameState == GAME_STATE_COMPARE_SCORES`); - outputMessage = comparePlayersScores(); + outputMessage = `${comparePlayersScores()} +
Click Submit again to continue playing for another round.`; resetGame(); console.log(`Current player after reset: ${currentPlayer}`); console.log(`Game state after reset: ${gameState}`); From 244b29ee6009c1a6a7ac07cd8dbce24a8946edfb Mon Sep 17 00:00:00 2001 From: franticenigma Date: Thu, 8 Feb 2024 01:36:44 +0800 Subject: [PATCH 08/13] added leaderboard and descending scores in decreasing order --- script.js | 88 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 73 insertions(+), 15 deletions(-) diff --git a/script.js b/script.js index c3aaa00b..d19e8af6 100644 --- a/script.js +++ b/script.js @@ -18,6 +18,7 @@ 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 GAME_STATE_LEADERBOARD = "GAME_STATE_LEADERBOARD"; //first step of the game var gameState = GAME_STATE_DICE_ROLL; @@ -25,6 +26,15 @@ var currentPlayerRolls = []; var currentPlayer = 1; var allPlayersScore = []; var overallPlayersScore = []; +var player1Scores = []; +var player2Scores = []; +var runningPlayerScore1 = 0; +var runningPlayerScore2 = 0; +var i = 0; +var j = 1; +var k = 0; +var l = 1; +var roundCounter = 1; // helper function = rollDice var rollDice = function () { @@ -48,7 +58,8 @@ var rollDiceForPlayer = function () { } console.log(`rollDiceForPlayer changes, playerRolls: `, currentPlayerRolls); - return `Welcome, Player ${currentPlayer}

You rolled:
Dice 1: ${currentPlayerRolls[0]} | Dice 2: ${currentPlayerRolls[1]}

Now, please input either "1" or "2" to choose corresponding dice to be used as the first digit of your final value.
`; + return `ROUND ${roundCounter}!!!

+ Welcome, Player ${currentPlayer}

You rolled:
Dice 1: ${currentPlayerRolls[0]} | Dice 2: ${currentPlayerRolls[1]}

Now, please input either "1" or "2" to choose corresponding dice to be used as the first digit of your final value.
`; }; var getPlayerScore = function (playerInput) { @@ -107,19 +118,58 @@ var comparePlayersScores = function () { // need a function that will calculate the sum of numbers based on when/where they were pushed in the array var runningSumOfNumbers = function () { console.log(`adding player scores`); - var runningPlayerScore1 = 0; - var runningPlayerScore2 = 0; - var runningMessage = ``; - for (var i = 0; i < overallPlayersScore.length; i += 2) { + while (i < overallPlayersScore.length) { runningPlayerScore1 += overallPlayersScore[i]; + console.log(`running player 1 score is: ${runningPlayerScore1}`); + i += 2; } - for (var j = 1; j < overallPlayersScore.length; j += 2) { + while (j < overallPlayersScore.length) { runningPlayerScore2 += overallPlayersScore[j]; + console.log(`running player 2 score is: ${runningPlayerScore2}`); + j += 2; } - runningMessage = `
Overall total score:
- Player 1 is: ${runningPlayerScore1}
- Player 2 is: ${runningPlayerScore2}`; - return runningMessage; +}; + +// need a function that will store overall scores of each player +var leaderboard = function () { + var rankingMessage = ``; + console.log(`store each player roll`); + while (k < overallPlayersScore.length) { + player1Scores.push(overallPlayersScore[k]); + console.log(`summary player 1 scores: ${player1Scores}`); + var sortedPlayer1Scores = player1Scores.slice().sort(function (a, b) { + return b - a; + }); + k += 2; + } + while (l < overallPlayersScore.length) { + player2Scores.push(overallPlayersScore[l]); + console.log(`summary player 2 scores: ${player2Scores}`); + var sortedPlayer2Scores = player2Scores.slice().sort(function (a, b) { + return b - a; + }); + l += 2; + } + if (runningPlayerScore1 > runningPlayerScore2) { + rankingMessage = `Leaderboard as of Round ${roundCounter}:
+ Rank 1 🥇 is Player 1 with total of: ${runningPlayerScore1}
+ Rolled Dices: ${sortedPlayer1Scores}
+ Rank 2 🥈 is Player 2 with: ${runningPlayerScore2}
+ Rolled Dices: ${sortedPlayer2Scores}
`; + } else if (runningPlayerScore1 < runningPlayerScore2) { + rankingMessage = `Leaderboard as of Round ${roundCounter}:
+ Rank 1 🥇 is Player 2 with: ${runningPlayerScore2}
+ Rolled Dices: ${sortedPlayer2Scores}
+ Rank 2 🥈 is Player 1 with: ${runningPlayerScore1}
+ Rolled Dices: ${sortedPlayer1Scores}
`; + } else { + rankingMessage = `Leaderboard as of Round ${roundCounter}: It's a tie!
+ Player 1 with: ${runningPlayerScore1}
+ Rolled Dices: ${sortedPlayer1Scores}
+ Player 2 with: ${runningPlayerScore2}
+ Rolled Dices: ${sortedPlayer2Scores}`; + } + return rankingMessage; }; var resetGame = function () { @@ -132,7 +182,7 @@ var main = function (input) { console.log(`Checking game state on submit click: `, gameState); console.log(`Checking currentPlayer on submit click: `, currentPlayer); var outputMessage = ``; - var runningScoreMessage = ``; + var leaderboardMessage = ``; if (gameState == GAME_STATE_DICE_ROLL) { console.log(`Control flow: gameState == GAME_STATE_DICE_ROLL`); // Display dice rolled as output message @@ -167,14 +217,22 @@ var main = function (input) { if (gameState == GAME_STATE_COMPARE_SCORES) { console.log(`Control flow: gameState == GAME_STATE_COMPARE_SCORES`); - outputMessage = `${comparePlayersScores()} -
Click Submit again to continue playing for another round.`; + gameState = GAME_STATE_LEADERBOARD; + outputMessage = `${comparePlayersScores()}`; + return `${outputMessage}
+ Click Submit to see the Leaderboard.`; + } + if (gameState == GAME_STATE_LEADERBOARD) { + console.log(`Control flow: gameState == GAME_STATE_LEADERBOARD`); + runningSumOfNumbers(); + leaderboardMessage = leaderboard(); resetGame(); console.log(`Current player after reset: ${currentPlayer}`); console.log(`Game state after reset: ${gameState}`); console.log(`allPlayersScoreArray: ${allPlayersScore}`); console.log(`overallPlayersScoreArray: ${overallPlayersScore}`); - runningScoreMessage = runningSumOfNumbers(); - return `${outputMessage}
${runningScoreMessage}`; + roundCounter = roundCounter + 1; + return `${outputMessage}
${leaderboardMessage} +

Click Submit again to continue playing for another round.`; } }; From 964f4d08a47e451368cbafa96678592f398ed131 Mon Sep 17 00:00:00 2001 From: franticenigma Date: Thu, 8 Feb 2024 04:00:30 +0800 Subject: [PATCH 09/13] latest version, trying to fix validation code --- script.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/script.js b/script.js index d19e8af6..641c8133 100644 --- a/script.js +++ b/script.js @@ -19,6 +19,7 @@ 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 GAME_STATE_LEADERBOARD = "GAME_STATE_LEADERBOARD"; +var INVALID_STATE = "INVALID_STATE"; //first step of the game var gameState = GAME_STATE_DICE_ROLL; @@ -58,29 +59,25 @@ var rollDiceForPlayer = function () { } console.log(`rollDiceForPlayer changes, playerRolls: `, currentPlayerRolls); - return `ROUND ${roundCounter}!!!

- Welcome, Player ${currentPlayer}

You rolled:
Dice 1: ${currentPlayerRolls[0]} | Dice 2: ${currentPlayerRolls[1]}

Now, please input either "1" or "2" to choose corresponding dice to be used as the first digit of your final value.
`; + return `ROUND ${roundCounter}!

+ Welcome, Player ${currentPlayer}.

You rolled:
Dice 1: ${currentPlayerRolls[0]} | Dice 2: ${currentPlayerRolls[1]}

Now, please input either "1" or "2" to choose 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 input... NOT 1 AND NOT 2` - ); - return `Error! Please only input "1" or "" to choose which dice to use as the first digit.

Your dice rolls are:
Dice 1: ${currentPlayerRolls[0]} | Dice 2: ${currentPlayerRolls[1]}.`; + gameState = INVALID_STATE; } // input == 1 - if (playerInput == 1) { + else if (playerInput == 1) { console.log(`Control flow: input == 1`); playerScore = Number( String(currentPlayerRolls[0]) + String(currentPlayerRolls[1]) ); } - // input == 2 - if (playerInput == 2) { + else if (playerInput == 2) { console.log(`Control flow: input == 2`); playerScore = Number( String(currentPlayerRolls[1]) + String(currentPlayerRolls[0]) From dcc7e49469f20867b642166837e23392181cf894 Mon Sep 17 00:00:00 2001 From: franticenigma Date: Thu, 8 Feb 2024 15:56:18 +0800 Subject: [PATCH 10/13] fixed input validation settings --- script.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/script.js b/script.js index 641c8133..1adcffee 100644 --- a/script.js +++ b/script.js @@ -66,8 +66,9 @@ var rollDiceForPlayer = function () { var getPlayerScore = function (playerInput) { var playerScore; // input validation - if (playerInput != 1 && playerInput != 2) { - gameState = INVALID_STATE; + if (!(playerInput == 1 || playerInput == 2)) { + return `Invalid input. Game will not continue until you enter either "1" or "2".

+ You rolled:
Dice 1: ${currentPlayerRolls[0]} | Dice 2: ${currentPlayerRolls[1]}`; } // input == 1 else if (playerInput == 1) { @@ -194,16 +195,16 @@ var main = function (input) { // Caller playerScore function outputMessage = getPlayerScore(input); - if (currentPlayer == 1) { + if (outputMessage.includes("Invalid input")) { + return outputMessage; + } else if (currentPlayer == 1) { console.log( `Control flow: end of player 1's turn, now it's player 2's turn.` ); currentPlayer = 2; gameState = GAME_STATE_DICE_ROLL; return `${outputMessage}

It is now player 2's turn!
Please click Submit again to roll the dice.`; - } - - if (currentPlayer == 2) { + } else if (currentPlayer == 2) { console.log( `Control flow: end of player 2's turn, next submit click will calculate scores` ); From c5b4659ec494a08f06d9ffec2250b9c4541332c7 Mon Sep 17 00:00:00 2001 From: franticenigma Date: Thu, 8 Feb 2024 16:07:50 +0800 Subject: [PATCH 11/13] deleted unused game state --- script.js | 1 - 1 file changed, 1 deletion(-) diff --git a/script.js b/script.js index 1adcffee..994204fc 100644 --- a/script.js +++ b/script.js @@ -19,7 +19,6 @@ 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 GAME_STATE_LEADERBOARD = "GAME_STATE_LEADERBOARD"; -var INVALID_STATE = "INVALID_STATE"; //first step of the game var gameState = GAME_STATE_DICE_ROLL; From abac948dea84dc6ae71135edcb7687782a69dfcb Mon Sep 17 00:00:00 2001 From: franticenigma Date: Thu, 8 Feb 2024 16:12:39 +0800 Subject: [PATCH 12/13] adding html properties --- index.html | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 74f9da2a..0ffeaec5 100644 --- a/index.html +++ b/index.html @@ -18,7 +18,7 @@ } #container { - background-color: pink; + background-color: rgb(37, 151, 130); margin: 40px auto; max-width: 800px; padding: 38px 31px; @@ -49,8 +49,13 @@ -

Basics: Beat That! 🚀

+

Fundamentals: Beat That! 🚀

+

Hello! Welcome to Beat That! Click submit to start the game.

+

+ Create a two-digit number by selecting the order of your dice rolls. +

+

The player with the highest number wins! Good luck!

Input:


From c6f7d8c63f0bb526c41df07be6984d47a91a6230 Mon Sep 17 00:00:00 2001 From: franticenigma Date: Thu, 8 Feb 2024 16:30:55 +0800 Subject: [PATCH 13/13] updated header title from basics to fundamentals --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index 0ffeaec5..3190680e 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,7 @@ - Coding Basics + Coding Fundamentals