CS111 Lab 12 -- Tic-Tac-Toe

The purpose of today's lab is to gain practice implementing GUI layout and behavior. We will be constructing a Java version of Tic-Tac-Toe. The code for this lab can be found in the directory TicTacToe in /usr/users/cs111/download/labs_morning on nike. You can play the game on the web or you can try out the applet which is available in the Test directory.

Contracts for all of the GUI components are available in the JDK 1.0.2. API Documentation. We will refer to this often, so please keep the window open (though not necessarily visible) during lab. Learning how to use the available documentation from Sun is an important skill for all Java programmers.

This lab is broken down into two parts:

  1. GUI layout
  2. GUI behavior
The GUI layout must be completed before the GUI behavior. We will do the GUI layout on paper and code the GUI behavior in lab. The files provided already have the GUI layout code. Finally, it's already enough work just to write up the GUI layout and behavior for this Tic-Tac-Toe game. Therefore, a new GUI component has been provided for this lab. A TicTacToeBoard is a GUI component that you can just add to your applet. The contract for the TicTacToeBoard is given below:

TicTacToeBoard Contract

A TicTacToeBoard is a GUI component which implements a basic TicTacToeBoard. A TicTacToeBoard is divided into nine areas labelled as follows:
123
456
789

public TicTacToeBoard ()
Constructor method; creates default TicTacToeBoard with basic colors and fonts.

public void setBoardColors (Color color, Color bgColor)
Allows one to set the foreground and background colors of the TicTacToeBoard.

public void setXfont (Font f, Color c)
Sets the font for the X player.

public void setOfont (Font f, Color c)
Sets the font for the O player.

public void clearBoard ()
Clears the board.

public int getMouseArea ()
Returns the area of the board which was last clicked on. Returns a zero(0) if an invalid part of the board was clicked on (i.e. boundaries).

public boolean placeX (int n)
Returns true if successfully placed X in the area specified by n. Otherwise, returns false (invalid area or element already at area).

public boolean placeO (int n)
Returns true is successfully placed O in the area specified by n. Otherwise, returns false (invalid area or element already at area).


The TicTacToeBoard is an example of how we can write our own GUI components. Note that you are not responsible for understanding how the TicTacToeBoard is constructed. The code is provided for those who are interested. You can probably understand most of it. Feel free to ask questions.


GUI Layout

The following instance variables are defined in our TicTacToe applet:
Component TypeVariable Name(s)Description
TicTacToeBoard board The game board.
Checkbox xCheckbox, oCheckbox Pick which player the preferences are for.
TextField nameField Input name of player.
Choice colorChoice Select color for player's mark on board.
Choice fontChoice Select font for player's mark on board.
Choice fontStyleChoice Select font style for player's mark on board.
Choice fontSizeChoice Select font size for player's mark on board.
Button prefButton Button to press to set the player's preferences.
Label gameStatus Label which indicates game status: whose turn, who wins, tied games.
Label xNameLabel, oNameLabel Label showing player's name.
TextField xWinField, oWinField Field showing number of games player has won.
List historyList List showing order of games won/tied and by whom.
Note that these are not the only GUI components on the board. These particular components are made into instance variables because we will need to refer to them later. All the other components are items that we can either just place on the applet and forget or there are other ways to reference them (like identifying Buttons by their labels).

Your task: Draw the layout decomposition for this TicTacToe applet using two forms of representation: tree representation and individual layout representation. Use the same standards as used in lecture. Some guidelines to follow for the tree representation are:

Some guidelines to follow for the individual layout representation are:
Some hints:

GUI Behavior

The following 11 methods are all it takes to make the Tic-Tac-Toe game work.

public boolean action (Event evt, Object arg)
This method dispatches most of the GUI events. See handleEvent for responding to mouse clicks.

public void setPlayerPreferences ()
This method sets the player preferences in response to the prefButton being pressed. It also clears the nameField and switches the player selector (so it is less likely one will accidentally overwrite the other player's preferences). Finally, it updates the appropriate name label (xNameLabel or oNameLabel) in the user interface.

public String getFontChoice ()
This method returns the name of the font selected.

public int getFontStyle ()
This method returns the font style selected. Font styles are indicated by the following constants (which are actually integer values): Font.BOLD, Font.ITALIC, and Font.PLAIN. In order to specify a font which is both bold and italic, one must send the value equal to Font.BOLD+Font.ITALIC.

public int getFontSize ()
This method returns the font size selected.

public Color getColorChoice ()
This method returns the color selected.

public void startNewGame ()
This method starts a new Tic-Tac-Toe game. Starting a new games involves the following events:

public void resetScores ()
This method sets the number of games won by each player and the total number of games to zero. It also sets the textfields appropriately and clears the history list. Note that the history list will not be redrawn until the applet redraws it.

public boolean handleEvent (Event evt)
This method responds to mouse clicks and a host of other events. The applet calls handleEvent before calling action for events of type ACTION_EVENT. We use this method to call makeNextMove if the game is active and the mouse was pressed on the game board.

public void makeNextMove (int area)
This method attempts to move for the currently active player in the area specified. If the move is successful, the game is checked to see if it is over yet. If not, the area is added to the list of areas the player has marked. The next player becomes the active player and the game status changes to indicate the next player's turn.

public boolean isGameOver ()
This method checks to see if the game is over or not. If the game is not over, this method returns false. If the game is over (one player wins or there is a tie), the total number of games increases by one, the game status message gets updated to reflect the end of the game and the game goes into the history list. If a particular player won, their winning game count goes up by one and this is reflected in their TextField (xWinField or oWinField). The game is no longer active and the "Set Preferences" button is enabled.


Our task is to implement the methods above. Note the the following instance variables have been defined for our use:
Data TypeVariable Name(s)Description
String xName, oName Stores names of players.
int xWins, oWins Stores number of games each player has won.
int games Stores total number of games played.
IntList xList, oList Stores which areas each player has marked.
boolean isGameActive True if game is active; false otherwise.
boolean isXturn True if it is X's turn; false otherwise.

Some hints: