From 1b6c0fc563e78f96d49f8128283237d3470b396e Mon Sep 17 00:00:00 2001 From: elkyli Date: Thu, 15 Feb 2024 18:46:35 +0800 Subject: [PATCH 1/2] first commit --- script.js | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/script.js b/script.js index bbe8a293..dd3c5805 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,81 @@ +// Create game states +var playerToRollDice = "rollDice"; +var playerChooseDiceOrder = "chooseDiceOrder"; + +var player2ToRollDice = "rollDice2"; +var player2ChooseDiceOrder = "chooseDiceOrder2"; + +var compareScores = "compareScores"; + +// Default game state +var currentGameMode = playerToRollDice; + +var player1DiceRoll1, player1DiceRoll2; +var player2DiceRoll1, player2DiceRoll2; + +var player1DiceOrder, player2DiceOrder; + +var randomRollDice = function () { + var randomDecimal = Math.random() * 6; + var randomInteger = Math.floor(randomDecimal); + var diceNumber = randomInteger + 1; + return diceNumber; +}; + var main = function (input) { - var myOutputValue = 'hello world'; - return myOutputValue; + // player 1 roll dice + if (currentGameMode == playerToRollDice) { + player1DiceRoll1 = randomRollDice(); + player1DiceRoll2 = randomRollDice(); + currentGameMode = playerChooseDiceOrder; + return `Player 1, here's what you rolled! 🎲

Dice 1: ${player1DiceRoll1}
Dice 2: ${player1DiceRoll2}

Choose your dice order by entering '1' or '2' in the input field.`; + } + + // player 1 choose dice order + if (currentGameMode == playerChooseDiceOrder) { + if (input == "1") { + player1DiceOrder = "" + player1DiceRoll1 + player1DiceRoll2; + } else { + player1DiceOrder = "" + player1DiceRoll2 + player1DiceRoll1; + } + currentGameMode = player2ToRollDice; + return `Player 1, your number is ${player1DiceOrder}.

Now it's Player 2's turn!`; + } + + // player 2 roll dice + if (currentGameMode == player2ToRollDice) { + player2DiceRoll1 = randomRollDice(); + player2DiceRoll2 = randomRollDice(); + currentGameMode = player2ChooseDiceOrder; + return `Player 2, here's what you rolled! 🎲

Dice 1: ${player2DiceRoll1}
Dice 2: ${player2DiceRoll2}

Choose your dice order by entering '1' or '2' in the input field.`; + } + + // player 2 choose dice order + if (currentGameMode == player2ChooseDiceOrder) { + if (input == "1") { + player2DiceOrder = "" + player2DiceRoll1 + player2DiceRoll2; + } else { + player2DiceOrder = "" + player2DiceRoll2 + player2DiceRoll1; + } + currentGameMode = compareScores; + return `Player 2, your number is ${player2DiceOrder}.

Let's see who wins!`; + } + + // compare player 1 score with player 2 score and define winner + if (currentGameMode == compareScores) { + var player1Score = parseInt(player1DiceOrder); + var player2Score = parseInt(player2DiceOrder); + var resultMessage = ""; + + if (player1Score > player2Score) { + resultMessage = `🏆 Player 1 wins! 🎉

Player 1: ${player1Score}
Player 2: ${player2Score}

Click on the button to play again!`; + } else if (player1Score < player2Score) { + resultMessage = `🏆 Player 2 wins! 🎉

Player 1: ${player1Score}
Player 2: ${player2Score}

Click on the button to play again!`; + } else { + resultMessage = `It's a tie! 🤝

Player 1: ${player1Score}
Player 2: ${player2Score}

Click on the button to play again!`; + } + + currentGameMode = playerToRollDice; + return resultMessage; + } }; From 33db359e44bf3de6305d49c5ec28c18af58c0e2d Mon Sep 17 00:00:00 2001 From: elkyli Date: Thu, 15 Feb 2024 18:47:01 +0800 Subject: [PATCH 2/2] update html --- index.html | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/index.html b/index.html index 74f9da2a..6ea88a2f 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,7 @@ - Coding Basics + Beat That!