From 2db3a29fa2b37070b1c753954a03a4dd0cbd98ce Mon Sep 17 00:00:00 2001 From: Ying Ling Tan Date: Mon, 5 Feb 2024 17:36:26 +0800 Subject: [PATCH 01/11] vers 1 one player rolls and orders dice --- script.js | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 2 deletions(-) diff --git a/script.js b/script.js index bbe8a293..6b4fc90c 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,97 @@ +// ===== Requirements ===== // +// 1) there are 2 players and players take turns +// 2) when a player clicks submit, the game rolls 2 dice and shows the dice rolls, for example 3 and 6 +// 3) 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 +// 4) after both players have rolled and chosen dice order, the player with the higher combined number wins + +// ==== problem breakdown and planing ==== // +// ver 1. rolls 2 dice and turns the output for 1 player. That player chooses the dice order and get the correct return output. +// ver 2. refactored code to include player 2 +// 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 + +// Global Variables +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; + +var playerRolls = []; + +// Helper Function +var rollDice = function () { + var randomDecimal = Math.random() * 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) { + 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 the corresponding dice to be used as the first digit of your final value." + ); +}; + +var getPlayerScore = function (input) { + // input validation + if (input != 1 && input != 2) { + console.log( + "Control flow: input validation, invalid input... is NOT 1 AND NOT 2" + ); + return ( + "Error! Please only input '1' or '' to choose which dice to use as first digit.

Your dice rolls are:
Dice 1: " + + playerRolls[0] + + " | Dice 2: " + + playerRolls[1] + + "." + ); + } + // input == 1 + if (input == 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 (input == 2) { + console.log("Control flow: input == 2"); + var playerScore = Number(String(playerRolls[0]) + String(playerRolls[0])); + return "Your chosen 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 disce 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(playerInput); + gameState = GAME_STATE_DICE_ROLL; + return outputMessage; + } }; From 4da8b3e8b92fd1378be06cf3cb2fbfd916183f47 Mon Sep 17 00:00:00 2001 From: yinglingtan Date: Wed, 7 Feb 2024 12:26:19 +0800 Subject: [PATCH 02/11] vers 1. one player rolls and orders dice --- script.js | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/script.js b/script.js index 6b4fc90c..3dce325e 100644 --- a/script.js +++ b/script.js @@ -19,7 +19,10 @@ var playerRolls = []; // Helper Function 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 to 6 var randomInteger = Math.floor(randomDecimal) + 1; console.log("rollDice output, randomInteger: ", randomInteger); @@ -36,49 +39,49 @@ var rollDiceForPlayer = function () { console.log("rollDiceForPlayer changes, playerRolls: ", playerRolls); return ( - "Welcome

You rolled:
Dice 1: " + + "Welcome

