How to make Reversi Part 27 – Rules – Game Over

How to make Reversi Part 27 – Rules – Game Over


– Before we had added all of the code that
restricted moves to places where it was valid to move according to the game of Reversi,
we had set up our server so that the game was over when all the positions had been filled. Well, in a game of Reversi that’s played according
to the rules, it’s possible that the game could be over before the spots are filled. And so what we need to do now is we need to
change the way that our game-over is being calculated, so that we check whether or not
there are any valid moves left, and if not, then we declare a game over with the appropriate
winner as the victor. So we can do that by editing code in server.js,
and it’s not very complicated a little bit of logic in our shut down bit. And so what we want to do is we want to go
to the location where we do our game update. Send game update. That’s not it, where’s our message? We are in server.js, Let’s see. I’m… There we go, all right, so we are in the right
spot here. So where am I? I am in the function send_game_update in server.js. Down here previously we had checked to see
whether all positions were filled with tokens. And now we need to change that, and so what
we’re gonna need to do is we’re gonna need to calculate a winner, and we’re going to
need to calculate whether or not there are any valid moves left. So let’s go ahead up here where we’re keeping
track of our count, and let’s add an amount that we’re going to keep track of for black
and white, and this will be how many spots that they have captured on the board. And as we go through in our row and our column,
we will say that count is now gonna be the number of legal moves that are left over. So we will say that if(games[Game_id] dot
legal_moves [row][column] If it’s not equal to zero then… Then there’s a legal move available ’cause
legal moves have a character when someone can move there. So that indicates that there’s one more legal
move. Right, okay, so now let me copy that code
and we’ll say that if, but not the legal moves, but if the board is equal, double equal, to
be, we can do triple equal if we want, then we’re gonna increase the number of tokens
that black has captured, copy those three lines, and if it’s equal to white then we’ll
increase the number of tokens that white has captured. Right, so this is just gonna go through and
it’s gonna give us the state of the world. How many legal moves are left, how many tokens
does black have, how many tokens does white have? Alright, and then down here, rather than checking
whether the count is equal to 64, we’re gonna check whether count is zero, meaning we have
no legal moves. In which case we’re gonna send a game over
message and we’re gonna have to set up who the winner is. We’ll say that the fault is that it is a tie
game but if black is greater than white, then we’ll say the winner equals black. But if the opposite is true, that if white
is greater than black, then we’ll say that the winner is white, and then in our success
data, rather than saying that everyone won, we’ll say that the winner has won. And yeah, that’s it, so now we’re checking
if there’s no legal moves, and if so then the game is over and then we declare who the
winner is by counting. And actually I think that’s everything, so
let’s go to our demo, and I will play through a game real quick, trying to end it as soon
as possible. This is our, the last one’s running here. End that, and let’s run a new server. Let’s go back one and back one. And, oh, I forgot apostrophes, so back here
under tie game, need another apostrophe there. And try it again. There we go, server is running. And we’ll reload our game. And I will attempt to end a game quickly. Okay it’s black’s turn, I’m white, it’s black’s
turn, I’m black. Alright, black can go there, and let’s have
white go there. I’m trying to throw the game for black, I’m
not doing so well. Great, game over, black won, score looks right,
all done. All right, just a little bit of cleanup, and
then I think we’re done with Reversi. One more thing we wanna add is how long it’s
been since the last player has played. That’ll be in our next code, thanks for your
attention.

Leave a Reply

Your email address will not be published. Required fields are marked *