CS361
Project 4
Finishing the Carpet Solitaire game
due: Friday, March 21 or whenever you leave for Spring Break (whichever comes first)
Assignment
Add menus and menu items to complete the Swing window that you created in Project 3:
- Add a File menu at the top of the window with six menu items:
- New game -- when selected, this menu item creates another random order of the cards with the empty slots to the left of each row and resets the number of reshuffles to 2.
- Replay -- when selected, this menu item restores the cards to their original random order and position when the game began and resets the number of reshuffles to 2. It is just like the New game menu item except that it doesn't create a new random ordering of the cards and instead reuses the initial random ordering of the cards.
- Shuffle -- when selected, this menu item causes all cards other than the cards that are in their winning position to be shuffled together and then laid out filling the remaining slots in each row. The empty slots are always moved to the end of the winning positions in each row. The user is allowed only two such shuffles.
- Save as... -- when selected, this menu item saves the current state of the game to a file.
- Open... -- when selected, this menu loads a game that was previously saved to a file.
- Quit -- when selected, the window closes and the program quits.
- Add an Edit menu at the top of the window with three menu items:
- Undo -- when selected, this menu item restores the game to its state prior to the last card movement.
- Redo -- when selected, this menu items restores the game to its state prior to the last undoing.
- Statistics... -- when selected, displays a dialog box listing the number of games played, the number of games won, and the percentage of games won. It also includes a "Clear" button that sets the number of games played and won back to 0.
Details
- You can find message dialogs in the Swing JOptionPane class.
- You can read about JMenuBars, JMenus, and JMenuItems in the Java Swing online documentation.
- I want you to add an appropriate keyboard shortcut for each menu item. The shortcut for Undo should be either Command-Z or Control-Z and the shortcut for Redo should be either Command-Y or Control-Y. The shortcut for Open should be either Command-O or Control-O and the shortcut for Save as should be either Command-Shift-S or Control-Shift-S.
- The user should be allowed to undo moves back all the way to the original state of the game.
- Furthermore, after undoing several moves, the user should be able to redo them.
- If, after undoing several moves, the user then makes a new move, it is impossible to redo the moves that were undone.
- All undo and redo states should be cleared when a new game is started or a game is reset.
- A shuffle of the non-winning cards (via the Shuffle menu item) is undoable and redoable. However, if you redo a shuffle, it need not generate the same random positioning of the cards.
- When Save as is chosen, a dialog box (see JFileChooser in the Java Swing documentation) appears for the user to choose a directory and a file name for saving the game. If the user enters a file name and presses the OK button in the dialog, then the application should create a new text file by that name and write to that file all the information concerning the current state of the game so that the game can later be reloaded. I don't care where (that is, in which directory) the file is saved. If the user presses the Cancel button in the dialog, then the dialog closes and no saving occurs.
- You do not save the undo and redo states when you save a game. That is, when a game has been reloaded from a file, all undo and redo states are cleared.
- When Open is chosen, a dialog box appears for the user to choose a directory and a file in that directory to open. If the user chooses a file that had been created by your application, your application should replace the current game with the saved game. If the user chooses a file that is not readable or parsable by your application, then a error message should appear in a dialog.
- Saving a game to a file is not an undoable action and so undo/redo are not affected by saving the game to a file.
- You can't undo the action of opening a game from a file.
- The current game should be stored in XML format. The advantage of using an XML file is that (a) it is a humanly readable text file and (b) there are many free parsers for converting the XML back into the data you need.
- To write to a file, I recommend using a PrintWriter object, such as in the following, where fileName is the name of the file into which you are saving the game:
PrintWriter writer = new PrintWriter(new FileWriter(fileName));
- To get the data from an XML text file to pass to the XML parser, I suggest that you use a FileInputStream, such as in the following, where fileName is the name of the file you are loading, and docBuilder is an object of class javax.xml.parsers.DocumentBuilder:
docBuilder.parse(new FileInputStream(fileName));
- I am happy to help you with XML parsing, but check out the classes in the javax.xml.parsers package first and read any tutorial materials you can find about those classes.
Extra Credit
- For one point extra credit, add a Help Menu with two menu items (with keyboard shortcuts):
- Rules... -- displays a dialog with the rules of Carpet Solitaire
- About... -- displays info such as the version, author and contact info.
- For one point extra credit, add the following feature:
When it is impossible to undo (because you are back to
the original state of the game) or to redo (because a new move was made after the last undo), then the
Undo or Redo menu items should be disabled.
- For one point extra credit, add the following feature: In addition to the Save as menu item, add a Save menu item (with a keyboard shortcut) that behaves in the conventional fashion.
What to hand in
Hand in a hard copy
of all the source code files you created or modified and also zip up all source
code files and send the zip file to me
as an email attachment. See the course
web page on handing in assignments for further details.
For
fun: An Ambiguous Recommendation
"I can assure you that nobody would be better than Jack."