Continuing an Old Game

At any time the player can decide to quit playing our Sudoku game and go do something else. Maybe their boss walked in, or they got a phone call or a notification of an important appointment. Whatever the reason, we want to allow the player to come back later and continue where they left off.

First we need to save the current state of the puzzle somewhere. The preferences API can be used for more than just options; it can store any small stand-alone bits of information that go with your program. In this case, the state of the puzzle can be saved as a string of eighty-one characters, one for each tile.

In the Game class, we'll start by defining a couple of constants—one for the puzzle data key and one for a flag to tell us to continue the previous game rather than start a new one.

Download Sudokuv4/src/org/example/sudoku/

private static final String PREF_PUZZLE = "puzzle" ; protected static final int DIFFICULTY_CONTINUE = -1;

Next we need to save the current puzzle whenever the game is paused. See Section 2.2, It's Alive!, on page 35 for a description of onPause() and the other life-cycle methods.

Download Sudokuv4/src/org/example/sudoku/

@Override protected void onPause() { super .onPauseO; Log.d(TAG, "'onPause"); Music.stop(this);

// Save the current puzzle getPreferences(MODE_PRIVATE).edit0.putStn'ng(PREF_PUZZLE, toPuzzleStnng(puzzle)).commitO;

Now the puzzle is saved, but how do we read the saved data? Remember that when the game is started, the getPuzzle( ) method is called, and the difficulty level is passed in. We'll use that for continuing as well.

Download Sudokuv4/src/org/example/sudoku/

private int[] getPuzzle(int diff) { String puz; switch (diff) { case DIFFICULTY_CONTINUE:

puz = getPreferences(MODE_PRIVATE).getString(PREF_PUZZLE, easyPuzzle);

return fromPuzzleString(puz);

All we need to do is add a check for DIFFICULTY_CONTINUE. If that is set, then instead of starting with a fresh puzzle, we read the one we stuffed into the preferences.

Next, we need to make the Continue button on the main screen (see Figure 3.4, on page 53) actually do something. Here is where we set that up.

Download Sudokuv4/src/org/example/sudoku/

public void onClick(View v) { switch (v.getId()) { case

startGame(Game.DIFFICULTY_CONTINUE); break; // ...

We added a case in Sudoku.onClick() to call startGame() when the Continue button is pressed, passing it DIFFICULTY_CONTINUE. startGame() passes the difficulty to the Game activity, and Game.onCreate( ) calls Intent.getIntExtra() to read the difficulty and passes that to getPuzzle() (you can see the code for that in Section 4.2, Starting the Game, on page 78).

There's one more thing to do: restore from our saved game when our activity goes away and comes back on its own (such as if another activity is started and then the user comes back to the Game activity). This modification to the Game.onCreate() method will take care of that:

Download Sudokuv4/src/org/example/sudoku/

@Override protected void onCreate(Bundle savedInstanceState) { // ...

// If the activity is restarted, do a continue next time getIntent().putExtra(KEY_DIFFICULTY, DIFFICULTY_CONTINUE);

That pretty much covers it for preferences. Next let's look at saving instance state.

0 0

Post a comment