From 87410f60dc554577502caf61b05c53bdd1930418 Mon Sep 17 00:00:00 2001 From: ryx19 Date: Mon, 27 Nov 2023 12:03:05 +0800 Subject: [PATCH 1/7] changes made to script --- script.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index bbe8a293..282e4d96 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,6 @@ var main = function (input) { - var myOutputValue = 'hello world'; + var myOutputValue = "hello world"; + var name = ""; return myOutputValue; + // edited }; From 510ff01afb87c80720d781ed81328ded03596829 Mon Sep 17 00:00:00 2001 From: ryx19 Date: Sat, 2 Dec 2023 23:10:30 +0800 Subject: [PATCH 2/7] ver 1. one player roll --- index.html | 2 ++ script.js | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index 74f9da2a..e7f7eadf 100644 --- a/index.html +++ b/index.html @@ -51,6 +51,8 @@

Basics: Beat That! 🚀

+

Click on the submit button to roll dice

+

Input:


diff --git a/script.js b/script.js index 282e4d96..5efedd95 100644 --- a/script.js +++ b/script.js @@ -1,6 +1,72 @@ +// There are 2 players and players take turns. +// When a player clicks Submit, the game rolls 2 dice and shows the dice rolls, 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. You can choose how the player specifies dice order. +// After both players have rolled and chosen dice order, the player with the higher combined number wins. + +// GLOBAL STATES + +var DICE_ROLL_GAME_STATE = ""; +var ORDER_OF_DICE = ""; +var gameState = DICE_ROLL_GAME_STATE; +var player = 1; +var playerRolls = []; + +// roll dice functinon +var rollDice = function () { + var randomDecimal = Math.random() * 6; + var randomInterger = Math.floor(randomDecimal) + 1; + return randomInterger; +}; + +var rollDiceForPlayer = function () { + var counter = 0; + while (counter < 2) { + playerRolls.push(rollDice()); + counter += 1; + } + console.log("player rolls", playerRolls); + return `Welcome! You rolled Dice one: ${playerRolls[0]} and Dice two: ${playerRolls[1]}
Please enter '1' or '2' to choose the corresponding dice to be used as the fiorst digit of your final value.`; +}; + +var getPlayerScore = function (playerInput) { + // input validation + if (playerInput != 1 && playerInput != 2) { + return `Error! Please input only '1' or '2'.
Dice one value is ${playerRolls[0]}
Dice two value is ${playerRolls[1]} `; + } + // input == 1 + if (playerInput == 1) { + var playerScore = Number(String(playerRolls[0])) + String(playerRolls[1]); + return `You chose ${playerScore}`; + } + // input ==2 + if (playerInput == 1) { + var playerScore = Number(String(playerRolls[0])) + String(playerRolls[1]); + return `The value you chose ${playerScore}`; + } + if (playerInput == 2) { + var playerScore = Number(String(playerRolls[1])) + String(playerRolls[0]); + return `The value you chose ${playerScore}`; + } +}; + var main = function (input) { - var myOutputValue = "hello world"; - var name = ""; + var myOutputValue = ""; + // diceRoll function gets called twice. + if (gameState == DICE_ROLL_GAME_STATE) { + myOutputValue = rollDiceForPlayer(); + // change the game state + gameState = ORDER_OF_DICE; + } + + if (gameState == ORDER_OF_DICE) { + console.log(`Control flow: gameState == ORDER_OF_DICE`); + + // call playerSCore function into the gameState when it is ORDER_OF_DICE and pass in the input from the main function + myOutputValue = getPlayerScore(input); + } return myOutputValue; - // edited }; + +// assign the values into a global variable +// function for an input to be collected 1 or 2 +// concatanate both values and store it back in global variable From 2366613a238f20a4328e25058d35fb9e00c93148 Mon Sep 17 00:00:00 2001 From: ryx19 Date: Sun, 3 Dec 2023 01:19:57 +0800 Subject: [PATCH 3/7] ver1 updated --- script.js | 65 +++++++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/script.js b/script.js index 5efedd95..e56fb73d 100644 --- a/script.js +++ b/script.js @@ -1,72 +1,65 @@ -// There are 2 players and players take turns. -// When a player clicks Submit, the game rolls 2 dice and shows the dice rolls, 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. You can choose how the player specifies dice order. -// After both players have rolled and chosen dice order, the player with the higher combined number wins. +var GAME_STATE_DICE_ROLL = "GAME_STATE_DICE_ROLL"; +var GAME_STATE_CHOOSE_DICE_ORDER = "GAME_STATE_CHOOSE_DICE_ORDER "; +var gameState = GAME_STATE_DICE_ROLL; -// GLOBAL STATES - -var DICE_ROLL_GAME_STATE = ""; -var ORDER_OF_DICE = ""; -var gameState = DICE_ROLL_GAME_STATE; -var player = 1; var playerRolls = []; -// roll dice functinon +// HELPER FUNCTION var rollDice = function () { + console.log("Control flow: start of rollDice()"); var randomDecimal = Math.random() * 6; var randomInterger = Math.floor(randomDecimal) + 1; + console.log("CrollDice output, randomInterger,", randomInterger); return randomInterger; }; var rollDiceForPlayer = function () { + console.log("Control flow: start of rollDiceForPlayer()"); + var counter = 0; while (counter < 2) { playerRolls.push(rollDice()); counter += 1; } - console.log("player rolls", playerRolls); - return `Welcome! You rolled Dice one: ${playerRolls[0]} and Dice two: ${playerRolls[1]}
Please enter '1' or '2' to choose the corresponding dice to be used as the fiorst digit of your final value.`; + console.log(`rollDiceForPlayer changes, playerRolls`, playerRolls); + return `Welcome

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

Now, select '1' or '2' to choose the corresponding dice to be used as the first digit of your final value`; }; var getPlayerScore = function (playerInput) { - // input validation if (playerInput != 1 && playerInput != 2) { - return `Error! Please input only '1' or '2'.
Dice one value is ${playerRolls[0]}
Dice two value is ${playerRolls[1]} `; + console.log("Control flow: gameState = GAME_STATE_CHOOSE_DICE_ORDER"); + return `ERROR! Please only input '1' or '2' to choose the corresponding dice to be used as the first digit of your final value.

You rolled
Dice 1: ${playerRolls[0]} | Dice 2: ${playerRolls[1]}`; } - // input == 1 - if (playerInput == 1) { - var playerScore = Number(String(playerRolls[0])) + String(playerRolls[1]); - return `You chose ${playerScore}`; - } - // input ==2 + if (playerInput == 1) { + console.log("Control flow: input == 1"); var playerScore = Number(String(playerRolls[0])) + String(playerRolls[1]); - return `The value you chose ${playerScore}`; + return `Your chosen value is: ${playerScore}`; } + if (playerInput == 2) { + console.log("Control flow: input == 2"); var playerScore = Number(String(playerRolls[1])) + String(playerRolls[0]); - return `The value you chose ${playerScore}`; + return `Your chosen value is: ${playerScore}`; } }; var main = function (input) { + console.log(`Checking game state on submit click: ${gameState}`); var myOutputValue = ""; - // diceRoll function gets called twice. - if (gameState == DICE_ROLL_GAME_STATE) { + + if ((gameState = GAME_STATE_DICE_ROLL)) { + console.log("Control flow: gameState = GAME_STATE_DICE_ROLL"); + myOutputValue = rollDiceForPlayer(); - // change the game state - gameState = ORDER_OF_DICE; - } - if (gameState == ORDER_OF_DICE) { - console.log(`Control flow: gameState == ORDER_OF_DICE`); + gameState = GAME_STATE_CHOOSE_DICE_ORDER; + } - // call playerSCore function into the gameState when it is ORDER_OF_DICE and pass in the input from the main function + if (gameState == GAME_STATE_CHOOSE_DICE_ORDER) { + console.log("Control flow: input validation for input not 1 and 2"); myOutputValue = getPlayerScore(input); + + return myOutputValue; } - return myOutputValue; }; - -// assign the values into a global variable -// function for an input to be collected 1 or 2 -// concatanate both values and store it back in global variable From f1eb2860ff757b1c28932a98e6cce396821b0fe7 Mon Sep 17 00:00:00 2001 From: ryx19 Date: Sun, 3 Dec 2023 12:54:48 +0800 Subject: [PATCH 4/7] ver2 player2 added --- script.js | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/script.js b/script.js index e56fb73d..b854363c 100644 --- a/script.js +++ b/script.js @@ -1,8 +1,12 @@ 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 playerRolls = []; +var currentPlayerRolls = []; + +var currentPlayer = 1; +var allPlayerScore = []; // HELPER FUNCTION var rollDice = function () { @@ -18,48 +22,72 @@ var rollDiceForPlayer = function () { var counter = 0; while (counter < 2) { - playerRolls.push(rollDice()); + currentPlayerRolls.push(rollDice()); counter += 1; } - console.log(`rollDiceForPlayer changes, playerRolls`, playerRolls); - return `Welcome

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

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

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

Now, select '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; if (playerInput != 1 && playerInput != 2) { console.log("Control flow: gameState = GAME_STATE_CHOOSE_DICE_ORDER"); - return `ERROR! Please only input '1' or '2' to choose the corresponding dice to be used as the first digit of your final value.

You rolled
Dice 1: ${playerRolls[0]} | Dice 2: ${playerRolls[1]}`; + return `ERROR! Please only input '1' or '2' to choose the corresponding dice to be used as the first digit of your final value.

You rolled
Dice 1: ${currentPlayerRolls[0]} | Dice 2: ${currentPlayerRolls[1]}`; } if (playerInput == 1) { console.log("Control flow: input == 1"); - var playerScore = Number(String(playerRolls[0])) + String(playerRolls[1]); + playerScore = + Number(String(currentPlayerRolls[0])) + String(currentPlayerRolls[1]); + return `Your chosen value is: ${playerScore}`; } if (playerInput == 2) { console.log("Control flow: input == 2"); - var playerScore = Number(String(playerRolls[1])) + String(playerRolls[0]); - return `Your chosen value is: ${playerScore}`; + + playerScore = + Number(String(currentPlayerRolls[1])) + String(currentPlayerRolls[0]); } + allPlayerScore.push(playerScore); + 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 myOutputValue = ""; - if ((gameState = GAME_STATE_DICE_ROLL)) { + if (gameState == GAME_STATE_DICE_ROLL) { console.log("Control flow: gameState = GAME_STATE_DICE_ROLL"); myOutputValue = rollDiceForPlayer(); gameState = GAME_STATE_CHOOSE_DICE_ORDER; + return myOutputValue; } if (gameState == GAME_STATE_CHOOSE_DICE_ORDER) { console.log("Control flow: input validation for input not 1 and 2"); myOutputValue = getPlayerScore(input); - return myOutputValue; + if (currentPlayer == 1) { + console.log(`Control flow: end of player 1, now player 2`); + currentPlayer = 2; + gameState = gameState = GAME_STATE_DICE_ROLL; + return myOutputValue + `

It is now player 2's turn`; + } + if (currentPlayer == 2) { + console.log( + `Control flow: end of player 2, Next submit click will calculate score` + ); + gameState = GAME_STATE_COMPARE_SCORES; + } + return myOutputValue + "

Press submit to calculate scores!"; } }; From d981096641fb063c3adb3eceacda456fe235e325 Mon Sep 17 00:00:00 2001 From: ryx19 Date: Sun, 3 Dec 2023 13:10:16 +0800 Subject: [PATCH 5/7] ver3 compare and declare winner --- script.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index b854363c..76fce104 100644 --- a/script.js +++ b/script.js @@ -53,11 +53,27 @@ var getPlayerScore = function (playerInput) { playerScore = Number(String(currentPlayerRolls[1])) + String(currentPlayerRolls[0]); } + allPlayerScore.push(playerScore); currentPlayerRolls = []; return `Player ${currentPlayer}, your chosen value is: ${playerScore}`; }; +var comparePlayerScores = function () { + var compareMessage = `Player 1 score: ${allPlayerScore[0]}
Player 2 score: ${allPlayerScore[1]} `; + if (allPlayerScore[0] > allPlayerScore[1]) { + compareMessage = `${compareMessage}
Player 1 wins!`; + } + + if (allPlayerScore[0] < allPlayerScore[1]) { + compareMessage = `${compareMessage}
Player 2 wins!`; + } + if (allPlayerScore[0] == allPlayerScore[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}`); @@ -87,7 +103,13 @@ var main = function (input) { `Control flow: end of player 2, Next submit click will calculate score` ); gameState = GAME_STATE_COMPARE_SCORES; + return myOutputValue + "

