-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
80 lines (73 loc) · 3.32 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
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
*/
let activePlayer = 0 ;
let players = [0,1];
globalScore = "#score-" + players[activePlayer];
globalCurrent = "#current-" + players[activePlayer];
globalInfo = ".info-" + players[activePlayer];
function rollDiceAnimate(){
document.querySelector(".dice").src = "die.gif";
setTimeout(rollDiceUpdate, 1000);
}
function rollDiceUpdate(){
diceNumber = Math.floor(Math.random() * 6 + 1);
document.querySelector(".dice").src = "dice-" + diceNumber + ".png";
updateScore(diceNumber);
}
function updateScore(value){
if (value == 1){
document.querySelector(globalInfo).innerText = " - " + document.querySelector(globalCurrent).innerText;
document.querySelector(globalCurrent).innerText = 0;
changePlayer();
}else
{
let score = parseInt(document.querySelector(globalCurrent).innerText) + parseInt(value);
document.querySelector(globalCurrent).innerText = score;
document.querySelector(globalInfo).innerText = " + " + value;
}
}
function holdScore(){
let toBeHoldedScore = parseInt(document.querySelector(globalCurrent).innerText) + parseInt(document.querySelector(globalScore).innerText);
document.querySelector(globalScore).innerText = toBeHoldedScore;
document.querySelector(globalCurrent).innerText = 0;
document.querySelector(globalInfo).innerHTML = " ";
if (parseInt(document.querySelector(globalScore).innerText) >= 100){
winner();
}else{
changePlayer();
}
}
function changePlayer(){
document.querySelector("#player-" + activePlayer).classList.remove("active");
activePlayer == 0 ? ++activePlayer : --activePlayer;
document.querySelector("#player-" + activePlayer).classList.add("active");
globalScore = "#score-" + players[activePlayer];
globalCurrent = "#current-" + players[activePlayer];
globalInfo = ".info-" + players[activePlayer];
return window.activePlayer;
}
function winner(){
document.querySelector(".btn-roll").classList.add("invisible");
document.querySelector(".btn-hold").classList.add("invisible");
document.querySelector(".dice").classList.add("invisible");
document.querySelector(".info-"+players[activePlayer]).innerText = "### Winner !!! ###";
document.querySelector(".info-"+players[activePlayer]).classList.add("winner");
}
function newStart(){
location.reload();
}
function showRules(){
alert(`GAME RULES:\n
- The game has 2 players, playing in rounds\n
- In each turn, a player rolls a dice as many times as he whishes. Each result get added to his CURRENT score\n
- BUT, if the player rolls a 1, all his CURRENT score gets lost. After that, it's the next player\'s turn\n
- The player can choose to \'Hold\', which means that his CURRENT score gets added to his MAIN score. After that, it's the next player\'s turn\n
- The first player to reach 100 points on MAIN score wins the game\n
`);
}