From 58406bc951febbdf69c030cec94610cdd0b4512b Mon Sep 17 00:00:00 2001 From: Nathan Date: Mon, 5 Feb 2024 21:33:36 +0800 Subject: [PATCH 1/2] Version 1 --- script.js | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 113 insertions(+), 4 deletions(-) diff --git a/script.js b/script.js index bbe8a293..cd1a9f5b 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,113 @@ -var main = function (input) { - var myOutputValue = 'hello world'; - return myOutputValue; -}; +var gameStateDiceRoll = `GAME STATE DICE ROLL`; +var gameStateChooseDiceOrder = `GAME STATE CHOOSE DICE ORDER`; +var gameState = gameStateDiceRoll; +var gameStateCompareScores = "GAME STATE COMPARE SCORES"; + +var currentPlayerRolls = []; +var currentPlayer = 1; +var allPlayerScores = []; + +function rollDice() { + return Math.floor(Math.random() * 6) + 1; +} + +function rollDIceForPlayer() { + console.log(`startroll for player 1`); + var counter = 0; + + for (counter = 0; counter < 2; counter++) { + currentPlayerRolls.push(rollDice()); + } + + console.log(`the player1 roll` + currentPlayerRolls); + return ( + "Welcome player " + + currentPlayer + + " you rolled Dice 1 : " + + currentPlayerRolls[0] + + " | Dice 2 : " + + currentPlayerRolls[1] + + " . Now please enter 1 or 2 to choose which number to be used as the first digit of ur final value" + ); +} + +function getplayerScore(playerInput) { + var playerscore; + if (playerInput != 1 && playerInput != 2) { + return ( + "please put in 1 or 2. Your rolls are:
Dice1: " + + currentPlayerRolls[0] + + " | Dice 2: " + + currentPlayerRolls[1] + ); + } + + if (playerInput == 1) { + playerscore = Number( + String(currentPlayerRolls[0]) + String(currentPlayerRolls[1]) + ); + return "your chosen score is " + playerscore; + } + + if (playerInput == 2) { + playerscore = Number( + String(currentPlayerRolls[1]) + String(currentPlayerRolls[0]) + ); + return "your chosen score is " + playerscore; + } + + //pushing the players score into the array + allPlayerScores.push(playerscore); + + currentPlayerRolls = []; + + return "player " + currentPlayer + " your chosen value is: " + playerscore; +} + +function main(input) { + var myOutputMessage = ""; + + if (gameState == gameStateDiceRoll) { + myOutputMessage = rollDIceForPlayer(); + + gameState = gameStateChooseDiceOrder; + return myOutputMessage; + } + + if (gameState == gameStateChooseDiceOrder) { + myOutputMessage = getplayerScore(input); + + if (currentPlayer == 1) { + currentPlayer = 2; + gameState = gameStateDiceRoll; + return myOutputMessage + "

it is now player 2's turn"; + } + + if (currentPlayer == 2) { + gameState = gameStateCompareScores; + return myOutputMessage + "

press to calculate score"; + } + } + + if (gameState == gameStateCompareScores) { + console.log("The game state now : " + gameStateCompareScores); + myOutputMessage = + " player 1 score: " + + allPlayerScores[0] + + "player 2 score" + + allPlayerScores[1]; + + // if player 1 wins + if (allPlayerScores[0] > allPlayerScores[1]) { + myOutputMessage = myOutputMessage + "player 1 wins"; + } + if (allPlayerScores[1] > allPlayerScores[0]) { + myOutputMessage = myOutputMessage + "player 2 wins"; + } + + if (allPlayerScores[0] == allPlayerScores[1]) { + myOutputMessage = "it is a tie"; + } + return myOutputMessage; + } +} From fd7d819c8b35cf2d5ba67b25808a89cf85c41a3b Mon Sep 17 00:00:00 2001 From: Nathan Date: Tue, 6 Feb 2024 11:27:09 +0800 Subject: [PATCH 2/2] making it replayable --- script.js | 81 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 28 deletions(-) diff --git a/script.js b/script.js index cd1a9f5b..5b9c7d5f 100644 --- a/script.js +++ b/script.js @@ -12,14 +12,17 @@ function rollDice() { } function rollDIceForPlayer() { - console.log(`startroll for player 1`); - var counter = 0; + console.log(`start roll for player ${currentPlayer}`); + //var currentPlayerRolls = []; - for (counter = 0; counter < 2; counter++) { + for (var counter = 0; counter < 2; counter++) { currentPlayerRolls.push(rollDice()); } - console.log(`the player1 roll` + currentPlayerRolls); + console.log( + `the player ${currentPlayer} roll: ${currentPlayerRolls[0]} and ${currentPlayerRolls[1]}` + ); + return ( "Welcome player " + currentPlayer + @@ -27,7 +30,7 @@ function rollDIceForPlayer() { currentPlayerRolls[0] + " | Dice 2 : " + currentPlayerRolls[1] + - " . Now please enter 1 or 2 to choose which number to be used as the first digit of ur final value" + "

Now please enter 1 or 2 to choose which number to be used as the first digit of ur final value" ); } @@ -35,7 +38,7 @@ function getplayerScore(playerInput) { var playerscore; if (playerInput != 1 && playerInput != 2) { return ( - "please put in 1 or 2. Your rolls are:
Dice1: " + + "please put in 1 or 2.

Your rolls are:
Dice1: " + currentPlayerRolls[0] + " | Dice 2: " + currentPlayerRolls[1] @@ -46,6 +49,8 @@ function getplayerScore(playerInput) { playerscore = Number( String(currentPlayerRolls[0]) + String(currentPlayerRolls[1]) ); + currentPlayerRolls = []; + allPlayerScores.push(playerscore); return "your chosen score is " + playerscore; } @@ -53,17 +58,43 @@ function getplayerScore(playerInput) { playerscore = Number( String(currentPlayerRolls[1]) + String(currentPlayerRolls[0]) ); + currentPlayerRolls = []; + allPlayerScores.push(playerscore); return "your chosen score is " + playerscore; } //pushing the players score into the array - allPlayerScores.push(playerscore); - - currentPlayerRolls = []; return "player " + currentPlayer + " your chosen value is: " + playerscore; } +function comparescores() { + var compareMessage = + " player 1 score: " + + allPlayerScores[0] + + "

player 2 score: " + + allPlayerScores[1]; + + // if player 1 wins + if (allPlayerScores[0] > allPlayerScores[1]) { + compareMessage = compareMessage + "

player 1 wins"; + } + if (allPlayerScores[1] > allPlayerScores[0]) { + compareMessage = compareMessage + "

player 2 wins"; + } + + if (allPlayerScores[0] == allPlayerScores[1]) { + compareMessage = "it is a tie"; + } + return compareMessage; +} + +function resetGame() { + currentPlayer = 1; + gameState = gameStateDiceRoll; + allPlayerScores = []; +} + function main(input) { var myOutputMessage = ""; @@ -77,37 +108,31 @@ function main(input) { if (gameState == gameStateChooseDiceOrder) { myOutputMessage = getplayerScore(input); + if (currentPlayer == 0) { + gameState = gameStateChooseDiceOrder; + return myOutputMessage; + } + if (currentPlayer == 1) { currentPlayer = 2; gameState = gameStateDiceRoll; return myOutputMessage + "

it is now player 2's turn"; - } - - if (currentPlayer == 2) { + } else if (currentPlayer == 2) { gameState = gameStateCompareScores; return myOutputMessage + "

press to calculate score"; + } else { + gameState = gameStateChooseDiceOrder; + return myOutputMessage; } } if (gameState == gameStateCompareScores) { console.log("The game state now : " + gameStateCompareScores); - myOutputMessage = - " player 1 score: " + - allPlayerScores[0] + - "player 2 score" + - allPlayerScores[1]; - - // if player 1 wins - if (allPlayerScores[0] > allPlayerScores[1]) { - myOutputMessage = myOutputMessage + "player 1 wins"; - } - if (allPlayerScores[1] > allPlayerScores[0]) { - myOutputMessage = myOutputMessage + "player 2 wins"; - } - if (allPlayerScores[0] == allPlayerScores[1]) { - myOutputMessage = "it is a tie"; - } + myOutputMessage = comparescores(); + + resetGame(); + return myOutputMessage; } }