In the last lesson (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.
The game should work this 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.



Create a Player Class
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 30×30 pixels (for the cells are 60×60 pixels each), or else the game would not work as planned.
Add States to GameBall Class
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:
enum BallState { UNCLICKED, GOLD, STEEL };
To declare variables of this type:
BallState state = BallState.UNCLICKED;
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.
This is the code for GameBall so far:
public class GameBall extends Actor {
enum BallState { UNCLICKED, GOLD, STEEL };
BallState state = BallState.UNCLICKED;
GameBall() {
State = BallState.UNCLICKED;
};
public void setGold(){
state = BallState.GOLD;
};
public void setSteel(){
state = BallState.STEEL;
};
Public void reset(){
State = BallState.UNCLICKED.
};
}
Next, let’s add code to change a GameBall’s look according to its state. Since a GameBall extends from the Actor class, it inherits a set of functions from the Actor class. Inheritance is a very important concept in Object-Oriented Programming. Simply put, it’s a way to build new classes based on existing classes. When a class extends or inherits from another class, it’s said to be the subclass of that class, which is called the superclass. A subclass has all member variables and functions that its superclass has, and more. A subclass can has additional variables and functions to those of its superclass, and they often do.
Post a Comment
You must be logged in to post a comment.