Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2006
    Location
    Oslo Norway
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Java Sudoku Programming

    Hi!

    I found a site which gave its student the task of creating a sudoku solver in java.


    _________________________________________________________________
    // Task: Create a javaprogram that solves sudoku task by making filled sudokuboards.

    // Theese concept is to be used:
    // * Square is the smallest unit on the board to be used, the fields in witch the number are written inn.
    // * Board is all n x n squares.
    // * Row is a horisontal, (from the left to right on the board), line with n fields.
    // * Collumn is a vertical, (from up to down), line with n fields.
    // * Box is several horisontal and vertical squares, marked with thicker lines in the tasks. In the 9x9-exampel, a box 3x3 squares, but sometimes a box can be 2x3 squares.

    // It is to be as genneral so it can solve sudokuboard that do not have square boxes. For example boxes of 2x3 squares.
    // And, 9x9 is not an upper limit for the size of the field

    // The program is supposed to be able to solve a specific sukokutask A sudokuotask is a parcial filled board with three possible solutions:
    // * One solution.
    // * None. The numbers are positioned so that there is not any solution.
    // * Several solutions. For fue prefilled numbers.

    // The program shall print all solutions, (none, one or all), for a specific sudokutask If there is more than one, they shall be numbered, (1,2,3, ... m) or write
    // the number of solutions in the end. It is hinted to make a program that first generates all solutions for an empty board. Board and boxsizes as the only indata.
    // so it can be expanded later with some predetermend values for the squares.

    // It shall contain class Square, and Board. Board must contain a 2-dimensinal array tha point out all squares.

    // In addition there must be class Box, Column and Row. You must construct an object of these classes for every row, column and box on the board.

    // When a square checks if it can use a value, it must call for a method that does this in the sqaures column-object, Row-object and Box-object.

    // The rekursion is supposed to fill all squares with correct numbers. Every singles square must contain a method, tryAllNumberMeAndTheRest which tries to put numbers
    // in it self and, and if this is a success for a number, then the same method, (tryAllNumberMeAndTheRest), is called in the next square. In the solution you must start by
    // calling the method tryAllNumberMeAndTheRest in the top left square, and the the method calles the method in the square to the right. When a horisontal row is finnished,
    // the recursion continues in the left square in the next row, and so on...

    // The program should be programmed in a way so it later on can be devolped so that the communicaton with the user and drawing of the board on screan is taken in hand.
    // (Graphical Interface)

    _________________________________________________________________

    I started to write a program acording to the stated rules, but I cant find a solution for the problem.
    Here is my program so far:


    Code:
    import easyIO.*;
    import java.util.*;
    
    public class Sudoku {
        In key = new In(); // Found inn easyIO
    
        public static void main(String[] args) { // Main method for starting the program
    		Sudoku su = new Sudoku(); // Pointer to Sudoku class
    		su.commandLoop(); // Start commandLoop in Sudoku class
        }
    
        void commandLoop() { // Small menu
    		int menuChoise = 1; // For use in the menu
    		while (menuChoise != 3) { // Keeps the user in the menu until he chooses to exit
    			System.out.println("   SuDoKu   "); // Menu
    			System.out.println("-1-) ReadFromFile "); // For the possibilitie later on
    			System.out.println("-2-) Solve all possible "); // Does the job of solving the task
    			System.out.println("-3-) Exit "); // Exit the program
    			System.out.println("Chose: "); // Ask for a choise
    			menyChoise = key.inInt(); // Stores the choise
    			switch (menuChoise) {
    			case 1:
    			solveBoardFromFile();
    			break;
    			case 2:
    			solveAllPossible();
    			break;
    			case 3:
    			exit();
    			break;
    			default:
    			System.out.println("Possible choises: 1-3"); // Default message if while statement is violated
    			break;
    			}
    		}
        }
    
        void solveBoardFromFile() {}
        void solveAllPossible() {}
        void exit() {
    	System.out.println("Thank you for choosing this program..."); // Message on exit
        }
    
    }
    
    
    class Square {
    }
    
    
    class Board {
    	In tast    = new In();
        Square[][] square = new Square[9][9]; // Starts with a 9*9 board
    }
    
    
    class Box {
    }
    
    
    class Collumn {
    }
    
    
    class Row {
    }

  • #2
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Alright, you need to help us out here. What is your particular problem?
    Omnis mico antequam dominus Spookster!

  • #3
    New Coder
    Join Date
    Mar 2006
    Location
    Oslo Norway
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well I can't see where I should begin. I have added the posibilitie to create a blank board, but I do not now how to test all solutions on that. And I am alos uncertain on how I should use the classes. I guess a square need to know in wich box, row and column it is in. But I do not get any further...

    I have also been lookin around for existing code for a sudokusolver, but they are above my skills and knowlegde.

    This is what I have done so far:



    Code:
    import easyIO.*;
    import java.util.*;
    
    public class Sudoku {
        In key = new In(); // Found inn easyIO
        int n = 9;
    	Board br;
    
    	public Sudoku() {
    		br = new Board(n);
    	}
    
        public static void main(String[] args) { // Main method for starting the program
    		Sudoku su = new Sudoku(); // Pointer to Sudoku class
    		su.commandLoop(); // Start commandLoop in Sudoku class
        }
    
        void commandLoop() { // Small menu
    		int menuChoise = 1; // For use in the menu
    		while (menuChoise != 3) { // Keeps the user in the menu until he chooses to exit
    			System.out.println("---==)SuDoKu   "); // Menu
    			System.out.println("-=1=-) ReadFromFile "); // For the possibilitie later on
    			System.out.println("-=2=-) Solve all possible "); // Does the job of solving the task
    			System.out.println("-=3=-) Exit "); // Exit the program
    			System.out.print("Chose ) "); // Ask for a choise
    			menuChoise = key.inInt(); // Stores the choise
    			switch (menuChoise) {
    			case 1:
    			solveBoardFromFile();
    			break;
    			case 2:
    			solveAllPossible();
    			break;
    			case 3:
    			exit();
    			break;
    			default:
    			System.out.println("Possible choises: 1-3"); // Default message if while statement is violated
    			break;
    			}
    		}
        }
    
        void solveBoardFromFile() {}
    
    
        void solveAllPossible() {
    		int choise = 6;
    		boolean b1 = true;
    		while (b1) {
    			System.out.println("---==) Enter 13 to go back");
    			System.out.print("---==) Size, (6,9,12): ");
    			int n = key.inInt();
    			if (n == 13) {
    				b1 = false;
    			} else if (choise == 6 || choise == 9 || choise == 12) {
    				br.Brett(n);
    				br.solveAllPossible();
    				b1 = false;
    			} else {
    				System.out.println("---==) Must be 6, 9 or 12");
    				continue;
    			}
    		}
    	}
    
        void exit() {
    	System.out.println("Thank you for choosing this program..."); // Message on exit
        }
    
    }
    
    
    class Square {
        int contains;
        boolean found = false;
        Row r;
        Column k;
        Box b;
    
        Square(Row r, Column k, Boxx b) {
    	this.r = new Row();
    	this.k = new Column();
    	this.b = new Box();
        }
    
        void row() {
        }
    
        void column() {
        }
    
        void box() {
        }
    
        void tryAllNumberMeAndTheRest(int n) {
    
    	}
    }
    
    
    class Board {
        In key = new In();
        int[][] square;
        int n = 9;
        Square sq = new Square();
    
        void Board(int n) {
    		this.square = new int[n][n];
    		this.n = n;
    	}
    
        Board(int n) {
    		this.square = new int[n][n];
    		this.n = n;
    	}
    
    	void solveAllPossible() {
    		int number = 0;
    		for (int i = 0; i < n; i++) {
    			for (int j = 0; j < n; j++) {
    				rute[i][j] = 0; // Create an empty board
    			}
    		}
    		System.out.println("---==) Solution " + number);
    		final int WIDTH = 3;
    		for (int i = 0; i < n; i++) {
    			System.out.println();
    			for (int j  = 0; j < n; j++) {
    				System.out.print(square[i][j], WIDTH, Out.RIGHT);
    			}
    		}
    		number = number + 1;
    		System.out.println();
    		System.out.println();
    		sq.tryAllNumberMeAndTheRest(1);
    	}
    }
    
    
    class Box {
    }
    
    
    class Collumn {
    }
    
    
    class Row {
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •