small medium large xlarge

14 Mar 2012, 21:18
ILIAS (5 posts)

Hello all I’m new to android programming and I have a problem. I created a sudoku app with your source code. The thing is that I made a puzzle generator for my puzzles to make them infinite.Now here is my problem : To save your pre-defined puzzle we had to use :

getPreferences(MODE_PRIVATE).edit().putString(PREF_PUZZLE, toPuzzleString(puzzle)).commit();

In order to load the saved puzzle to make the Continue option to work we had to use :

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

BUT this works for the predefined puzzle stored in a private final string called “easypuzzle” at the beggining of the game.class . My puzzle is generated and stored in a 1 dimentional array when user clicks New Game.As a result I’m trying to pass my generated puzzle as a reference(this is what I think) cause i tried to pass it like this :


and when I close the game or just go back and try to Continue my game generates a new puzzle for me (or this is what I think it is doing) instead of loading the saved one.

Ps: Because my puzzle is stored in a 2 dimentional array I had to make a new method to convert it my array to a 1 dimentional array in order to make your toPuzzleString and fromPuzzle string work.

My method is:

private int[] to1dimarray(int old[][]) { int newarray[] = new int [81]; int newsize = 0; if (newsize < 80) { ` for (int x = 0; x < 9; x++) { for (int y = 0; y < 9; y++) { newarray[newsize] = game[x][y]; newsize = newsize + 1; } } } return newarray; }`</code> I know it’s not perfect but I’m new to android so forgive me.I have my generated and converted to 1 dimention array in
a variable and I wanna pass it somehow to :

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

But I don’t know how! And I’m not sure I completely understand why you use “easypuzzle” in this getPreferences method which is inside getPuzzle method.Could you pls explain this to me? What am I doing wrong? Thank you all for your time any help would be appreciated….

14 Mar 2012, 22:18
Ed Burnette (1341 posts)

The second parameter of getString() is the default that will be returned in case the preference is not already set.

What you want to do is make a version of toPuzzleString and fromPuzzleString which works with your two dimensional array, converting it to and from an 81 character string consisting of the letters ‘0’ to ‘9’.

17 Mar 2012, 15:57
ILIAS (5 posts)

Hello there.Thank you for really quick answer.I had problems posting in the forum for the past 2 days I don’t know why.Maybe forums were down? Anyway instead of making fromPuzzleString and to PuzzleString work with 2 dimention arrays I used my function mentioned above and now the game is running very well! I have one more question now…Can you explain to me why the used array which is used to calculate the used tiles is a 3 dimention array? Or this statement private final int used[][][] = new int[9][9][]; mean something else? I’m asking cause I wanna use this array ,convert it to a string and use string.length() command to rise up a dialog when user ends his game.And 3 dimention really confuse me…Thank you for your time Mr Burnette..

19 Mar 2012, 19:38
Edgar Quinto (3 posts)

I have a very basic problem with the Sudoku example as well. When typing the line “public class Sudoku extends Activity implements OnclickListener { I get the following error message: “The type Sudoku must implement the inherited abstract method View.OnClickListener.onClick(View)” in the file. Could you please advice?

Edgar - Mexico

12 Apr 2012, 17:41
Ed Burnette (1341 posts)

Ilias, think of it as a two dimensional array, rows by columns. For a given row and column, it stores a list of the numbers that have already been seen from this spot.

12 Apr 2012, 17:46
Ed Burnette (1341 posts)

Edgar, near the top of you should have:

import android.view.View.OnClickListener;

and inside the body of the Sudoku class you should have:

public void onClick(View v) { switch (v.getId()) { case Intent i = new Intent(this, About.class); startActivity(i); break; // More buttons go here (if any) … } }

Compare your version against this one and pay careful attention to the indentation:


It may help if you press Ctrl+Shift+F to reformat your code.

16 Apr 2012, 14:04
ILIAS (5 posts)

Mr Burnette I eventually managed to show the popup dialog when user ends the game with a different way and additionally I managed to make the (given) numbers unchangeable and different color.Thank you for your help

08 May 2012, 20:30
Ed Burnette (1341 posts)

You’re welcome.

21 Apr 2015, 18:58
alex human (1 post)

ILIAS, give me please your code sudoku, when you done finish game. PLEASE