Java Programming Challenge: Saving Tic-Tac-Toe - dummies

Java Programming Challenge: Saving Tic-Tac-Toe

By Doug Lowe

This programming challenge is relatively simple: Starting with the Swing version of the Tic-Tac-Toe game you created in response to Java Programming Challenge: Adding a GUI to the Tic-Tac-Toe Program add a feature that enables the user to save the status of the game to a disk file, restore the saved game later, and resume where play let off.

Granted, the game of Tic-Tac-Toe isn’t complicated enough that a user would frequently want to save the game status and resume the game later. But the challenge of saving the state of a Tic-Tac-Toe game is similar to saving the status of more complicated games (as well as other types of programs).

Your solution should add Save and Resume buttons to the user interface, as shown in the figure.

The Tic-Tac-Toe game with Save and Resume buttons.

The Tic-Tac-Toe game with Save and Resume buttons.

When the user clicks Save, a Save dialog box appears, allowing the user to browse to the location where the file should be saved. If the user clicks Resume, an Open dialog box appears so the user can select the file that contains the saved game. Once the user opens the file, your program should populate the Tic-Tac-Toe board according to the saved game status and then immediately resumes play.

You’re free to choose any method you wish to save the status of the game. I suggest that you either (a) create a simple text-file format that you can parse when restoring the saved game, or (b) create your own schema and save the file in XML format.

You can find the solution to this challenge at on the Downloads tab of the Java All-in-One For Dummies, 4th Edition product page.

Good luck!