CMPU 101 - Assignment 6 Due: by 11:59 PM on Friday, April 7th

$Revision: 1.1 $

In this assignment, you will write methods to read TicTacToe boards from the keyboard, and examine them to determine whether or not either player has won.

Your Task

Your task is to read a game board from the keyboard in the following format.  Each row of the game board will be specified as a single token, which should be a string of length 3.  The characters 'X', 'O', and '-' will be used to indicate an X, O, or blank square.

After reading in a game board, your program should evaluate it to determine whether X wins, O wins, or the game is a draw and no one wins.

You should use a two-dimensional array of ints to represent the game board.  In your class, define named constants to define integer values that represent blank squares, squares containing X, and squares containing O.  Recall that a named constant is a private static final variable, e.g.

private static final int PLAYER_X = 1;

would define the named constant PLAYER_X to represent the integer value 1.  Each element in the game board array should contain one of these values (depending on whether the square represented by the element contains an X, an O, or is blank).

Here are several example transcripts showing what the program should look like when it runs.  User input is shown in bold.

Please enter a game board
X wins

Please enter a game board
O wins

Please enter a game board

Please enter a game board
X wins


Make sure you use a two-dimensional array of ints to represent the game board.

Use top-down design to design your program.  The pseudo-code for the overall program should become the main method.  Add other methods as needed.  Each method's pseudo-code should appear in the method as comments.  Make sure you choose meaningful names for variables and methods.

5% of your grade will be based on the quality of your design, and 5% will be based on the quality of your comments and variable/method names.  The easier your program is to read and understand, the more credit you will receive.

All of your methods should be static.  Do not use any static variables (other than named constants).

Testing Your Work

Before running your program, make sure you have compiled it.  Also, make sure your source file is in the Home directory (/home/cs101).

Open a terminal window using the button that looks like this:

Assuming the class in which your program is defined is called TicTacToe, run the following command from the terminal window:

java TicTacToe

If you need to kill your program and return to the command prompt, type Control-C in the terminal window.

Extra Credit

You may implement any of the following features for extra credit.  Please add a comment at the top of your source file indicating which extra credit features you have implemented.

  1. Input validation.  If the user enters an invalid game board, print and error message and try reading the game board again, until the user enters a valid game board.

  2. Repeat.  After evaluating the game board, prompt the user to ask whether or not he/she wants to enter another game board.  Continue reading and evaluating game boards until the user declines to continue.

  3. Implement a game. (Hard)  Turn the program into a game where the user plays TicTacToe against the computer.  You should be able to make use of the methods you have already developed.

Submitting Your Work

When your program compiles and run correctly, submit the Java source file using the CS 101 Submission WebsiteMake sure you submit the Java source file: the filename should end in ".java", not ".java~" or ".class".