Now, third step in our loop: displaying the game.
And now, fourth step...
... let's shift this all to have a clearer picture...
... fourth step of our loop: alternating the colors,
i.e changing the color of the current player.
As usual, we write comments to be as clear as possible.
So, we change the color very easily.
We could also write a method to do this, actually this would be a good place to do so,
but the method is simply: if the current color is yellow,
then we change it to red.
Otherwise, we set it to yellow.
So, <i>if</i> the current player's color is yellow,
we set it to red.
So if the color of the player is yellow, I set it to red.
Otherwise, I set it to yellow.
<i>couleur_joueur</i>, remember, is the variable representing
the color of the current player.
There, we have finished our loop
and all we need to add is the condition.
What condition?
The game continuation condition.
We continue playing while...
Well, I will leave this to the next episode,
and leave this blank.
This concludes our <i>main</i> method that allows us to play.
I encourage you to compile and test it.
Of course, you have to write something here in the blank space.
At first, you can write <i>while true</i>
to have an infinite loop,
and you can stop the program with the environment.
I encourage you to systematically
make tests like this
when you are at such a step.
Personally, I would prefer to review this code right now with you
to organize it better, to modularize it,
that is, to create methods to handle the most important parts.
We have already said that we could write a display method here
and we will come back to this later on.
We could write a method to alternate between the players.
And, on a larger scale,
we could make a method of all of this,
since it is essentially the core of the game.
The <i>main</i> is way too large here. As you can see,
it doesn't fit on one slide
which is a sign that it is not well written.
A <i>main</i> must be very concise, very clear.
So we will modularize it, we will chop it up into methods.
The easiest is to directly cut out the part we want to make into a method,
as we explained to you in the episode on methodology for
function and method development.
Then we start to write the method call as we envision it,
so for example, let's say that this part corresponds to asking and playing,
so we will call it demandeEtJoue (TN: means "askAndPlay").
This will be the name of our method, demandeEtJoue.
And what does this method need to receive?
It needs the current game of course, so the grid
and the color of the current player.
Those are the two arguments we pass it.
And now we can proceed to writing this method.
As a reminder, here is the call in comment.
So we pass the grid and the player's color to demandeEtJoue
This gives the following header:
void demandeEtJoue taking a grid
and a color.
Then, in the body of the method, we will simply
copy everything we had cut out of the <i>main</i>
to make it into a method.
At this stage, one should be very careful not to make any mistakes in the names.
For example, here we used <i>couleurJoueur</i> again
and I strongly encourage you to save, compile and verify
that the code is correct and that we haven't made any mistake.
Of course, you should proofread it,
and possibly test it too
with a small test method.
In the spirit of modularizing,
let's try to avoid copy-pasting as much as possible.
I see several copies of the same thing here.
I see that "Joueur" was repeated here.
I see that here, we repeated the message asking the user for a column number.
This all is not right and we will have to make it modular.
To do so, we will find the common parts
and remove them from the condition on the color.
Because, which part of this depends on the color?
The only part that does is this output here
of the X and O.
So we will put the common parts preceding the display of the X
before the <i>if</i>,
and we will put the common parts following the display of the X and O
after the <i>if</i>.
This gives the following code:
We start by displaying the common message "Joueur "..
and careful here, we are using <i>print</i> and not <i>println</i>
since the line is not finished and we do not want to display a newline.
Then we make our test on the color and in this case,
if the color is yellow, we display the X
otherwise, we display the O.
Then we have another common part which is displayed using <i>println</i>
thus displaying the end of the message.
And now, we see again that we could modularize
a little bit more.
And this little bit of code here, these few lines,
could be a color output
which would be useful both in displaying the game
and displaying the player like we did here.