Java Games Lesson3: Tic-Tac-Toe Player
Java Games Lesson3: Tic-Tac-Toe Player
In Lesson 2, we created the Tic-Tac-Toe Board and GameBall classes. We also added GameBall objects to
the Board object. In this lesson, we will add a Player class such that a Player object interacts with
GameBall objects in a meaningful way.
This game takes two players, who will take turn clicking. When the game starts, all nine cells on the
board are blank. When the first player clicks at a blank cell, a Gold ball will be placed in that cell; when
the second player clicks at a blank cell, a Steel ball will be placed in that cell.
To interact with the GameBall objects, we need a class called Player. Right click on the Actor button and
select New subclass… from the pop-up menu. At New class name, enter “Player”, then import the first
image, ant.png, by selecting animals->ant.png, and click OK.
Now the Player class has been created but we still need to import another image to represent another
player. To do so, right click on the Player icon and select Set Image… from the drop-down list. Import
another image, ant-with-food.png, by selecting animals->ant-with-food.png.
NOTE: You can select other images but make sure they are not larger than 30x30 pixels (for the cells are
60x60 pixels each), or else the game would not work as planned.
Before adding interaction between the Player class and the GameBall class, we need to add states to the
GameBall class. Three states are needed: UNCLICKED, GOLD, and STEEL. We will add a member variable-
ballState-to hold the state information and three member functions-setGold, setSteel, and reset-to
access and control the member variable.
A Java class can have member variables and member functions. Member variables are like states or
settings of an object, whereas member functions are mechanism to access and control these settings.
When an object is created, it’s assigned a unique segment of memory space to hold its member
variables. Moreover, an object has access to a shared set of class functions.
Take GameBall class for example, if we create two GameBall objects (object of the GameBall class type)
called ball_one and ball_two, then each of them will have a ballState variable and will have access to
setGold(), setSteel(), and reset() functions.
To implement the three states, we use Java’s enumerated type. An enumerated type has a finite
number of named values. For example:
When the GameBall object is first created, we would like its state to be UNCLICKED. As the game goes
on, players set the GameBall state via set functions.
Going back to the GameBall’s code, it is the subclass of the Actor, based on this statement:
The Actor class can have many subclasses; in fact, most new classes you created in Greenfoot are
subclasses of the Actor class. GameBall and Player are both subclasses of the Actor.
The Actor class has a public function called setImage, which changes the image file path. Since GameBall
inherits from Actor, it can call setImage function like this:
setImage("gold-ball.png");
Or like this:
super.setImage(“gold-ball.png”);
You will see the documentation for the act and getImage functions, among others.
Now we are ready to complete the GameBall code. Add an enumerated type call “BallState”, a member
variable called “state”, and three public functions: “setGold”, “setSteel”, and “reset”. A member function
can be public, protected, or private. A public function is accessible to all other classes and a private
function is accessible only inside its own class. For example, if class A has a public function f1 and a
private function f2, then class B can only access f1, but not f2. But f1 is accessible inside f2.
You may have noticed the empty act function which I will explain in the next step.
Next, we will add codes to the Player class so Player objects can interact with the GameBall objects. The
Player class has two states: PLAYER1 and PLAYER2. Similar to what was done to GameBall, add an
enumerated type call “PlayerMode”, a member variable called “mode”, and two public functions:
“setPlayer1” and “setPlayer2”.
Next, let’s add code to Player’s act function so that a Player object handles mouse clicks. The act
function is called automatically and repeatedly by the Greenfoot framework; if you want your Actor to
perform a task repeatedly, you would put the code related to that task inside the act function.
The first we need to add to act is the code to “glue” the Player to the mouse:
With this code, each time the mouse clicks, the Player object would first check whether it has collided
with a GameBall, is yes (ball is not null), then it switches mode to another player by calling either
setPlayer1 or setPlayer2. Moreover, when player1 is playing, each mouse click turns a ball from to STEEL;
on the other hand, when player2 is playing, each mouse click turns the ball to GOLD.
Save all three classes-Board, Player, and GameBall-and compile the game by clicking the “Compile All”
button. Then hit Run to try.
You should be able to click each cell and turn them to gold or steel balls.
10
This concludes Lesson 3. In Lesson 4, we will complete the Tic-Tac-Toe game by adding code to
determine how the game is won and code to jazz up the game a bit. I will also show you how to export a
Greenfoot game and how to post it on either Greenfoot site or your own site.