CSE 8B:
Introduction to Computer Science: Java
(Winter, 2006)
Home | Syllabus |
Schedule | Exams |
Programming Assignments
Programming Assignment 6
Due Monday, February 27 at 11:59 PM.
Last modified:
Previous modifications:
Overview
You'll be writing a GUI application that allows two people to play tic-tac-toe.
The main class will be TicTacToe. You'll run it with:
java TicTacToe
A sample can be found in ~/../public/P6/tictactoe.jar. Run it with
java -jar ~/../public/P6/tictactoe.jar
Requirements
- The program will need to have a status that displays:
- Whose turn it is (unless the game is over)
- That the game is a draw (sometimes called a cat's game) if no moves can be made and nobody won.
- Who won the game (if someone got three in a row). (In this case, no further squares should be able to be clicked).
- There will need to be an options menu with two items:
- New Window will create another tic-tac-toe window.
- New Game will erase the current game and allow a new game.
- After a person has clicked on a square, that square will display an X or an O and can no longer be clicked.
- Closing the last Tic-tac-toe window will quit the application.
Your tic-tac-toe program need not look identical to the sample, but should be reasonably
attractive (for example, the nine buttons should be laid out in a nice grid).
Provided files
All files are in ~/../public/P6.
There is a single sample Tic-Tac-Toe in a jar file.
Unit Testing
You haven't yet learned how to unit test Swing applications.
However, I highly suggest you still incrementally develop your application. For example, you might first
get it to display some static status text, then add the buttons, then add the menu. Once you've got that going, you
can start adding behavior.
Hints
- You won't be able to ssh into ieng6 and work remotely since you won't be able to run a java gui app remotely.
One option is to install the Java Development Kit (JDK) on your local machine.
You want Java 2 Platform Standard Edition 5.0 (Update 6).
You can download "JDK 5.0 Update 6" for most platforms from here.
Note that if you're running on Mac OS X, even after you download that version of the JDK, the command-line java and javac
will still use the older Java 1.4. See this link
for a shell script that'll allow you to change the version of the JDK that's used.
Keep in mind that we'll
be grading it on ieng6. Therefore, do final testing on that machine.
- Represent the tic-tac-toe board as a grid layout of 9 buttons. Store the buttons in an array so that you can
- Enable/disable particular buttons (disable after clicked on or the game is won; enable to start a new game).
- Change the text of a button (to set it to "X", "O"; or "" to start a new game)
- Get the text of a button (to look for 3 in a row).
- Use JLabel (or a non-editable JTextField) for the status.
- Use Google to investigate the methods available to you for JLabel (or JTextField), JButton, JMenuItem, etc.
- This is not a particularly hard program as GUI programs go, but since you're new to it, plan
on spending some time working on it. Add appearance bit-by-bit; once you've got the right appearance, then add
behavior bit-by-bit. Don't try to write the whole thing at once; grow it incrementally.
Extra credit
The sample tic-tac-toe program has the following features you could add to yours:
- A keyboard shortcut for the New Window command.
- New windows are offset 10 pixels horizontally and vertically (so that the window doesn't completely
obscure the old ones).
You could add even more:
- Play human vs. computer
- Undo the last move
- ...
Turnin
Create a directory on ieng6 with the following file:
To turn in your assignment, cd to that directory and run the command
bundleP6
and carefully follow the dialog.
If it is before the due deadline and all necessary assignment files are present,
this will turn in your solution files.
You can run bundleP6 more than once if you want; only the last one will count.
Grading
We'll be running your tic-tac-toe program and trying it out on ieng6, so make sure you've tested your app there.
The assignment is worth 20 points:
- 8 points: Basic functionality of alternately entering X and O.
- 2 points: status works correctly (including recognizing winning game and disabling buttons).
- 2 points: New game works correctly.
- 2 points: New window works.
- 2 points: Closing last window quits the application (closing non-last windows don't quit the application).
- 4 points: Style points for your class. Good naming, not repeating yourself, correct indentation,
not exceeding 80 characters per line, good organizing of methods into classes, etc.
2 points are available for extra credit functionality.
As always, an additional 5% (1 point) is available if you turn the assignment in at least 24 hours early.