In part three, the challenge was to write code that determines the winner.
Right now, the app lets the player choose a spot on the board, but we need to write logic that stops
the game as soon as one of the player gets three straight access or three straight O's.
This is not easy to implement, so I'll go through it slowly.
Here in TASC six, we can say with the usual public static, the function returns an integer.
It's called Chegwidden.
And it receives the tic tac toe board as a parameter.
Now, inside the function, the first step is to make a count variable that starts at zero in count
is equal to zero.
And we can silence this error by returning the count right now.
That was easy, but task sex is not over yet.
If you scroll all the way up, I left you some instructions.
So here I need to call the function check when.
And I hope it's clear to you that every time the loop runs during every single turn, we're using check
when to check if anybody one.
Quinn returns an integer, and I'm going to store this return value in an integer called Count.
All right, if that injury happens to be three, that means X got three in a row, so we need to print
X wins.
I'm going to manifest this in the code F count equals three.
Print X wins.
And break the loop if it's the opposite, if the energy return from calling the check, one function
is equal to negative three.
Then we can print Erwin's.
And break the loop.
In essence, we're checking for a winner after every single turn.
So right now, our function doesn't do anything, it just return zero.
So we need to start with task seven.
We need to check every single row for straight Xs or straight oaks.
Now, running through every character in a 2D array requires a nested loop.
The outer loop is going to start with a row index of zero.
It's going to keep running as long as the row index is less than the length of our board, which is
three, the number of rows.
And then as we hit the inner loop, Jay is going to start by indexing the first element in the current
row, Jake will zero and the loop keeps running until Jay indexes every element in that row.
J plus plus.
And remember that the first bracket gets Dré, the road that we're interested in, we will use the Arrow
Index to index each arrow and the second bracket gets the element in that row.
We're going to use the counter j to index every element in the current row.
So as the outer loop counterspy is fixed to a row, the inner loop countered J indexes every value in
that row as a reminder of how nested loops work.
Here's an animation that I made for you.
If you'd like to skip it, then you can fast forward to the following timestamp.
OK, as the inner loop runs through every character and a fixed arrow, we're going to check if that
character is inducts.
If so, we're going to add one to the counts.
If the character is, oh, we're going to subtract one.
So after the inner loop, we just went through an entire row of characters and we're going to check
if the row count resulted in a value of three or negative three.
If so, we're going to break the whole function prematurely by returning the counts.
Otherwise, we have to reset the count.
This is a really important.
Because you want your next inner loop to start counting again from zero.
And with that being said, our code is now able to check every row for a straight X or a straight O.
That's around the code.
I'm going to keep playing put x.
Oh.
Oh, and before I put my last text, let's make sure we understand what's really going on here, after
I played the latest video, it printed the board, obviously.
And then check when was called.
First, the Roman Dukas fixed that zero in the inner loop, countered Jay's indexing every value in
that road.
There are two X's, so that counts to.
Nothing happens, so the count resets to.
Now we're indexing every value in a row, one, there are two O's, so that counts negative to.
Again, nothing happens, it resets the count to zero, and now we're indexing every value in a row
to there's nothing here, so the count is zero.
So our function returns a count of zero.
And that's not going to do anything for us.
But in our next turn, if I put X X twins Nahar.
Now, as we're checking the first rule, that counts three.
The return key word breaks the function prematurely and returns the count.
And since the return value is three X wins, breaks the loop and the game is done.
Cool.
Now, before moving on, we need to test for Stratos.
I'll keep playing till I get all those.
And perfect.
This time, once the ROE index is fixed, that ROE one, it counts three values.
Which results in a count of negative three.
And after we're done checking that specific row, the return keyword breaks the function prematurely
and returns the count.
And negative three means a witness that is all for task six and seven in the next video, we're going
to implement Task eight.
See you there.