On This Page
Overview
During this assignment, you will:
- Program the game tic-tac-toe
- Use an array of
char values to represent the board
^ top
Operation
- The program first displays a board as follows and prompts user X for a move.
Enter your move using the number of the square.
Press the Ctrl-C keys together at any time to exit.
1 2 3
4 5 6
7 8 9
Player X make your move:
Players enter their move by entering the position number they wish to mark.
After each move, the program displays the changed board and prompts the next player to move.
X 2 3
4 5 6
7 8 9
Player O make your move:
Play continues alternating between the two players until one of them wins.
X O 3
X O 6
X 8 9
Player X wins!
If no player wins, the game displays that the cat wins.
Cat wins!
^ top
Specifications
- Write a program that allows two users to play tic-tac-toe that operates as shown in the Operation section.
- Create a class named
Board to store the board data, display the board and make moves on the board. This class must contain at least these variables and methods:
private char[] board;
/**
* Returns a display of the current board.
*/
public void displayBoard()
/**
* Sets the move at the move position for a player.
*
* @param move The board position to play.
* @param move The player to which the move is assigned.
*/
public void setMove(int move, char player)
/**
* Tests to see if a board position is open for play.
*
* @return true if the position is open, otherwise false.
*/
public boolean isOpenMove(int move)
/**
* Checks to see if the game has a winner.
*
* @return true if this board has a winner, otherwise false.
*/
public boolean isWinner()
- Create a class named
Player to represent a player in a game. This class should contain an instance variable of type Board, an instance variable of type char that holds the name of the player, and this constructor and method:
/**
* Initialize Player to specified values.
*
* @param newBoard The Board to play upon.
* @param newName The name of the player ('X' or 'O').
*/
public Player(Board newBoard, char newName)
/**
* Prompts for a move and makes the move if valid.
* Otherwise, posts an error message and asks the user
* to enter a correct move.
*/
public void makeMove()
- Whenever the user enters
Ctrl-C together, the program exits.
You pressed Ctrl-C.
Goodbye!
- Your program must validate user input and gracefully handle all error conditions.
Player X make your move: a
Error -- enter digits only!
- Your program must catch and handle moves that are less than 1 or greater than 9 and display the message, "Invalid move!":
Player X make your move: -1
Invalid move!
Player X make your move:
- Your program must prevent a player from making a move that is already played and display the message, "Invalid move!":
X 2 3
4 5 6
7 8 9
Player O make your move: 1
Invalid move!
Player O make your move:
- If all the moves have been made and there is no winner, your program must display the message, "Cat's game!":
X O X
O O X
X X O
Cat's game!
- You may use magic numbers for array indexes for this assignment.
- Do not forget to document your code.
^ top
Test Cases
Testing is an important part of software development. Usually, programmers develop tests for each unit of code they produce. This is know as "unit testing" and allows the programmer to verify that the code they develop works correctly.
To assist you in developing your code, I provide tests for this assignment. These tests, known collectively as "test cases", help to make sure that your assignment meets its requirements. I will use these test cases (and perhaps others) to test your assignment, and you should too. If the test cases do not pass, then you will lose points for the assignment.
Install the tests by copying the following file into the same directory as your .java source code files:
Run the tests as you would any other Java program.
^ top
Extra Credit
The following are worth extra credit points:
- Complete the assignment using pair programming. (1 point)
- Add an option that allows either player to enter a zero ('
0') and have the computer generate the next move automatically. Implement the computer-generated move inside a method of the Player class with the following heading. (1-2 points depending on the "intelligence" of the moves)
/**
* Compute the next move automatically. (Extra credit)
*/
public int autoMove()
Make certain that your README.txt file describes any extra credit attempted.
^ top
Grading Criteria
The instructor will evaluate your assignment using the following criteria. Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive.
Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.
For information on grading policies, including interpretation of scores, see the course information page.
Program Compilation
- 4: Source code compiles with no errors or warnings
- 2: Source code compiles with warnings
- 0: Does not compile
Functionality
- 10: Demonstrates mastery of the assignment
- Has extra features or demonstrates techniques beyond the assignment
- Applies concepts from the lessons appropriately
- Meets all specifications (see above) with particularly elegant solutions
- Runs to completion with no abnormal error conditions
- Generates correct output given correct input
- Behaves in a reasonable way in response to incorrect data
- All test cases pass
- 8: Has all the functionality expected of the assignment
- Demonstrates many techniques from the lesson
- Meets all specifications (see above)
- Implementation seems more complicated than necessary.
- May have one minor error
- All test cases pass
- 6: Has most of the functionality expected of the assignment
- Demonstrates some techniques from the lesson
- Meets all but one of the specifications (see above)
- Implementation seems excessively complicated.
- May have 2-3 minor errors
- All but one test case passes
- 4: Has some of the functionality expected of the assignment
- Demonstrates some techniques from the lesson
- Meets at least 1/2 of the specifications (see above)
- Implementation seems excessively complicated.
- May have more than 3 minor errors
- At least 1/2 of all test cases pass
- 2: Serious functional problems but shows some effort and understanding
- Meets less than 1/2 of the of the specifications (see above)
- Has a major error or many minor errors
- Implementation seems very convoluted
- Demonstrates few techniques from the lesson
- Less than 1/2 of all test cases pass
- 0: Does not execute
Program Style
- 4: Code is well-documented
- 3: Code has minor documentation errors
- Has 1 documentation error
- 2: Code has some documentation errors
- Has 2-3 documentation errors
- 1: Code has many documentation errors
- Has more than 3 documentation errors
- 0: No apparent attempt to document code
README.txt File
- 2: README.txt file submitted with specified information included
- 1: README.txt submitted but some information was not included
- 0: No README.txt submitted
Maximum Score: 20, plus extra credit
^ top
What to Turn In
Submit your assignment following the instructions for homework. Include the following items for grading:
README.txt file
Board.java
Player.java
GameApp.java
- Any other source code needed to make your program function
You must submit all the files needed to make your assignment function properly. Do not assume that the instructors has any files unless explicitly stated by the instructor. Your assignment must work as submitted.
^ top
Home
| WebCT
| Announcements
| Schedule
| Expectations
| Course info
Help
| FAQ's
| HowTo's
| Links
Last Updated: November 02 2005 @14:31:15
|