Posted By

on 04/03/10


Tagged


Versions (?)

Sudoku (Game.java)


 / Published in: Java
 

from "Hello Android 2nd Edition" (ISBN: 1-934356-49-2)

  1. package org.example.sudoku;
  2.  
  3. import android.app.Activity;
  4. import android.app.Dialog;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.Gravity;
  8. import android.widget.Toast;
  9.  
  10. public class Game extends Activity {
  11. private static final String TAG = "Sudoku";
  12.  
  13. public static final String KEY_DIFFICULTY = "org.example.sudoku.difficulty";
  14. public static final int DIFFICULTY_EASY = 0;
  15. public static final int DIFFICULTY_MEDIUM = 1;
  16. public static final int DIFFICULTY_HARD = 2;
  17.  
  18. private int puzzle[] = new int[9 * 9];
  19.  
  20. private PuzzleView puzzleView;
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. Log.d(TAG, "onCreate");
  26.  
  27. int diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_EASY);
  28. puzzle = getPuzzle(diff);
  29. calculateUsedTiles();
  30.  
  31. puzzleView = new PuzzleView(this);
  32. setContentView(puzzleView);
  33. puzzleView.requestFocus();
  34. }
  35.  
  36. /** Open the keypad if there are any valid moves */
  37. protected void showKeypadOrError(int x, int y) {
  38. int tiles[] = getUsedTiles(x, y);
  39. if(tiles.length == 9) {
  40. Toast toast = Toast.makeText(this, R.string.no_moves_label, Toast.LENGTH_SHORT);
  41. toast.setGravity(Gravity.CENTER, 0, 0);
  42. toast.show();
  43. } else {
  44. Log.d(TAG, "showKeypad: used=" + toPuzzleString(tiles));
  45. Dialog v = new Keypad(this, tiles, puzzleView);
  46. v.show();
  47. }
  48. }
  49.  
  50. /** Change the tile only if it's a vaoid move */
  51. protected boolean setTileIfValid(int x, int y, int value) {
  52. int tiles[] = getUsedTiles(x, y);
  53. if(value != 0) {
  54. for(int tile : tiles) {
  55. if(tile == value) return false;
  56. }
  57. }
  58. setTile(x, y, value);
  59. calculateUsedTiles();
  60. return true;
  61. }
  62.  
  63. /** Cache of used tiles */
  64. private final int used[][][] = new int[9][9][];
  65.  
  66. /** Return cached used tiles visible from the given coords */
  67. protected int[] getUsedTiles(int x, int y) {
  68. return used[x][y];
  69. }
  70.  
  71. /** Compute the two dimensional array of used tiles */
  72. private void calculateUsedTiles() {
  73. for(int x = 0; x < 9; x++) {
  74. for(int y = 0; y < 9; y++) {
  75. used[x][y] = calculateUsedTiles(x, y);
  76. // Log.d(TAG, "used[" + x + "][" + y + "] = "
  77. // + toPuzzleString(used[x][y]));
  78. }
  79. }
  80. }
  81.  
  82. /** Compute the used tiles visible from this position */
  83. private int[] calculateUsedTiles(int x, int y) {
  84. int c[] = new int[9];
  85. // horizontal
  86. for(int i = 0; i < 9; i++) {
  87. if(i == y) continue;
  88. int t = getTile(x, i);
  89. if(t != 0) c[t - 1] = t;
  90. }
  91. // vertical
  92. for(int i = 0; i < 9; i++) {
  93. if(i == x) continue;
  94. int t = getTile(i, y);
  95. if(t != 0) c[t - 1] = t;
  96. }
  97. // same cell block
  98. int startx = (x / 3) * 3;
  99. int starty = (y / 3) * 3;
  100. for(int i = startx; i < startx + 3; i++) {
  101. for(int j = starty; j < starty + 3; j++) {
  102. if(i == x && j == y) continue;
  103. int t = getTile(i, j);
  104. if(t != 0) c[t - 1] = t;
  105. }
  106. }
  107. // compress
  108. int nused = 0;
  109. for(int t : c) {
  110. if(t != 0) nused++;
  111. }
  112. int c1[] = new int[nused];
  113. nused = 0;
  114. for(int t : c) {
  115. if(t != 0) c1[nused++] = t;
  116. }
  117. return c1;
  118. }
  119.  
  120. private final String easyPuzzle =
  121. "360000000004230800000004200" +
  122. "070460003820000014500013020" +
  123. "001900000007048300000000045";
  124. private final String mediumPuzzle =
  125. "650000070000506000014000005" +
  126. "007009000002314700000700800" +
  127. "500000630000201000030000097";
  128. private final String hardPuzzle =
  129. "009000000080605020501078000" +
  130. "000000700706040102004000000" +
  131. "000720903090301080000000600";
  132.  
  133. /** Given a difficulty level, come up with a new puzzle */
  134. private int[] getPuzzle(int diff) {
  135. String puz;
  136. // TODO: Continue last game
  137. switch(diff) {
  138. case DIFFICULTY_HARD:
  139. puz = hardPuzzle;
  140. break;
  141. case DIFFICULTY_MEDIUM:
  142. puz = mediumPuzzle;
  143. break;
  144. case DIFFICULTY_EASY:
  145. default:
  146. puz = easyPuzzle;
  147. break;
  148. }
  149. return fromPuzzleString(puz);
  150. }
  151.  
  152. /** Convert an array into a puzzle string */
  153. static private String toPuzzleString(int[] puz) {
  154. StringBuilder buf = new StringBuilder();
  155. for(int element : puz) {
  156. buf.append(element);
  157. }
  158. return buf.toString();
  159. }
  160.  
  161. /** Convert a puzzle string into an array */
  162. static protected int[] fromPuzzleString(String string) {
  163. int[] puz = new int[string.length()];
  164. for(int i = 0; i < puz.length; i++) {
  165. puz[i] = string.charAt(i) - '0';
  166. }
  167. return puz;
  168. }
  169.  
  170. /** Return the tile at the given coordinates */
  171. private int getTile(int x, int y) {
  172. return puzzle[y * 9 + x];
  173. }
  174.  
  175. /** Change the tile at the given coordinates */
  176. private void setTile(int x, int y, int value) {
  177. puzzle[y * 9 + x] = value;
  178. }
  179.  
  180. /** Return a string for the tile at the given coordinates */
  181. protected String getTileString(int x, int y) {
  182. int v = getTile(x, y);
  183. if(v == 0) return "";
  184. else return String.valueOf(v);
  185. }
  186.  
  187. @Override
  188. protected void onResume() {
  189. super.onResume();
  190. Music.play(this, R.raw.game);
  191. }
  192.  
  193. @Override
  194. protected void onPause() {
  195. super.onPause();
  196. Music.stop(this);
  197. }
  198. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: rubioberry on June 20, 2012

How do I resolve the error on line 190 R.raw.game ( raw cannot be resolved or is not a field )?

You need to login to post a comment.