I'm a new programmer. After making some programs through my self learning in java, now I'm interested to make a sudoku in that language. Can anyone explain me how to control the 9x9 grids and randomising the numbers each time the program runs? Thanks in advance. I have just written a code to take the input of only integers myself. Here's the code :
import javax.swing.UIManager.LookAndFeelInfo;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.border.Border;
import javax.swing.*;
public class GUI_project extends JFrame {
private JMenuBar menuBar;
private JTextField textfield1;
String k;
public GUI_project(){
this.setTitle("GUI_project");
this.setSize(500,400);
this.setJMenuBar(menuBar);
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(500,400));
contentPane.setBackground(new Color(192,192,192));
textfield1 = new JTextField();
textfield1.setBounds(105,109,148,108);
textfield1.setBackground(new Color(255,255,255));
textfield1.setForeground(new Color(0,0,0));
textfield1.setEnabled(true);
textfield1.setFont(new Font("sansserif",0,12));
textfield1.setText("");
textfield1.setVisible(true);
textfield1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
chkdl();
}
});
textfield1.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt){
chkdl();
}
public void keyTyped(KeyEvent evt){
chkdl();
}
public void keyReleased(KeyEvent evt){
chkdl();
}
});
contentPane.add(textfield1);
this.add(contentPane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.pack();
this.setVisible(true);
}
private void chkdl () {
//TODO
String f = textfield1.getText();
try{
int g = Integer.parseInt(f);
if(g>0 && g<10){
textfield1.setText(Integer.toString(g));
k=Integer.toString(g);
textfield1.setEditable(true);
}
else{
textfield1.setText(k);
textfield1.setEditable(true);
}
}
catch(NumberFormatException ex){
if(f.equals("")){
textfield1.setText("");
k="";
}
else{
textfield1.setText(k);
textfield1.setEditable(true);}
}
}
public static void main(String[] args){
System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GUI_project();
}
});
}
}
Thanks for your support. I have progressed that far.
import java.util.*;
class rnd{
public static void main(String [] args){
int [][] game = new int[10][10];
for(int o = 0;o<9;o++){
for(int o2 = 0;o2<9;o2++){
game[o][o2]=0;
}
}
int a = 0;
Random r = new Random();
int row = 1;
int clm = 1;
int cnt = 0;
int chk=0;
int fill = 0;
while(row<9){
while(clm<9){
fill = r.nextInt(9)+1;
chk = 0;
if(clm==1 || clm==4 || clm==7){
for(int cnc = clm;cnc!=(clm+2);cnc++){
chk=retchk(row,game,cnc,fill);
}
}
else if(clm==2 || clm==5 || clm==8){
for(int cnc = (clm-1);cnc!=(clm+1);cnc++){
chk=retchk(row,game,cnc,fill);
}
}
else if(clm==3||clm==6||clm==9){
for(int cnc = (clm-2);cnc!=clm;cnc++){
chk=retchk(row,game,cnc,fill);}
}
for(int rw = 0;rw!=9;rw++){
if(game[row][rw]==fill){
chk=1;
cnt++;
break;
}
}
if(chk==0){
for(int c = 0;c!=9;c++){
if(game[c][clm]==fill){
chk=1;
cnt++;
break;
}
}
if(chk==0){
int qa = 0;
try{
qa = (game[row][clm]=fill);}
catch(ArrayIndexOutOfBoundsException ml){
System.out.print("\n"+row+"\n"+clm);
}
System.out.print(qa+" ");
clm++;
}
if(clm==9){
clm=0;
System.out.print(" Row "+(row)+" filled up\n");
row++;
}
}
}
}
}
public static int retchk(int row,int [][] game,int cnc,int fill){
int chk = 0;
if(row==1 || row==4 || row==7){
for(int cnr = row;cnr!=(row+2);cnr++){
if(game[cnr][cnc]==fill){
chk=1;
break;
}
}
}
else if(row==2 || row==5 || row==8){
for(int cnr = (row-1);cnr!=(row+1);cnr++){
if(game[cnr][cnc]==fill){
chk=1;
break;
}
}
}
else if(row==3 || row==6 || row==9){
for(int cnr = (row-2);cnr!=row;cnr++){
if(game[cnr][cnc]==fill){
chk=1;
break;
}
}
}
return chk;
}
}
But two problems are there. First of all, as you can see, the array is always giving an arrarindexoutofboundsexception that i have to catch. And the algorithm i'm tring to work in, is not properly working. And the third, the code for the 3x3 grids is not working. Have any idea?