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:

  1. TTTEngine - Maintains TicTacToe game state and defines functions for accessing or mutating the game state.
  2. 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.
  3. Computer - Makes a valid move for the computer. This class is provided.
  4. C201 - Reads integers from the keyboard in the terminal window. This class is provided.
  5. 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

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

  1. Create a class TTTEngine.

    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.

     

  2. Add a TTTEngine constructor.
  3. 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:

     

  4. Add the public void printBoard() method.

    x|x|x
    ------
    o|o|x
    ------
    x|o|o

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

     

     

  6. Add the public boolean tie() method.

     

  7. Add the public boolean win() method.

     

  8. Add the public char[] getBoard() accessor method; it returns the board array field.
  9. Add the public void start() method.

TicTacToe

TicTacToe class (supplied) plays the game using your TTTEngine to maintain the game state.

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. 

Turn in

C201 OnCourse Dropbox - Create a HW6 folder in the C201 OnCourse DropBox. Copy the following:

  1. TTTEngine JAVA file.
  2. A Word file with:

    Terminal Window of TicTacToe:

    1. A tie game.
    2. A win for the computer.
    3. 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.