Skip to content

Commit 16c20ba

Browse files
committed
update game guide
1 parent 36c85a7 commit 16c20ba

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

07-Pig-Game/README.md

+52-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ A flowchart is simply a graphical representation of the functionality of an appl
1313
- The blue box shows conditions like (true of false) conditions.
1414
- Finally the red box shows the result of a condition.
1515

16-
## Building the game
16+
## Selecting the Elements
1717

1818
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:
1919

@@ -24,6 +24,8 @@ const diceEl = document.querySelector(".dice");
2424
const btnNew = document.querySelector(".btn--new");
2525
const btnRoll = document.querySelector(".btn--roll");
2626
const btnHold = document.querySelector(".btn--hold");
27+
const current0El = document.getElementById("current--0");
28+
const current1El = document.getElementById("current--1");
2729
```
2830

2931
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,
4648

4749
### Display the dice
4850

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.
5052

5153
```js
54+
const currentScore = 0;
55+
5256
btnRoll.addEventListener("click", function () {
5357
// 1. Generate a random dice roll
5458
const dice = Math.trun(Math.random() * 6) + 1;
@@ -59,4 +63,49 @@ btnRoll.addEventListener("click", function () {
5963
});
6064
```
6165

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+
const scores = [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).
106+
document.getElementById(`current--${activePlayer}`).textContent =
107+
currentScore;
108+
} else {
109+
// switch to next player
110+
}
111+
```

0 commit comments

Comments
 (0)