Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lena #27-2 Project 2 :Beat That! #559

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

17 changes: 16 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}

#container {
background-color: pink;
background-color: lightcyan;
margin: 40px auto;
max-width: 800px;
padding: 38px 31px;
Expand Down Expand Up @@ -51,6 +51,21 @@
<body>
<h1 id="header">Basics: Beat That! 🚀</h1>
<div id="container">
<p>Hello! Welcome to Beat That!</p>
<p>
There are two game modes. Please enter "regular" or "reverse" to choose
your preferred mode.
</p>
<p>Regular Game mode:</p>
<p>
Roll two dices and put them in order to make the highest number
possible.The player with the highest number wins!
</p>
<p>Reverse Game mode:</p>
<p>
Roll two dices and put them in order to make the lowest number
possible.The player with the highest number wins!
</p>
<p>Input:</p>
<input id="input-field" />
<br />
Expand Down
205 changes: 201 additions & 4 deletions script.js
Original file line number Diff line number Diff line change
@@ -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 🎲
<br> You rolled ${player1Dice[0]} for dice one and ${player1Dice[1]} for dice two.
<br> Your number is ${sum1}
<br> It's now Player 2's turn.`;
gameStatus = "Player 2";
} else {
gameStatus = "Player 1: choose dice order";
message = `🎲 WELCOME, Player One 🎲
<br> You rolled ${player1Dice[0]} for dice one and ${player1Dice[1]} for dice two.
<br>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 🎲
<br> You rolled ${player2Dice[0]} for dice one and ${player2Dice[1]} for dice two.
<br> Your number is ${sum2}
<br> Press "Submit" to find out who's the winner.`;
gameStatus = "result";
} else {
gameStatus = "Player 2: choose dice order";
message = `🎲 WELCOME, Player Two 🎲
<br> You rolled ${player2Dice[0]} for dice one and ${player2Dice[1]} for dice two.
<br>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 🎲 <br>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 🎲 <br>You chose Dice 2 first. Your number is ${sum1}. <br> 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 🎲 <br>You chose Dice 1 first. Your number is ${sum2}. <br>Press "Submit" to find out who's the winner.`;
gameStatus = "result";
} else if (order == 2) {
sum2 = player2Dice[1] * 10 + player2Dice[0];
message = `🎲 PLAYER 2 🎲 <br>You chose Dice 2 first. Your number is ${sum2}. <br> Press "Submit" to find out who's the winner.`;
gameStatus = "result";
} else {
message = ` <br> 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!🏆 <br> Player One: ${player1score} <br> Player Two: ${player2score} <br>Press "Submit" to continue the game.`;
gameStatus = "Player 1";
} else if (player2score > player1score) {
winner = players[1];
message = `${winner} is leading!🏆 <br> Player Two: ${player2score}<br> Player One: ${player1score} <br>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!🏆 <br> Player One: ${player1score} <br> Player Two: ${player2score} <br>Press "Submit" to continue the game.`;
gameStatus = "Player 1";
} else if (player2score < player1score) {
winner = players[1];
message = `${winner} is leading!🏆 <br> Player Two: ${player2score}<br> Player One: ${player1score} <br>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;
}
}
}