In the previous video, you saw how to write the main loop that allows the players to play in turn. All that was missing was the break condition, that is, what must be written here so that the loop stops when one of the players has won, or when the grid is full. So for the moment, we'll concentrate on the case where one of the two players has won. I'll therefore introduce a new method that I'll name 'estCeGagne' and that I'll call like this: I'll put the return value of my 'estCeGagne' method in a variable that I'll name 'gagne', of boolean type. To function, my 'estCeGagne' method needs the grid and the color of the player that has just played. I need to declare my 'gagne' variable, and I need to declare it before the loop, as I'm going to use it within the loop's break condition. It's of boolean type and I can now use it to define the break condition, and we'll repeat the loop until the player hasn't won. We have to write this 'estCeGagne' method, which is probably the most difficult part of our program. There are many ways of writing this 'estCeGagne' method, and we propose the following one. We'll navigate through the grid line by line and column by column until we find a chip of the color of the player that has just played. Let's say for example that the red player has just played, and that we find this chip. We'll start from this chip and navigate through the grid in several directions, horizontally, vertically, and diagonally, to count how many chips of the same color there are starting from this chip. For example, if we consider this direction, we can count one, two, three, four red chips, we therefore know that the player has won. Instead, if we navigated through the whole grid and we've always counted less than four chips, we know that the player hasn't won. We can note that we don't need to consider all the directions and in fact we'll content ourselves to count the chips in four directions only, and we'll choose for example upwards to the right, to the right, downwards to the right, and straigth downwards. So why is it sufficient to consider these four directions? I said that we'll browse the grid line by line and column by column. Let's suppose once again that the red player has just played. The first chip we'll find is this one. We'll first of all count the chips in this directions, we'll count a single chip, this one. The same goes for this direction, and this direction, there's only one chip in these three directions. In this direction, there are two chips. This isn't sufficient to win, so we'll continue navigating the grid. The next red chip that we'll find is this one. In this direction and this direction we'll count only two chips, which isn't enough to win. In this direction and this direction, we'll count only one chip, which still isn't enough to win. We'll continue navigating the gris until we reach this chip; we'll continue counting the chips in this direction and this time we'll count one, two,three, four chips and we know that the player has won. To prove that the red player has just aligned these four chips, we can either start from this chip here and go in this direction, or start from this chip here and go in this direction. Therefore, we aren't obliged to consider these two directions, we can content ourselves of only one, and the same goes for the six other directions. We'll only look at this direction and not this direction. In this direction and not this one, in this direction and not this one. We are left with four directions to consider. We now have to code this strategy by writing the 'estCeGagne' method. Let me remind you that we call the 'estCeGagne' method by the following way, for example. We put the return value of the 'estCeGagne' method in a variable of boolean type. And the method awaits a grid and the color of the player that has just played as parameters. The method's header will simply be the following. Other strategies consist of navigating through the grid, which we'll do with the help of these two for-loops. And as we need the chip's coordinates, we'll use two standard for-loops like this. The ligne (= line) and colonne (= column) variables will contain the disc's coordinates from which we'll count the number of aligned discs. To simplify a bit the code, I'll introduce a local variable that I'll name 'couleurCase' to contain this chip's color, which is grille[ligne] [colonne]. If the cell's color is the same as the player that has just played, then I must count the chips in the four directions in which we are interested. So, as it's a bit difficult to count the chips, well I'll introduce a method that I'll simply name 'compte' (= count) and that will do this task. What arguments does the 'compte' method need? Well first of all, of course, of the chip, the coordinates of the disc from which we want to count the aligned chips. These coordinates are contained in the 'ligne' and colonne' variables. And we need the direction in which we want to count the aligned chips. So how can we define this direction? Let's suppose for example that I want to count the chips diagonally to the top-right. In this case, to go from one cell to another, we'll have to subtract one from the line and add one to the column. Therefore, I'll use these two values, this minus one and this plus one, as the arguments for my 'compte' method, to say that I want to go to the top-right. I'll send to my 'compte' method the number of aligned chips and if this number of chips is greater than or equal to four, then I know that the player has won. So why greater than or equal to four, and not simply equal to four? Well, that's simply because one can win by aligning more than four discs. So of course, I need to consider the three other directions and that will be written like this. To count the chips horizontally, I won't change the line, and that's why I use zero as argument here. But the column will change, and I'll go to the next column, that's why I'll use the value plus one as last argument. Then, to count the discs diagonally to the bottom-right, to go from one cell to the other, I'll add one to the line, that's why I use the value plus one here, and I also add one to the column, and that's why I use the value plus one here. Finally, there remains a last direction. To count the chips vertically downwards, I'll go from one cell to the other, By adding one to the line, that's why I have the value plus one here, But the column won't change and I use the value zero here. If one of these four conditions is true, then I know that the player has won. It is sufficient that one of these four conditions is true, that's why I use an 'or' here. And in this case, I'll return everything to my method, I'll close the curly braces I had previously opened, and if I get to this point of my method, it's because I've never passed by here, that is, I never found at least four aligned chips, and I know that in this case, the player hasn't won yet. So I'll return 'false' from my method, and as a good programmer, I'll add comments to explain the difficult points of my method. Of course, we still need to write the 'compte' method. Let me remind you that we'll call the 'compte' method like this, for example. That is, the method will return a number of discs, that's why I'll use the 'int' type to define the type of the result value of the method. The first parameter is the grid, the two next parameters are the line and the column of the disc from which we'll count. They are both of 'int' type, and the two last parameters are the direction for the line and the column that I'll call 'dirLigne' and 'dirColonne', as they are of the same nature, and one refers to the line and the other to the column. I'll declare a 'compteur' variable initialized with zero to count the number of aligned discs. What must the 'compte' method do exactly? Well, it must start from the line 'ligneDepart colonneDepart', and must browse the grid in the given direction starting from 'ligneDepart' 'colonneDepart' while we find chips of the same color as the chip at 'ligneDepart' 'colonneDepart'. So how will we achieve this? Well, we'll use two variables that I'll name 'ligne' and 'colonne', that we'll respectively initialize with 'ligneDepart' and 'colonneDepart'. And to go from one cell to the other, at each loop cycle, we'll add the value of 'dirLigne' to 'ligne', and the value of 'dirColonne' to 'colonne'. As we'll browse the grid while we find discs of the same color as the disc that is at 'ligneDepart colonneDepart', we'll use a while-loop. During each cycle of the while-loop, we'll have found a new chip of the same color and we'll increment the 'compteur' variable. How will we write all this? Well, we'll begin by declaring the 'ligne' and 'colonne' variables, initialized with 'ligneDepart' and 'colonneDepart', we'll therefore use a while-loop with a break condition that depends on the color of the disc at position 'ligne colonne', and we'll continue while the color of this disc is the same as the color of the disc at the position 'ligneDepart colonneDepart'. During each loop cycle,we'll increment the 'compteur' variable, as we've found a new disc of the same color as the first, and we'll add 'dirLigne' and 'dirColonne' to 'ligne' and 'colonne'. By the way, note that I added spaces here and here to align the equal and plus characters on these two lines, that makes the code more readable, it highlights the fact that the variables 'ligne' and 'colonne' are of the same nature. When we exit the loop, it means that we've found a disc that wasn't of the same color, and we can return the value contained in the 'compteur' variable. Except that our method isn't totally correct, and maybe you see why. As with the 'joue' method, we could leave the grid, that is, the 'ligne' or 'colonne' variables can take illicit values. So, to correct this, I'll add a few tests to the while-loop condition. I'll verify that the line is indeed greater than or equal to zero, and that it is less than the number of lines, and I'll add the same tests for the column. Finally, I'll add a comment to describe what the loop does. To conclude, I'll come back to my 'estCeGagne' method to note that if the starting cell's line has the value zero, one, or two, it isn't necessary to look in this direction, for example, as we already know that the player can't align at least four discs. If we start from the line two, we know that the player can align a maximum of three discs. The same goes when we start from the right-side of the grid. It isn't necessary to look in this direction, and the same goes when we start form the bottom of the grid, it isn't necessary to look to the bottom. Our 'estCeGagne' method does more than necessary, so I'll modify the code to avoid counting the number of discs when we are sure that there won't be at least four aligned discs. So, that will be written like this. When we consider the direction diagonally to the top-right, to be able to align at least four discs, the line must be greater than or equal to three. And the column mustn't be too big, and, I'll let you verify when you do it, the column must be less than the number of columns minus four. When we look to the right horizontally, it is sufficient that the column isn't great enough and that it is, similarly, less than the number of columns minus four. When we look diagonally to the bottom-right, the line mustn't be too big, and as with the column, the line must be less than the number of lines minus four, and the column mustn't be too big. And when we count the number of discs downwards, it is sufficient that the line is less than the number of lines minus four. Now, you can observe that I've introduced a few repetitions, as this expression appears here and here, and this expression also appears twice. I'll therefore introduce constants to replace these repetitions, constants that I'll logically name 'ligneMax' and 'colonneMax', and which will avoid the repetitions. Voilà, this time was really the final version of the 'estCeGagne' and 'compte' methods. We still have to handle the case where the grid is full without any of the players having won, and that's what we'll see in the last video.