diff --git a/README.md b/README.md index 796fa5ec..633f7503 100644 --- a/README.md +++ b/README.md @@ -1 +1,22 @@ # Rocket Academy Coding Basics: Beat That! + +## Overview +This is a simple two-player dice game built as part of Rocket Academy's Coding Fundamentals course. The game has two modes: "regular" and "reverse." In the regular mode, the player with the highest dice combination wins, while in the reverse mode, the player with the lowest combination wins. + +## How to Play +1. Enter "regular" or "reverse" to select a game mode. +2. Player 1 rolls two dice. +3. If the two dice are different, Player 1 chooses the order of the dice. +4. Player 2 rolls two dice and chooses the order if needed. +5. The game compares both players' scores and announces the winner. +6. Players can continue the game by pressing "Submit" after each round. + + +## Instructions +1. Clone or download the project files. +2. Open the index.html file in your browser. +3. Follow the on-screen instructions to play the game. + +## Game Screenshot +![Game Screenshot_0](https://github.com/user-attachments/assets/e9026fd0-936d-4a84-89e4-4b1a9b16afe5) + diff --git a/index.html b/index.html index 74f9da2a..bdd55cb4 100644 --- a/index.html +++ b/index.html @@ -18,7 +18,7 @@ } #container { - background-color: pink; + background-color: lightcyan; margin: 40px auto; max-width: 800px; padding: 38px 31px; @@ -51,6 +51,21 @@

Basics: Beat That! 🚀

+

Hello! Welcome to Beat That!

+

+ There are two game modes. Please enter "regular" or "reverse" to choose + your preferred mode. +

+

Regular Game mode:

+

+ Roll two dices and put them in order to make the highest number + possible.The player with the highest number wins! +

+

Reverse Game mode:

+

+ Roll two dices and put them in order to make the lowest number + possible.The player with the highest number wins! +

Input:


