CS 260
Homework 2 - Designing a Tic-Tac-Toe Game with Limited Artificial Intelligence
Due: Fri., Feb. 23 at the beginning of class
You want to write a tic-tac-toe computer game that allows a human player to play against a computer that has limited artificial intelligence. Here are the specifications of the program:
- All I/O in the program will be text-based.
- An electronic coin-flip will determine whether the computer or human goes first. The human always plays 'X'; the computer plays 'O'.
- When it is the computer's turn, the computer uses the following algorithm to choose its move.
- If there is an open square that allows the human to win on the next move, the computer will choose that square.
- Otherwise, if the center square is available, the computer chooses it.
- Otherwise, if there is a corner square available, the computer chooses one at random.
- Otherwise, the computer chooses any other open square at random.
- When it is the human's turn, the board is printed out and the human is prompted for her move. The human inputs her move as a row number and a column number, separated by a single space. Rows and columns are number from '0' to '2'. If the human enters numbers that specify a square outside the grid, or specifies a square that is already occupied, an error message is printed and the human is prompted to enter another move. This process continues until the human enters a legal move.
- When the board is printed, it will be shown using only the following characters: {0, 1, 2, X, O (the letter), |, -, the space character}. Here is an example of what the board should look like when printed:
0 1 2
0 X|O|
-----
1 O|X|
-----
2 X| |O
- The game is over when either the human or computer has won, or there are no more empty squares (a draw).
- After each move, the program will check to see if the game is over. If so, the program prints the board a final time, and then prints the outcome, using exactly this output:
- If the human wins, print: "The inferior human has won, this time."
- If the computer wins, print: "I, the computer, have crushed you."
- If it is a draw: "It is a draw, human."
Complete the homework in five steps.
Step A: Identify the classes that would be used in this program. Use the process we discussed during lecture to identify the classes. Think carefully about the classes that would be useful and necessary. Remember that a class should represent a single, simple concept, but the concept should not be so simple that it could be represented by a primitive.
Step B: Identify the responsibilities of each class. Again, use the technique we discussed in class. List only the major responsibilities of the class. You can omit setters and getters, even if you think your class will need them.
Step C: Identify the relationships between the classes you identified in part A. Use only the inheritance, aggregation and dependency relationships.
Step D: Evaluate your design, and then repeat parts A, B and C until you are satisfied that you have a good design. Remember that one of your design goals is to minimize coupling among classes. (Hint: if your design is tightly coupled, it probably means that one or more of your classes are too complex. Identify the classes that have many responsibilities and break them into simpler classes that have fewer responsibilities.)
Step E: Use Dia to draw a UML diagram that illustrates your design. Don't show any instance variables (attributes) in your diagram, and show only the methods that you identified as important in Part B. Place the following information in a text box in the upper-right corner of your UML diagram:
- your name,
- the course number
- the project title: "Tic-Tac-Destroy" (That is the working title of our game.)
What to turn in: Print out a copy of the UML diagram to turn in.