@@ -37,6 +37,8 @@ const updateGame = (p1,p2,gameState) => {
37
37
{
38
38
game . isOver = true
39
39
gameState = game . isOver
40
+ resultDiv . innerText = game . declareWinner ( game . isOver , p1 , p2 )
41
+ return gameState
40
42
}
41
43
}
42
44
@@ -55,24 +57,31 @@ class Player {
55
57
strike ( player , enemy , attackDmg ) {
56
58
57
59
// Get random number between 1 - 10 and that is damageAmount
58
-
60
+ let damageAmount = Math . ceil ( Math . random ( ) * attackDmg )
59
61
// Subtract the enemy health with the damageAmount
60
-
61
- // Update the game and DOM with updateGame()
62
-
62
+ enemy . health -= damageAmount
63
+ // Update the game and DOM with updateGame
64
+ updateGame ( p1 , p2 , gameState )
63
65
// Return a message of 'player name attacks enemy name for damageAmount'
64
-
66
+ return ` ${ player . name } attacks ${ enemy . name } for ${ damageAmount } `
65
67
}
66
68
// ** Heal the player for random number from 1 to 5 **
67
69
heal ( player ) {
68
70
69
71
// Get random number between 1 - 5 and store that in hpAmount
72
+ let hpAmount = Math . ceil ( Math . random ( ) * 5 )
70
73
71
74
// Add hpAmount to players health
75
+ if ( player . health < 100 )
76
+ {
77
+ player . health += hpAmount
78
+ }
72
79
73
80
// Update the game and DOM with updateGame()
81
+ updateGame ( p1 , p2 , gameState )
74
82
75
83
// Return a message of 'player name heals for hpAmount HP'
84
+ return `${ player . name } heals for ${ hpAmount } HP!`
76
85
77
86
}
78
87
}
@@ -87,34 +96,55 @@ class Game {
87
96
88
97
// ** If the game is over and a player has 0 health declare the winner! **
89
98
declareWinner ( isOver , p1 , p2 ) {
90
-
99
+ let message
91
100
// Create a message variable that will hold a message based on the condition
92
-
93
101
// If isOver is true AND p1 health is <= 0 then update message variable to 'p1 WINS!'
102
+ if ( isOver == true && p1 . health <= 0 )
103
+ {
104
+ message = `${ p2 . name } WINS!`
105
+ }
94
106
95
107
// Else if isOver is true AND p2 health is <= 0 then update message variable to 'p2 WINS!'
108
+ else if ( isOver == true && p2 . health <= 0 )
109
+ {
110
+ message = `${ p1 . name } WINS!`
111
+ }
112
+ console . log ( isOver , p1 . health , p2 . health ) ;
113
+
96
114
// Play victory sound
115
+ document . getElementById ( 'victory' ) . play ( )
97
116
98
117
// Return message variable
118
+ return message
99
119
100
120
}
101
121
102
122
// ** Reset the players health back to it's original state and isOver to FALSE **
103
123
reset ( p1 , p2 ) {
104
124
// set p1 health and p2 health back to 100 and isOver back to false and clear resultDiv.innerText and don't forget to updateGame()
125
+ p1 . health = 100
126
+ p2 . health = 100
127
+ this . isOver = false
128
+ resultDiv . innerText = ""
129
+ updateGame ( p1 , p2 )
105
130
106
131
}
107
132
108
133
// ** Simulates the whole match untill one player runs out of health **
109
134
play ( p1 , p2 ) {
110
135
// Reset to make sure player health is back to full before starting
111
-
136
+ this . reset ( p1 , p2 )
112
137
// Make sure the players take turns untill isOver is TRUE
113
138
while ( ! this . isOver )
114
139
{
115
140
//Make sure both players get strike() and heal() once each loop
141
+ p1 . strike ( p1 , p2 , p1 . attackDmg )
142
+ p2 . heal ( p2 )
143
+ p2 . strike ( p2 , p1 , p2 . attackDmg )
144
+ p1 . heal ( p1 )
116
145
}
117
146
// Once isOver is TRUE run the declareWinner() method
147
+ return this . declareWinner ( this . isOver , p1 , p2 )
118
148
119
149
}
120
150
@@ -133,13 +163,15 @@ let p2 = player2
133
163
let game = new Game ( )
134
164
135
165
// ** Intialize the game by calling updateGame() **
136
- updateGame ( p1 , p2 , game . isOver )
166
+ updateGame ( p1 , p2 )
137
167
138
168
// ** Save intial isOver from the game object inside this variable **
139
- let gameState ;
140
-
169
+ let gameState = game . isOver
141
170
142
171
// ** Add a click listener to the simulate button that runs the play() method on click and pass in the players **
172
+ play . onclick = ( ) => {
173
+ resultDiv . innerText = game . play ( p1 , p2 )
174
+ }
143
175
144
176
145
177
// Add functionality where players can press a button to attack OR heal
@@ -153,18 +185,17 @@ document.addEventListener('keydown',function (e) {
153
185
// After striking then play attack sound
154
186
document . getElementById ( 'p1attack' ) . play ( )
155
187
}
156
-
157
188
} ) ;
158
189
159
190
document . addEventListener ( 'keydown' , function ( e ) {
160
191
161
192
// if you press a AND the player health is greater than 0 AND isOver is still false then strike()
162
193
if ( e . key == "a" && p2 . health > 0 )
163
194
{
195
+ p1 . heal ( p1 )
164
196
// After healing then play heal sound
165
197
document . getElementById ( 'p1heal' ) . play ( )
166
198
}
167
-
168
199
} ) ;
169
200
170
201
// ** Player 2 Controls **
@@ -177,7 +208,6 @@ document.addEventListener('keydown',function (e) {
177
208
// After striking then play attack sound
178
209
document . getElementById ( 'p2attack' ) . play ( )
179
210
}
180
-
181
211
} ) ;
182
212
183
213
document . addEventListener ( 'keydown' , function ( e ) {
@@ -188,6 +218,5 @@ document.addEventListener('keydown',function (e) {
188
218
player2 . heal ( p2 )
189
219
document . getElementById ( 'p2heal' ) . play ( )
190
220
}
191
-
192
221
} ) ;
193
222
0 commit comments