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.

8 thoughts on “Java with Greenfoot Lesson4: Tic-Tac-Toe Game Part III”

  1. Hi,
    I have been learning Java code and I stumbled upon your tutorial to make a Tic Tac Toe Game. So far all the lessons have been very helpful in understanding how Jave code works. I have come across a error in my code tho and I have been trying to figure out what is wrong.

    My error occurs when you add how to figure out the game winning ball. My question to you is, where do I put the code for the winning ball? Because from the lesson I thought you are suppose to put it into the world board class, under the act method. But this brings up and issue because from friends of mine tell me that the world class should not have act methods in the code.

    Thank you,
    Anthony

    • Hi Anthony
      The logic of a winning ball should best be added to an object other than the ball objects. You can either add another class or reuse a non-ball object. I chose to the World object.

    • Yes, you put in the Board class. This is the only class that has access to cell_1, cell_2, etc. Your friends are wrong. There is nothing wrong with having an act() method in a subclass of World.

  2. bis lesson 3 ist alles ok. ab lessen vier läuft das spiel nicht mehr, bekomme die meldung jaya.lang nullpointer. bitte helfen Sie mir weiter.

  3. Hi Folks,
    I have just stumbled over this series and it looks great. However, the sourcecode hyperlinks do not seem to work.

    Any suggestions where I can get the code ?

    Steve

Leave a Reply to Nathan Cancel reply