In this article I will use a version which includes an intuitive interface which has the ability to use help and to check for errors. If we turn on help it will mark all the possible fields for selected a number. Then after checking the errors, the program will mark all the valid fields green and invalid fields red. The rules which will be used for implementation are: An integer will appear only once in the same row, same column, and the same 3*3 region. There is only one solution for the game. Now for implementation of this game, the most vital part is game class which should contain the following functionality:
When certain changes have been performed, observers will be informe since the game class extends observable. The application contains two observers, ButtonPanel and SudokuPanel. When the Game class executes set Changed () followed by notifyObservers(...), the observers will execute their update(...)method. The model consists of an enumeration called UpdateAction which will tell observers what kind of changes have taken place.
First of all we have to generate a solution before we start generating a game. There is a method to generate a solution which is given below which have to be called by a user as generateSudoku(new int[9][9],0). The following steps are taken:
Listing 1: Shows the code for generating a solution
private int[][] generateSolution(int[][] game, int index) {
if (index > 80)
return game;
int x = index % 9;
int y = index / 9;
List<Integer> numbers = new ArrayList<Integer>();
for (int i = 1; i <= 9; i++)
numbers.add(i);
Collections.shuffle(numbers);
while (numbers.size() > 0) {
int number = getNextPossibleNumber(game, x, y, numbers);
if (number == -1)
return null;
game[y][x] = number;
int[][] tmpGame = generateSolution(game, index + 1);
if (tmpGame != null)
return tmpGame;
game[y][x] = 0;
}
return null;
}
For obtaining the next possible number this method getNextPossibleNumber(int[][], int, int, List<Integer>) is used. It takes a number from the list and checks whether it is possible at the given x and y position in the given game. When found , the number is returned. If the list is empty and thus no number is available at this location, -1 is returned.
Listing 2: Shows the code to get next possible number
private int getNextPossibleNumber(int[][] game, int x, int y, List<Integer> numbers) {
while (numbers.size() > 0) {
int number = numbers.remove(0);
if (isPossibleX(game, y, number)
&& isPossibleY(game, x, number)
&& isPossibleBlock(game, x, y, number))
return number;
}
return -1;
}
Now the next step is to generate a game. We can simply generate a game by constantly removing a random field but making sure that the game is still valid. Validity of game means that there is only one solution. We can achieve this by methods given below. First of all, user should call the first method, which in turn uses the second method. I will again describe the steps:
Listing 3: Shows the code to generate a game
private int[][] generateGame(int[][] game) {
List<Integer> positions = new ArrayList<Integer>();
for (int i = 0; i < 81; i++)
positions.add(i);
Collections.shuffle(positions);
return generateGame(game, positions);
}
Listing 4: Shows the code for the method which is used to pass default values
private int[][] generateGame(int[][] game, List<Integer> positions) {
while (positions.size() > 0) {
int position = positions.remove(0);
int x = position % 9;
int y = position / 9;
int temp = game[y][x];
game[y][x] = 0;
if (!isValid(game))
game[y][x] = temp;
}
return game;
}
Now we use new int[] {0} as it is the most common way of passing an integer by address instead of by value.
Listing 5: Show the code for passing an integer
private boolean isValid(int[][] game) {
return isValid(game, 0, new int[] { 0 });
}In a valid game there are numbers 1 to 9 in every row, every column, and every region. There should be only one solution. For achieving this, all open fields should be filled with the first valid value. Even after finding a solution, the search continues by putting the next valid value in an open field. If a second solution is found, then the search will be stopped and the method returns false. There will always be at least one solution. So if there are less than two solutions, the game is valid and the method returns true. Now the steps for achieving this:
Listing 6: Shows the code to check for valid game
private boolean isValid(int[][] game, int index, int[] numberOfSolutions) {
if (index > 80)
return ++numberOfSolutions[0] == 1;
int x = index % 9;
int y = index / 9;
if (game[y][x] == 0) {
List<Integer> numbers = new ArrayList<Integer>();
for (int i = 1; i <= 9; i++)
numbers.add(i);
while (numbers.size() > 0) {
int number = getNextPossibleNumber(game, x, y, numbers);
if (number == -1)
break;
game[y][x] = number;
if (!isValid(game, index + 1, numberOfSolutions)) {
game[y][x] = 0;
return false;
}
game[y][x] = 0;
}
} else if (!isValid(game, index + 1, numberOfSolutions))
return false;
return true;
}
Now we will check the game by checking user input. With checking input we will compare each field in the game against the corresponding field in the solution. The result is stored in a two dimensional Boolean array.
The selected number is also set to 0 which mean no number is selected. Now all observers are informed that the model has changed with the corresponding UpdateAction.
Listing 7: Shows the code to check the game
public void checkGame() {
selectedNumber = 0;
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++)
check[y][x] = game[y][x] == solution[y][x];
}
setChanged();
notifyObservers(UpdateAction.CHECK);
}

Figure 1: Shows the snapshot of a Sudoku game that is developed
Finally, in this article, we have learnt about the game Sudoku and the method of building it using o JAVA . We have followed a step by step approach for making this application.








See the prices for this post in Mr.Bool Credits System below: