Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > General forums > Development
User Name
Password
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 12-06-2006, 08:02 PM   #1 (permalink)
Avellon
Registered User
 
Join Date: Sep 2006
Posts: 431
+0 Internets
Java and Sudoku

So I've been working on a problem in Java where I create a class Sudoku, which I've made using a 2 dimensional array.

For anyone who might not know, a sudoku is a 9*9 square of cells, each cell contains a number.
The rules of Sudoku are very simple:

* Every column must contain the numbers 1-9 inclusive (this automatically means it must only contain each number once)
* Every row must contain the numbers 1-9 inclusive (this automatically means it must only contain each number once)
* Every 3*3 major sub-square must contain the numbers 1-9 inclusive (this automatically means it must only contain each number once)

So it might look like this,

1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8

Of course this isnt a valid solution, as the 3*3 subgrids don't have 1-9.

My problem comes in that I'm trying to create the sudoku, and then give a message if its a valid solution or not. I'm kind of new to Java, and was wondering what would be the best means to create an algorithm to search thrrough the rows and columns and subgrids to see if 1-9 is there, and if not to return back false as well as where in the sudoku the solution first wasn't satisfied.

Would a linear or binary search work here? Or how would you implement a solution?

This is sort of the framework I'm working on.

import java.io.*;
import java.util.*;
public class Sudoku {
private int[ ][ ] puzzle;
private String message;
public Sudoku ( String fileName ) throws FileNotFoundException {
. . .
}
private boolean isRowValid(int row) {
. . .
}
private boolean isColumnValid(int col) {
. . .
}
private boolean isSubSquareValid(int row, int col) {
. . .
}
public boolean isASolution() {
. . .
}
public String toString() {
. . .
}
}
Avellon is offline   Reply With Quote
Old 12-07-2006, 02:49 AM   #2 (permalink)
Vorph
Never Go Full Retard
 
Vorph's Avatar
 
Join Date: May 2002
Location: Hell
Posts: 5,369
Do you have to actually solve a puzzle or just determine if a given 9x9 grid is valid under the rules of sudoku?

If you're just checking for valid grids, a linear search is the simplest way (binary is for sorted lists). Not the most efficient thing in the world, but for evaluating a single 9 by 9 grid it doesn't really matter. Looking for duplicates is probably a little more efficient, but at 6AM I make no promises about my ability to calculate big O.

Solving a puzzle is a lot more complicated and you should read up on how a backtracking search algorithm works.
__________________
Vorph is offline   Reply With Quote
Old 12-07-2006, 03:04 AM   #3 (permalink)
Zuuljin
So there's this plane on a treadmill...
 
Zuuljin's Avatar
 
Join Date: Jan 2005
Location: Southern California
Posts: 2,904
+3 Internets
Send a message via AIM to Zuuljin
Seems fine to me. Just have it search each row/column linearly, and a mini search for each 3x3 square.

I actually wrote a sudoku app in java for fun awhile ago, but I actually built each puzzle from the start, and just saved the answer to compare too at the end, so I dont have it check the whole board.
Zuuljin is offline   Reply With Quote
Old 12-07-2006, 09:56 AM   #4 (permalink)
Zippygoose
Math Enthusiast/Badass MC
 
Zippygoose's Avatar
 
Join Date: Jun 2002
Location: Seattle
Posts: 596
-1 Internets
Send a message via AIM to Zippygoose
Like Vorph said, do a linear search since binary is for sorted lists. The groups you are validating are so small, however, that you could use any method of searching (however inefficient) and not really see a difference in performance.

If this is for a class though, you'll want to use best practices of course
__________________
Join the FoH stock market game

Zippygoose is online now   Reply With Quote
Old 12-07-2006, 11:17 AM   #5 (permalink)
Avellon
Registered User
 
Join Date: Sep 2006
Posts: 431
+0 Internets
Thanks for the help all.

Yea, I only need to validate the rows/columns/squares under the sudoku rules. I'm not yet at a point to solve it.

I know I would be able to search to see if 1-9 were in particular row/column/grid, would it also work to see if one of the cells was blank, or if there were more than one of the same number, like 3 in 2 different cells in a column?
Avellon is offline   Reply With Quote
Old 12-07-2006, 11:24 AM   #6 (permalink)
Kallian
CHARLIE DON'T SURF!
 
Kallian's Avatar
 
Join Date: Jul 2004
Posts: 778
+0 Internets
You could scan a row/col for 1-9, adding up the total number of each number like so:

int [] check = new int[9];

as you scan a row, keep track of totals like:
check[n - 1]++;

where n is the number you just read in.

check[0] would contain the total number of 1s.
check[1] would contain the total number of 2s.
etc.

if each element in the array is 1, then you're good, else, you're not.
__________________
In Soviet Russia, Exception throws you!
Kallian is offline   Reply With Quote
Old 12-07-2006, 12:02 PM   #7 (permalink)
Zippygoose
Math Enthusiast/Badass MC
 
Zippygoose's Avatar
 
Join Date: Jun 2002
Location: Seattle
Posts: 596
-1 Internets
Send a message via AIM to Zippygoose
It's threads like this that make me want to start periodic "problem threads". They wouldn't be someone's school assignment (although it's fun to help with those too) but rather just a generic problem to be solved. People could post their solutions and we could decide on the best/fastest/most efficient solution. I suppose that may be beyond the scope of this board though. Any thoughts?
__________________
Join the FoH stock market game

Zippygoose is online now   Reply With Quote
Old 12-07-2006, 12:34 PM   #8 (permalink)
Zuuljin
So there's this plane on a treadmill...
 
Zuuljin's Avatar
 
Join Date: Jan 2005
Location: Southern California
Posts: 2,904
+3 Internets
Send a message via AIM to Zuuljin
I have a whole book of problems that I used in training for the ACM competition. They range from fun to rediculous and obscure. If there were a thread, i'd dump those on there. =)
Zuuljin is offline   Reply With Quote
Old 12-09-2006, 08:22 AM   #9 (permalink)
Benito Fireslinger
Registered User
 
Join Date: Feb 2002
Location: Georgia
Posts: 333
+0 Internets
and this is why i started codewiki =( too bad i have no time to devote to it
__________________
Archimonde
Benito Fireslinger is offline   Reply With Quote
Old 12-11-2006, 06:46 AM   #10 (permalink)
Bralkan
Registered User
 
Bralkan's Avatar
 
Join Date: Jan 2002
Location: Atlanta GA
Posts: 2,118
-1 Internets
Send a message via AIM to Bralkan
I hear leprechauns will write your code if you make it open source.
__________________
We're all idiots.
http://nottheparrot.blogspot.com
Bralkan is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
uberguilds network



All times are GMT -7. The time now is 10:48 AM.


Powered by vBulletin® Version 3.6.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6