You rolled:
Dice 1; " + playerRolls[0] + - " | Dice 2: " + + " | Dice 2:" + playerRolls[1] + ".

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 (input) { +var getPlayerScore = function (playerInput) { // input validation - if (input != 1 && input != 2) { + if (playerInput != 1 && playerInput != 2) { console.log( - "Control flow: input validation, invalid input... is NOT 1 AND NOT 2" + "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 first digit.

Your dice rolls are:
Dice 1: " + + 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 (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 (input == 2) { + if (playerInput == 2) { console.log("Control flow: input == 2"); - var playerScore = Number(String(playerRolls[0]) + String(playerRolls[0])); + var playerScore = Number(String(playerRolls[1]) + String(playerRolls[0])); return "Your chosen value is: " + playerScore; } }; - var main = function (input) { console.log("Checking game state on submit click: ", gameState); - var OutputMessage = ""; + var myOutputMessage = ""; + if (gameState == GAME_STATE_DICE_ROLL) { console.log("Control flow: gameState == GAME_STATE_DICE_ROLL"); - // Display disce rolled as output message + // Display dice rolled as output message outputMessage = rollDiceForPlayer(); // Change the game state @@ -89,9 +92,8 @@ var main = function (input) { if (gameState == GAME_STATE_CHOOSE_DICE_ORDER) { console.log("Control flow: gameState == GAME_STATE_CHOOSE_DICE_ORDER"); - // Call playerScore function - outputMessage = getPlayerScore(playerInput); - gameState = GAME_STATE_DICE_ROLL; + // Cal playerScore function + outputMessage = getPlayerScore(input); return outputMessage; } }; From f6dc2bb1398e091749ce6e76edd6b4ccfdc774ed Mon Sep 17 00:00:00 2001 From: yinglingtan Date: Wed, 7 Feb 2024 23:03:01 +0800 Subject: [PATCH 03/11] vers 1 one player rolls and orders dice --- script.js | 60 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/script.js b/script.js index 3dce325e..4ac0884b 100644 --- a/script.js +++ b/script.js @@ -7,6 +7,9 @@ // ==== problem breakdown and planing ==== // // ver 1. rolls 2 dice and turns the output for 1 player. That player chooses the dice order and get the correct return output. // ver 2. refactored code to include player 2 +// - global variables for currentPlayer; allPlayersScore +// - 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 @@ -15,7 +18,10 @@ 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; -var playerRolls = []; +var currentPlayerRolls = []; + +var currentPlayer = 1; +var allPlayersScore = []; // Helper Function var rollDice = function () { @@ -33,21 +39,26 @@ 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 the corresponding dice to be used as the first digit of your final value." + console.log( + "rollDiceForPlayer changes, currentPlayerRolls: ", + currentPlayerRolls ); + return; + "Welcome, Player " + + currentPlayer + + "

You rolled:
Dice 1; " + + currentPlayerRolls[0] + + " | Dice 2:" + + currentPlayerRolls[1] + + ".

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 playerScores; // input validation if (playerInput != 1 && playerInput != 2) { console.log( @@ -55,27 +66,38 @@ var getPlayerScore = function (playerInput) { ); 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] + + currentPlayerRolls[0] + " | Dice 2: " + - playerRolls[1] + + currentPlayerRolls[1] + "."; } // input == 1 if (playerInput == 1) { console.log("Control flow: input == 1"); - var playerScore = Number(String(playerRolls[0]) + String(playerRolls[1])); + var playerScore = Number( + String(currentPlayerRolls[0]) + String(currentPlayerRolls[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 chosen value is: " + playerScore; + var playerScore = Number( + String(currentPlayerRolls[1]) + String(currentPlayerRolls[0]) + ); } + // Store playerScore in array + allPlayersScore.push(playerScore); + + // clear current player rolls array + currentPlayer = []; + return "Player" + currentPlayer + ", your chosen value is: " + playerScore; }; + var main = function (input) { console.log("Checking game state on submit click: ", gameState); + console.log("Checking game state on submit click: ", currentPlayer); var myOutputMessage = ""; if (gameState == GAME_STATE_DICE_ROLL) { @@ -94,6 +116,16 @@ var main = function (input) { // Cal 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 + "

It is now player 2's turn!"; + } + + if (currentPlayer == 2) { + } return outputMessage; } }; From e9248d61c41079ec086c45e93204d437e0d6cfbd Mon Sep 17 00:00:00 2001 From: yinglingtan Date: Thu, 8 Feb 2024 00:08:15 +0800 Subject: [PATCH 04/11] vers 1 one player rolls and orders dice --- script.js | 51 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/script.js b/script.js index 4ac0884b..44a68880 100644 --- a/script.js +++ b/script.js @@ -16,6 +16,7 @@ // 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 = []; @@ -47,14 +48,15 @@ var rollDiceForPlayer = function () { "rollDiceForPlayer changes, currentPlayerRolls: ", currentPlayerRolls ); - return; - "Welcome, Player " + + return ( + "Welcome, Player " + currentPlayer + "

You rolled:
Dice 1; " + currentPlayerRolls[0] + " | Dice 2:" + currentPlayerRolls[1] + - ".

Now, please input either '1' or '2' to choose the corresponding dice to be used as the first digit of your final value."; + ".

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) { @@ -64,12 +66,13 @@ var getPlayerScore = function (playerInput) { 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.

Your dice rolls are:
Dice 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) { @@ -91,14 +94,14 @@ var getPlayerScore = function (playerInput) { allPlayersScore.push(playerScore); // clear current player rolls array - currentPlayer = []; + 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 game state on submit click: ", currentPlayer); - var myOutputMessage = ""; + var outputMessage = ""; if (gameState == GAME_STATE_DICE_ROLL) { console.log("Control flow: gameState == GAME_STATE_DICE_ROLL"); @@ -114,7 +117,7 @@ var main = function (input) { if (gameState == GAME_STATE_CHOOSE_DICE_ORDER) { console.log("Control flow: gameState == GAME_STATE_CHOOSE_DICE_ORDER"); - // Cal playerScore function + // Call playerScore function outputMessage = getPlayerScore(input); if (currentPlayer == 1) { @@ -125,7 +128,37 @@ var main = function (input) { } 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 + "

Press submit to calculate scores!"; } + } + + if (gameState == GAME_STATE_COMPARE_SCORES) { + console.log("Control flow: gameState == GAME_STATE_COMPARE_SCORES"); + + outputMessage = + "Player 1 score: " + + allPlayersScore[0] + + "
Player 2 score: " + + allPlayersScore[1]; + + // player 1 wins + if (allPlayerScore[0] > allPlayersScore[1]) { + outputMessage = outputMessage + "

Player 1 wins!"; + } + // player 2 wins + if (allPlayerScore[0] > allPlayersScore[1]) { + outputMessage = outputMessage + "

Player 2 wins!"; + } + // tie + if (allPlayersScore[0] == allPlayersScore(1)) { + outputMessage = outputMessage + "

It's a tie!"; + } + return outputMessage; } }; From 68f052493e9addd4eaf927ded35a8395f80102d7 Mon Sep 17 00:00:00 2001 From: yinglingtan Date: Thu, 8 Feb 2024 00:57:00 +0800 Subject: [PATCH 05/11] ver 3 --- script.js | 67 +++++++++++++++++++++++-------------------------------- 1 file changed, 28 insertions(+), 39 deletions(-) diff --git a/script.js b/script.js index 44a68880..3690836a 100644 --- a/script.js +++ b/script.js @@ -1,18 +1,3 @@ -// ===== Requirements ===== // -// 1) there are 2 players and players take turns -// 2) when a player clicks submit, the game rolls 2 dice and shows the dice rolls, for example 3 and 6 -// 3) 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 -// 4) after both players have rolled and chosen dice order, the player with the higher combined number wins - -// ==== problem breakdown and planing ==== // -// ver 1. rolls 2 dice and turns the output for 1 player. That player chooses the dice order and get the correct return output. -// ver 2. refactored code to include player 2 -// - global variables for currentPlayer; allPlayersScore -// - 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 - // Global Variables var GAME_STATE_DICE_ROLL = "GAME_STATE_DICE_ROLL"; var GAME_STATE_CHOOSE_DICE_ORDER = "GAME_STATE_CHOOSE_DICE_ORDER"; @@ -22,7 +7,7 @@ var gameState = GAME_STATE_DICE_ROLL; var currentPlayerRolls = []; var currentPlayer = 1; -var allPlayersScore = []; +var allPlayerScore = []; // Helper Function var rollDice = function () { @@ -51,16 +36,16 @@ var rollDiceForPlayer = function () { return ( "Welcome, Player " + currentPlayer + - "

You rolled:
Dice 1; " + + "

You rolled:
Dice 1: " + currentPlayerRolls[0] + - " | Dice 2:" + + " | Dice 2: " + currentPlayerRolls[1] + ".

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 playerScores; + var playerScore; // input validation if (playerInput != 1 && playerInput != 2) { console.log( @@ -91,13 +76,35 @@ var getPlayerScore = function (playerInput) { ); } // Store playerScore in array - allPlayersScore.push(playerScore); + allPlayerScore.push(playerScore); // clear current player rolls array currentPlayerRolls = []; return "Player" + currentPlayer + ", your chosen value is: " + playerScore; }; +var comparePlayerScore = function () { + var outputMessage = + "Player 1 score: " + + allPlayerScore[0] + + "
Player 2 score: " + + allPlayerScore[1]; + + // player 1 wins + if (allPlayerScore[0] > allPlayerScore[1]) { + outputMessage = outputMessage + "

Player 1 wins!"; + } + // player 2 wins + if (allPlayerScore[0] < allPlayerScore[1]) { + outputMessage = outputMessage + "

Player 2 wins!"; + } + // tie + if (allPlayerScore[0] == allPlayerScore[1]) { + outputMessage = outputMessage + "

It's a tie!"; + return outputMessage; + } +}; + var main = function (input) { console.log("Checking game state on submit click: ", gameState); console.log("Checking game state on submit click: ", currentPlayer); @@ -140,25 +147,7 @@ var main = function (input) { if (gameState == GAME_STATE_COMPARE_SCORES) { console.log("Control flow: gameState == GAME_STATE_COMPARE_SCORES"); - outputMessage = - "Player 1 score: " + - allPlayersScore[0] + - "
Player 2 score: " + - allPlayersScore[1]; - - // player 1 wins - if (allPlayerScore[0] > allPlayersScore[1]) { - outputMessage = outputMessage + "

Player 1 wins!"; - } - // player 2 wins - if (allPlayerScore[0] > allPlayersScore[1]) { - outputMessage = outputMessage + "

Player 2 wins!"; - } - // tie - if (allPlayersScore[0] == allPlayersScore(1)) { - outputMessage = outputMessage + "

It's a tie!"; - } - + outputMessage = comparePlayerScore(); return outputMessage; } }; From 606198af47863292054a4640dd760d641beca80a Mon Sep 17 00:00:00 2001 From: yinglingtan Date: Wed, 14 Feb 2024 11:11:17 +0800 Subject: [PATCH 06/11] ver 2. two players roll and order --- script.js | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/script.js b/script.js index 3690836a..e6112392 100644 --- a/script.js +++ b/script.js @@ -7,7 +7,7 @@ var gameState = GAME_STATE_DICE_ROLL; var currentPlayerRolls = []; var currentPlayer = 1; -var allPlayerScore = []; +var allPlayersScore = []; // Helper Function var rollDice = function () { @@ -62,7 +62,7 @@ var getPlayerScore = function (playerInput) { // input == 1 if (playerInput == 1) { console.log("Control flow: input == 1"); - var playerScore = Number( + playerScore = Number( String(currentPlayerRolls[0]) + String(currentPlayerRolls[1]) ); return "Your chosen value is: " + playerScore; @@ -71,12 +71,12 @@ var getPlayerScore = function (playerInput) { // input == 2 if (playerInput == 2) { console.log("Control flow: input == 2"); - var playerScore = Number( + playerScore = Number( String(currentPlayerRolls[1]) + String(currentPlayerRolls[0]) ); } // Store playerScore in array - allPlayerScore.push(playerScore); + allPlayersScore.push(playerScore); // clear current player rolls array currentPlayerRolls = []; @@ -84,22 +84,26 @@ var getPlayerScore = function (playerInput) { }; var comparePlayerScore = function () { + if (allPlayersScore.length < 2) { + return "Error: Not all player scores are defined."; + } + var outputMessage = "Player 1 score: " + - allPlayerScore[0] + + allPlayersScore[0] + "
Player 2 score: " + - allPlayerScore[1]; + allPlayersScore[1]; // player 1 wins - if (allPlayerScore[0] > allPlayerScore[1]) { + if (allPlayersScore[0] > allPlayersScore[1]) { outputMessage = outputMessage + "

Player 1 wins!"; } // player 2 wins - if (allPlayerScore[0] < allPlayerScore[1]) { + if (allPlayersScore[0] < allPlayersScore[1]) { outputMessage = outputMessage + "

Player 2 wins!"; } // tie - if (allPlayerScore[0] == allPlayerScore[1]) { + if (allPlayersScore[0] == allPlayersScore[1]) { outputMessage = outputMessage + "

It's a tie!"; return outputMessage; } @@ -107,7 +111,7 @@ var comparePlayerScore = function () { var main = function (input) { console.log("Checking game state on submit click: ", gameState); - console.log("Checking game state on submit click: ", currentPlayer); + console.log("Checking currentPlayer on submit click: ", currentPlayer); var outputMessage = ""; if (gameState == GAME_STATE_DICE_ROLL) { @@ -144,10 +148,10 @@ var main = function (input) { } } - if (gameState == GAME_STATE_COMPARE_SCORES) { - console.log("Control flow: gameState == GAME_STATE_COMPARE_SCORES"); + // if (gameState == GAME_STATE_COMPARE_SCORES) { + // console.log("Control flow: gameState == GAME_STATE_COMPARE_SCORES"); - outputMessage = comparePlayerScore(); - return outputMessage; - } + // outputMessage = comparePlayerScore(); + // return outputMessage; + // } }; From eb3526dfe1b286a3dc1bf9768c0be4b1ef905e42 Mon Sep 17 00:00:00 2001 From: yinglingtan Date: Wed, 14 Feb 2024 11:24:38 +0800 Subject: [PATCH 07/11] ver 2: two player roll and order dice --- script.js | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/script.js b/script.js index e6112392..c0295eb7 100644 --- a/script.js +++ b/script.js @@ -83,32 +83,6 @@ var getPlayerScore = function (playerInput) { return "Player" + currentPlayer + ", your chosen value is: " + playerScore; }; -var comparePlayerScore = function () { - if (allPlayersScore.length < 2) { - return "Error: Not all player scores are defined."; - } - - var outputMessage = - "Player 1 score: " + - allPlayersScore[0] + - "
Player 2 score: " + - allPlayersScore[1]; - - // player 1 wins - if (allPlayersScore[0] > allPlayersScore[1]) { - outputMessage = outputMessage + "

Player 1 wins!"; - } - // player 2 wins - if (allPlayersScore[0] < allPlayersScore[1]) { - outputMessage = outputMessage + "

Player 2 wins!"; - } - // tie - if (allPlayersScore[0] == allPlayersScore[1]) { - outputMessage = outputMessage + "

It's a tie!"; - return outputMessage; - } -}; - var main = function (input) { console.log("Checking game state on submit click: ", gameState); console.log("Checking currentPlayer on submit click: ", currentPlayer); @@ -124,7 +98,6 @@ var main = function (input) { 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"); @@ -137,7 +110,6 @@ var main = function (input) { 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 score" @@ -147,11 +119,4 @@ 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 = comparePlayerScore(); - // return outputMessage; - // } }; From 229a9d95ea62804d88eab3779e97b01c12728eef Mon Sep 17 00:00:00 2001 From: yinglingtan Date: Wed, 14 Feb 2024 11:58:38 +0800 Subject: [PATCH 08/11] ver3. compare players score --- script.js | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/script.js b/script.js index c0295eb7..055e9635 100644 --- a/script.js +++ b/script.js @@ -65,11 +65,9 @@ var getPlayerScore = function (playerInput) { playerScore = Number( String(currentPlayerRolls[0]) + String(currentPlayerRolls[1]) ); - return "Your chosen value is: " + playerScore; } - // input == 2 - if (playerInput == 2) { + else if (playerInput == 2) { console.log("Control flow: input == 2"); playerScore = Number( String(currentPlayerRolls[1]) + String(currentPlayerRolls[0]) @@ -83,6 +81,29 @@ var getPlayerScore = function (playerInput) { return "Player" + currentPlayer + ", your chosen value is: " + playerScore; }; +var comparePlayerScore = 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); @@ -119,4 +140,11 @@ 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 = comparePlayerScore(); + return outputMessage; + } }; From 35c83d5b42d246984224a65770b5639b5b311a75 Mon Sep 17 00:00:00 2001 From: yinglingtan Date: Wed, 14 Feb 2024 12:05:18 +0800 Subject: [PATCH 09/11] vers 4 reset game --- script.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/script.js b/script.js index 055e9635..9caa3836 100644 --- a/script.js +++ b/script.js @@ -104,6 +104,12 @@ var comparePlayerScore = 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); console.log("Checking currentPlayer on submit click: ", currentPlayer); @@ -145,6 +151,12 @@ var main = function (input) { 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; } }; From cbfe11787830aad204b4bdc4d832e43d8f218d8e Mon Sep 17 00:00:00 2001 From: yingling Date: Wed, 14 Feb 2024 12:30:24 +0800 Subject: [PATCH 10/11] Create CNAME --- CNAME | 1 + 1 file changed, 1 insertion(+) create mode 100644 CNAME diff --git a/CNAME b/CNAME new file mode 100644 index 00000000..3c65d52c --- /dev/null +++ b/CNAME @@ -0,0 +1 @@ +hey.yingling \ No newline at end of file From 3cb1e5f92873cb4a7e86e85da554eedc77b17617 Mon Sep 17 00:00:00 2001 From: yingling Date: Wed, 14 Feb 2024 12:30:38 +0800 Subject: [PATCH 11/11] Delete CNAME --- CNAME | 1 - 1 file changed, 1 deletion(-) delete mode 100644 CNAME diff --git a/CNAME b/CNAME deleted file mode 100644 index 3c65d52c..00000000 --- a/CNAME +++ /dev/null @@ -1 +0,0 @@ -hey.yingling \ No newline at end of file