You may work on this with another person. If you do, you need to EITHER do REAL pair programming, or talk about it together and do your coding individually (but you can ask for help with bugs, etc).
The program should allow a human to play tic-tac-toe against a computer. The human always goes first, (you can hardcode a name and the symbol, (such as "X") that they use). The computer always uses "O" as its symbol. When it is the computer's turn to go, it chooses its square randomly. It might choose a square that is already occupied; if so, it keeps generating position choices until it finds an empty square.
If the user enters incorrect input, they get an error message and are prompted to try again. When someone wins or ties, a message to this effect is printed out and the game is over.
Before each turn, the board is printed out, as well as a message saying whose turn it is. Then a prompt is given for the user to enter a row number, and another prompt is given to enter a column number. See the sample output below for more details. We just printing out an ASCII representation of the board.
You should first write pseudo-code for the main algorithms, and then convert that into java code.
Your code must be documented in such a way that javadoc prints out useful information about author, parameters, and return values. You should also have other standard comments in the code.
We have provided information to guide you through the design and programming of this problem. This information includes CRC cards and names of classes, instance variables, and methods.
Below are shown some CRC cards and some class and method definitions for the design of a TicTacToe program. Your job is to fill in the java code to implement this program. We want you to be sure to use the structure as outlined although you deviate from the design somewhat, if you like.
We want you to turn in both hardcopy and softcopy. Put all of your pseudo-code, output, .JAVA & .CLASS files into a folder named login_hw3 (e.g., ksung_hw3) and ZIP it and email to ksung@sims.berkeley.edu, so we can run the code. Also, in class turn in harcopy of the pseudo-code, output, and java files.
TicTacToe: Playing Marti versus computer
_______
| | | |
| | | |
| | | |
-------
Next Turn: Marti (X)
Enter row number > 0
Enter column number > 0
_______
|X| | |
| | | |
| | | |
-------
Next Turn: computer (O)
_______
|X|O| |
| | | |
| | | |
-------
Next Turn: Marti (X)
Enter row number > 0
Enter column number > 1
Position 0 1 is taken!
_______
|X|O| |
| | | |
| | | |
-------
Enter row number > a
Enter an integer!
Enter row number > 1
Enter column number > 1
_______
|X|O| |
| |X| |
| | | |
-------
Next Turn: computer (O)
_______
|X|O| |
| |X| |
| |O| |
-------
Next Turn: Marti (X)
Enter row number > 2
Enter column number > 2
_______
|X|O| |
| |X| |
| |O|X|
-------
Player Marti wins!
Game
Write pseudo-code for playTicTacToe() and main(). The four String parameters to Game below represent the two player names and their two symbols (such as "X" and "O").
class Game {
Board
Write pseudo-code for printBoard(), takeTurn(Player), computeIfWinner(), and updateBoard(). The other methods may differ from those shown below, if you like. Note that Board contains a two dimensional array of type String. I also added a variable boardSize that allows one to make boards greater than 3x3, but you don't have to do this. I am using method overloading for takeTurn(). I use takeTurn(String) as the call when it is the computer's turn to go; String here refers to the symbol the computer is using. I use takeTurn(Player) when it is the user as player.
class Board {
Player
Player doesn't do much; it is an object that stores information about the player's name and their symbol ("X") or sign.
class Player extends Object {
Last updated 9/26/02. MAH