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 DiceRoll_Game #4178

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
52 changes: 52 additions & 0 deletions Games/DiceRoll_Game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# **Game_Name**

Dice Game

<br>

## **Description 📃**

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 🎮**

1.Two Players: The game is played with two players.

2.Turn-Based Play: Players alternate turns rolling a single die.

3.Scoring: On their turn, a player can roll the die as often as they wish. Each roll adds the die's value to the player's temporary score for that turn.

4.Rolling a 1: If a player rolls a 1, their temporary score for that turn is reset to 0, and their turn ends immediately.

5.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.

6.Winning the Game: The first player to reach a total score of 100 points wins the game.

- <br>

## **How to play? 🕹️**

1.Both start with a score of 0.

2.Players alternate turns rolling a single six-sided die.

3.On their turn, a player can:

Roll the Die: Add the rolled value to their temporary score.
If a 1 is rolled, the temporary score resets to 0, and the turn ends.

Hold: Add the temporary score to their total score and end their turn.

### Winning

The first player to reach 100 points wins.-

<br>

## **Screenshots 📸**

<br>

![Game Screenshot](../../assets/images/DiceRoll_Game.png)
Binary file added Games/DiceRoll_Game/images/dice-1.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/DiceRoll_Game/images/dice-2.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/DiceRoll_Game/images/dice-3.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/DiceRoll_Game/images/dice-4.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/DiceRoll_Game/images/dice-5.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/DiceRoll_Game/images/dice-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions Games/DiceRoll_Game/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="style.css" />
<title>Pig Game</title>
</head>
<body>
<main>
<section class="player player--0 player--active">
<h2 class="name" id="name--0">Player 1</h2>
<p class="score" id="score--0">43</p>
<div class="current">
<p class="current-label">Current</p>
<p class="current-score" id="current--0">0</p>
</div>
</section>
<section class="player player--1">
<h2 class="name" id="name--1">Player 2</h2>
<p class="score" id="score--1">24</p>
<div class="current">
<p class="current-label">Current</p>
<p class="current-score" id="current--1">0</p>
</div>
</section>

<img src="images/dice-1.png" alt="Playing dice" class="dice" />
<button class="btn btn--new">🔄 New game</button>
<button class="btn btn--roll">🎲 Roll dice</button>
<button class="btn btn--hold">📥 Hold</button>
</main>
<script src="script.js"></script>
</body>
</html>
97 changes: 97 additions & 0 deletions Games/DiceRoll_Game/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"use strict";

// Selecting elements
const player0El = document.querySelector(".player--0");
const player1El = document.querySelector(".player--1");
const score0El = document.querySelector("#score--0");
const score1El = document.getElementById("score--1");
const current0El = document.getElementById("current--0");
const current1El = document.getElementById("current--1");

const diceEl = document.querySelector(".dice");
const btnNew = document.querySelector(".btn--new");
const btnRoll = document.querySelector(".btn--roll");
const btnHold = document.querySelector(".btn--hold");

let scores, currentScore, activePlayer, playing;

// Starting conditions
const init = function () {
scores = [0, 0];
currentScore = 0;
activePlayer = 0;
playing = true;

score0El.textContent = 0;
score1El.textContent = 0;
current0El.textContent = 0;
current1El.textContent = 0;

diceEl.classList.add("hidden");
player0El.classList.remove("player--winner");
player1El.classList.remove("player--winner");
player0El.classList.add("player--active");
player1El.classList.remove("player--active");
};
init();

const switchPlayer = function () {
document.getElementById(`current--${activePlayer}`).textContent = 0;
currentScore = 0;
activePlayer = activePlayer === 0 ? 1 : 0;
player0El.classList.toggle("player--active");
player1El.classList.toggle("player--active");
};

// Rolling dice functionality
btnRoll.addEventListener("click", function () {
if (playing) {
// 1. Generating a random dice roll
const dice = Math.trunc(Math.random() * 6) + 1;

// 2. Display dice
diceEl.classList.remove("hidden");
diceEl.src = `images/dice-${dice}.png`;

// 3. Check for rolled 1
if (dice !== 1) {
// Add dice to current score
currentScore += dice;
document.getElementById(`current--${activePlayer}`).textContent =
currentScore;
} else {
// Switch to next player
switchPlayer();
}
}
});

