From 83ce978fd03fde90046653b2c45d5011c9368f21 Mon Sep 17 00:00:00 2001 From: CapitolJazz Date: Sun, 4 Feb 2024 11:19:15 +0800 Subject: [PATCH 1/5] version01_one player rolls & order dice --- script.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index bbe8a293..37167ee8 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,65 @@ +var GAME_STATE_DICE_ROLL = 'GAME_STATE_DICE_ROLL'; +var GAME_STATE_CHOOSE_DICE_ROLL = 'GAME_STATE_CHOOSE_DICE_ROLL'; +var gameState = GAME_STATE_DICE_ROLL; + +var playerRolls = []; + +var rollDice = function(){ + console.log('Control flow: start of rollDice()') + var randomInteger = Math.ceil(Math.random() * 6); + + 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(playerInput){ + + if(playerInput != 1 && playerInput != 2){ + console.log('Control flow: input validation, valid input... Not 1 & not 2'); + return"Error! Please only input '1' or '2' to choose which dice to use as the first digit.

Your dice rolls are:
Dice 1: " + playerRolls[0] + "| Dice 2: " + playerRolls[1] + "."; + } + + if(playerInput == 1){ + var playerScore = Number(String(playerRolls[0]) + String(playerRolls[1])); + return "Your chosen value is:" + playerScore; + } + + if(playerInput == 2){ + var playerScore = Number(String(playerRolls[1]) + String(playerRolls[0])); + return "Your chosen value is:" + playerScore; + } +}; + var main = function (input) { - var myOutputValue = 'hello world'; + console.log('Checking game state on submit click:', gameState); + var myOutputValue = ''; + + if(gameState == GAME_STATE_DICE_ROLL){ + console.log ('Control flow: gameState == GAME_STATE_DICE_ROLL'); + } + + outputMessage = rollDiceForPlayer(); + gameState = GAME_STATE_CHOOSE_DICE_ROLL; + + if(gameState == GAME_STATE_CHOOSE_DICE_ROLL){ + console.log('Control flow: gameState == GAME_STATE_CHOOSE_DICE_ROLL'); + + outputMessage = getPlayerScore(input); + return outputMessage + + } + return myOutputValue; }; From 3378e2d629efb5686f75315c510813e3ad5acd0c Mon Sep 17 00:00:00 2001 From: CapitolJazz Date: Sun, 4 Feb 2024 16:06:05 +0800 Subject: [PATCH 2/5] vers02. two players roll and order dice --- script.js | 57 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/script.js b/script.js index 37167ee8..f72aad72 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_ROLL = 'GAME_STATE_CHOOSE_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 allPlayersScore = []; var rollDice = function(){ console.log('Control flow: start of rollDice()') @@ -16,50 +20,69 @@ 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 playerScore; + if(playerInput != 1 && playerInput != 2){ console.log('Control flow: input validation, valid input... Not 1 & not 2'); - return"Error! Please only input '1' or '2' 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 '2' to choose which dice to use as the first digit.

Your dice rolls are:
Dice 1: " + currentPlayerRolls[0] + "| Dice 2: " + currentPlayerRolls[1] + "."; } if(playerInput == 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; } if(playerInput == 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])); + + allPlayersScore.push(playerScore); + + curren = []; + return "Player "+ currentPlayer + ", your chosen value is: " + playerScore; } }; var main = function (input) { console.log('Checking game state on submit click:', gameState); - var myOutputValue = ''; + 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'); - } - outputMessage = rollDiceForPlayer(); - gameState = GAME_STATE_CHOOSE_DICE_ROLL; + outputMessage = rollDiceForPlayer(); - if(gameState == GAME_STATE_CHOOSE_DICE_ROLL){ + gameState = GAME_STATE_CHOOSE_DICE_ORDER; + return outputMessage; + } + + if(gameState == GAME_STATE_CHOOSE_DICE_ORDER){ console.log('Control flow: gameState == GAME_STATE_CHOOSE_DICE_ROLL'); outputMessage = getPlayerScore(input); - return outputMessage + 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){ + 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."; + } } - return myOutputValue; -}; +} \ No newline at end of file From dd6807ef0b6f16ea83ffb275b3a804b429730895 Mon Sep 17 00:00:00 2001 From: CapitolJazz Date: Sun, 4 Feb 2024 16:39:54 +0800 Subject: [PATCH 3/5] vers03. compare scores and declare winner --- script.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/script.js b/script.js index f72aad72..7ae56928 100644 --- a/script.js +++ b/script.js @@ -52,6 +52,26 @@ var getPlayerScore = function(playerInput){ } }; +//Need to check this function +var comparePlayersScore = function(){ + + var compareMessage = "Player 1 score:" + allPlayersScore[0]+ "
Player 2 score: " + allPlayersScore[1]; + + if(allPlayersScore[0] > allPlayersScore[1]){ + compareMessage = compareMessage + "

