Winning Part 2 - Diagonal Checks

## Contents

###### Defining the Game Mechanics
1
Course Introduction
PREVIEW12m 47s
10
11
12

## The course is part of this learning path

Start course
Difficulty
Intermediate
Duration
2h 36m
Students
19
Ratings
5/5
Description

In this course, we begin building a game in Solidity, and more specifically, we begin to look at defining the mechanics and the components of the game.

Transcript

In the previous lecture, we were talking about how to check the columns and the rows of the game. So, we set the winner. And in this lecture, we are going to define the diagonal and the anti diagonal. So, how is that working? First of all, we have to figure out if we are on the diagonal, and that is fairly easy. We have ether on the top left, middle, middle, bottom right corner, so that is either 0,0 1,1 or 2,2. That means x and y are the same. So, if x is y then we are on the diagonal. On the other hand, if we are on the anti diagonal, meaning from the top right down to the bottom left, then we have x is 0 and y is 2. So, in total, that's 2. x and y is 1, so in total that's 2 or y is 2 and x is 0. No, the other way around. x is 2 and y is 0. So, that's 2 as well. So, if you have x + y is the same as boardSize-1 to have it more generalized, then we are on the anti diagonal. And I'm going to show you how to get the winner right for the anti diagonal and the coding challenge will be how to get the winner right for the diagonal. Okay, so we do pretty much the same as with the columns and rows. We just have to get our indexes right. Hope we have a uint8, i is 0. It's smaller than boardSize. And this must increase with every iteration. And then, here comes the crucial part. We have the gameBoard and here we go, the anti diagonal. So, we start on the bottom left corner. Going from the (boardSize-1) -i). And here we have the same logic as with our columns and rows. If we don't have the activePlayer there then we break here and if we reach the end of the boardSize, if you reach the end of the board, then we have a winner and that is the activePlayer, and we can return here. So, the coding challenge will be, how do you do this for the diagonal?   So, this one is the anti diagonal. And here we have the diagonal. How do you do this? If you want to do the coding challenge then pause the video now or else I'm going to show you in a second how this is done. I'm sure you've figured it out fairly quickly. So, obviously we have this for loop again, uint8, i is 0 and this i goes to the boardSize and every time on every iteration it increases. And now comes the crucial part. If the gameBoard[i] [i] not the activePlayer then we break here, or if i == boardSize-1, we have a winner which is the activePlayer. And now, comes the most fun part. Going to reload this, and we're going to give it a try. We go over to the run tab, deploy the GameManager, give it 0.1 ether, start a new game. Getting my game address here. And I was discussing this before with a colleague of mine and yes, you can definitely, if you feel like this is a really weird way of starting a game, you can modify the ThreeInARow game that you can just deploy the ThreeInARow game right away, so you don't have to add in  a GameManager at all. I'm just doing it this way because the way the game was developed, it's always going by the GameManager. So, if you feel like deploying a GameManager then starting a game for the GameManager and so on is a really weird way just to test something, then obviously you can just deploy the ThreeInARow game.

Let me take the other account, 0.1 ether and we are joining the game. And now comes the crucial part. We set our stone to 0,0. That is going to 0,1 and we set the stone to 1,1. That one is setting the stone to 2,0. And now, in between we obviously we can always have a look at our board if you want to and later in JavaScript you will see that this will be a nice 3 * 3 array like this one where we have the address over here. Then the address over here. Then there is 0,0 and then there's another 0,0 here. Then there is the address of the second player. So, we have like, here we have x, here we have a o. Here we have nothing, nothing. Here we have x now. Then we have 0, then we have here an o and nothing and nothing.

And now, the last player which is this one it has to set the stone to 2,2 sort of closed around, and he is the winner, and he gets the 0.2 ether which we had as a game cost. All right, that's it for this lecture. It was a shorter one. And our game logic is pretty complete. We have one more thing to do, which is, what if one of the players just suddenly stopped playing, we don't want to have our ether getting stuck in there? So, we play a little bit, or we implement something with time. So, we have time constraints. We are not letting a player play longer than five minutes, maybe. And after that, we're already talking about high scores, and we add some more events. And then, in the next sections we are talking about HTML, Truffle, JavaScript, In-depth, DVM and so on. So, definitely stay tuned. And if you have any questions, then reach out to us in the course Q and A. We are always here for you, and I'll see you in the next lecture. 