You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 07-Pig-Game/README.md
+52-3
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ A flowchart is simply a graphical representation of the functionality of an appl
13
13
- The blue box shows conditions like (true of false) conditions.
14
14
- Finally the red box shows the result of a condition.
15
15
16
-
## Building the game
16
+
## Selecting the Elements
17
17
18
18
As usual we'll store our classes and IDs in variables. In the previous lecture, we learned how to select elements using `document.querySelector()`, but there is a special way of specifically selecing IDs in a webpage and that is:
But unlike the `querySelector`, we're only passing in the name of the ID without the class selector(`#`).
@@ -46,9 +48,11 @@ And remeber this will generate a number from 0 to almost one so 0 - 0.9999999e,
46
48
47
49
### Display the dice
48
50
49
-
Since we hid the dice earlier by adding the hidden class using the `classList()`method, we'll show the dice by removing the class this time. To show a random dice image, we'll use the `.src()` property to define what image we want to show based on the random generated number.
51
+
Since we hid the dice earlier by adding the hidden class using the `classList()`property, we'll show the dice by removing the class this time. To show a random dice image, we'll use the `.src()` property to define what image we want to show based on the random generated number.
50
52
51
53
```js
54
+
constcurrentScore=0;
55
+
52
56
btnRoll.addEventListener("click", function () {
53
57
// 1. Generate a random dice roll
54
58
constdice=Math.trun(Math.random() *6) +1;
@@ -59,4 +63,49 @@ btnRoll.addEventListener("click", function () {
59
63
});
60
64
```
61
65
62
-
Here `dice` represents the random dice number from 1 - 6;
66
+
Here `dice` represents the random dice number from 1 - 6. The next step is to write our condition to check if the dice rolled is a 1, if it is, then the score of the current player will be 0 and it will switch to the next player or else, the point gets added to the current score. But we need to capture that current score and not just have it changed on the DOM.
67
+
68
+
We'll use the let keyword since the current score will be constantly updated and also declare this variable outside of our handler function because we want to persist that value and not have it set to 0 every time the btnRoll function is executed.
69
+
70
+
```js
71
+
// 3. Check for rolled 1: If true
72
+
let currentScore =0;
73
+
74
+
if (dice !==1) {
75
+
// Add dice to current score
76
+
// currentScore = currentScore + dice0El; // Can also be written as
77
+
currentScore += dice;
78
+
// display score on current element (on the first player for now).
79
+
current0El.textContent= currentScore;
80
+
} else {
81
+
// switch to next player
82
+
}
83
+
```
84
+
85
+
### Switcing the players
86
+
87
+
Next step is switching between the players if the score is 1. In other to do that, we need to capture the player that is currently playing, essentially the active player. To do that, we'll store the value of the active player in a variable and set it to 0 because player 1 is 0 and player 2 is 1.
88
+
89
+
The reason we're doing this is because we'll store the score of each player in an array and since arrays are zero based, the first score will be at position 0 and second score at positon 1.
90
+
91
+
With this activePlayer variable, we can now dynamically set the score of the current player and not just for the first player alone.
92
+
93
+
For the else block, we want to
94
+
95
+
```js
96
+
// 3. Check for rolled 1: If true
97
+
let currentScore =0;
98
+
constscores= [0, 0];
99
+
let activePlayer =0;
100
+
101
+
if (dice !==1) {
102
+
// Add dice to current score
103
+
// currentScore = currentScore + dice0El; // Can also be written as
104
+
currentScore += dice;
105
+
// display score on current element (on the first player for now).
0 commit comments