Page 1 of 1

Sudoku VII - Grid Generator GUI Rate Topic: -----

#1 pbl  Icon User is offline

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

Reputation: 8370
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Posted 22 October 2010 - 07:04 PM

This new class will allow you to generate a new Sudoku grid and to edit it (remove cells) to generate a Sudoku problem as complicated as you want.

Your run the program, click the JButton asking to generate a new Grid and the new Grid is displayed. Then you click with on any Cell with the mouse to clear it. You can click again to restore it's old value.

The program will validate that clearing that cell will still make the Sudoku problem as a valid one. That means that the solution is still unique.

This new class requires modifications to the Grid class and the SudokuGenerator class.
Please update your and from the previous post where they have been updated.

The GUI used the standard GridGuiOne class to display the Grid.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.*;
 * Use the following already posted classes:
 * - Grid the center call
 * - SudokuGenerator to create a new Grid
 * - BruteForce to validate that the Sudoku solution os unique
 * - GridGuiOne the standard Sudoku JPanel
 * Sudoku generator that can be edited by the user to remove (restore) cells
 * The program will also verify, when cells are cleared, that the Sudoku
 * is still valid meaning that is still has a single solution
public class SudokuGeneratorGui extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	// to remember the cells values if the user decides to fall back
	private int[][] cellValue;
	// the Grid object
	private Grid grid;
	// it's size
	private int size;
	// My generator
	private SudokuGenerator generator;
	// the JButton to perform reload with a new Grid
	private JButton btnReload = new JButton("Reload with a new Grid");
	// a JLabel for the status at the bottom (init with with a \n so it will be showed)
	private JLabel statusLabel = new JLabel("\n");
	 * Constructors
	SudokuGeneratorGui(Grid grid) {
		super("Sudoku generator");
		this.grid = grid;
		// the generator
		generator = new SudokuGenerator(grid);
		// create the array to save the cell values
		size = grid.getSize();
		cellValue = new int[size][size];
		// get the cells to inform them that I will be their MouseListener
		Cell[][] cell = grid.getCells();
		// loop throutg them
		for(int i = 0; i < size; i++) {
			for(int j = 0; j < size; j++) {
				// add as mouseListener my own inner class passing the coords as parameter
				cell[i][j].addMouseListener(new Listener(i,j));
		// put the JButton in the North region
		add(btnReload, BorderLayout.NORTH);
		// use a standard GuiGridOne as Panel in the center
		add(new GridGuiOne(grid), BorderLayout.CENTER);
		// add the status label at the bottom
		statusLabel.setBackground(new Color(255, 255, 125)); // light yellow
		add(statusLabel, BorderLayout.SOUTH);
	public void actionPerformed(ActionEvent e) {
		// erase previous message
		// a reload was requested
		// save the cells values
		for(int i = 0; i < size; i++) {
			for(int j = 0; j < size; j++) {
				cellValue[i][j] = grid.getValue(i, j);

	 * an inner class to receive the mouse click and process them
	class Listener implements MouseListener {
		// coords in the Grid of the Cell that has been clicked
		private int x, y;
		 * Constructor
		Listener(int x, int y) {
			this.x = x;
			this.y = y;
		public void mouseClicked(MouseEvent arg0) {
			// erase previous message
			// if no problem loaded yet the cell value will be 0
			if(cellValue[x][y] == 0) {
				statusLabel.setText("Load a Grid first using the \"Reload with a new Grid\" button");
			// test if the Grid value is 0 in that case it is a reload
			// or if it is a cell that we want to clear
			if(grid.getValue(x, y) == 0) {
				// restore the old value
				grid.setCellValue(x, y, cellValue[x][y]);
			else {
				// ok set the Grid value to 0
				grid.setCellValue(x, y, 0);
				// and verify if the Sudoku grid is still valid
				boolean status = BruteForce.isValidSudoku(grid);
				// ok Sudoku grid still we can exit
				// sorry invalid try
				// send message to the user
				statusLabel.setText("Sorry clearing this cell would make the Sudoku invalid");
				// restore the Cell value
				grid.setCellValue(x, y, cellValue[x][y]);
		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mousePressed(MouseEvent e) {}
		public void mouseReleased(MouseEvent e) {}
	 * To start the GUI
	public static void main(String[] args) {
		// create a Sudoku Grid 9x9
		Grid grid = new Grid(9);
		JFrame frame = new SudokuGeneratorGui(grid);
		// the packed frame will only be good to display "" blank labels when
		// number will pe put it wont resize so we will have to make it a little bit bigger
		Dimension d = frame.getSize();
		d.height *= 7;
		d.width *= 7;
		d.height /= 6;
		d.width /= 6;


Is This A Good Question/Topic? 0
  • +

Page 1 of 1