Defining the Game Class

Here's the outline of the Game activity:

Download Sudokuv2/src/org/example/sudoku/Game.java

package org.example.sudoku;

import android.app.Activity; import android.app.Dialog; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.widget.Toast;

public class Game extends Activity {

private static final String TAG = "Sudoku";

public static final String KEY_DIFFICULTY =

"org.example.sudoku.difficulty"; public static final int DIFFICULTY_EASY = 0; public static final int DIFFICULTY_MEDIUM = 1; public static final int DIFFICULTYJHARD = 2;

private PuzzleView puzzleView;

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "'onCreate");

int diff = getIntent().getIntExtra(KEY_DIFFICULTY,

DIFFICULTY_EASY); puzzle = getPuzzle(diff); calculateUsedTiles();

puzzleView = new PuzzleView(this);

setContentView(puzzleView);

puzzleView.requestFocus();

The onCreate() method fetches the difficulty number from the intent and selects a puzzle to play. Then it creates an instance of the PuzzleView class, setting the PuzzleView as the new contents of the view. Since this is a fully customized view, it was easier to do this in code than in XML.

The calculateUsedTiles() method, which is defined in Section 4.4, The Rest of the Story, on page 93, uses the rules of Sudoku to figure out, for each tile in the nine-by-nine grid, which numbers are not valid for the tile because they appear elsewhere in the horizontal or vertical direction or in the three-by-three subgrid.

This is an activity, so we need to register it in AndroidManifest.xml:

Download Sudokuv2/AndroidManifest.xml

<activity android:name= ".Came"

android:label="@string/game_title"/>

We also need to add a few more string resources to res/values/strings.xml:

Download Sudokuv2/res/values/strings.xml

<string name="game_title">Game</string> <string name="no_moves_label">No moves</string> <string name="keypad_title">Keypad</string>

0 0

Post a comment