diff --git a/app.js b/app.js new file mode 100755 index 0000000..bbed96e --- /dev/null +++ b/app.js @@ -0,0 +1,106 @@ +/* +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 = true; + +// We initiate the game with the basic variables +init(); + +// Clicking the button to roll the dice +document.querySelector(".btn-roll").addEventListener("click", function(){ + if(gamePlaying) { + // 1. Get the random number + var dice = Math.floor((Math.random() * 6)) + 1; + + + // 2. Display the result + var diceDOM = document.querySelector(".dice"); + diceDOM.style.display = "block"; + diceDOM.src = "dice-" + dice + ".png"; + + + // 3. Update the roundScore, but only if that's not 1 + if(dice !== 1) { + //Add score + roundScore += dice; + document.querySelector("#current-" + activePlayer).textContent = roundScore; + + } else { + // Next player + nextPlayer() + } + } +}); + +document.querySelector(".btn-hold").addEventListener("click", function(){ + if(gamePlaying){ + // Add currentScore to globalScore + scores[activePlayer] += roundScore; + + // Update the UI to show the globalScore + document.querySelector("#score-" + activePlayer).textContent = scores[activePlayer]; + + // Check if player won the game + if(scores[activePlayer] >= 100){ + document.querySelector("#name-" + activePlayer).textContent = "Winner!"; + document.querySelector(".dice").style.display = "none"; + document.querySelector(".player-" + activePlayer + "-panel").classList.add("winner"); + document.querySelector(".player-" + activePlayer + "-panel").classList.remove("active"); + gamePlaying = false; + } else { + nextPlayer() + } + } +}) + +function nextPlayer() { + //Next player with ternary operator + activePlayer === 0 ? activePlayer = 1 : activePlayer = 0; + roundScore = 0; + + document.getElementById("current-0").textContent = "0"; + document.getElementById("current-1").textContent = "0"; + + document.querySelector(".player-0-panel").classList.toggle("active"); + document.querySelector(".player-1-panel").classList.toggle("active"); + document.querySelector(".dice").style.display = "none"; +} + +document.querySelector(".btn-new").addEventListener("click", init); + +function init(){ + scores = [0,0]; + activePlayer = 0; + roundScore = 0; + + // document.querySelector("#current-" + activePlayer).textContent = dice; + document.querySelector(".dice").style.display = "none"; + document.getElementById("score-0").textContent = "0"; + document.getElementById("score-1").textContent = "0"; + document.getElementById("current-0").textContent = "0"; + document.getElementById("current-1").textContent = "0"; + document.getElementById("name-0").textContent = "Player 1"; + document.getElementById("name-1").textContent = "Player 2"; + document.querySelector(".player-0-panel").classList.remove("active"); + document.querySelector(".player-1-panel").classList.remove("active"); + document.querySelector(".player-0-panel").classList.remove("winner"); + document.querySelector(".player-1-panel").classList.remove("winner"); + + document.querySelector(".player-0-panel").classList.add("active");; +} + +// Clicking for instructions +document.querySelector(".instructions").addEventListener("click", function(){ + document.querySelector(".modal").classList.add("show"); + document.querySelector(".close").addEventListener("click", function(){ + document.querySelector(".modal").classList.remove("show"); + }); +}); diff --git a/back.jpg b/back.jpg new file mode 100755 index 0000000..dcd9a7d Binary files /dev/null and b/back.jpg differ diff --git a/dice-1.png b/dice-1.png new file mode 100755 index 0000000..0d911ca Binary files /dev/null and b/dice-1.png differ diff --git a/dice-2.png b/dice-2.png new file mode 100755 index 0000000..f3c32af Binary files /dev/null and b/dice-2.png differ diff --git a/dice-3.png b/dice-3.png new file mode 100755 index 0000000..e3ef2b5 Binary files /dev/null and b/dice-3.png differ diff --git a/dice-4.png b/dice-4.png new file mode 100755 index 0000000..0c785f7 Binary files /dev/null and b/dice-4.png differ diff --git a/dice-5.png b/dice-5.png new file mode 100755 index 0000000..b4b41e3 Binary files /dev/null and b/dice-5.png differ diff --git a/dice-6.png b/dice-6.png new file mode 100755 index 0000000..6f4d9b3 Binary files /dev/null and b/dice-6.png differ diff --git a/farm.jpg b/farm.jpg new file mode 100644 index 0000000..eca1e48 Binary files /dev/null and b/farm.jpg differ diff --git a/index.html b/index.html new file mode 100755 index 0000000..5c28d61 --- /dev/null +++ b/index.html @@ -0,0 +1,52 @@ + + +
+ + + + + +Instructions
+- 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
+ +