Briefly, a brute force program would solve a puzzle by placing the digit "1" in the first cell and checking if it is allowed to be there. If there are no violations (checking row, column, and box constraints) then the algorithm advances to the next cell, and places a "1" in that cell. When checking for violations, it is discovered that the "1" is not allowed, so the value is advanced to a "2". If a cell is discovered where none of the 9 digits is allowed, then the algorithm leaves that cell blank and moves back to the previous cell. The value in that cell is then incremented by one. The algorithm is repeated until the allowed value in the 81st cell is discovered. The construction of 81 numbers is parsed to form the 9 x 9 solution matrix.

In this turorial we will show how an optimized Brute Force algorithm can work

You will need the classes posted in Sudoku-I to run the following programs.

But first we will introduce a new class Problems.java
this class contains, for the moment, 2 different Sudoku problems. One intermediate one, and one which, according to internet research is a very nice canditate to be a worst case to be solved with the Brute ALgorithm.

This Problems class will probably grow as we will study other problems. It contains static methods to fill your Sudoku Grid with already registered problems. It also contains a main() method for unit tests.

Now the BruteForce class. It has a console mode and a GUI mode. For now jjust use the console mode.
It uses a Grid fill with data and solves using the BruteForce algorithm a Sudoku problem.

Right now it's main methot uses setProblem1() method to load a standard Sudoku problem
Try it with that set up and then, try with the setProble2() method. You will see that it will take much longer to found the solution.

import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
/*
* Contains methods to solve by brute force a Sudoku Grid
* It implements an ActionListener for whn run in GUI mode
*/
public class BruteForce implements ActionListener {
// the Grid I will have to work on
private Grid grid;
// the ArrayList containing the cells that have to be fixed
private ArrayList<Point> al;
// the number of cells to fix (the ArrayList size)
private int nbTodo;
// the index we are at
private int index;
// the SwingTimer use when do in GUI mode
private Timer timer;
/*
* Constructor
*/
public BruteForce(Grid grid) {
this.grid = grid;
}
/*
* To solve the Grid in console mode will throw an exception if the problem is not feasible
*/
public void solve(boolean reverse) {
// The ArrayList of cells to fill
al = grid.getCellsToFill();
// get it's size once
nbTodo = al.size();
// if nothing to do
if(nbTodo == 0)
return;
// if as to be reversed call the method doing it
if(reverse)
al = reverse(al);
// index we will search
index = 0;
// for the console method we just loop until we are done
while(index < nbTodo) {
// common method
performFill();
}
return;
}
/*
* To solve the Grid with GUI will throw an exception if the problem is not feasible
*/
public void solveWithGui(boolean reverse) {
// The ArrayList of cells to fill
al = grid.getCellsToFill();
// get it's size once
nbTodo = al.size();
// if nothing to do
if(nbTodo == 0)
return;
// if as to be reversed call the method doing it
if(reverse)
al = reverse(al);
// index we will search
index = 0;
// create the Timer
timer = new Timer(100, this);
timer.start();
return;
}
/*
* The common method used by both the Console mode and the GUI mode
* The console mode method solve() just call it in a while
* the GUI mode calls it through a SwingTimer
*/
private void performFill() {
// process next cell if index = -1 it means the solution is not feasible
// it is in invalid Sudoku problem (you havent fill the cells with the GUI)
if(index < 0)
throw new IllegalStateException("Trying to solve an invalid Sudoku problem");
Point p = al.get(index);
// get its possible values
int[] guess = grid.getAllAvailableValues(p.x, p.y);
// if no possible values fall back to previous cell at next call
if(guess.length == 0) {
--index; // use previous one
return;
}
// get its value (will be 0 at first trial)
int value = grid.getValue(p);
// if it is first trial use the first one in the array
if(value == 0) {
grid.setCellValue(p.x, p.y, guess[0]);
++index; // go check next one at next call
return;
}
// test if the value used is the last of the list
if(value == guess[guess.length - 1]) {
// reset this cell value to 0 to start all the process again
grid.setCellValue(p.x, p.y, 0);
// and go back to the previous one
--index;
return;
}
// OK we will have to try the next available one
for(int lastUsed = 0; lastUsed < guess.length; ++lastUsed) {
// when we found the last one we have tried
if(value == guess[lastUsed]) {
// use +1 to check the next one
grid.setCellValue(p.x, p.y, guess[lastUsed+1]);
++index;
break; // no need to continue the loop
}
}
}
/*
* Called by the SwingTimer when in GUI mode to update
* the search and the GUI
*/
@Override
public void actionPerformed(ActionEvent e) {
// if we are done
if(index == nbTodo) {
timer.stop();
return;
}
// not done we have to update one batch
performFill();
}
/*
* Reverse and ArrayList of Point to test the solution in reverse
* this is used to find if the the solution is unique
*/
private ArrayList<Point> reverse(ArrayList<Point> order) {
// create new ArrayList
ArrayList<Point> al = new ArrayList<Point>();
int size = order.size();
// loop in reverse
for(int i = size-1; i >= 0; i--)
al.add(order.get(i));
// return it
return al;
}
/*
* To Unit test the whole thing
*/
public static void main(String[] args) {
// Build a Grid
Grid grid = new Grid(9);
// load it with one of our prepared model
Problems.setProblem1(grid);
grid.printGrid();
BruteForce bf = new BruteForce(grid);
bf.solve(false);
grid.printGrid();
}
}

The next tutorial will introduce 2 GUIs that you will be able to play with.