-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
100 lines (92 loc) · 2.85 KB
/
Copy pathscript.js
File metadata and controls
100 lines (92 loc) · 2.85 KB
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
"use strict";
let dice;
const diceEl = document.querySelector(".dice");
const curPlayer1ScoreEl = document.getElementById("current-player1-score");
const curPlayer2ScoreEl = document.getElementById("current-player2-score");
const globalPlayer1ScoreEl = document.getElementById("player1-score");
const globalPlayer2ScoreEl = document.getElementById("player2-score");
const rollDiceBtn = document.getElementById("roll-dice");
const fields = document.querySelectorAll(".player");
const holdBtn = document.getElementById("hold");
const newGameBtn = document.getElementById("new-game");
const PLAYER_1 = {
name: "player 1",
currScore: 0,
globalScore: 0,
currScoreEl: curPlayer1ScoreEl,
globalScoreEl: globalPlayer1ScoreEl,
field: fields[0],
};
const PLAYER_2 = {
name: "player 2",
currScore: 0,
globalScore: 0,
currScoreEl: curPlayer2ScoreEl,
globalScoreEl: globalPlayer2ScoreEl,
field: fields[1],
};
let currentPlayer = PLAYER_1;
const newGame = () => {
PLAYER_1.currScore = 0;
PLAYER_1.globalScore = 0;
PLAYER_1.currScoreEl.textContent = PLAYER_1.currScore;
PLAYER_1.globalScoreEl.textContent = PLAYER_1.globalScore;
PLAYER_2.currScore = 0;
PLAYER_2.globalScore = 0;
PLAYER_2.currScoreEl.textContent = PLAYER_2.currScore;
PLAYER_2.globalScoreEl.textContent = PLAYER_2.globalScore;
}
const win = (player) =>{
alert(player.name + " wins!");
newGame();
}
const hold = () => {
if (currentPlayer.currScore > 0) {
currentPlayer.globalScore += currentPlayer.currScore;
currentPlayer.globalScoreEl.textContent = currentPlayer.globalScore;
currentPlayer.currScore = 0;
currentPlayer.currScoreEl.textContent = currentPlayer.currScore;
if (currentPlayer.globalScore >= 100)
win(currentPlayer);
else
changePlayer();
}
else {
alert("You should roll a dice at least once!");
}
};
const changePlayer = () => {
if (currentPlayer === PLAYER_1) {
currentPlayer = PLAYER_2;
console.log(fields);
PLAYER_1.field.classList.add("inactive");
currentPlayer.field.classList.remove("inactive");
} else {
currentPlayer = PLAYER_1;
PLAYER_2.field.classList.add("inactive");
currentPlayer.field.classList.remove("inactive");
}
};
const addCurrentScore = (player) => {
//add and draw cur score
player.currScore += dice;
player.currScoreEl.textContent = player.currScore;
};
const drawDice = () => {
diceEl.innerHTML = `<img src="images/dice-${dice}.png" alt="${dice}">`;
};
const roll = (e) => {
dice = Math.floor(Math.random() * 6 + 1);
drawDice();
if (dice !== 1) {
addCurrentScore(currentPlayer);
} else {
currentPlayer.currScore = 0;
currentPlayer.currScoreEl.textContent = currentPlayer.currScore;
changePlayer();
}
console.log(PLAYER_1);
};
rollDiceBtn.addEventListener("click", roll);
holdBtn.addEventListener("click", hold);
newGameBtn.addEventListener("click", newGame);