Player 1 wins."; + } + + if(allPlayersScore[0] < allPlayersScore[1]){ + compareMessage = compareMessage + "

Player 2 wins."; + } + + if(allPlayersScore[0] == allPlayersScore[1]){ + compareMessage = compareMessage + "

It is 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); @@ -85,4 +105,10 @@ var main = function (input) { } } + if(gameState == GAME_STATE_COMPARE_SCORES){ + console.log('Control flow: gameState == GAME_STATE_COMPARE_SCORES'); + + outputMessage = comparePlayersScore(); + return outputMessage; + } } \ No newline at end of file From 903e725bea2d4c8fc6a90a248938e82c05dbf142 Mon Sep 17 00:00:00 2001 From: CapitolJazz Date: Sun, 4 Feb 2024 16:52:20 +0800 Subject: [PATCH 4/5] vers04. reset and restart game without refreshing browser --- script.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/script.js b/script.js index 7ae56928..9746ce8b 100644 --- a/script.js +++ b/script.js @@ -53,7 +53,7 @@ var getPlayerScore = function(playerInput){ }; //Need to check this function -var comparePlayersScore = function(){ +var comparePlayersScores = function(){ var compareMessage = "Player 1 score:" + allPlayersScore[0]+ "
Player 2 score: " + allPlayersScore[1]; @@ -72,6 +72,12 @@ var comparePlayersScore = 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); @@ -108,7 +114,12 @@ var main = function (input) { if(gameState == GAME_STATE_COMPARE_SCORES){ console.log('Control flow: gameState == GAME_STATE_COMPARE_SCORES'); - outputMessage = comparePlayersScore(); + outputMessage = comparePlayersScores(); + + resetGame(); + console.log("Current player after reset: ", currentPlayer); + console.log("Game state after reset:", gameState); + console.log("allPlayersScore array: ", allPlayersScore); return outputMessage; } } \ No newline at end of file From 7923afd08de226a4e2e8c10290e5c19fd5f0065a Mon Sep 17 00:00:00 2001 From: CapitolJazz Date: Tue, 6 Feb 2024 16:12:34 +0800 Subject: [PATCH 5/5] vers04. reset and restart game without refreshing browser --- script.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/script.js b/script.js index 9746ce8b..babdb018 100644 --- a/script.js +++ b/script.js @@ -55,7 +55,7 @@ var getPlayerScore = function(playerInput){ //Need to check this function var comparePlayersScores = function(){ - var compareMessage = "Player 1 score:" + allPlayersScore[0]+ "
Player 2 score: " + allPlayersScore[1]; + var compareMessage = "Player 1 score:" + allPlayersScore[0] + "
Player 2 score: " + allPlayersScore[1]; if(allPlayersScore[0] > allPlayersScore[1]){ compareMessage = compareMessage + "

Player 1 wins."; @@ -72,7 +72,7 @@ var comparePlayersScores = function(){ return compareMessage; }; -var resetGame = function(){ +var resetGame = function(){ currentPlayer = 1; gameState = GAME_STATE_DICE_ROLL; allPlayersScore = [];