/*
GAME RULES:

- The game has 2 players, playing in rounds
- In each turn, a player rolls a dice as many times as he whishes. Each result get added to his ROUND score
- BUT, if the player rolls a 1, all his ROUND score gets lost. After that, it's the next player's turn
- The player can choose to 'Hold', which means that his ROUND score gets added to his GLBAL score. After that, it's the next player's turn
- The first player to reach 100 points on GLOBAL score wins the game

*/

var scores, roundScore, activePlayer;
var gamePlaying;

var dice = 0;
console.log('Lets Play');

var diceDOM = document.querySelector('.dice')

init();

document.querySelector('.btn-roll').addEventListener('click', function() {
    if(gamePlaying){
        // Random Number
        dice = Math.floor(Math.random() * 6 + 1);
        // Display Result
        diceDOM.style.display = 'block';
        diceDOM.src = 'dice-' + dice + '.png';
        // Update Score
        if(dice !== 1){
            // add score
            roundScore += dice;
            document.querySelector('#current-' + activePlayer).textContent = roundScore;
        }
        else{
            nextPlayer();
        }   
    }
});

function nextPlayer(){
    // next player
    activePlayer === 0 ? activePlayer = 1 : activePlayer = 0;
    // round score 0
    roundScore = 0;
        
    document.getElementById('current-0').textContent = '0';
    document.getElementById('current-1').textContent = '0';
        
    // change active player
    document.querySelector('.player-0-panel').classList.toggle('active');
    document.querySelector('.player-1-panel').classList.toggle('active');
        
    // clear dice
    diceDOM.style.display = 'none';
};

document.querySelector('.btn-hold').addEventListener('click', function(){
    if(gamePlaying){
        // Add Current Score to Global score
        scores[activePlayer] += roundScore;
        // Update UI
        document.querySelector('#score-' + activePlayer).textContent = scores[activePlayer];
        // Check if player wins
        if(scores[activePlayer] >= 100)
            {
                document.querySelector('#name-' + activePlayer).textContent = 'Winner!';
                diceDOM.style.display = 'none';
                document.querySelector('.player-' + activePlayer + '-panel').classList.add('winner');
                document.querySelector('.player-' + activePlayer + '-panel').classList.remove('active');
                gamePlaying = false;
            }
        else    nextPlayer();
    }
});

// New Game btn
document.querySelector('.btn-new').addEventListener('click', init);

function init(){
    scores = [0,0];
    roundScore = 0;
    activePlayer = 0;
    gamePlaying = true;
    
    // Clear Scores
    document.getElementById('score-0').textContent = '0';
    document.getElementById('score-1').textContent = '0';
    document.getElementById('current-0').textContent = '0';
    document.getElementById('current-1').textContent = '0';

    // Reset UI
    diceDOM.style.display = 'none';
    document.getElementById('name-0').textContent = 'Player 1';
    document.getElementById('name-1').textContent = 'Player 2';
    
    document.querySelector('.player-0-panel').classList.remove('winner');
    document.querySelector('.player-1-panel').classList.remove('winner');
    document.querySelector('.player-0-panel').classList.remove('active');
    document.querySelector('.player-0-panel').classList.add('active');
    document.querySelector('.player-1-panel').classList.remove('active');
}

@@ -1669,24 +1669,26 @@
 ar dice%0A    
+//
 diceDOM.styl

Clear Dice