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

Added 'Roll the dice' game #4004

Closed
wants to merge 11 commits into from
Closed
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
46 changes: 46 additions & 0 deletions Games/Dice_Game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Dice Game


## Description

An engaging two-player dice game where the objective is to be the first to reach a total score of 100 points. Players take turns rolling a die and accumulate points during their turn. The twist is that if a player rolls a 1, their current turn's score is reset to 0, and their turn ends. Players must strategize when to roll and when to hold to avoid losing points and give themselves the best chance to win.

## Functionalities

- **Two Players**: The game is played with two players.
- **Turn-Based Play**: Players alternate turns rolling a single die.
- **Scoring**: On their turn, a player can roll the die as many times as they wish. Each roll adds the die's value to the player's temporary score for that turn.
- **Rolling a 1**: If a player rolls a 1, their temporary score for that turn is reset to 0, and their turn ends immediately.
- **Holding**: A player can choose to hold at any time during their turn, adding their temporary score to their total score and ending their turn.
- **Winning the Game**: The first player to reach a total score of 100 points wins the game.

## How to play?

1. **Two Players**: The game is played with two players taking turns.
2. **Turn-Based Play**: Players take turns to roll a single die. The current player's actions during their turn determine their score and turn duration.
3. **Scoring**: On their turn, a player can roll the die as many times as they choose. Each roll adds the die's value to the player's temporary score for that turn.
4. **Rolling a 1**: If the player rolls a 1:
- Their temporary score for that turn is reset to 0.
- Their turn ends immediately.
5. **Holding**: A player can choose to hold at any time during their turn.
- When a player holds:
- Their temporary score for that turn is added to their total score.
- Their turn ends.
6. **Winning the Game**: The game continues with players taking turns until one player reaches or exceeds a total score of 100 points.
- The first player to reach or exceed 100 points wins the game.

## How to Run

1. Clone the repository.
2. Ensure you have the dice images in an `images` folder.
3. Open the `index.html` file in a web browser.
4. Refresh the page to roll the dice and see the results.

