473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Sudoku Verification, Turning Pseudo-Code into real code!

Hi, and thanks for possible help in advance.
Here's my dilemma. I've been making a sudoku generator, and I'm now stuck on one part. I must be able to take a 'solution' and verify that it is correct, by checking a few things.
1. There are enough numbers, no 'periods' which signify '0' or 'no input'.
2. That there are no duplicate numbers in any row, column, or 3x3 box.

I have been thinking of ways to do this and I've come up with 2 things. Firstly is this bit of code. When I put i in, however, it won't work.

//requires that you only allow valid input of characters 1-n where n is the size of the board
Expand|Select|Wrap|Line Numbers
  1. private boolean validateSudoku(int[][] b)
  2. for (int i = 0; i < b.length; i++) {
  3. String row = "";
  4. String col = "";
  5. for (int j = 0; j < b[i].length; j++) {
  6. if(!row.contains(b[i][j] + ""))
  7. row +=b[i][j] + "";
  8. if(!col.contains(b[j][i] + ""))
  9. row += b[j][i] + ""; 
  10. if(row.length() + col.length() != b.length + b[0].length)
  11. return false;
  12. }
  13. return true; 
  14. }
The Second is this pseudo code I've created, and although sure this method is better, I can't seem to figure out how to make this happen.

take an array with 9 numbers; this represents a row, or a column, or a 3x3 square, whatever, call this listArray

if listArray is not exactly 9 elements long, it's invalid, so return false

create blank array, call this testArray

for each number in listArray:
if testArray contains this number:
listArray is invalid
return false
else:
add this number to testArray

return true

Oh here's my full code I've done so far by the way.


Expand|Select|Wrap|Line Numbers
  1. public class Sudoku {
  2.     private int N = 3; //sets 'N'. This can be changed to make bigger/smaller sudoku
  3.     private int[][]board; // creates the board.
  4.     private int square = N*N;
  5.  
  6.     public static void main (String []Args){
  7.         Sudoku s1 = new Sudoku();
  8.         s1.show();
  9.         s1.isSolution();
  10.     }
  11.  
  12.     public Sudoku(){ //constructor
  13.         board = new int [square][square]; //Sets the overall size of the board.
  14.         for (int i=0; i< square; ++i){
  15.             for(int j=0; j< square; ++j){
  16.                 board [i][j]=0; // Goes through all columns and rows, sets it to 0
  17.             }
  18.         }
  19.     }
  20.  
  21.     public    int getSquare(int i, int j){ // Gets the value of board[i][j] 
  22.  
  23.         if(i >=0 && i< square && j>=0 && j<square)
  24.             return board[i][j];
  25.         else
  26.             return 0;
  27.  
  28.     }
  29.  
  30.     public void setSquare(int i, int j, int val){// Sets board[i][j] = val.
  31.         board [i][j] = val;
  32.  
  33.     }
  34.  
  35.     public void show() {
  36.         for (int i=0; i< square; ++i){
  37.             if (i%3 == 0 && i > 0)
  38.                 System.out.println("------+------+------");
  39.             for(int j=0; j< square; ++j){
  40.                 if (board [i][j] == 0)
  41.                     System.out.print(" .");
  42.                 else 
  43.                     System.out.print(" " + board [i][j]);
  44.                 if ((j+1)%3 == 0 && j < square -1)    
  45.                     System.out.print("|");
  46.             }
  47.             System.out.println();
  48.         }
  49.     }
  50.  
  51.     public boolean isSolution(){
  52.  
  53.  
  54.     }
  55. }
I'm getting kinda frustrated and a point in the right direction or even some help would be greatly appreciated. Thank you for your time and help.
Oct 16 '09 #1
3 5940
I have another bit to add in. Another part of the assignment is
Write a method

void fill(String[] lines);
in the Sudoku class that takes an array of N*N Strings. Each string has N*N elements, each of which is either a digit or period. Periods indicate empty squares.
Expand|Select|Wrap|Line Numbers
  1. So far I have
  2.     public void fill(String[] lines){
  3.         for(int x=0; x< square; x++){
  4.             lines[x] = 
  5.         }
square being N*N, in this case 9.
I want this code to pick up only periods and digits. None of the border of +,-,| around it. Is there a type of code to do that? Forgive me if you can't tell I'm pretty darn rusty here.
Oct 16 '09 #2
JosAH
11,448 Expert 8TB
Check the Java insights section, it has a three part article on Sudoku, including the reading and initializing, solving and printing the darn thing.

kind regards,

Jos
Oct 16 '09 #3
My question has been moved to http://forums.sun.com/thread.jspa?th...art=0&tstart=0 and is coming along well.
Oct 16 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: sub1ime_uk | last post by:
Thought I'd offer a method for solving all possible 9x9 sudoku puzzles in one go. It'll takes a bit of time to run however (and 9x9 seems to be about as big as is reasonably possible before...
5
by: Stewart Gordon | last post by:
I have a few Sudoku puzzles on my site. http://www.stewartsplace.org.uk/mindbenders/ But adding extra columns and rows to separate the 3x3 blocks seems a rather kludgy approach, and the result...
12
by: kalinga1234 | last post by:
hy guys i am having a problem with my sudoku program which i coded using c++.; currently in my program if a duplicate number exist in either row/column/block i would make the particualr square...
0
by: pwilliams | last post by:
NCOALink Change of Address Verification Each year over 40 million Americans change their mailing addresses. This change is equivalent to every person in California deciding to change addresses...
4
by: hashimtk | last post by:
Hi, i done a sudoku solving program ,and i got solution for easy sudoku, Can u explain any logic to find out medium and hard sudoku.
6
by: blux | last post by:
I am working on a function to check the validity of a sudoku puzzle. It must check the 9x9 matrix to make sure it follows the rules and is a valid sudoku puzzle. this is what I have come up with...
0
by: swint002 | last post by:
Here's another game written in T-SQL. Sudoku: http://www.geocities.com/swint002/Sudoku.txt Let me know what you think.
21
by: ningxin | last post by:
Hi, i am currently taking a module in c++ in the university, and was given an assignment. because i have no prior background on the subject, everything is kind of new to me. i have tried for quite...
38
by: Boon | last post by:
Hello group, I've been toying with a simple sudoku solver. I meant for the code to be short and easy to understand. I figured "brute force is simple" -- who needs finesse, when you've got...
62
jkmyoung
by: jkmyoung | last post by:
Does anyone have some super, super hard Sudoku puzzles? Back in February this year, I had enough time to finally program a Sudoku solver in Java. Right now, I'm looking for solvable puzzles, but...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.