Results 1 to 14 of 14
Thread: trying to create a soduku puzzle
- 04-07-2010, 04:01 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
trying to create a soduku puzzle
There are two parts to this question so bare with me with the explanation.
I am trying to create a soduku puzzle where I can use a printed puzzle for java to solve. I am new to Java and I am trying to figure out how or where I need to go to create this puzzle. I would greatly appreciate all help. I am using eclipse and have never in my life programmed a computer and this is my first language I have ever tried to program.
The second part that I am doing is to have Eclipse generate puzzles randomly and then solve them. I have been up for 96 hours straight with trying to figure out how to get started and how to solve and finish the second puzzle where it is a random one.
Thank you so much:o very frustrated:(
- 04-07-2010, 04:34 PM #2
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
There are some examples of sudoku solvers on the net.
Maybe we should get this straight first: you are trying to create something that generates a random sudoku puzzle and then solves it?
create the puzzle in a text file, easy and simple for when you are starting out. Put each new row on a new line and each number separated by a space or something that is the way I used to do sudoku-related Java stuff because that way it is easy for anyone to read and understand. Or if you don't need to produce a tangible result that you can look at forever just have it do it in memory and print the results out then you don't even need to handle files.Last edited by porchrat; 04-07-2010 at 04:55 PM.
- 04-07-2010, 05:18 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
I wrote a three part article once here, named Sudoku A, Sudoku B and Sudoku C; it contains full source code; do with it what you want. It's a solver and it's quite fast.
kind regards,
Jos
- 04-07-2010, 05:19 PM #4
here you will find a Java Sudoku Solver sudoku solver.
- 04-07-2010, 07:59 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
New to Java
To clarify and to show everyone how my thought processes are going here is the two code
package soduku
public class soduku;
public static void main string ,(args[]
import java.awt.GridLayout;;
for int(i=0 and j=0 i<9 and j<9)
This first one I need to be able to enter in numbers and then have java solve it.
The second one is:
package SUDUKO;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Suduko
{
private JTextField[][]cells;
int[] variable1r = new int[9];
int[] variable2r = new int[9];
int[] variable3r = new int[9];
int[] variable4r = new int[9];
int[] variable5r = new int[9];
int[] variable6r = new int[9];
int[] variable7r = new int[9];
int[] variable8r = new int[9];
int[] variable9r = new int[9];
public static void main( String[]args )
{
try
{
Suduko suduko = new Suduko();
suduko.init();
} catch (Exception e)
{
e.printStackTrace();
}
}
private void init()
{
cells = new JTextField[9][9];
JPanel panel = new JPanel();
panel.setLayout( new GridLayout( 9, 9, 5, 5 ) );
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
cells[i][j] = new JTextField();
panel.add( cells[i][j] );
}
}
JFrame frame = new JFrame();
frame.add( panel );
frame.setSize( 600, 600 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );
initGame();
}
private void initGame()
{
Random r = new Random( 9 );
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
int randomValue = r.nextInt( 9 ) + 1;
if (IsValid( i, j, randomValue ))
{
cells[i][j].setText( String.valueOf( randomValue ) );
} else
{
j--;
}
}
}
}
private boolean IsValid( int row, int column, int value )
{
System.out.println( "Checking " + row + " " + column + " for " + value );
{
{
return false;
}
}}}
This one when you run it, it will not display any numbers and it will show if you are using eclipse or for that matter netbeans dr. java that it doesn't see out of the grid point 0,0.
Please help!
Thank you for everyone who responded before and will respond to this post.
- 04-07-2010, 08:11 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
Your IsValid method always returns false so no numbers will be considered valid; try to make it return true just for fun (is doesn't test anything so you have nothing to lose) and see what happens.
kind regards,
Jos
ps. next time you post Java code use those [code] tags for readability reasons.
- 04-08-2010, 02:56 AM #7
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
update to Soduku Puzzle
Okay I would like to let everyone know that I am trying to figure out how to make my code work to where it will check and allow my soduku puzzle to be one that is legal by the rules of soduku.
I am using Eclipse and when ever I type for for the forloop (which beinging a a green horn to Java I do not understand what this fully does.
I would like to see guidance for the code and if someone knows of a very good Java book that I could get that would help me understand Java well for a greenhorn let me know because I really need to know.
private void initGame()
{
Random r = new Random( 9 );
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
int randomValue = r.nextInt( 9 ) + 1;
if (IsValid( i, j, randomValue ))
{
cells[i][j].setText( String.valueOf( randomValue ) );
} else
{
j--;
}
}
}
}
private boolean IsValid( int row, int column, int value )
{
System.out.println( "Checking " + row + " " + column + " for " + value );
{
{
return true;
}
}}}I would hope that everyone could help me out on this and those who do thank you so much and also thanks for suggesting java books to help me understand the language better.
- 04-09-2010, 07:51 PM #8
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
Almost there just stuck on a few new to Java
Okay I am having problems implamenting the 3x3 check if you can help me with telling me what I need to do step by step that would help because I am making it a GUI and is quite frustrating with it being a project for the first time.
Thank you for reply to this thread.
package SUDUKO;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Suduko
{
private JTextField[][]cells;
int[] variable1r = new int[9];
int[] variable2r = new int[9];
int[] variable3r = new int[9];
int[] variable4r = new int[9];
int[] variable5r = new int[9];
int[] variable6r = new int[9];
int[] variable7r = new int[9];
int[] variable8r = new int[9];
int[] variable9r = new int[9];
public static void main( String[]args )
{
try
{
Suduko suduko = new Suduko();
suduko.init();
} catch (Exception e)
{
e.printStackTrace();
}
}
private void init()
{
cells = new JTextField[9][9];
JPanel panel = new JPanel();
panel.setLayout( new GridLayout( 9, 9, 5, 5 ) );
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
cells[i][j] = new JTextField();
panel.add( cells[i][j] );
}
}
JFrame frame = new JFrame();
frame.add( panel );
frame.setSize( 600, 600 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );
initGame();
}
private void initGame()
{
Random r = new Random( 9 );
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
int randomValue = r.nextInt( 9 ) + 1;
if (IsValid( i, j, randomValue ))
{
cells[i][j].setText( String.valueOf( randomValue ) );
} else
{
j--;
}
}
}
}
private boolean IsValid( int row, int column, int value )
{
System.out.println( "Checking " + row + " " + column + " for " + value );
{
{
return true;
}
}}
{
for(int i=1; i<9; i--);//want to have it test the rows
{
for(int j=2; j<9 j--);//want to be column tester
}
{
for(int k=3; k<9 k--);//want to have 3x3 tester
}
//want for loop to repeat till finished but need it to be for three things rows but how? Is this correct
// I am trying to remember everything about what I learned in Java so far plus what you have taught me as well.
}} //problem cannot figure out how to create a 3x3 check.
//want for loop to repeat till finished but need it to be for three testers rows amd colums and 3x3 but how?
//Would like this for loop to check columns of the system.
// would if statements be better for checking columns and rows?
//would like to create a 3x3 check with using int k but do not know what method to use.
again please let me know the step by step process if you could please.
- 04-09-2010, 07:56 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
- 04-09-2010, 07:56 PM #10
Member
- Join Date
- Apr 2010
- Posts
- 14
- Rep Power
- 0
wrong board, your looking for
Rent A Coder: How Software Gets Done -- Home of the worlds' largest number of completed software projects
Good luck
- 04-09-2010, 08:04 PM #11
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
I found soduku 3 but it is confusing because I cannot find the 1 and 2.
Thanks for your help and I will continue to hunt for the 1 and 2 articles.
- 04-09-2010, 08:10 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
- 04-10-2010, 10:16 PM #13
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
trying to create a randomized soduku puzzle solver
I have been trying to solve the soduku puzzle with the checker and I do not know what is wrong but it is not fallowing the rules of soduku.
package SUDUKO;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Suduko
{
private JTextField[][]cells;
int[] variable1r = new int[9];
int[] variable2r = new int[9];
int[] variable3r = new int[9];
int[] variable4r = new int[9];
int[] variable5r = new int[9];
int[] variable6r = new int[9];
int[] variable7r = new int[9];
int[] variable8r = new int[9];
int[] variable9r = new int[9];
public static void main( String[]args )
{
try
{
Suduko suduko = new Suduko();
suduko.init();
} catch (Exception e)
{
e.printStackTrace();
}
}
private void init()
{
cells = new JTextField[9][9];
JPanel panel = new JPanel();
panel.setLayout( new GridLayout( 9, 9, 5, 5 ) );
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
cells[i][j] = new JTextField();
panel.add( cells[i][j] );
}
}
JFrame frame = new JFrame();
frame.add( panel );
frame.setSize( 600, 600 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );
initGame();
}
private void initGame()
{
Random r = new Random( 9 );
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
int randomValue = r.nextInt( 9 ) + 1;
if (IsValid( i, j, randomValue ))
{
cells[i][j].setText( String.valueOf( randomValue ) );
} else
{
j--;
}
}
}
}
private boolean IsValid( int row, int column, int value )
{
System.out.println( "Checking " + row + " " + column + " for " + value );
{
{
return true;
}
}}
{
for (int i=1; i<9; i--);//want to have it test the rows
{
for(int j=2; j<9; j--);//want to be column tester
}
{
for(int k=3; k<9; k--);//want to have 3x3 tester
}
//want for loop to test till finished but need it to be for three things rows but how? Is this correct
// I am trying to remember everything about what I learned in Java so far plus what you have taught me as well.
}
//problem cannot figure out how to create a 3x3 check.
//want for loop to repeat till finished but need it to be for three testers rows amd colums and 3x3 but how?
//Would like this for loop to check columns of the system.
// would if statements be better for checking columns and rows?
//would like to create a 3x3 check with using int k but do not know what method to use.
{
boolean[][] newvarible1r= new boolean[9][9];
boolean[][] newvarible2r= new boolean[9][9];
boolean[][] newvarible3r= new boolean[9][9];
boolean[][] newvarible4r= new boolean[9][9];
boolean[][] newvarible5r= new boolean[9][9];
boolean[][] newvarible6r= new boolean[9][9];
boolean[][] newvarible7r= new boolean[9][9];
boolean[][] newvarible8r= new boolean[9][9];
boolean[][] newvarible9r= new boolean[9][9];
//this should be my checker for the rows.
}}
end of Another soduku puzzle that I am working with I have to create a puzzle where I can enter in the numbers as well and then have Java solve the puzzle.
here is that code
package soduku;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class soduku
{
public static void main( String args[] )
{
for (int i=0, j=0;i<9 && j<9;i++,j++)
{
}
}
{
JFrame frame = new JFrame();
frame.setSize( 600, 600 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true);}}
end of I of course am new to java and have been trying deligently with reading what other people have written with anything to do with Soduku I am having problems with assigning numbers to a row and colum on the soduku puzzle. I also have been unsuccessful with having this puzzle display in a window with the Jpanel and the Jframe.
Thank you for your help.
- 04-13-2010, 02:30 AM #14
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
Need help with solving the random puzzle soduku
I am almost finished on my Soduku puzzle I just have one more step this has been a big pain because of it being a GUI soduku. I need to put in a solver for the puzzle and have attempted to set up a true statement for the puzzle to solve but I have been unable to do so.
package SUDUKO;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Suduko {
private JTextField[][] cells;
int[] variable1r = new int[9];
int[] variable2r = new int[9];
int[] variable3r = new int[9];
int[] variable4r = new int[9];
int[] variable5r = new int[9];
int[] variable6r = new int[9];
int[] variable7r = new int[9];
int[] variable8r = new int[9];
int[] variable9r = new int[9];
public static void main(String[] args) {
try {
Suduko suduko = new Suduko();
suduko.init();
} catch (Exception e) {
e.printStackTrace();
}
}
private void init() {
cells = new JTextField[9][9];
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(9, 9, 5, 5));
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
int blockIndex = ((int) i / 3) + ((int) j / 3);
Color c = Color.white;
if (blockIndex % 2 == 0) {
c = Color.lightGray;
}
cells[i][j] = new JTextField();
cells[i][j].setBackground(c);
panel.add(cells[i][j]);
}
}
JFrame frame = new JFrame();
frame.add(panel);
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
initGame();
}
private void initGame() {
Random r = new Random(9);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
int randomValue = r.nextInt(9) + 1;
if (IsValid(i, j, randomValue)) {
cells[i][j].setText(String.valueOf(randomValue));
} else {
//j--;
}
}
}
}
private boolean IsValid(int row, int column, int value) {
System.out.println("Checking " + row + " " + column + " for " + value);
// Checking row
for (int i = 0; i < 9; i++) {
if (cells[row][i].getText().equals(String.valueOf(value))) {
return false;
}
}
// Checking columns
for (int i = 0; i < 9; i++) {
if (cells[i][column].getText().equals(String.valueOf(value))) {
return false;
}
}
// Check 3x3 blocks
int rowStartIndex = ((int) (row / 3)) * 3;
int colStartIndex = ((int) (column / 3)) * 3;
for (int i = rowStartIndex; i < rowStartIndex + 3; i++) {
for (int j = colStartIndex; j < colStartIndex + 3; j++) {
if (cells[i][j].getText().equals(String.valueOf(value))) {
return false;
}
}
}
return true;
}
}
{
if (int i=0; i < 9);
if (int j=0; j<9);
return true;
}// trying to create the solver part wondering if I am on the right track.
Similar Threads
-
How to solve the Eight Queens puzzle without recursion?
By kiregad in forum New To JavaReplies: 2Last Post: 03-27-2010, 03:30 AM -
N-Puzzle Help!
By evan42781 in forum New To JavaReplies: 12Last Post: 04-29-2009, 11:34 PM -
Need help with Trees...(8-puzzle)
By ventrue in forum New To JavaReplies: 2Last Post: 03-23-2009, 11:04 PM -
8-Square puzzle loop
By SapphireSpark in forum New To JavaReplies: 7Last Post: 12-04-2008, 07:21 PM -
Java Drawing PUZZLE
By Cyorxamp in forum AWT / SwingReplies: 3Last Post: 06-09-2008, 10:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks