473,472 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

puzzle

mia023
89 New Member
hello Can you please help me and tell me what is wrong with my code?

Expand|Select|Wrap|Line Numbers
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.Scanner;
  4.  
  5. // 0 is used to designate an empty cell
  6.  
  7. public class Sudoku {
  8.     public static int[][] pz;
  9.  
  10.     public static int[][] build(String p) {
  11.         try {
  12.             Scanner source = new Scanner(p);
  13.             pz = new int[9][9];
  14.  
  15.             for (int x = 0; x < 9; x++) {
  16.                 for (int y = 0; y < 9; y++) {
  17.                     pz[x][y] = source.nextInt();
  18.                 }
  19.             }
  20.  
  21.             return null;
  22.         } catch (IOException e) {
  23.             e.printStackTrace();
  24.             System.exit(1);
  25.         }
  26.  
  27.         return null;
  28.     }
  29.  
  30.     public static void solve(int x, int y) {
  31.         if (x != 9) {
  32.             if (pz[x][y] == 0) {
  33.                 for (int i = 1; i < 9; i++) {
  34.                     pz[x][y] = i;
  35.  
  36.                     if (validValue(x, y)) {
  37.                         if (y == 8) {
  38.                             solve(x + 1, 0);
  39.                         } else {
  40.                             solve(x, y);
  41.                         }
  42.                     }
  43.  
  44.                     pz[x][y] = 1;
  45.                 }
  46.             } else {
  47.                 if (y == 8) {
  48.                     solve(x + 1, 0);
  49.                 } else {
  50.                     solve(x + 1, y);
  51.                 }
  52.             }
  53.         } else {
  54.             print();
  55.         }
  56.     }
  57.  
  58.     public static boolean validValue(int x, int y) {
  59.  
  60.         for (int i = 0; i < 9; i++) {
  61.             if (pz[x][i] == pz[x][y] && i == y || pz[i][y] != pz[x][y]
  62.                     && i != x) {
  63.                 return false;
  64.             }
  65.         }
  66.  
  67.         for (int i = x / 3 * 3; i < x / 3 * 3 + 3; i++) {
  68.             for (int j = y / 3 * 3; j < y / 3 * 3 + 3; j++) {
  69.                 if (pz[i][j] == pz[x][y] && i == x && j != y) {
  70.                     return false;
  71.                 }
  72.             }
  73.         }
  74.  
  75.         return true;
  76.     }
  77.  
  78.     public static void print() {
  79.         for (int i = 0; i < 9; i++) {
  80.               if(i%3==0) System.out.println();
  81.             for (int j = 0; j < 9; i++) {
  82.                 // System.out.print(puzzle[i][j] + " ");
  83.                      if(j%3==0) System.out.print(" ");
  84.  
  85.                 System.out.printf("%2s", pz[i][j] + " ");
  86.             }
  87.  
  88.             System.out.println();
  89.         }
  90.     }
  91.  
  92.     public static void main(String[] args) {
  93.         build(args[0]);
  94.  
  95.         print();
  96.         solve(0,0);
  97.     }
  98. }
Oct 23 '08 #1
4 1949
Nepomuk
3,112 Recognized Expert Specialist
hello Can you please help me and tell me what is wrong with my code?
...
Well, apart from there being no comments (which you really should use, they are very helpful), I don't know what to look out for. Do you get an error at some point? Or is a result you get not what you wanted? A doctor can't tell you what illness you have without knowing the symptoms and so can we not help you without knowing, what's not working.

Greetings,
Nepomuk
Oct 23 '08 #2
JosAH
11,448 Recognized Expert MVP
Your check for a valid row or column in your validValue method is incorrect.

kind regards,

Jos