diff --git a/script.js b/script.js index bbe8a293..00c2c492 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,201 @@ -var main = function (input) { - var myOutputValue = 'hello world'; - return myOutputValue; -}; +// Declare global variables +var player1Dice = []; +var player2Dice = []; +var sum1 = 0; +var sum2 = 0; +var player1score = 0; +var player2score = 0; + +var winner = " "; +var players = ["Player 1", "Player 2"]; +var message = " "; +var gameStatus = "Player 1"; +var numOfDice = 2; +var gameMode = "Waiting for Game Mode"; + +// Helper functions +function getDice() { + var randomNum = Math.floor(Math.random() * 6) + 1; + return randomNum; +} + +function compareDiceNum(num1, num2) { + if (gameStatus == "Player 1") { + if (num1 == num2) { + sum1 = player1Dice[0] * 10 + player1Dice[1]; + message = `🎲 WELCOME, Player One 🎲 +
You rolled ${player1Dice[0]} for dice one and ${player1Dice[1]} for dice two. +
Your number is ${sum1} +
It's now Player 2's turn.`; + gameStatus = "Player 2"; + } else { + gameStatus = "Player 1: choose dice order"; + message = `🎲 WELCOME, Player One 🎲 +
You rolled ${player1Dice[0]} for dice one and ${player1Dice[1]} for dice two. +
Choose the order of the dice by entering "1" or "2"`; + } + } else if (gameStatus == "Player 2") { + if (num1 == num2) { + sum2 = player2Dice[0] * 10 + player2Dice[1]; + message = `🎲 WELCOME, Player Two 🎲 +
You rolled ${player2Dice[0]} for dice one and ${player2Dice[1]} for dice two. +
Your number is ${sum2} +
Press "Submit" to find out who's the winner.`; + gameStatus = "result"; + } else { + gameStatus = "Player 2: choose dice order"; + message = `🎲 WELCOME, Player Two 🎲 +
You rolled ${player2Dice[0]} for dice one and ${player2Dice[1]} for dice two. +
Choose the order of the dice by entering "1" or "2"`; + } + } + return message; +} + +function getDiceMessage(order) { + if (gameStatus == "Player 1: choose dice order") { + if (order == 1) { + sum1 = player1Dice[0] * 10 + player1Dice[1]; + message = `🎲 PLAYER 1 🎲
You chose Dice 1 first. Your number is ${sum1}. It's now Player 2's turn.`; + gameStatus = "Player 2"; + } else if (order == 2) { + sum1 = player1Dice[1] * 10 + player1Dice[0]; + message = `🎲 PLAYER 1 🎲
You chose Dice 2 first. Your number is ${sum1}.
It's now Player 2's turn.`; + gameStatus = "Player 2"; + } else { + message = `You rolled ${player1Dice[0]} for dice one and ${player1Dice[1]} for dice two.Please enter 1 or 2.`; + } + } else if (gameStatus == "Player 2: choose dice order") { + if (order == 1) { + sum2 = player2Dice[0] * 10 + player2Dice[1]; + message = `🎲 PLAYER 2 🎲
You chose Dice 1 first. Your number is ${sum2}.
Press "Submit" to find out who's the winner.`; + gameStatus = "result"; + } else if (order == 2) { + sum2 = player2Dice[1] * 10 + player2Dice[0]; + message = `🎲 PLAYER 2 🎲
You chose Dice 2 first. Your number is ${sum2}.
Press "Submit" to find out who's the winner.`; + gameStatus = "result"; + } else { + message = `
You rolled ${player2Dice[0]} for dice one and ${player2Dice[1]} for dice two. Please enter 1 or 2.`; + } + } + return message; +} + +function getWinnerMessage(player1score, player2score) { + if (gameMode == "regular") { + if (player1score > player2score) { + winner = players[0]; + message = `${winner} is leading!🏆
Player One: ${player1score}
Player Two: ${player2score}
Press "Submit" to continue the game.`; + gameStatus = "Player 1"; + } else if (player2score > player1score) { + winner = players[1]; + message = `${winner} is leading!🏆
Player Two: ${player2score}
Player One: ${player1score}
Press "Submit" to continue the game.`; + gameStatus = "Player 1"; + } else { + message = `It's a draw. Press "Submit" to continue the game.`; + gameStatus = "Player 1"; + } + return message; + } else if (gameMode == "reverse") { + if (player1score < player2score) { + winner = players[0]; + message = `${winner} is leading!🏆
Player One: ${player1score}
Player Two: ${player2score}
Press "Submit" to continue the game.`; + gameStatus = "Player 1"; + } else if (player2score < player1score) { + winner = players[1]; + message = `${winner} is leading!🏆
Player Two: ${player2score}
Player One: ${player1score}
Press "Submit" to continue the game.`; + gameStatus = "Player 1"; + } else { + message = `It's a draw. Press "Submit" to continue the game.`; + gameStatus = "Player 1"; + } + return message; + } +} + +function main(input) { + if (gameMode == "Waiting for Game Mode") { + if (input == "regular") { + gameMode = "regular"; + message = + "The chosen game mode is regular. The player with the highest combined number win the game.Please press submit to start."; + } else if (input == "reverse") { + gameMode = "reverse"; + message = + "The chosen game mode is reverse. The player with the lowest combined number win the game.Please press submit to start."; + } else { + message = "There are only 2 game modes. Please enter regular or reverse."; + } + return message; + } else if (gameMode == "regular") { + if (gameStatus == "Player 1") { + player1Dice = []; + for (var i = 0; i < numOfDice; i++) { + player1Dice.push(getDice()); + } + message = compareDiceNum(player1Dice[0], player1Dice[1]); + + return message; + } else if (gameStatus == "Player 1: choose dice order") { + var order = input; + message = getDiceMessage(order); + return message; + } + + if (gameStatus == "Player 2") { + player2Dice = []; + for (var i = 0; i < numOfDice; i++) { + player2Dice.push(getDice()); + } + message = compareDiceNum(player2Dice[0], player2Dice[1]); + + return message; + } else if (gameStatus == "Player 2: choose dice order") { + var order = input; + message = getDiceMessage(order); + return message; + } + + while (gameStatus == "result") { + player1score += sum1; + player2score += sum2; + message = getWinnerMessage(player1score, player2score); + return message; + } + } else if (gameMode == "reverse") { + if (gameStatus == "Player 1") { + player1Dice = []; + for (var i = 0; i < numOfDice; i++) { + player1Dice.push(getDice()); + } + message = compareDiceNum(player1Dice[0], player1Dice[1]); + + return message; + } else if (gameStatus == "Player 1: choose dice order") { + var order = input; + message = getDiceMessage(order); + return message; + } + + if (gameStatus == "Player 2") { + player2Dice = []; + for (var i = 0; i < numOfDice; i++) { + player2Dice.push(getDice()); + } + message = compareDiceNum(player2Dice[0], player2Dice[1]); + + return message; + } else if (gameStatus == "Player 2: choose dice order") { + var order = input; + message = getDiceMessage(order); + return message; + } + + while (gameStatus == "result") { + player1score += sum1; + player2score += sum2; + message = getWinnerMessage(player1score, player2score); + return message; + } + } +}