Java with Greenfoot Lesson4: Tic-Tac-Toe Game Part III

In this lesson, we will make the Board class check the game progress and stop the game once someone has won. I will then introduce the concept of Java Arrays. We will add code to constantly check whether three game balls of the same color has lined up and to mark those winning game balls.

Step 1: Add isGold(), isSteel(), and isUnclicked() functions to the Game Ball Class

To check the status of a game, we check the status of each game piece of the game board. The status of a game piece in this Tic-Tac-Toe game is: UNCLICKED, GOLD, and STEEL.   To allow objects of other classes to check the status of a Game Ball object, we add three more  access functions: isGold(), isSteel(), and isUnclicked(). The access functions of a class are functions to get or set the member variables of that class. The setGold(), setSteel(), and reset() functions that we added in the previous lessons are also considered as access functions of the GameBall class. 

Step2: Add Progress Checking to the Board Class

There are eight ways of winning a Tic-Tac-Toe game:

This is the code to check for player1’s win (three steel balls line up):

This is the code to check for player2’s win (three gold balls line up):

If these codes make your head spin and you feel like you are about to punch someone, hang in there and I will explain these cryptic texts

.

Here is a glimpse of the code just shown, but with three important parts boxed in pink. The first box that says “cell_1.isSteel()” is a function call that returns a Boolean value (true or false); the second box that says “&&” is a symbol that means AND; finally the third box that says “||” is a symbol that means OR.

All programming languages have their own syntax, just like English language has its syntax. Syntax, simply put, is the mechanism to put a sentence together. Let’s take a look at how we can turn several English sentences into Java codes.
“If cell one contains a gold ball, then switch player to player two, and stop the game.” can be re-written in Java as:
if (  cell_1.isGold()  ){
player.setPlayer2();
Greenfoot.stop();
}
“If cell one contains a gold ball, and cell two contains a gold ball, and cell three contains a gold ball, then switch player to player two, and stop the game “ can be re-written in Java as:
if ( cell_1.isGold() && cell_2.isGold() && cell_3.isGold() ){
player.setPlayer2();
Greenfoot.stop();
}

“If any one of the following conditions is true, then switch player to player two, and stop the game. Condition one is that cell one contains a gold ball, and cell two contains a gold ball, and cell three contains a gold ball. Condition two is that cell four contains a gold ball, and cell five contains a gold ball, and cell six contains a gold ball” can be re-written in Java as:

If (
( cell_1.isGold() && cell_2.isGold() && cell_3.isGold()   )   ||
( cell_4.isGold() && cell_5.isGold() && cell_6.isGold() )
) {
player.setPlayer2();
Greenfoot.stop();
}

Hopefully now your frustrate has abided and this code below  is making sense to you.


Try running the game now by clicking “Compile All” and then “Run”. If there are typos in your code, Greenfoot will quickly let you know. Make sure you have each open parenthesis has a matching closing parenthesis.

Step 3: Mark the Winning Line

There is one more thing we need to add. I would like mark the winning line. To do so, let’s first import an image for a winning game ball. Right click on GameBall button and select “SetImage” to open the Select class image window. Select “object”->”ball.png” and click OK.
Next, add two functions to the Game Ball class: setWinIfGold() and setWinIfSteel(). These two functions will be called when a winning line is drawn. The function setWinIfGold() sets the game ball image to ball.png if the game ball is currently gold; the function setWinIfSteel sets the game ball image to ball.png if the game ball is currently steel.

Then add the code inside the pink boxes in the Board class’ act() function so that all nine cells or game pieces  will check to see if they should change to the winning “costume”.


Note that there are three complete statements lined up in one line. It’s legal to line them up like this.


This block of code above is equivalent to:


Now test the game again by doing a “Compile All” and “Run”. Now when the game is over, the game balls on the winning line will turn to purple balls.

To download the source code, click HERE.