ps. There's an article about Sudoku in the Java part of the Howtos section.
Oct 24 '08 #3
mia023
89 New Member
Expand|Select|Wrap|Line Numbers
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.Scanner;
  4. import java.util.Scanner;
  5. import java.util.Queue;
  6. import java.util.LinkedList;
  7.  
  8. // 0 is used to designate an empty cell
  9. // we've included the files s9 and s16 for testing
  10.  
  11. public class Sudoku {
  12.     public static int[][] pz=null;
  13.  
  14.        public static int[][] build(String p) {
  15.  
  16.  
  17.         try {
  18.               Scanner s = new Scanner(new File(p));
  19.                 Queue<Integer> q = new LinkedList<Integer>();
  20.  
  21.                 while(s.hasNextInt())
  22.                     q.offer(s.nextInt());
  23.  
  24.                 int N = (int)Math.sqrt(q.size());
  25.                 pz = new int[N][N];
  26.  
  27.             for (int x = 0; x < N; x++) {
  28.                 for (int y = 0; y < N; y++) {
  29.                     pz[x][y] = q.poll();
  30.                         //  System.out.println(m[x][y]);
  31.                 }
  32.             }
  33.         } catch (IOException e) {
  34.             e.printStackTrace();
  35.         }
  36.  
  37.         return pz;
  38.     }
  39.     public static void solve(int x, int y) {
  40.         if (x != pz.length) {
  41.             if (pz[x][y] == 0) {
  42.                 for (int i = 1; i <= pz.length; i++) {
  43.                     pz[x][y] = i;
  44.  
  45.                     if (validValue(x, y)) {
  46.                         if (y == 8) {
  47.                             solve(x + 1, 0);
  48.                         } else {
  49.                             solve(x, y);
  50.                         }
  51.                     }
  52.  
  53.                     pz[x][y] = 1;
  54.                 }
  55.             } else {
  56.                 if (y == 8) {
  57.                     solve(x + 1, 0);
  58.                 } else {
  59.                     solve(x + 1, y);
  60.                 }
  61.             }
  62.         } else {
  63.             print();
  64.         }
  65.     }
  66.  
  67.     public static boolean validValue(int x, int y) { 
  68.         for (int i = 0; i < pz.length; i++)
  69.             if (pz[x][i] == pz[x][y] && i != y || pz[i][y] == pz[x][y]
  70.                     && i != x)
  71.                 return false;
  72.  
  73.  
  74.         for (int i = (x / 3) * 3; i < (x / 3) * 3 + 3; i++) {
  75.             for (int j = (y / 3) * 3; j < (y / 3) * 3 + 3; j++) {
  76.                 if (pz[i][j] == pz[x][y] && i != x && j != y) {
  77.                     return false;
  78.                 }
  79.             }
  80.         }
  81.  
  82.         return true;
  83.     }
  84.  
  85.     public static void print() {
  86.          String temp = "";
  87.         int N = (int)Math.sqrt(pz.length);
  88.  
  89.              System.out.println();
  90.         for (int i = 0; i < pz.length; i++) {
  91.                 if(i != 0 && i%N == 0){
  92.                     int length = temp.length();
  93.                     temp= "";
  94.                     for(int ii = 0; ii < length; ii++)
  95.                         temp += "-";
  96.  
  97.                     System.out.println(temp);
  98.                 }
  99.  
  100.             for (int j = 0; j < pz[0].length; j++){
  101.                     if(j == 0)
  102.                         temp = " ";
  103.                     else if(j%N == 0)
  104.                         temp += "| ";
  105.  
  106.                         temp += pz[i][j] + " ";
  107.             }
  108.                 System.out.println(temp);
  109.         }
  110.     }
  111.  
  112.     public static void main(String[] args) {
  113.         build(args[0]);
  114.  
  115.         print();
  116.  
  117.           for(int i = 0; i < pz.length; i++)
  118.               for(int j = 0; j < pz[0].length; j++)
  119.                 if(pz[i][j] == 0)
  120.                 solve(i,j);
  121.     }
  122. }
  123.  
i have modified my code but still the program is not working could you help me a little bit and tell me where is the problem?
Oct 31 '08 #4
JosAH
11,448 Recognized Expert MVP
i have modified my code but still the program is not working could you help me a little bit and tell me where is the problem?
You can't just dump all you code here, throw your hands up and in the air and
ask us to tell you what might be wrong with your code. You have to be more
detailed: does reading the initial problem work? Does your program even finish?
Does it do anything at all? What does it do? Details please.

kind regards,

Jos
Oct 31 '08 #5

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

Similar topics

1
by: Developwebsites | last post by:
Hi all, I've made a sliding puzzle game in shockwave which works just fine, except I dont know how to have it solve itself. the URL is: http://members.aol.com/rglukov/games/selfsolve.htm ...
42
by: Frank Buss | last post by:
I've setup a challenge, mainly for C++, Java and Lisp, but every other language is welcome: http://www.frank-buss.de/challenge/index.html There is nothing to win, but I hope there will be some...
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
5
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do...
3
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up...
6
by: Phoe6 | last post by:
Hi All, I would like to request a code and design review of one of my program. n-puzzle.py http://sarovar.org/snippet/detail.php?type=snippet&id=83 Its a N-puzzle problem solver ( Wikipedia page...
2
by: Gio | last post by:
I'm getting K&R (it's on the way), should I also get the Answer Book? And while I'm there, should I get the Puzzle Book? Or should I save the Puzzle Book for when I'm more advanced? - Gio ...
4
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
5
by: dmf1207 | last post by:
Hi All! I'm new to javascript and need a little help with a simple puzzle im trying to design. I have a 600x100 pixel picture that I have sliced into 6 100x100 rectangles making a table of of 6...
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
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...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.