-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
134 lines (99 loc) · 4.64 KB
/
main.ts
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
124
125
126
127
128
129
130
131
132
133
134
import { state } from "./src/model.js";
import winnigModal from "./src/components/winnigModal.js";
import areUSureModal from "./src/components/areUSureModal.js";
const diceEl = document.querySelector(".dice") as HTMLElement;
const diceNumberEl = document.querySelector("#diceNumber") as HTMLElement;
const playerOneCardEl = document.querySelector(".playerOneCard") as HTMLElement;
const playerOneScoreEl = document.querySelector("#playerOneScore") as HTMLElement;
const playerOneCurrentScoreEl = document.querySelector("#playerOneCurrentScore") as HTMLElement;
const playerTwoCardEl = document.querySelector(".playerTwoCard") as HTMLElement;
const playerTwoScoreEl = document.querySelector("#playerTwoScore") as HTMLElement;
const playerTwoCurrentScoreEl = document.querySelector("#playerTwoCurrentScore") as HTMLElement;
const newGameBtn = document.querySelector("#newGameBtn") as HTMLElement;
const rollDiceBtn = document.querySelector("#rollDiceBtn") as HTMLElement;
const holdBtn = document.querySelector("#holdBtn") as HTMLElement;
const rollDice = () => {
state.diceNumber = Math.floor((Math.random() * 10) - 3);
for(let i = 1; state.diceNumber <= 0 ; i++) {
state.diceNumber = Math.floor((Math.random() * 10) - 3);
}
if(state.role === 1) {
state.playerOne.currentScore = state.playerOne.currentScore + state.diceNumber;
playerOneCurrentScoreEl.innerHTML = `${state.playerOne.currentScore}`;
} else if(state.role === 2) {
state.playerTwo.currentScore = state.playerTwo.currentScore + state.diceNumber;
playerTwoCurrentScoreEl.innerHTML = `${state.playerTwo.currentScore}`;
}
if(state.diceNumber === 1) {
state.role === 1 ? state.role = 2 : state.role = 1;
if(playerOneCardEl.classList.contains("role")) {
playerOneCardEl.classList.remove("role");
playerOneCardEl.classList.add("notRole");
playerTwoCardEl.classList.remove("notRole");
playerTwoCardEl.classList.add("role");
state.playerOne.currentScore = 0;
playerOneCurrentScoreEl.innerHTML = `${state.playerOne.currentScore}`;
} else {
playerOneCardEl.classList.remove("notRole");
playerOneCardEl.classList.add("role");
playerTwoCardEl.classList.remove("role");
playerTwoCardEl.classList.add("notRole");
state.playerTwo.currentScore = 0;
playerTwoCurrentScoreEl.innerHTML = `${state.playerTwo.currentScore}`;
}
}
diceNumberEl.innerHTML = `${state.diceNumber}`;
}
rollDiceBtn.addEventListener("click", rollDice);
const holdScores = () => {
if(state.role === 1) {
state.playerOne.score = state.playerOne.score + state.playerOne.currentScore;
state.playerOne.currentScore = 0;
playerOneCurrentScoreEl.innerHTML = `${state.playerOne.currentScore}`;
playerOneScoreEl.innerHTML = `${state.playerOne.score}`;
state.role = 2;
} else if(state.role === 2) {
state.playerTwo.score = state.playerTwo.score + state.playerTwo.currentScore;
state.playerTwo.currentScore = 0;
playerTwoCurrentScoreEl.innerHTML = `${state.playerTwo.currentScore}`;
playerTwoScoreEl.innerHTML = `${state.playerTwo.score}`;
state.role = 1;
}
if(playerOneCardEl.classList.contains("role")) {
playerOneCardEl.classList.remove("role");
playerOneCardEl.classList.add("notRole");
playerTwoCardEl.classList.remove("notRole");
playerTwoCardEl.classList.add("role");
} else {
playerOneCardEl.classList.remove("notRole");
playerOneCardEl.classList.add("role");
playerTwoCardEl.classList.remove("role");
playerTwoCardEl.classList.add("notRole");
}
if(state.playerOne.score >= 100) {
winnigModal.openModal(1, state.playerOne);
}
if(state.playerTwo.score >= 100) {
winnigModal.openModal(2, state.playerTwo);
}
}
holdBtn.addEventListener("click", holdScores);
export function restart() {
diceNumberEl.innerHTML = "1";
playerOneCardEl.classList.remove("notRole");
playerOneCardEl.classList.add("role");
playerTwoCardEl.classList.remove("role");
playerTwoCardEl.classList.add("notRole");
playerOneCurrentScoreEl.innerHTML = "0";
playerOneScoreEl.innerHTML = "0";
playerTwoCurrentScoreEl.innerHTML = "0";
playerTwoScoreEl.innerHTML = "0";
state.diceNumber = 0;
state.playerOne.currentScore = 0;
state.playerOne.score = 0;
state.playerTwo.currentScore = 0;
state.playerTwo.score = 0;
state.role = 1;
areUSureModal.closeModal();
}
newGameBtn.addEventListener("click", () => areUSureModal.openModal());