Press submit to calculate scores!"; } - return myOutputValue + "

Press submit to calculate scores!"; + } + + if (gameState == GAME_STATE_COMPARE_SCORES) { + console.log(`Control flow: comparing scores`); + myOutputValue = comparePlayerScores(); + return myOutputValue; } }; From 71b07c39b781de56deba41e402d97a6984389b32 Mon Sep 17 00:00:00 2001 From: ryx19 Date: Sun, 3 Dec 2023 13:12:07 +0800 Subject: [PATCH 6/7] ver4 --- script.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/script.js b/script.js index 76fce104..507ff2f8 100644 --- a/script.js +++ b/script.js @@ -74,6 +74,12 @@ var comparePlayerScores = function () { return compareMessage; }; +var resetGame = function () { + currentPlayer = 1; + gameState = GAME_STATE_DICE_ROLL; + allPlayerScore = []; +}; + var main = function (input) { console.log(`Checking game state on submit click: ${gameState}`); console.log(`Checking currentPlayer on submit click: ${currentPlayer}`); @@ -110,6 +116,7 @@ var main = function (input) { if (gameState == GAME_STATE_COMPARE_SCORES) { console.log(`Control flow: comparing scores`); myOutputValue = comparePlayerScores(); + resetGame(); return myOutputValue; } }; From ccebaca14eaee782ee6ee7c8a556fb951227dd46 Mon Sep 17 00:00:00 2001 From: ryx19 Date: Mon, 4 Dec 2023 12:17:18 +0800 Subject: [PATCH 7/7] completed base Beat That! game after code along --- script.js | 178 +++++++++++++++++++++++++++--------------------------- 1 file changed, 89 insertions(+), 89 deletions(-) diff --git a/script.js b/script.js index 507ff2f8..577f4fbb 100644 --- a/script.js +++ b/script.js @@ -1,122 +1,122 @@ -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 allPlayerScore = []; +// GLOBAL VARIABLES +var gameStateDiceRoll = "gameStateDiceRoll"; +var gameStateOrder = "gameStateOrder"; +var gameStateCompare = "gameStateCompare"; +var gameStateReset = "gameStateReset"; +var gameState = "gameStateDiceRoll"; +var player = 1; +var currentRoll = []; +var diceCalcHolder = []; // HELPER FUNCTION -var rollDice = function () { - console.log("Control flow: start of rollDice()"); +// Dice roll function +var diceRoll = function () { var randomDecimal = Math.random() * 6; - var randomInterger = Math.floor(randomDecimal) + 1; - console.log("CrollDice output, randomInterger,", randomInterger); + var randomInterger = Math.floor(randomDecimal + 1); + console.log(`Random interger ${randomInterger}`); return randomInterger; }; -var rollDiceForPlayer = function () { - console.log("Control flow: start of rollDiceForPlayer()"); - +// Function that rolls the dice twice and store it in the array currentRoll +var playerRoll = function () { var counter = 0; + var message = ""; + while (counter < 2) { - currentPlayerRolls.push(rollDice()); counter += 1; + currentRoll.push(diceRoll()); } - console.log( - `rollDiceForPlayer changes, currentPlayerRolls`, - currentPlayerRolls - ); - return `Welcome ${currentPlayer}

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

