02 Jan 2012, 23:27
Generic-user-small

Richard Yeh (2 posts)

Hi,

Right now I’m attempting to create different Sudoku puzzles that have a variety of sizes, not just 9x9. To do so, I’ve gone through every reference to 9 in the code and changed it to correspond to a static int in the Game class. This variable is set after the user picks a game level.

If I set that variable to 9, the game launches correctly and displays the puzzle correctly. However, if I set the variable to any other number, even if I edit the puzzle strings (easyPuzzle, etc.) accordingly, I receive an ArrayIndexOutOfBoundsException. Does anyone have any tips for what other changes should be made in order to accomodate puzzles sizes besides 9x9?

Edit: I just realized that it shouldn’t be a static. If you have a saved puzzle that’s say 4x4 and you begin a new 9x9 game, you don’t want it to overwrite the puzzle size variable!

06 Jan 2012, 01:40
Burnette_ed_pragsmall

Ed Burnette (1316 posts)

You’re also going to have trouble if you want a number larger than 9 because the code assumes each tile is a single digit. With some extra credit work, you could rewrite it to use letters or hex numbers.

06 Jan 2012, 19:22
Generic-user-small

Richard Yeh (2 posts)

Oh, I’m not looking to represent double-digit numbers. I’m just looking to generate puzzles of different sizes. Trying to create puzzles that are smaller than 9x9 currently doesn’t work, either.

12 Jan 2012, 03:55
Burnette_ed_pragsmall

Ed Burnette (1316 posts)

Don’t forget to change your easy, medium, and difficult puzzles to have only the valid digits, for example 1-4 if you’re making a 4x4 puzzle. I tried it and it seemed to work ok. Here are the diffs:

Index: src/org/example/sudoku/PuzzleView.java
===================================================================
--- src/org/example/sudoku/PuzzleView.java	(revision 110294)
+++ src/org/example/sudoku/PuzzleView.java	(working copy)
@@ -36,8 +36,8 @@
    /* START:onSizeChanged */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
-      width = w / 9f;
-      height = h / 9f;
+      width = w / 4f;
+      height = h / 4f;
       getRect(selX, selY, selRect);
       Log.d(TAG, "onSizeChanged: width " + width + ", height "
             + height);
@@ -65,7 +65,7 @@
       Paint light = new Paint();
       light.setColor(getResources().getColor(R.color.puzzle_light));
       // Draw the minor grid lines
