Skip to content
Open
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
47 changes: 21 additions & 26 deletions intermediate-projects/hasnain666-pig-game/script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
//Selecting Elements and storing them is const variables

// Selecting Elements and storing them is const variables
const player0El = document.querySelector('.player--0');
const player1El = document.querySelector('.player--1');
const score0El = document.getElementById('score--0');
Expand All @@ -10,77 +11,71 @@ const btnRoll = document.querySelector('.btn--roll');
const btnHold = document.querySelector('.btn--hold');
const current0El = document.getElementById('current--0');
const current1El = document.getElementById('current--1');

let scores, currentScore, activePlayer, playing;

const initialization = function () {
const initialization = function() {
currentScore = 0;
activePlayer = 0;
scores = [0, 0];
playing = true;

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

player0El.classList.remove('player--winner');
player1El.classList.remove('player--winner');
player0El.classList.add('player--active');
player1El.classList.remove('player--active');

diceEl.classList.add('hidden');
};

initialization();

const switchPlayer = function () {
const switchPlayer = function() {
document.getElementById(`current--${activePlayer}`).textContent = 0;
currentScore = 0;
// switch
activePlayer = activePlayer === 0 ? 1 : 0; //if the active player is 0, switch to 1 and if its 1 switch to 0
activePlayer = activePlayer === 0 ? 1 : 0;

player0El.classList.toggle('player--active');
player1El.classList.toggle('player--active');
};

btnRoll.addEventListener('click', function () {
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 = `dice-${dice}.png`;

//3. Check if rolled 1, switch to another player
if (dice !== 1) {
//add dice to currentScore
currentScore += dice;
document.getElementById(`current--${activePlayer}`).textContent =
currentScore;
document.getElementById(`current--${activePlayer}`).textContent = currentScore;
} else {
switchPlayer();
}
}
});
btnHold.addEventListener('click', function () {

btnHold.addEventListener('click', function() {
if (playing) {
//Add current score to active player's score
// to check if player's score is >= 100
//finish the game
//else switch to the next player
scores[activePlayer] += currentScore;

// it means scores[1] = scores[1] + currentScore
document.getElementById(`score--${activePlayer}`).textContent =
scores[activePlayer];
document.getElementById(`score--${activePlayer}`).textContent = scores[activePlayer];

if (scores[activePlayer] >= 100) {
playing = false;

diceEl.classList.add('hidden');
document
.querySelector(`.player--${activePlayer}`)
.classList.add('player--winner');
document
.querySelector(`.player--${activePlayer}`)
.classList.remove('player--active');
document.querySelector(`.player--${activePlayer}`).classList.add('player--winner');
document.querySelector(`.player--${activePlayer}`).classList.remove('player--active');
} else {
switchPlayer();
}
}
});

btnNew.addEventListener('click', initialization);