Now, select '1' or '2' to choose the corresponding dice to be used as the first digit of your final value`; + console.log(`While loop to roll dice twice`, currentRoll); + message = `Welcome player ${player}. Please select which order dice one and two to form the highest value by selecting '1' or '2'
+ Dice one: ${currentRoll[0]}
Dice two: ${currentRoll[1]}`; + + return message; }; -var getPlayerScore = function (playerInput) { - var playerScore; +// Function that concatanate two strings together +var addNum = function (playerInput) { + var diceCalc; + // input validation if (playerInput != 1 && playerInput != 2) { - console.log("Control flow: gameState = GAME_STATE_CHOOSE_DICE_ORDER"); - return `ERROR! Please only input '1' or '2' to choose the corresponding dice to be used as the first digit of your final value.

You rolled
Dice 1: ${currentPlayerRolls[0]} | Dice 2: ${currentPlayerRolls[1]}`; - } - - if (playerInput == 1) { - console.log("Control flow: input == 1"); - playerScore = - Number(String(currentPlayerRolls[0])) + String(currentPlayerRolls[1]); - - return `Your chosen value is: ${playerScore}`; + myOutputValue = `ERROR!
Please select which order dice one and two to form the highest value by selecting '1' or '2'
+ Dice one: ${currentRoll[0]}
Dice two: ${currentRoll[1]}`; + return myOutputValue; } - - if (playerInput == 2) { - console.log("Control flow: input == 2"); - - playerScore = - Number(String(currentPlayerRolls[1])) + String(currentPlayerRolls[0]); + // playerInput + else if (playerInput == 1) { + diceCalc = Number(String(currentRoll[0])) + String(currentRoll[1]); + myOutputValue = `Your value added up to be ${diceCalc}. `; + } else if (playerInput == 2) { + diceCalc = Number(String(currentRoll[1])) + String(currentRoll[0]); + myOutputValue = `Your value added up to be ${diceCalc}. `; } - - allPlayerScore.push(playerScore); - currentPlayerRolls = []; - return `Player ${currentPlayer}, your chosen value is: ${playerScore}`; + console.log(`Push diceCalc value into holder ${diceCalc}`); + diceCalcHolder.push(diceCalc); + currentRoll = []; + return myOutputValue; }; -var comparePlayerScores = function () { - var compareMessage = `Player 1 score: ${allPlayerScore[0]}
Player 2 score: ${allPlayerScore[1]} `; - if (allPlayerScore[0] > allPlayerScore[1]) { - compareMessage = `${compareMessage}
Player 1 wins!`; - } - - if (allPlayerScore[0] < allPlayerScore[1]) { - compareMessage = `${compareMessage}
Player 2 wins!`; - } - if (allPlayerScore[0] == allPlayerScore[1]) { - compareMessage = `${compareMessage}
It's a tie!`; +// Function that compares the two scores +var scoreComparison = function () { + var message = ""; + if (gameState == gameStateCompare) { + // Player 1 win condition + if (diceCalcHolder[0] > diceCalcHolder[1]) { + message = `🥇Player 1 wins🥇!

