CSE 11 Spring 2001
Homework #6
Turn in due date: Saturday,
May 26
Interview due date: Thursday, May 31
Updates:
The messages:
-whose turn is next
-who won
go in a TextField not in a window as stated in an earlier version of this assignment.
(5-15-01)
This assignment will be discussed in lecture Thursday May 17
Updates Continued - More
Hints:
Your text are can be just 3 by 3 so it displays only the x's and o's like:
xox
x o
o x
If you can make the display prettier, that's good, but a simple 3 by
3 text area will be acceptable.
To reprogram the close window button see Display 14.20 in the text (2nd edition) and the accompanying text.
You will have a class for each kind of window that pops up. As in Display 14.20, place these extra class definitions inside the definition of your main class. That makes them inner classes, but you need not know about inner class to do this. Just define the classes the same as if they were in separate files, but put them inside the main class.
Note: You can do
this and take an interview without turning in the assignment, as long as it
is before the
turnin deadline (and until the deadline is announced it is definitely before
the deadline).
Programming:
Write a GUI interface for a tic-tac-toe game using the Swing libraray.
The main window will show two 3 by 3 keypads. One for X and one for O to select moves.
The display of the tic-tac-toe
board will be done on a text area. It will display the x's and o's (lower case)
played so far.
When somebody wins, the board display will highlight the winning 3 squares by
changing three x's (or three o's) that determine a win to uppercase X or O.
Give some thought to the layout of the text area to make it look good. Note
that 3 by 3 will not be big enough for a nice display.
Use a larger layout and try for something like:
x|o|x
-----
| |
-----
o| |
Your GUI will also have
a window for the following messages:
1 - Whose turn is next
2 - Who won
If a player chooses an illegal move, then a second window pops up. This second window has an error message and an "OK" button. If the user clicks the OK button, the error window (and only the error window) goes away and the game resumes,that is, the user gets to try another move instead. This error window should not go away when the close window button is clicked, only when the OK button is clicked.
To end the game, the close window button is clicked, but if there has not yet been a winner, then another window pops up telling the players something like:
The game is not over..
Are you sure you want to end the game?
There should be Yes and No buttons. This window will only go away if a yes or no button is pressed. It will not go away if the close window button is pressed. If the user clicks the No button the game resumes. If the user clicks the Yes button, the program ends.
Note that if the close window button is clicked after the game is over, the program just ends without any extra window.
There should also be a "new game" button. When this button is pushed different things happen, depending on the state of the game.
If the game is over, the window reinitializes for a new game. If the game is not over, then another window pops up telling the players something like:
The game is not over..
Are you sure you want to start a new game?
There will be Yes and No buttons. This window will only go away if a yes or no button is pressed. It will not go away if the close window button is pressed. If the user clicks the No button the current game resumes. If the user clicks the Yes button, the window reinitializes for a new game.
Hints:
It is OK to have one or more long switch-statements, or even more likely, long
multibranch if-elses.
Store the textfield contents
as a two dimensional array of chars. Every time there is a change, change the
array and then copy the entire arry to the text field.
Copy the array by coping row by row and inserting a '\n' after each row.
Use action commands on the buttons that correspond to the array locations, for example the button for X's 2,3 array location might be "X2,3". This is just for ease of reading the program. You will still probably use a big switch or if-else to map button clicks to array changes.
A game is only over when the next player has no legal move. Your program need not determine when a player has no hope of winning.