Part two of the challenge was the code, the logic for taking turns, so in this video, I'm going to
go through the solutions for tasks three to five, starting with task three, which tells you to run
a loop that goes through each turn.
There are nine turns in a game of tic tac toe.
So I'm going to create a loop that runs nine times for enties equal to zero I smaller than nine I a
plus plus.
And for now, we'll print the counter during each run.
I know we didn't write a lot of code, but it's always a good idea to keep testing.
I want to get you in the habit of doing that, so I'll run.
First, it prints the grid and then it prints a bunch of numbers nine times.
We can now remove the print statement, the next step is to create an if statement that runs code if
it's X's turn, as well as an else plot that runs code if it's ostern, I left you with a hint to learn
the parts explains when the counter is even, whereas O plays on the counter is Ezzard.
A number is even if dividing it by two returns, the remainder of zero zero, I'm going to check if
the number modulus two is equal to zero.
Then I'll print.
Turn X otherwise.
Turno.
All right, and that's really it for TASC three.
Feel free to test your code.
And as expected, it keeps changing turns.
Now, before we read any more logic inside the final statements, we need to create some functions.
So moving on to task for.
Our next task is to write a function that lets the user choose a spot.
So we'll follow our comment public static.
The function returns an entero.
The function is called ask user.
And it receives one parameter, the board.
In order to satisfy this area, we need to create an array and return it, but I don't feel like creating
an array.
This is actually a great time to employ the array trick that I left you in the cheat sheet.
I provided you with the syntax and what this does is it offers a cool way to return a raise on the fly,
so I'm going to follow the syntax and return a new integer array.
Were the random values of 011 for now.
Now, you know me, I like to keep testing my code as I write it.
And there is still a lot of code we need to write for this function, so as you're writing it, we're
going to call it for each turne ask user pass in the board and call the function for Ostern as well.
Inside the function, I left you some instructions.
First, we're going to ask the user to pick a spot on the grid in the form of a row and column number.
If you go back to learn the part, pay attention to the output, the user writes each valley beside
the question.
Accordingly, I'm going to print the message using print, not print line.
And since we're asking the user to input two values, we need to call upon Skinner, I declared Skinner
for you at the level of the class so we should be able to access it from anywhere.
And here, I'm going to use it to pick up the RO.
The element in that row, i.e., the column.
OK, I think we have enough to start seeing some pretty cool output, run your code.
And you know what, I'm going to add some white space right after the Colin.
I'll use controls to stop the output we run the code.
Turn exile to zero zero and inside the row, I'll choose the first inducts.
Here, I'll do Rosero second index, and this loop runs exactly nine times.
So players can keep picking spots until the game ends.
All right, good.
After nine turns, the loop breaks.
Now, instead of returning to random numbers, the function is going to return the spot that the user
chooses.
All right, there's another step for task force, but we can't do it yet right now, we need to populate
the board with X's and O's.
And that would be task five.
During each turn, the function call returns an array of integers that contains the user spot, so we
need to store that array in a variable.
So I'm going to create a variable int spot.
And whether it's Axis Turner O's turn, we're going to set the spot variable equal to the return value.
We're going to use the spot variable to update the board.
The first bracket specifies the row.
And we know the road that the user chooses is index zero of our spot array.
So we'll use that value to index the row and the column, the element in that row would be in the second
index of our spot array.
And if we're inside the statement, we're going to update that spot on the board to X and if we're inside
or else it's OK turn.
So we're going to update the spot that they choose on the board of.
After the player picks their spot and we update the border Ray, we need to print the board and thank
goodness we can just call the function print board.
OK, now run your code.
I'm going to pick the spot zero zero.
And check it at.
The function excuser return to my spot is an array of two integers.
And we're using each integer to index the board and update the first element at Rosero.
After updating the array, we're reprinting it.
Now I'm going to update the third element of Roe to euro to index two, and everything works perfectly.
I'll choose the first element from one zero one zero.
The third element in row zero zero two and everything works well, OK, we're looking good so far.
And you know what, I'm going to rerun the code.
There is one thing I forgot to address, and that is the fact that the user can override a previous
spot.
Let me actually demonstrate that.
If I pick a spot to zero, players should not be allowed to take that spot.
But it does happen and we need to come up with a strategy to prevent this.
If we turn back the task for there was still one more thing we had to do inside the function, and that
was to check if the spot is taken.
If so, let the user choose again.
Conveniently, the function receives the board as a parameter so we can use a while loop and I can keep
asking the user to pick a new spot if the one that they choose is taken.
So while the spot that they choose.
Is already equal to X.
Or.
If it's already equal to oh.
Then we're going to print.
Spot taken.
Try again.
It's important that we use print, not print line, because once again, we need the user to enter his
enter beside the prompts and first we pick up the row, row is equal to scan to an extent.
And then we'll pick up the Cullom.
And I believe that wraps up part two.
Because we're officially done tasks three, four and five.
Let's run the code.
I'm going to pick up some random spots.
OK, good.
Now I'm going to pick a spot that's already taken.
It stops me from doing so, and it's going to keep stopping me until I choose one that isn't taken.
Purrfect.
What's happening is once the user picks a spot that isn't taken, the while loop breaks, it returns
the spot in the form of an array and we're using the spot to index the board and update it with the
appropriate symbol.
Now, I'd like to show you that there are always so many ways to write code that achieves the exact
same output, we know that the underscore represents a free spot.
So instead of this big logical comparison, I can just check if the spot the player chooses is not free.
Then keep asking them to pick another one.
This code should achieve the exact same output.
Once again, I'll pick random spots.
OK, good, now pick a spot that's already taken, it stops me from doing so and like before it's going
to keep stopping me until I choose one that isn't taken.
Beautiful.
Our game is really coming together.
I'll see you in part three.