java games lesson3: tic-tac-toe player

10
Shall We Learn | shallwelearn.com 1 Java Games (with Greenfoot) Lesson 3: 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. 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. 1. 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 30x30 pixels (for the cells are 60x60 pixels each), or else the game would not work as planned.

Upload: jessica-chiang

Post on 12-Nov-2014

2.075 views

Category:

Documents


2 download

DESCRIPTION

Third installment of Java Games with Greenfoot. It demonstrates how to create Players to interact with GameBalls. This series is great for high-schoolers to young college students who's interested in games and/or programming

TRANSCRIPT

Page 1: Java Games Lesson3: Tic-Tac-Toe Player

Shall We Learn | shallwelearn.com

1

Java Games (with Greenfoot) Lesson 3: 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.

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.

1. 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 30x30 pixels (for the cells are

60x60 pixels each), or else the game would not work as planned.

Page 2: Java Games Lesson3: Tic-Tac-Toe Player

Shall We Learn | shallwelearn.com

2

2. 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.

};

}

Page 3: Java Games Lesson3: Tic-Tac-Toe Player

Shall We Learn | shallwelearn.com

3

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.

Going back to the GameBall’s code, it is the subclass of the Actor, based on this statement:

public class GameBall extends Actor {

}

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”);

Page 4: Java Games Lesson3: Tic-Tac-Toe Player

Shall We Learn | shallwelearn.com

4

The setImage function is an example of Actor’s function. You can look up on more Actor’s functions by right-clicking on the Actor icon and selecting Open Document from the pop-up menu.

You will see the documentation for the act and getImage functions, among others.

Page 5: Java Games Lesson3: Tic-Tac-Toe Player

Shall We Learn | shallwelearn.com

5

Since we have not imported gold-ball.png, let’s do so now. Right click on GameBall icon, select Set Image… from the pop-up menu to open the Select class image window. Select objects->bold-ball.png, and then hit OK.

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.

public class A {

private void f1( ) {

};

public void f2( ){

f1( ); //OK

};

}

public class B{

public void f3(){

A objA = new A( );

objA.f1( ); //ERROR

objA.f2( ); //OK

}

}

Page 6: Java Games Lesson3: Tic-Tac-Toe Player

Shall We Learn | shallwelearn.com

6

The completed code of GameBall class is now as shown:

You may have noticed the empty act function which I will explain in the next step.

Page 7: Java Games Lesson3: Tic-Tac-Toe Player

Shall We Learn | shallwelearn.com

7

3. Let Player interact with GameBall

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:

Page 8: Java Games Lesson3: Tic-Tac-Toe Player

Shall We Learn | shallwelearn.com

8

This piece of code, when added to act function, will move the Player object to wherever the mouse is.

Next, we will add code to check whether the Player object has collided with a GameBall object when the

mouse clicks. This is the code to do so:

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.

Page 9: Java Games Lesson3: Tic-Tac-Toe Player

Shall We Learn | shallwelearn.com

9

The complete code for the Player class thus is now:

Page 10: Java Games Lesson3: Tic-Tac-Toe Player

Shall We Learn | shallwelearn.com

10

4. Test the Game

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.

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.