-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
123 lines (105 loc) · 4.04 KB
/
script.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
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
- A player looses his ENTIRE score when he rolls two 6 in a row.
- 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 Final Score (default value 100) points on GLOBAL score wins the game
*/
var currentPlayer,
scores,
currentTotal,
gamestatus,
finalScore,
checkFinalScoreValue;
// At the begining we set default values with newGame() function.
newGame();
//Roll dice button actions
//Get random two number and show them on screen.
document.getElementById("rollBtn").addEventListener("click", function() {
if (gamestatus) {
// Getting two random number 1-6.
var dice1 = Math.ceil(Math.random() * 6);
var dice2 = Math.ceil(Math.random() * 6);
// Display random numbers with dice picture on screen.
document.getElementById("dice-1").src = `dice-${dice1}.png`;
document.getElementById("dice-2").src = `dice-${dice2}.png`;
// Change our current status.
currentTotal += dice1 + dice2;
document.querySelector(
`.player_${currentPlayer}_current`
).textContent = currentTotal;
// when any dice value equals 1 player will be change.
if (dice1 === 1 || dice2 === 1) {
//reset and change
changePlayer();
}
// when any bot of dices value is equals 6, player result value will be reseted.
else if (dice1 === 6 && dice2 === 6) {
scores[currentPlayer - 1] = 0;
document.querySelector(`.player_${currentPlayer}_result`).textContent =
scores[currentPlayer - 1];
}
}
});
//hold button actions
document.getElementById("holdBtn").addEventListener("click", function() {
if (gamestatus) {
scores[currentPlayer - 1] = scores[currentPlayer - 1] + currentTotal;
document.querySelector(`.player_${currentPlayer}_result`).textContent =
scores[currentPlayer - 1];
// Get final score value for checking winner
checkFinalScore();
// before we change player, we will look for winning status
if (scores[currentPlayer - 1] >= finalScore) {
currentTotal = 0;
document.querySelector(`.player_${currentPlayer}_name`).textContent =
"WINNER";
gamestatus = false;
} else {
//reset and change player
changePlayer();
}
}
});
// changing player function.
function changePlayer() {
document
.querySelector(`.player_${currentPlayer}_name`)
.classList.toggle("active");
currentTotal = 0;
document.querySelector(
`.player_${currentPlayer}_current`
).textContent = currentTotal;
currentPlayer === 1 ? (currentPlayer = 2) : (currentPlayer = 1);
document
.querySelector(`.player_${currentPlayer}_name`)
.classList.toggle("active");
}
//new game button actions
document.getElementById("new_game_btn").addEventListener("click", newGame);
// new game function
function newGame() {
currentPlayer = 1;
scores = [0, 0];
currentTotal = 0;
gamestatus = true;
finalScore = 100;
document.querySelector(`.player_1_name`).textContent = "Player 1";
document.querySelector(`.player_2_name`).textContent = "Player 2";
document.querySelector(`.player_1_name`).classList.remove("active");
document.querySelector(`.player_2_name`).classList.remove("active");
document.querySelector(`.player_1_name`).classList.add("active");
document.querySelector(`.player_1_current`).textContent = "current";
document.querySelector(`.player_2_current`).textContent = "current";
document.querySelector(`.player_1_result`).textContent = "result";
document.querySelector(`.player_2_result`).textContent = "result";
checkFinalScore();
}
function checkFinalScore() {
checkFinalScoreValue = document.querySelector(".final-score").value;
if (parseInt(checkFinalScoreValue) !== "Nan" && checkFinalScoreValue > 0) {
finalScore = checkFinalScoreValue;
}
}