Player 1 dice value is ${diceCalcHolder[0]}
Player 1 dice value is ${diceCalcHolder[1]}


Click submit to reset game.`; + } + // Player 2 win condition + else if (diceCalcHolder[1] > diceCalcHolder[0]) { + message = `🥇Player 2 wins🥇!

Player 2 dice value is ${diceCalcHolder[1]}
Player 1 dice value is ${diceCalcHolder[0]}


Click submit to reset game.`; + } + // TIE + else if ((diceCalcHolder[1] = diceCalcHolder[0])) { + message = `It's a tie!

Player 2 dice value is ${diceCalcHolder[1]}
Player 1 dice value is ${diceCalcHolder[0]}


Click submit to reset game.`; + } } - return compareMessage; -}; - -var resetGame = function () { - currentPlayer = 1; - gameState = GAME_STATE_DICE_ROLL; - allPlayerScore = []; + return message; }; +// MAIN FUNCTION var main = function (input) { - console.log(`Checking game state on submit click: ${gameState}`); - console.log(`Checking currentPlayer on submit click: ${currentPlayer}`); var myOutputValue = ""; - if (gameState == GAME_STATE_DICE_ROLL) { - console.log("Control flow: gameState = GAME_STATE_DICE_ROLL"); - - myOutputValue = rollDiceForPlayer(); - - gameState = GAME_STATE_CHOOSE_DICE_ORDER; + // Dice roll + if (gameState == gameStateDiceRoll) { + console.log(`Control flow: Set game to dice roll state`); + gameState = gameStateOrder; + myOutputValue = playerRoll(); return myOutputValue; } - if (gameState == GAME_STATE_CHOOSE_DICE_ORDER) { - console.log("Control flow: input validation for input not 1 and 2"); - myOutputValue = getPlayerScore(input); - - if (currentPlayer == 1) { - console.log(`Control flow: end of player 1, now player 2`); - currentPlayer = 2; - gameState = gameState = GAME_STATE_DICE_ROLL; - return myOutputValue + `