-      for (int i = 0; i < 9; i++) {
+      for (int i = 0; i < 4; i++) {
          canvas.drawLine(0, i * height, getWidth(), i * height,
                light);
          canvas.drawLine(0, i * height + 1, getWidth(), i * height
@@ -76,8 +76,8 @@
                getHeight(), hilite);
       }
       // Draw the major grid lines
-      for (int i = 0; i < 9; i++) {
-         if (i % 3 != 0)
+      for (int i = 0; i < 4; i++) {
+         if (i % 2 != 0)
             continue;
          canvas.drawLine(0, i * height, getWidth(), i * height,
                dark);
@@ -108,8 +108,8 @@
       float x = width / 2;
       // Centering in Y: measure ascent/descent first
       float y = height / 2 - (fm.ascent + fm.descent) / 2;
-      for (int i = 0; i < 9; i++) {
-         for (int j = 0; j < 9; j++) {
+      for (int i = 0; i < 4; i++) {
+         for (int j = 0; j < 4; j++) {
             canvas.drawText(this.game.getTileString(i, j), i
                   * width + x, j * height + y, foreground);
          }
@@ -126,9 +126,9 @@
             getResources().getColor(R.color.puzzle_hint_1),
             getResources().getColor(R.color.puzzle_hint_2), };
       Rect r = new Rect();
-      for (int i = 0; i < 9; i++) {
-         for (int j = 0; j < 9; j++) {
-            int movesleft = 9 - game.getUsedTiles(i, j).length;
+      for (int i = 0; i < 4; i++) {
+         for (int j = 0; j < 4; j++) {
+            int movesleft = 4 - game.getUsedTiles(i, j).length;
             if (movesleft < c.length) {
                getRect(i, j, r);
                hint.setColor(c[movesleft]);
Index: src/org/example/sudoku/Game.java
===================================================================
--- src/org/example/sudoku/Game.java	(revision 110294)
+++ src/org/example/sudoku/Game.java	(working copy)
@@ -21,17 +21,17 @@
    /* END:Game */
    /* START:easyPuzzle */
    private final String easyPuzzle =
-      "360000000004230800000004200" +
-      "070460003820000014500013020" +
-      "001900000007048300000000045";
+      "000000000000000000000000000" +
+      "000000000000000000000000000" +
+      "000000000000000000000000000";
    private final String mediumPuzzle =
-      "650000070000506000014000005" +
-      "007009000002314700000700800" +
-      "500000630000201000030000097";
+      "000000000000000000000000000" +
+      "000000000000000000000000000" +
+      "000000000000000000000000000";
    private final String hardPuzzle =
-      "009000000080605020501078000" +
-      "000000700706040102004000000" +
-      "000720903090301080000000600";
+      "000000000000000000000000000" +
+      "000000000000000000000000000" +
+      "000000000000000000000000000";
    /* END:easyPuzzle */
 
    /* START:Game */
@@ -101,14 +101,14 @@
    /** Return the tile at the given coordinates */
    /* START:getTile */
    private int getTile(int x, int y) {
-      return puzzle[y * 9 + x];
+      return puzzle[y * 4 + x];
    }
    /* END:getTile */
    /** Change the tile at the given coordinates */
    /* START:getTile */
 
    private void setTile(int x, int y, int value) {
-      puzzle[y * 9 + x] = value;
+      puzzle[y * 4 + x] = value;
    }
    /* END:getTile */
 
@@ -143,7 +143,7 @@
    /* START:keypad */
    protected void showKeypadOrError(int x, int y) {
       int tiles[] = getUsedTiles(x, y);
-      if (tiles.length == 9) {
+      if (tiles.length == 4) {
          Toast toast = Toast.makeText(this,
                R.string.no_moves_label, Toast.LENGTH_SHORT);
          toast.setGravity(Gravity.CENTER, 0, 0);
@@ -158,7 +158,7 @@
 
    /** Cache of used tiles */
    /* START:getUsedTiles */
-   private final int used[][][] = new int[9][9][];
+   private final int used[][][] = new int[4][4][];
 
    /* END:getUsedTiles */
    /** Return cached used tiles visible from the given coords */
@@ -171,8 +171,8 @@
    /** Compute the two dimensional array of used tiles */
    /* START:calculateUsedTiles0 */
    private void calculateUsedTiles() {
-      for (int x = 0; x < 9; x++) {
-         for (int y = 0; y < 9; y++) {
+      for (int x = 0; x < 4; x++) {
+         for (int y = 0; y < 4; y++) {
             used[x][y] = calculateUsedTiles(x, y);
             // Log.d(TAG, "used[" + x + "][" + y + "] = "
             // + toPuzzleString(used[x][y]));
@@ -184,9 +184,9 @@
    /** Compute the used tiles visible from this position */
    /* START:calculateUsedTiles2 */
    private int[] calculateUsedTiles(int x, int y) {
-      int c[] = new int[9];
+      int c[] = new int[4];
       // horizontal
-      for (int i = 0; i < 9; i++) { 
+      for (int i = 0; i < 4; i++) { 
          if (i == x)
             continue;
          int t = getTile(i, y);
@@ -194,7 +194,7 @@
             c[t - 1] = t;
       }
       // vertical
-      for (int i = 0; i < 9; i++) { 
+      for (int i = 0; i < 4; i++) { 
          if (i == y)
             continue;
          int t = getTile(x, i);

I’ll leave it to you to make it more general for other sizes.

24 Oct 2013, 06:00
Generic-user-small

John Hour (1 post)

Hello, I want to create the word puzzle game by using this source code but unfortunately it will automatically force close when I play the new game in easy, medium or difficult level.

I create like this: private final String easyPuzzle = “FR000000000GIM0N0000000TI00” + “0I0AN000SEM00000STR000TR0T0” + “00IT0000000V0STO000000000ED”; private final String mediumPuzzle = “IS00000A0000T0R0000RG00000E” + “00A00CH0000NACTO00000C00B00” + “L00000II0000E0L0000E00000ED”; private final String hardPuzzle = “00R0000000V0R0N0G0R0C0PT000” + “000000C00L0P0O0N0OI00I00000” + “000CE0L0S0G0E0E0S0000000S00”;

So, could you tell me what is the wrong with the coding or any other solution to display the words in the puzzle? Thank you for your help if you can solve this problem or recommend the other alternative ways to me… Thank you so much…

  You must be logged in to comment