updateGameState() Method Solution2:15 with Ashley Boucher
See the solution for the updateGameState() method.
Welcome back. 0:01 Congrats on finishing the final step in developing our four in 0:01 a row game, we're done. 0:04 Let's go over the solution for the last assignment, and 0:06 then join me in the next and final video to demo the game. 0:09 First, you were tasked with adding a method to the player class called 0:13 checkTokens. 0:16 This method's job is to tell us if a player has any unused tokens left. 0:17 If they've used all their tokens, they can't play anymore. 0:22 We used another ternary expression here. 0:25 If the length of the unused tokens array equals 0, then we return false. 0:28 If it's not 0, we return true. 0:33 Okay, let's move to game.js to review the final method, updateGameState(). 0:36 The updateGameState() method receives two arguments, token and target. 0:43 Token is the token object that was most recently dropped by a player, and 0:48 target is the space object that that token now occupies. 0:53 Your first step inside the updateGameState() method 0:58 was to associate the space object with a dropped token. 1:01 This is done using the mark() method you wrote earlier in this stage. 1:05 We call the mark method on the target space and pass in the dropped token. 1:09 As you recall, 1:14 this will set the target space's token property to the dropped token object. 1:15 The next step is to check if the last move was a winning move. 1:21 This is done with a simple if-else statement. 1:25 If it was a winning move, 1:28 we pass a message to the gameOver() method and that's it. 1:29 If it wasn't, we have some other steps to take. 1:32 That logic is all handled in the first part of this if-else statement. 1:35 First, we switch players. 1:40 That's simply a call to the switchPlayers() method you wrote earlier. 1:43 Then, we use another if-else to check that the newly active player still has tokens. 1:47 If they don't have tokens available, then they can't complete their turn, and 1:53 the game is over. 1:56 If they do have tokens left, then we need to render a new HTML token for 1:58 them, using the drawHTMLToken() method you wrote much earlier in the course. 2:03 After that, we set the game state back to ready, and 2:09 voila, that's a wrap on four in a row. 2:12
You need to sign up for Treehouse in order to download course files.Sign up