Skip to content

Commit

Permalink
Apply DRY principle for code reused; Check if the player won the game…
Browse files Browse the repository at this point in the history
… else implement nextPlayer function
  • Loading branch information
kenvilar committed Dec 3, 2017
1 parent 14f4660 commit 29f2b75
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,43 @@ document.querySelector('.btn-roll-dice').addEventListener('click', function () {
rolledScore = rolledScore + dice;
document.querySelector('#current-' + activePlayer).textContent = rolledScore;
} else {
//Next player
document.getElementById('current-1').textContent = '0';
document.getElementById('current-2').textContent = '0';
nextPlayer();
}
});

rolledScore = 0;
document.querySelector('.btn-hold').addEventListener('click', function () {
//Add rolled score to the player main score
scores[activePlayer - 1] = scores[activePlayer - 1] + rolledScore;

activePlayer === 1 ? activePlayer = 2 : activePlayer = 1;
//Update and display the player main score
document.querySelector('#score-' + activePlayer).textContent = scores[activePlayer - 1];

document.querySelector('.player-1-panel').classList.toggle('active');
document.querySelector('.player-2-panel').classList.toggle('active');
//Check if the player won the game
if (scores[activePlayer - 1] >= 20) {
//id="name-1"
document.querySelector('#name-' + activePlayer).textContent = 'WINNER!';

document.querySelector('#dice-1').style.display = 'none';
document.querySelector('#dice-2').style.display = 'none';
} else {
nextPlayer();
}
});

document.querySelector('.btn-hold').addEventListener('click', function () {
scores[activePlayer - 1] = scores[activePlayer - 1] + rolledScore;

document.querySelector('#score-' + activePlayer).textContent = scores[activePlayer - 1];

function nextPlayer() {
document.getElementById('current-1').textContent = '0';
document.getElementById('current-2').textContent = '0';

rolledScore = 0;

activePlayer === 1 ? activePlayer = 2 : activePlayer = 1;

document.querySelector('.player-1-panel').classList.toggle('active');
document.querySelector('.player-2-panel').classList.toggle('active');

document.querySelector('#dice-1').style.display = 'none';
document.querySelector('#dice-2').style.display = 'none';
});
}


///////
Expand Down

0 comments on commit 29f2b75

Please sign in to comment.