It is now player 2's turn`; - } - if (currentPlayer == 2) { - console.log( - `Control flow: end of player 2, Next submit click will calculate score` + // Order the dice + if (gameState == gameStateOrder) { + console.log(`Control flow: Set game to dice order state`); + myOutputValue = addNum(input); + if (player == 1) { + player = 2; + gameState = gameStateDiceRoll; + return ( + myOutputValue + `
Click submit button for player 2 to roll the dice` ); - gameState = GAME_STATE_COMPARE_SCORES; - return myOutputValue + "

Press submit to calculate scores!"; + } else if (player == 2) { + gameState = gameStateCompare; + return myOutputValue + `
Click submit button to reveal the winner!`; } + return myOutputValue; } - if (gameState == GAME_STATE_COMPARE_SCORES) { - console.log(`Control flow: comparing scores`); - myOutputValue = comparePlayerScores(); - resetGame(); + // Compare the dice values + if (gameState == gameStateCompare) { + myOutputValue = scoreComparison(); + gameState = gameStateReset; return myOutputValue; } + + // Reset the game + if (gameState == gameStateReset) { + player = 1; + gameState = gameStateDiceRoll; + diceCalcHolder = []; + return `--GAME RESET--
Player 1 turn.
Click Submit to roll dice.`; + } };