Hello, I'm trying to build a program that solves sudokus and prints out the result on the screen. Here's the code for the class SudokuBoard. this will later be called in a class Sudoku. I'm a newbie, so making this took me hours and hours of time... -
// class SudokuBoard, will be called by class Sudoku
-
-
import java.util.Scanner;
-
import java.io.*;
-
-
public class SudokuBoard {
-
-
private int[][] board = new int[9][9];
-
-
// it reads the established sudoku board from a scanner, which
-
will be given when the class Sudoku calls upon the method.
-
public void readBoardFromScanner(Scanner s) {
-
-
for (int i=0; i<9; i++)
-
for (int j=0; j<9;j++)
-
board[i][j] = s.nextInt();
-
}
-
-
public void printBoard() {
-
-
System.out.println("The Board is:");
-
-
for(int i=0; i<9; i++){
-
for (int j=0; j<9; j++){
-
System.out.print(board[i][j]+" ");
-
}
-
System.out.println(" ");
-
}
-
-
}
-
// checks the row to see if a given # is okay
-
public boolean rowCheck(int i, int number) throws Exception {
-
-
for (int j = 0; j < 9 ; j++)
-
if (board[i][j] == number )
-
return false;
-
-
return true;
-
}
-
// checks the column to see if a given # is okay
-
public boolean columnCheck(int j, int number) throws Exception {
-
-
for (int i = 0; i < 9; i++)
-
if (board[i][j] == number )
-
return false;
-
-
return true;
-
}
-
// checks the 3*3 box to see if a given # is okay
-
public boolean smallBoxCheck(int i, int j, int number) throws Exception {
-
-
int k = (i/3);
-
int l = (j/3);
-
-
int row = k * 3;
-
int col = l * 3;
-
-
// the above steps are for round-offs so that i=6,7,8 can start
-
// altogether with row starting point 6, i=4,5,6 with row starting
-
// point 4, etc. Same logic with columns.
-
-
for (int x = 0; x < 3; x++)
-
for (int y = 0; y < 3; y++)
-
if(board[row+x][col+y] == number )
-
return false;
-
-
return true;
-
}
-
// the active part begins here
-
public void solve(int i, int j) throws Exception {
-
-
if ( i > 8 )
-
throw new Exception ( "Solution Found" ) ;
-
else
-
{
-
while( board[i][j] != 0 ) {
-
if ( ++j > 8 )
-
{
-
j = 0;
-
i ++;
-
-
if ( i > 8 )
-
throw new Exception ("Solution Found");
-
}
-
}
-
-
for (int num = 1; num < 10; num++) {
-
-
if (columnCheck(i,num) && rowCheck(i,num) && smallBoxCheck(i,j,num)) {
-
board[i][j] = num;
-
-
// after setting the number, call the next recursive call
-
if ( j < 8 )
-
solve( i, j+1);
-
else
-
solve (i+1, 0);
-
}
-
}
-
-
// no solutions found, set the spot to 0 and backtrack
-
board[i][j] = 0;
-
}
-
}
-
-
// Start solving at 0,0
-
public void startSolve() {
-
try {
-
solve(0,0);
-
}
-
-
catch (Exception exception )
-
{ }
-
}
-
}
-
-
-
and then, here is the call from the class Sudoku :
-
-
import java.io.*;
-
import java.util.Scanner;
-
-
public class Sudoku {
-
-
public static void main (String[] args) throws FileNotFoundException {
-
-
Scanner text;
-
text = setFileScanner("board1");
-
-
SudokuBoard sb = new SudokuBoard();
-
-
sb.readBoardFromScanner(text);
-
-
sb.printBoard(); // print the original board
-
-
sb.startSolve(); // solve the board
-
-
sb.printBoard(); // supposed to print the "solved" board
-
-
}
-
-
public static Scanner setFileScanner(String fileName) throws FileNotFoundException {
-
-
File randomName = new File(fileName);
-
Scanner t = new Scanner(randomName);
-
return t;
-
}
-
-
}
-
Okay, now, the second sb.printBoard(); still gives me the original board on the screen. I don't really understand what I did wrong, but it seems that none of the "solution" numbers are replacing the zeroes(blanks) within the original board. What am I doing wrong here? Thank you so much.
3 8575
I'm sorry, I didn't know that I shouldn't post questions here, if somebody(admin?) would kindly move it to an appropriate thread, I'll be grateful
Actually, this is the right place. (Of course, someone may have already moved it here, I don't know.) However, you didn't use the [code]...[/code] tags. I've added them for you this time, but please use them yourself from now on.
Now, to help you with your code. First of all, you shouldn't really be using Exceptions like that. When using recursion, you should stop at a certain point, but not by throwing an exception but by not calling itself again.
Also, your algorithm looks weird to me - what exactly are you trying to do there? Could you write it in pseudo code or describe it in words?
If you would like to look at a finished solution (you may not want to, that's your choice), you can check the Sudoku howto Part A, B and C.
Greetings,
Nepomuk (Moderator)
Okay, I'll describe what it does in words:
Class Sudoku exists to do the following:
read the file which includes a sudoku board and convert it into a scanner,
which will be then sent into class SudokuBoard as an argument within a
method that will read from scanner and convert them into an private array board[][]
and execute methods within SudokuBoard, which is just for the simplicity's sake.
Class SudokuBoard does the following:
read the Scanner file given by SudokuBoard and convert/stow them into a private array int[][] board; (the method readBoardFromScanner does this)
then, the methods rowCheck, columnCheck and smallBoxCheck will check whether numbers 0-9 fit into a cell. These methods will all be run in conjunction, and when all of these hold true, I will be placing that number into the cell(in the method solve).
Finally, the method solve:
it's supposed to do the following, i don't know if it did exactly that:
find a spot on the board which is zero, if there are no such spots,
return the board.
for each number from 1-9,
check to see if the boolean conditions from above three "checkers" are true.
if they are, set that number into the given cell. (since this will be in a loop, every number from 1-9 will be checked, yes?) and then solve the next cell.
I wasn't sure how to make the solver stop when it reaches the end of the board,
so I threw an exception. If that's not the way it's supposed to do it, how should I go about the problem? If I leave out that exception, it will go into an infinite loop since it doesn't know when to stop.
I will go look at the solutions you have given me now, and see if I can answer my own questions; but in the meantime, any help is appreciated. Thank you very very much for your time!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jessica M |
last post by:
I am having the following problem
http://jessica.ourprofile.net/
are being rendered differently on Firefox and IE6 (PC).
I have outline the div culprits in red to help with debugging....
|
by: outofmymind |
last post by:
Hi every1,
im trying to solve this question, i did some of it but i dont think that its correct or complete:
this is the question:
Write the definition of a class called Product. A Product...
|
by: nomad |
last post by:
Hi everyone:
I need help Please I'm still learning Java...
Here is part of my code where I'm having problems.
Problem is at public static void main(String args) {
The method main cannot be...
|
by: KeithAL |
last post by:
I'm a student whose taking comp 110, and my professor gave the class a practice exam to help us study for our exam on tuesday.
The question is:
A perfect number is a positive integer that is...
|
by: javabeginner123 |
last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is...
|
by: HypeBeast McStreetwear |
last post by:
Hello everyone. I got a assignment that states.
The set of linear equations
a11X1 = a12X2 = c1
a21X1 = a22X2 = c2
May be solved using Cramer’s rule:
X1 = c1a22 – c2a12
a11a22 –...
|
by: @$|-|. DUBEY |
last post by:
i have a interger
val = 99999;
and i want to add the content of val i.e., 9+9+9+9+9 = 45;
how to do that....
need urgent help
|
by: milk242 |
last post by:
Hi everyone,
I'm trying to write a recursive method that removes vowels from a string. If someone can help me formulate a pseudocode to do this, that would be great.
So far my code is:
...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |