Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Java Games Lesson3: Tic-Tac-Toe Player

Java Games Lesson3: Tic-Tac-Toe Player

Ratings: (0)|Views: 2,070|Likes:
Published by Jessica Chiang
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
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

More info:

Published by: Jessica Chiang on Sep 12, 2009
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





Shall We Learn | shallwelearn.com1
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 tothe Board object. In this lesson, we will add a Player class such that a Player object interacts withGameBall 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 theboard are blank. When the first player clicks at a blank cell, a Gold ball will be placed in that cell; whenthe second player clicks at a blank cell, a Steel ball will be placed in that cell.1.
Create a Player ClassTo interact with the GameBall objects, we need a class called Player. Right click on the
button andselect
New subclass…
from the pop-up menu. At
New class name
, enter “Player”, then import the firstimage, 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 anotherplayer. To do so, right click on the Player icon and select Set Image… from the drop-down list. Importanother image, ant-with-food.png, by selecting animals->ant-with-food.png.
: You can select other images but make sure they are not larger than 30x30 pixels (for the cells are60x60 pixels each), or else the game would not work as planned.
Shall We Learn | shallwelearn.com22.
Add States to GameBall ClassBefore adding interaction between the Player class and the GameBall class, we need to add states to theGameBall 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-toaccess and control the member variable.A Java class can have member variables and member functions. Member variables are like states orsettings 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 membervariables. 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 tosetGold(), setSteel(), and reset() functions.To implement the three states, we use Java’s enumerated type. An enumerated type has a finitenumber 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 goeson, 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.};}
Shall We Learn | shallwelearn.com3
Next, let’s add code to change a GameBall’s look according to its state. Since a GameBall
fromthe Actor class, it
a set of functions from the Actor class.
is a very important conceptin 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 iscalled the
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 aresubclasses 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 GameBallinherits from Actor, it can call setImage function like this:
Or like this:

Activity (18)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Babneet Singh liked this
Rohit Bhateja liked this
ggardu liked this
ggardu liked this
ggardu liked this
ujsh liked this
Serratia liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->