Page 1 of 1

Sudoku IV - Brute Force GUI A GUI to compare Brute Force Rate Topic: -----

#1 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Posted 20 October 2010 - 08:50 PM

In tutorial Sudoku II - Brute Force we saw how to solve a Sudoku problem using the Brute Force

The class posted here BruteForceGui.java is just a main() method that invokes classes prosted in previous tutorials.

The main() method creates 2 Grid objects with the same Sudoku problem coming from the Problems.class.

It creates 2 GridGuiOne object and put them together in a JFrame which has a GridLayout(1,2).

The 2 BruteForce class objects are created. Then their solveWithGui() method is invoked to solve by ButeForce the same problem. One using scanning the Sudoku grid from cell[0,0] to cell[8,8] the other by solving the problem in reversed order from cell[8,8] to cell[0,0].

The main method calls the Problems class setProblem3() method to fill the 2 grids. This is a very simple to solve Sudolu problem with most of the cells already filled.

You can also try using setProblem1() method, that will be longer, or if you are really patient, use setProblem2() method which load one of the "worst case" Sudoku problem to be fixed by the Brute Force method... that might take a while.

You can also edit the BruteForce.java file to change at which speed the GUI is updated. The line

timer = new Timer(100, this);

says to refresh the GUI 10 times a second. You an try to lower down to 20 times a second using

timer = new Timer(50, this);


BruteForceGui.java

import javax.swing.*;
import java.awt.*;
/*
 * A Demo of the Brute Force solving with a GUI
 * Two GidGuiOne will be created with the same problem
 * One Brute force will be used to fix the problem in order the other in reverse 
 * We will see how the cells are updated
 * 
 */
public class BruteForceGui {
	
	/*
	 * To test the Grid
	 */
	public static void main(String[] atgs) {
		// create a JFrame to hold the Grid
		JFrame frame = new JFrame("Demo with two different solvers running a different way");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		// Build a Grid of size 9
		Grid grid1 = new Grid(9);
		Grid grid2 = new Grid(9);
		// load the grid with predefined problem
		Problems.setProblem3(grid1);
		Problems.setProblem3(grid2);
		// build a GridGuiOne for each Grid object
		GridGuiOne ggo1 = new GridGuiOne(grid1);
		GridGuiOne ggo2 = new GridGuiOne(grid2);
				
		// add the Grid to the Frame
		frame.setLayout(new GridLayout(1,2, 20, 20));
		frame.getContentPane().setBackground(Color.YELLOW);
		frame.add(ggo1);
		frame.add(ggo2);
		// make it visible
		frame.setVisible(true);
		frame.pack();
		
		// build two BruteForce object
		BruteForce bf1 = new BruteForce(grid1);
		BruteForce bf2 = new BruteForce(grid2);
		// start the solving
		bf1.solveWithGui(false);
		bf2.solveWithGui(true);
	}
}



The next turorials will deal on how create a Sudoku grid.
How to clear already filled cells until the solution does not become unique.

Is This A Good Question/Topic? 2
  • +

Page 1 of 1