btnHold.addEventListener("click", function () {
if (playing) {
// 1. Add current score to active player's score
scores[activePlayer] += currentScore;
// scores[1] = scores[1] + currentScore

document.getElementById(`score--${activePlayer}`).textContent =
scores[activePlayer];

// 2. Check if player's score is >= 100
if (scores[activePlayer] >= 100) {
// Finish the game
playing = false;
diceEl.classList.add("hidden");

document
.querySelector(`.player--${activePlayer}`)
.classList.add("player--winner");
document
.querySelector(`.player--${activePlayer}`)
.classList.remove("player--active");
} else {
// Switch to the next player
switchPlayer();
}
}
});

btnNew.addEventListener("click", init);
171 changes: 171 additions & 0 deletions Games/DiceRoll_Game/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
@import url("https://fonts.googleapis.com/css2?family=Nunito&display=swap");

* {
margin: 0;
padding: 0;
box-sizing: inherit;
}

html {
font-size: 62.5%;
box-sizing: border-box;
}

body {
font-family: "Nunito", sans-serif;
font-weight: 400;
height: 100vh;
color: #333;
background-image: linear-gradient(to top left, #753682 0%, #bf2e34 100%);
display: flex;
align-items: center;
justify-content: center;
}

/* LAYOUT */
main {
position: relative;
width: 100rem;
height: 60rem;
background-color: rgba(255, 255, 255, 0.35);
backdrop-filter: blur(200px);
filter: blur();
box-shadow: 0 3rem 5rem rgba(0, 0, 0, 0.25);
border-radius: 9px;
overflow: hidden;
display: flex;
}

.player {
flex: 50%;
padding: 9rem;
display: flex;
flex-direction: column;
align-items: center;
transition: all 0.75s;
}

/* ELEMENTS */
.name {
position: relative;
font-size: 4rem;
text-transform: uppercase;
letter-spacing: 1px;
word-spacing: 2px;
font-weight: 300;
margin-bottom: 1rem;
}

.score {
font-size: 8rem;
font-weight: 300;
color: #c7365f;
margin-bottom: auto;
}

.player--active {
background-color: rgba(255, 255, 255, 0.4);
}
.player--active .name {
font-weight: 700;
}
.player--active .score {
font-weight: 400;
}

.player--active .current {
opacity: 1;
}

.current {
background-color: #c7365f;
opacity: 0.8;
border-radius: 9px;
color: #fff;
width: 65%;
padding: 2rem;
text-align: center;
transition: all 0.75s;
}

.current-label {
text-transform: uppercase;
margin-bottom: 1rem;
font-size: 1.7rem;
color: #ddd;
}

.current-score {
font-size: 3.5rem;
}

/* ABSOLUTE POSITIONED ELEMENTS */
.btn {
position: absolute;
left: 50%;
transform: translateX(-50%);
color: #444;
background: none;
border: none;
font-family: inherit;
font-size: 1.8rem;
text-transform: uppercase;
cursor: pointer;
font-weight: 400;
transition: all 0.2s;

background-color: white;
background-color: rgba(255, 255, 255, 0.6);
backdrop-filter: blur(10px);

padding: 0.7rem 2.5rem;
border-radius: 50rem;
box-shadow: 0 1.75rem 3.5rem rgba(0, 0, 0, 0.1);
}

.btn::first-letter {
font-size: 2.4rem;
display: inline-block;
margin-right: 0.7rem;
}

.btn--new {
top: 4rem;
}
.btn--roll {
top: 39.3rem;
}
.btn--hold {
top: 46.1rem;
}

.btn:active {
transform: translate(-50%, 3px);
box-shadow: 0 1rem 2rem rgba(0, 0, 0, 0.15);
}

.btn:focus {
outline: none;
}

.dice {
position: absolute;
left: 50%;
top: 16.5rem;
transform: translateX(-50%);
height: 10rem;
box-shadow: 0 2rem 5rem rgba(0, 0, 0, 0.2);
}

.player--winner {
background-color: #2f2f2f;
}

.player--winner .name {
font-weight: 700;
color: #c7365f;
}

.hidden {
display: none;
}
Loading
Loading