SHOW:
|
|
- or go back to the newest paste.
1 | //an array storing players' point | |
2 | let points = []; | |
3 | ||
4 | //points that a player scores after each dice roll | |
5 | let roundPoints; | |
6 | ||
7 | //variable controlling players if equal to 0 plays player1 if equal to 1 plays player2 | |
8 | //as in programming we number from 0, player1 will have a value of 0 player2 a value of 1 this will make it easier to retrieve points from the array | |
9 | let currentPlayer; | |
10 | ||
11 | //boolean variable controlling gameplay if true we can play if false gameplay will be impossible | |
12 | let canPlay; | |
13 | ||
14 | // add an array that will store the addresses to the bone | |
15 | const images = [ | |
16 | "https://cdn.glitch.com/8fbc579f-3346-47a0-abbc-945a83abb962%2Fkosc-1.png?v=1610038358032", | |
17 | "https://cdn.glitch.com/8fbc579f-3346-47a0-abbc-945a83abb962%2Fkosc-2.png?v=1610038358080", | |
18 | "https://cdn.glitch.com/8fbc579f-3346-47a0-abbc-945a83abb962%2Fkosc-3.png?v=1610038358032", | |
19 | "https://cdn.glitch.com/8fbc579f-3346-47a0-abbc-945a83abb962%2Fkosc-4.png?v=1610038358142", | |
20 | "https://cdn.glitch.com/8fbc579f-3346-47a0-abbc-945a83abb962%2Fkosc-5.png?v=1610038358032", | |
21 | "https://cdn.glitch.com/8fbc579f-3346-47a0-abbc-945a83abb962%2Fkosc-6.png?v=1610038358193", | |
22 | ]; | |
23 | ||
24 | //function responsible for preparing a new game | |
25 | newGame(); | |
26 | ||
27 | function newGame() { | |
28 | //enable game | |
29 | canPlay = true; | |
30 | ||
31 | //the game always starts with player 1 | |
32 | currentPlayer = 0; | |
33 | //hide the bone at the beginning of the game | |
34 | document.querySelector(".die").style.display = "none"; | |
35 | ||
36 | //zero all scores | |
37 | points = [0, 0]; | |
38 | roundPoints = 0; | |
39 | ||
40 | //we update the user interface with points | |
41 | document.getElementById("result-0").textContent = "0"; | |
42 | document.getElementById("result-1").textContent = "0"; | |
43 | document.getElementById("current-points-0").textContent = "0"; | |
44 | document.getElementById("current-points-1").textContent = "0"; | |
45 | ||
46 | //change the names to Player1 and Player2 because during gameplay we will modify these elements by setting the victory text | |
47 | document.getElementById("name-0").textContent = "Player1"; | |
48 | document.getElementById("name-1").textContent = "Player 2"; | |
49 | //remove the win class responsible for replacing the Player1/2 text with win | |
50 | document.querySelector(".player-0-panel").classList.remove("victory"); | |
51 | document.querySelector(".player-1-panel").classList.remove("victory"); | |
52 | ||
53 | // we remove the active class responsible for indicating the current player | |
54 | document.querySelector(".player-0-panel").classList.remove("active"); | |
55 | document.querySelector(".player-1-panel").classList.remove("active"); | |
56 | ||
57 | // we add the active class to Player1 because he is always the one who starts the game | |
58 | document.querySelector(".player-0-panel").classList.add("active"); | |
59 | } | |
60 | ||
61 | //we are adding an event detector to our page, this particular one will detect a click on the Throw Dice button | |
62 | // wecreate an anonymous function - one without a name, which we will use only in this particular context and will not be able to use it outside of the | |
63 | document.querySelector(".btn-throw").addEventListener("click", function () { | |
64 | if (canPlay) { | |
65 | //throw the dice, draw the value 1-6 | |
66 | const eyeNumber = Math.floor(Math.random() * 6) + 1; | |
67 | ||
68 | //we create the variable bonePicture storing a reference to the bone element on the page and display the cube | |
69 | ||
70 | const diePicture = document.querySelector(".die"); | |
71 | //select the appropriate graphic | |
72 | diePicture.src = images[eyeNumber - 1]; | |
73 | ||
74 | //display the bone | |
75 | diePicture.style.display = "block"; | |
76 | ||
77 | //update the result of the round if the dice didn't have a single eye rolled | |
78 | ||
79 | if (eyeNumber != 1) { | |
80 | //we add the score | |
81 | roundPoints += eyeNumber; | |
82 | document.querySelector("#current-points-" + currentPlayer).textContent = | |
83 | roundPoints; | |
84 | } else { | |
85 | //Please ask participants to write down the function call and we will write its definitions in the next lesson | |
86 | } | |
87 | } | |
88 | }); | |
89 | ||
90 | //mechanism to switch player | |
91 | ||
92 | function nextPlayer() { | |
93 | //conditional operator- condition is checked if true is returned then part after is executed ? if false then part after : | |
94 | currentPlayer == 0 ? (currentPlayer = 1) : (currentPlayer = 0); | |
95 | ||
96 | //we reset the round points | |
97 | roundPoints = 0; | |
98 | ||
99 | //we are zeroing the points in the user interface | |
100 | ||
101 | document.getElementById("current-points-0").textContent = "0"; | |
102 | document.getElementById("current-points-1").textContent = "0"; | |
103 | ||
104 | //switch the class so that it points to the current player toggle works so that if there is already a class indicated it removes it and if there is no class it is added to the given element | |
105 | document.querySelector(".player-0-panel").classList.toggle("active"); | |
106 | document.querySelector(".player-1-panel").classList.toggle("active"); | |
107 | } | |
108 | ||
109 | //program what to execute when the hold button is clicked | |
110 | document.querySelector(".btn-hold").addEventListener("click", function () { | |
111 | if (canPlay) { | |
112 | //add points for a specific player | |
113 | points[currentPlayer] += roundPoints; | |
114 | ||
115 | //we update the UI | |
116 | document.querySelector("#result-" + currentPlayer).textContent = | |
117 | points[currentPlayer]; | |
118 | ||
119 | //we check if the player has won | |
120 | if (points[currentPlayer] >= 100) { | |
121 | //we block gameplay | |
122 | canPlay = false; | |
123 | //we set the text Victory for the winner! | |
124 | document.querySelector("#name-" + currentPlayer).textContent = "Victory!"; | |
125 | //hide the die | |
126 | document.querySelector(".die").style.display = "none"; | |
127 | ||
128 | //add a win class to the current player that will modify the win text | |
129 | document | |
130 | .querySelector(".player-" + currentPlayer + "-panel") | |
131 | .classList.add("win"); | |
132 | } else { | |
133 | //if the player has not scored the required points to win then the player is switched over | |
134 | nextPlayer(); | |
135 | } | |
136 | } | |
137 | }); | |
138 | ||
139 | //clicking the new-game button activates the initialSettings function | |
140 | document.querySelector(".btn-new-game").addEventListener("click", newGame); | |
141 |