Homework 6 Fixed Sized Collections
|
Modified:
|
SI - Next session will give additional discussion of Homework 6 parts c and
e.
Overview - The purpose of the homework is to practice grouping objects
using fixed sized arrays by implementing a simple TicTacToe game in which a
human plays against the computer.
The HW6 project has five classes:
- TTTEngine - Maintains TicTacToe game state and defines functions for
accessing or mutating the game state.
- TicTacToe - The text-based user interface. Takes a valid move from human and
places an 'o' on the TicTacToe game board. This class is provided.
- Computer - Makes a valid move for the computer. This class is provided.
- C201 - Reads integers from the keyboard in the terminal window. This class is provided.
- GUITicTacToe - Provides the graphical user interface. Takes a valid move from human and
places an 'o' on the TicTacToe game board. This class is provided.

Getting started
- Download and Run the HW6 project file. Unzip to
folder HW6 in your BlueJ directory (normally C:\Users\username\bluej\HW6).
- The class diagram should appear as at right.
- Open the HW6 project in BlueJ.
Assignment: TTTEngine
Your TTTEngine class manages the game: maintaining the game board
state, checking for wins and ties, etc. An array of 9 characters represent
the positions of the board initialized as:
0|1|2
------
3|4|5
------
6|7|8
After the human enters moves to 0 and 1 and the computer to 2 and 4 the
board appears as:
o|o|x
------
3|x|5
------
6|7|8
- Create a class TTTEngine.
- Define a field named board as a fixed array of type
char.
The primitive char type is a single character denoted between single
quotes, 'x'. Examples of variables of type char is defined by:
char ch;
char [ ] board;
Type char has many of the same operations as other primitive data
types such as int:
- ch = 'a'
- ch > 'b'
- board[ 3 ] = 'x'
- board[ 3 ] == 'o'

WARNING: Only compile the TTTEngine. Surest method is to click the
Compile button in the editor.
- Add a TTTEngine constructor.
- Assign a fixed array with 9 char variables to the field board.
- Initialize board[0]='0', board[1]='1', etc. as in the object
diagram at right.
- Add a public void fillBoard() method for testing; it places 'x'
and 'o' appropriately on all positions of the board. Fill the top row (i.e. board[0],
board[1], and board[2]) with 'x's for testing that 'x' wins.
Test by:
- Create a TTTEngine object
- Call fillBoard()
- Inspect the object.
- Inspect the object reference. The board array should have 'x' and 'o' in
all board index positions.
- Add the public void printBoard() method.
- The board should print similar to below.
- Test by:
- Create a TTTEngine object
- Call fillBoard()
- Call printBoard()
x|x|x
------
o|o|x
------
x|o|o
- Add the mutator public boolean validMove(int position, char player) method
which places a player character ('x' or 'o') on the board when passed a valid
position for player.
- Check that position is on the board. If position
not within 0-8 range, return false.
- If board position is an 'x' or 'o' return false.
- Place the
player character at board position and return true.
- Add the public boolean tie() method.
- Return true when all board positions are occupied by any combination of
'x's or 'o's, otherwise return false. The alternative, return false when any
position is not a 'x' or 'o' may be simpler.
- Test by:
- Create a TTTEngine object
- Call tie() - The result should be false.
- Call fillBoard() which fills the board with 'x's and 'o's.
- Call tie() - The result should be true.
- Add the public boolean win() method.
- Return true when any row, column or diagonal board positions are
occupied by a set of 'x' or 'o', otherwise return false.
- Test by:
- Create a TTTEngine object
- Call win() - Before any moves the result should be false.
- Call fillBoard()
- Call win() - The result should be true because the top row is
"x x x".
- Test each row, column and diagonal for winning positions by modifying
fillboard() for each test case.
- Add the public char[] getBoard() accessor method; it returns the
board array field.
- Return the board field.
- Test by:
- Create a TTTEngine object
- Call getBoard()
- Inspect the object reference returned
- Call fillBoard()
- Call getBoard()
- Inspect the object reference returned
- Add the public void start() method.
- Fill the board field positions 0 with '0', 1 with '1', etc.
- Test by:
- Create a TTTEngine object
- Call start() method.
- Call printBoard() method.
TicTacToe
TicTacToe class (supplied) plays the game using your TTTEngine to maintain
the game state.
- The human always goes first followed by the computer.
- Neither the human or computer can cheat.
- Play continues until either a win or tie occurs.
- To stop prematurely, click the
and Reset Machine.
- To test:
- Click the TicTacToe class, then Compile.
- Create a TicTacToe object.
- Call game() method.
- Human plays o and moves first.
- Computer plays x.
GUITicTacToe
GUITicTacToe class (supplied) plays the game using your TTTEngine to maintain
the game state, presenting a Graphical User Interface rather than a text
interface. Well-behaved classes with well-defined method interfaces such as
your TTTEngine, allow interfacing to other classes without changes. As an example, the
text-based interface class TicTacToe or a GUI interface can use your TTTEngine.
- The human always goes first followed by the computer.
- Neither the human or computer can cheat.
- Play continues until either a win or tie occurs.
- To stop prematurely, close the window or click Reset.
- To test:
- Click the GUITicTacToe class, then Compile.
- Create a GUITicTacToe object, a new window should open with the
user interface displayed. Check the toolbar for the TicTacToe
window.
- Play the game.
Turn in
C201 OnCourse Dropbox - Create a HW6 folder in
the C201 OnCourse DropBox. Copy the following:
- TTTEngine JAVA file.
- A Word file with:
Terminal Window of TicTacToe:
- A tie game.
- A win for the computer.
- A win for you (if you can).
Screen shot of GUITicTacToe after several moves. Use Alt Prnt
Scrn to capture the window, then paste into Word.