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:(
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.
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.
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.