## Screenshots
![Screenshot 2024-06-01 at 9 49 04β€―AM](https://github.com/shivan2004/GameZone/assets/112183968/259cae80-a5c2-4000-91ff-6ea12362dcc4)
This is when a players score resets
![image](https://github.com/shivan2004/GameZone/assets/112183968/57077a6c-790a-4726-ac3b-b484df458960)
Upon rolling dice, score increases


## Working video
Binary file added Games/Dice_Game/images/dice1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Games/Dice_Game/images/dice2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Games/Dice_Game/images/dice3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Games/Dice_Game/images/dice4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Games/Dice_Game/images/dice5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Games/Dice_Game/images/dice6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions Games/Dice_Game/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dice Game</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Dice Game</h1>
<div class="scores">
<div>
<h2>Player 1</h2>
<p>Total Score: <span id="total-score-1">0</span></p>
<p>Current Score: <span id="current-score-1">0</span></p>
</div>
<div>
<h2>Player 2</h2>
<p>Total Score: <span id="total-score-2">0</span></p>
<p>Current Score: <span id="current-score-2">0</span></p>
</div>
</div>
<div class="dice-container">
<img id="dice" src="images/dice1.png" alt="Dice">
</div>
<div class="controls">
<button id="roll-button">Roll Dice</button>
<button id="hold-button">Hold</button>
</div>
<h2 id="winner"></h2>
</div>
<script src="script.js"></script>
</body>
</html>
39 changes: 39 additions & 0 deletions Games/Dice_Game/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
let currentPlayer = 1;
let totalScores = [0, 0];
let currentScores = [0, 0];

const rollDice = () => Math.floor(Math.random() * 6) + 1;

const switchPlayer = () => {
currentScores[currentPlayer - 1] = 0;
document.getElementById(`current-score-${currentPlayer}`).textContent = currentScores[currentPlayer - 1];
currentPlayer = currentPlayer === 1 ? 2 : 1;
};

const checkWinner = () => {
if (totalScores[currentPlayer - 1] >= 100) {
document.getElementById('winner').textContent = `Player ${currentPlayer} wins!`;
document.getElementById('roll-button').disabled = true;
document.getElementById('hold-button').disabled = true;
}
};

document.getElementById('roll-button').addEventListener('click', () => {
const diceValue = rollDice();
document.getElementById('dice').src = `images/dice${diceValue}.png`;
if (diceValue === 1) {
switchPlayer();
} else {
currentScores[currentPlayer - 1] += diceValue;
document.getElementById(`current-score-${currentPlayer}`).textContent = currentScores[currentPlayer - 1];
}
});

document.getElementById('hold-button').addEventListener('click', () => {
totalScores[currentPlayer - 1] += currentScores[currentPlayer - 1];
document.getElementById(`total-score-${currentPlayer}`).textContent = totalScores[currentPlayer - 1];
checkWinner();
if (totalScores[currentPlayer - 1] < 100) {
switchPlayer();
}
});
32 changes: 32 additions & 0 deletions Games/Dice_Game/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
margin: 0;
padding: 0;
}

.container {
margin-top: 50px;
}

.scores {
display: flex;
justify-content: space-around;
margin-bottom: 20px;
}

.dice-container {
margin: 20px;
}

img {
width: 100px;
height: 100px;
}

.controls {
display: flex;
justify-content: center;
gap: 20px;
}
26 changes: 26 additions & 0 deletions Games/Roll the Dice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Dice Game

A simple web-based dice game where two players roll a dice, and the player with the higher number wins. The dice rolls happen automatically when the page is refreshed.

## Features

- **Two Players**: Each player rolls one dice.
- **Dice Roll**: Dice rolls are random and occur upon page refresh.
- **Winning Condition**: The player with the higher number wins. If both roll the same number, the game results in a tie.
- **User Interface**: Displays dice images and announces the winner.

## Game Points

- **Initialization**: Dice rolls are initialized on page load.
- **Rolling Dice**: Random number generator simulates rolling a six-sided dice.
- **Comparison**: Compares the results of the two dice rolls.
- **Display Results**: Shows dice values and announces the winner.
- **Refresh Mechanism**: New dice rolls and winner determined on each page refresh.

## How to Run
- Clone the repository.
- Ensure you have the dice images in an images folder.
- Open the index.html file in a web browser.
- Refresh the page to roll the dice and see the results.

Demo : ![Roll_the_dice](https://github.com/shivan2004/GameZone/assets/112183968/edd7414f-e6b1-47e6-a521-596f3249a7ae)
Binary file added Games/Roll the Dice/images/dice1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Games/Roll the Dice/images/dice2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Games/Roll the Dice/images/dice3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Games/Roll the Dice/images/dice4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Games/Roll the Dice/images/dice5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Games/Roll the Dice/images/dice6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions Games/Roll the Dice/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dice Game</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Dice Game</h1>
<div class="dice-container">
<div class="player">
<h2>Player 1</h2>
<img id="dice1" src="images/dice1.png" alt="Dice 1">
<p id="result1"></p>
</div>
<div class="player">
<h2>Player 2</h2>
<img id="dice2" src="images/dice1.png" alt="Dice 2">
<p id="result2"></p>
</div>
</div>
<h2 id="winner"></h2>
</div>
<script src="script.js"></script>
</body>
</html>
23 changes: 23 additions & 0 deletions Games/Roll the Dice/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
window.onload = function() {
const rollDice = () => Math.floor(Math.random() * 6) + 1;

const player1Roll = rollDice();
const player2Roll = rollDice();

document.getElementById('dice1').src = `images/dice${player1Roll}.png`;
document.getElementById('dice2').src = `images/dice${player2Roll}.png`;

document.getElementById('result1').textContent = `Player 1 rolled: ${player1Roll}`;
document.getElementById('result2').textContent = `Player 2 rolled: ${player2Roll}`;

let winnerText = '';
if (player1Roll > player2Roll) {
winnerText = 'Player 1 wins!';
} else if (player2Roll > player1Roll) {
winnerText = 'Player 2 wins!';
} else {
winnerText = 'It\'s a tie!';
}

document.getElementById('winner').textContent = winnerText;
};
27 changes: 27 additions & 0 deletions Games/Roll the Dice/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
margin: 0;
padding: 0;
}

.container {
margin-top: 50px;
}

.dice-container {
display: flex;
justify-content: center;
gap: 50px;
margin-top: 20px;
}

.player {
text-align: center;
}

img {
width: 100px;
height: 100px;
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ This repository also provides one such platforms where contributers come over an
|[Color The Page](https://github.com/kunjgit/GameZone/tree/main/Games/Color_The_Page)|


| [Roll _the_ dice](https://github.com/shivan2004/GameZone/tree/main/Games/Roll%20the%20Dice) |
| [Dice_Game] (https://github.com/shivan2004/GameZone/tree/main/Games/Dice_Game) |


</center>
Expand Down
Binary file added assets/images/Roll_the_dice.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading