Friday, September 24, 2010

Sudoku

//By Er. Minal Bansal
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Logic {

//to start with a random index y of array 1 to 9
Random ranobj=new Random();
int y=ranobj.nextInt(9);

// this is final array
int arr[][]=new int[9][9];
int myarray[]=new int[9];

// i,j -> indices of array arr
//k -> index of arr upto which elements are not added
int i=0,j=0,k=8;

//flag1 is false if this element cannot be added
boolean flag1=true;

//elements to be added
int next[]={1,2,3,4,5,6,7,8,9};

Logic() {
firstRow();
fill();
}

public void firstRow() {

do {

//for every new element
flag1=true;
int x=next[y];

//check in row
if(flag1) {
for(int jprev=0;jprevif(arr[i][jprev]==x) { flag1=false; }
}
}

//if element can be added
if(flag1) {

myarray[j]=x;
j++;

//shift forward elements
for(int t=y;tnext[t]=next[t+1];
}

k--;
}

//take next element from this location
y=y+j;
if(y>k) { y=0; }

//repeat until all rows are not covered
}while(j<9);

} // firstRow

public void fill() {

int p,count,m,n=0;
//arrangement
int loc[][]={ {0,1,2,3,4,5,6,7,8},{3,4,5,6,7,8,0,1,2},{6,7,8,0,1,2,3,4,5} };

for(i=0,m=0;i<9;i++,m++) {

if(m%3==0) { m=0; }
if(i>2) { n=1; }
if(i>5) { n=2; }

for(j=0;j<9;j++,n++) {

if(n==9) { n=0; }

arr[i][j]=myarray[loc[m][n]];
} // for n

} // for m

} // fill

} //logic class

public class Design extends Logic implements KeyListener,MouseListener,Runnable
{
private JFrame myframe;
private JTextField txt[][]=new JTextField[9][9];
private JPanel pcenter,pnorth,p[]=new JPanel[9];
private JMenuBar menubar;
private JMenu game,help;
private JMenuItem newgame,restart,check,submit,exit,what,option,myself;
private int mychoice=3;
private boolean myflag=false;
private int row=0,column=0;
private java.util.Timer timer = new java.util.Timer();
private JLabel l1=new JLabel(),l2 = new JLabel(),l3 = new JLabel(),l4 = new JLabel(),l5 = new JLabel();
private static Thread t;

Design()
{
initialize();
}

private void initialize()
{

for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
txt[i][j]=new JTextField();
txt[i][j].setHorizontalAlignment(JTextField.CENTER);
txt[i][j].setBackground(Color.yellow);
txt[i][j].setFont(new Font("Verdana",Font.BOLD,25));
txt[i][j].setText(String.valueOf(arr[i][j]));
txt[i][j].setEditable(false);
txt[i][j].addKeyListener(this);
txt[i][j].addMouseListener(this);
}
}

for(int i=0;i<9;i++)
{
p[i]=new JPanel();
p[i].setBackground(Color.green);
p[i].setLayout(new GridLayout(3,3,2,2));
}

for(int m=0;m<9;m++)
{

int i=0,x=0,y=0,z=0;

if(m>0) { x=m; }
if(m>2) { x=m-3; }
if(m>5) { x=m-6; }
y=x*3;

if(m<9) { i=6; }
if(m<6) { i=3; }
if(m<3) { i=0; }
z=i+3;

for(;i{
for(int j=y;j{
p[m].add(txt[i][j]);
}
}
}

newgame=new JMenuItem("New game");
newgame.addActionListener(new MyMenuHandler1());
newgame.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,KeyEvent.CTRL_MASK));

restart=new JMenuItem("Restart this game");
restart.addActionListener(new MyMenuHandler2());
restart.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,KeyEvent.CTRL_MASK));

check=new JMenuItem("Check");
check.addActionListener(new MyMenuHandler3());
check.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,KeyEvent.CTRL_MASK));

submit=new JMenuItem("Submit");
submit.addActionListener(new MyMenuHandler4());
submit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,KeyEvent.CTRL_MASK));

exit=new JMenuItem("Exit");
exit.addActionListener(new MyMenuHandler5());
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,KeyEvent.CTRL_MASK));

game=new JMenu("Game");
game.setMnemonic('g');
game.add(newgame);
game.add(restart);
game.add(new JSeparator());
game.add(check);
game.add(submit);
game.add(new JSeparator());
game.add(exit);

what=new JMenuItem("What is it?");
what.addActionListener(new MyMenuHandler6());
what.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,KeyEvent.CTRL_MASK));

option=new JMenuItem("Options...");
option.addActionListener(new MyMenuHandler7());
option.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,KeyEvent.CTRL_MASK));

myself=new JMenuItem("About me...");
myself.addActionListener(new MyMenuHandler8());
myself.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M,KeyEvent.CTRL_MASK));

help=new JMenu("Help");
help.setMnemonic('h');
help.add(what);
help.add(option);
help.add(new JSeparator());
help.add(myself);

menubar=new JMenuBar();
menubar.add(game);
menubar.add(help);

pcenter=new JPanel();
pcenter.setBackground(Color.black);
pcenter.setVisible(false);
pcenter.setLayout(new GridLayout(3,3,5,5));
for(int i=0;i<9;i++)
{
pcenter.add(p[i]);
}

l1.setForeground(Color.white);
l2.setForeground(Color.white);
l3.setForeground(Color.white);
l4.setForeground(Color.white);
l5.setForeground(Color.white);

pnorth=new JPanel();
pnorth.setBackground(Color.black);
pnorth.add(l1);
pnorth.add(l4);
pnorth.add(l2);
pnorth.add(l5);
pnorth.add(l3);
pnorth.setVisible(false);

myframe=new JFrame("Sudoku \t \t \t \t ~~ created by Minal ~~ \t \t \t \t");
myframe.setVisible(true);
myframe.setSize(500,500);
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
myframe.setLocation(d.width/5,d.height/5);
myframe.setJMenuBar(menubar);
myframe.setLayout(new BorderLayout());
myframe.add(pcenter,BorderLayout.CENTER);
myframe.add(pnorth,BorderLayout.NORTH);
myframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
myframe.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
if(JOptionPane.showConfirmDialog(myframe,"Do you want to exit?", "Confirming Exit",JOptionPane.YES_NO_OPTION)==0)
{
System.exit(0);
}
}
});

} // initialize

public void keyPressed(KeyEvent ke)
{
int key=ke.getKeyCode();
int a=row,b=column;

if(key==KeyEvent.VK_RIGHT)
{
b++;
if(b==9) { a++;
if(a==9) { a=0; }
b=0;
}
txt[a][b].requestFocus(true);
column=b;
row=a;
}

if(key==KeyEvent.VK_DOWN)
{
a++;
if(a==9) { a=0; }
txt[a][b].requestFocus(true);
row=a;
}

if(key==KeyEvent.VK_LEFT)
{
b--;
if(b<0) { a--;
if(a<0) { a=8; }
b=8;
}
txt[a][b].requestFocus(true);
column=b;
row=a;
}

if(key==KeyEvent.VK_UP)
{
a--;
if(a<0) { a=8; }
txt[a][b].requestFocus(true);
row=a;
}

} //key

public void keyReleased(KeyEvent ke)
{
int x=Integer.valueOf(ke.getKeyChar());
x=x-48;
if(txt[row][column].isEditable())
{
if(x>=1 && x<=9) { txt[row][column].setText(String.valueOf(x)); }
else if(txt[row][column].getText().length()==1)
{
try { int y=Integer.parseInt(txt[row][column].getText()); }
catch(NumberFormatException e){ txt[row][column].setText(""); }
}
else if(txt[row][column].getText().length()>1) { txt[row][column].setText(txt[row][column].getText().substring(0,txt[row][column].getText().length()-1)); }
}
}

public void keyTyped(KeyEvent ke) {}

public void mouseClicked(MouseEvent me)
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(txt[i][j].isFocusOwner())
{
row=i;
column=j;
}
}
}
}

public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
public void mousePressed(MouseEvent me) {}
public void mouseReleased(MouseEvent me) {}

public void run()
{
l4.setText(" : ");
l5.setText(" : ");
l1.setText("00");
l2.setText("00");
l3.setText("00");
Repeat mytimer = new Repeat();
timer.schedule(mytimer,1000,1000);
} //run

class Repeat extends TimerTask
{
public void run()
{
int x = Integer.parseInt(l3.getText())+1;
int y = Integer.parseInt(l2.getText());
int z = Integer.parseInt(l1.getText());
if(x==60) { y=y+1; x=0; }
if(z==60) { z=z+1; y=0; }
String a = String.valueOf(x) , b = String.valueOf(y) , c = String.valueOf(z);
if(a.length()==1) { a = "0"+a; }
if(b.length()==1) { b = "0"+b; }
if(c.length()==1) { c = "0"+c; }
l3.setText(a);
l2.setText(b);
l1.setText(c);
}
} //repeat class

class MyMenuHandler1 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if(myflag)
{
if(JOptionPane.showConfirmDialog(myframe,"Start a new game?","",JOptionPane.YES_NO_OPTION)==0)
{

myframe.setVisible(false);
initialize();
new Difficult();
}
}
else
{
new Difficult();
}
}

class Difficult implements ActionListener
{

JFrame frame1;
JPanel pcenter1;
JLabel label;
JButton ok;
JRadioButton easy,average,difficult,hard;
ButtonGroup bg;

Difficult()
{

label=new JLabel("Choose your difficulty level :");
label.setFont(new Font("Georgia",Font.ITALIC,18));

easy=new JRadioButton(" Beginner ",false);
average=new JRadioButton(" Learner ",false);
hard=new JRadioButton(" Champion ",false);
difficult=new JRadioButton(" Player ",true);

easy.setFont(new Font("Gill Sans MT",Font.BOLD,16));
average.setFont(new Font("Gill Sans MT",Font.BOLD,16));
difficult.setFont(new Font("Gill Sans MT",Font.BOLD,16));
hard.setFont(new Font("Gill Sans MT",Font.BOLD,16));

easy.setBackground(Color.green);
average.setBackground(Color.green);
difficult.setBackground(Color.green);
hard.setBackground(Color.green);

bg=new ButtonGroup();
bg.add(easy);
bg.add(average);
bg.add(difficult);
bg.add(hard);

ok=new JButton("OK");
ok.addActionListener(this);

pcenter1=new JPanel();
pcenter1.setBackground(Color.green);
pcenter1.add(label);
pcenter1.add(easy);
pcenter1.add(new JLabel(" "));
pcenter1.add(new JLabel(" "));
pcenter1.add(average);
pcenter1.add(new JLabel(" "));
pcenter1.add(new JLabel(" "));
pcenter1.add(difficult);
pcenter1.add(new JLabel(" "));
pcenter1.add(new JLabel(" "));
pcenter1.add(hard);
pcenter1.add(new JLabel(" "));
pcenter1.add(new JLabel(" "));
pcenter1.add(ok);

frame1=new JFrame("Levels of Difficulty");
frame1.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame1.setVisible(true);
frame1.setSize(350,350);
frame1.add(pcenter1);
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
frame1.setLocation(d.width/3,d.height/3);

} //cons

public void actionPerformed(ActionEvent ae)
{
if(easy.isSelected())
{
mychoice=6;
}
if(average.isSelected())
{
mychoice=5;
}
if(difficult.isSelected())
{
mychoice=4;
}
if(hard.isSelected())
{
mychoice=3;
}
frame1.setVisible(false);
pcenter.setVisible(true);
hide(mychoice);
pnorth.setVisible(true);
l1.setText("00");
l2.setText("00");
l3.setText("00");
} //action
} //difficult
} // handler

class MyMenuHandler2 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if(pcenter.isVisible())
{
if(JOptionPane.showConfirmDialog(myframe,"Do you want to restart this game?","",JOptionPane.YES_NO_OPTION)==0)
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(txt[i][j].isEditable())
{
txt[i][j].setText(null);
txt[i][j].setBackground(Color.pink);
} //if
} //for j
} //for i
l1.setText("00");
l2.setText("00");
l3.setText("00");
}
}
}
}

class MyMenuHandler3 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
int number=0;
boolean flag=true;
for(int i=0;i<9 && flag;i++)
{
for(int j=0;j<9 && flag;j++)
{
if(txt[i][j].getText().length()==1 && txt[i][j].isEditable())
{
number=Integer.parseInt(txt[i][j].getText());
if(txt[i][j].getBackground()==Color.white)
{
txt[i][j].setBackground(Color.pink);
}
if(number!=arr[i][j])
{
txt[i][j].setBackground(Color.white);
flag=false;
}
}
} // for j
} // for i
} // action
} // handler

class MyMenuHandler4 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{

int i,j=0;
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
if(txt[i][j].isEditable())
{
if(txt[i][j].getText().length()<1)
{
if(JOptionPane.showConfirmDialog(myframe,"Sudoku is not Correct...Continue?","",JOptionPane.YES_NO_OPTION)==0)
{
return;
}
else
{

System.exit(0);
}
} //if txt<1
} //if txt editable
} //for j
} //for i
if(i==9 && j==9)
{

callToCheck();
}

} // action

void callToCheck()
{
boolean checkflag=true;
int sum=0;
for(int i=0;i<9 && checkflag;i++)
{
for(int j=0;j<9 && checkflag;j++)
{
sum=sum+Integer.parseInt(txt[i][j].getText());
}
if(sum==45)
{
sum=0;
}
else
{
checkflag=false;
}
}

if(checkflag)
{
int sum1=0;
for(int l=0;l<9 && checkflag;)
{
for(int k=0;k<9 && checkflag;)
{
for(int i=k;i{
for(int j=l;j{
sum1=sum1+Integer.parseInt(txt[i][j].getText());
} //for j
} //for i
if(sum1==45)
{
sum1=0;
}
else
{
checkflag=false;
}
k=k+3;
} //for k
l=l+3;
} //for l
}

if(checkflag)
{
timer1();
pcenter.setVisible(false);
return;
}
else
{
if(JOptionPane.showConfirmDialog(myframe,"Sudoku is not Correct...Continue?","",JOptionPane.YES_NO_OPTION)==0)
{
return;
}
else
{
System.exit(0);
}
}
} //callToCheck
} //handler

class MyMenuHandler5 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if(JOptionPane.showConfirmDialog(myframe,"Do you want to exit?", "Confirming Exit",JOptionPane.YES_NO_OPTION)==0)
{

System.exit(0);
}
}
} // handler

class MyMenuHandler6 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
new Info1();
} // action
} // handler

class MyMenuHandler7 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
new Info2();
} // action
} // handler

class MyMenuHandler8 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
new Info3();
} // action
} // handler


void hide(int num)
{
int count=0;
java.util.Random obj=new java.util.Random();
for(int i=0;i<9;i++)
{
for(int j=obj.nextInt(num);j<9;)
{
txt[i][j].setBackground(Color.pink);
txt[i][j].setText(null);
txt[i][j].setEditable(true);

if(count==0)
{
row=i;
column=j;
}
count++;
j=j+obj.nextInt(num);
}
}
myflag=true;
txt[row][column].requestFocus(true);

} // hide

private void timer1()
{
int x = Integer.parseInt(l3.getText());
int y = Integer.parseInt(l2.getText());
int z = Integer.parseInt(l1.getText());
if(z>0) { JOptionPane.showMessageDialog
(myframe,"Congratulations! It's completed but you took very long time.","",JOptionPane.PLAIN_MESSAGE);}
else if(mychoice!=3 && y<10) { JOptionPane.showMessageDialog
(myframe,"Congratulations! It's completed.Try next level.","",JOptionPane.PLAIN_MESSAGE);}
else if(mychoice==3 && y<10) { JOptionPane.showMessageDialog
(myframe,"Congratulations! It's completed.You are a master of Sudoku.","",JOptionPane.PLAIN_MESSAGE);}
else { JOptionPane.showMessageDialog
(myframe,"Congratulations! It's completed.","",JOptionPane.PLAIN_MESSAGE);}
pnorth.setVisible(false);
}

public static void main(String args[])
{
Design objDesign=new Design();
t = new Thread(objDesign);
t.start();
}

} //class

class Info1
{
JFrame f1;
JPanel p1;
JTextArea ta1;
JLabel lb1;

Info1()
{
lb1=new JLabel("Sudoku");
lb1.setForeground(Color.red);
lb1.setFont(new Font("Script MT Bold",Font.BOLD,30));

ta1=new JTextArea("\n\n\n\t The aim of the puzzle is"+
"\n\t to fill each cell of the grid "+
"\n\t with digits from 1 to 9."+
"\n\t Each row , column and "+
"\n\t subgrid must contain only"+
"\n\t one instance of each digit."+
"\n\t Finish the puzzle to win.");
ta1.setFont(new Font("Script MT Bold",Font.PLAIN,22));
ta1.setBackground(Color.yellow);
ta1.setEditable(false);

p1=new JPanel();
p1.setBackground(Color.yellow);
p1.setLayout(new BorderLayout());
p1.add(lb1,BorderLayout.NORTH);
p1.add(ta1,BorderLayout.CENTER);

f1=new JFrame("What is this ?");
f1.setVisible(true);
f1.setSize(450,450);
f1.add(p1);
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
f1.setLocation(d.width/4,d.height/4);
} //cons
} // info class

class Info2
{
JFrame f2;
JPanel p2;
JTextArea ta2;
JLabel lb2;

Info2()
{
lb2=new JLabel("You are provided with following options in the menubar :");
lb2.setForeground(Color.red);
lb2.setFont(new Font("Script MT Bold",Font.BOLD,22));

ta2=new JTextArea("\n\n\n New Game : To start a new sudoku. It will close the current puzzle."+
"\n Restart this game : To restart the current game. It will undo your puzzle."+
"\n Check : Shows the cell whose value is incorrect. Only 1 cell is shown once."+
"\n Submit : After finishing, submit your puzzle. Asks if it is incorrect."+
"\n Exit : To exit sudoku.");
ta2.setFont(new Font("Script MT Bold",Font.PLAIN,22));
ta2.setBackground(Color.yellow);
ta2.setEditable(false);

p2=new JPanel();
p2.setBackground(Color.yellow);
p2.setLayout(new BorderLayout());
p2.add(lb2,BorderLayout.NORTH);
p2.add(ta2,BorderLayout.CENTER);

f2=new JFrame("What is this ?");
f2.setVisible(true);
f2.setSize(660,450);
f2.add(p2);
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
f2.setLocation(d.width/4,d.height/4);
} //cons
} // info class

class Info3
{
Label three;
JTextArea ta3,me;
JTextField tf3;
JPanel p3;
JFrame f3;

Info3()
{
three=new Label(" \t Developed by :");
three.setForeground(Color.red);
three.setAlignment(Label.LEFT);
three.setFont(new Font("Lucida Sans Typewriter",Font.BOLD,16));

me=new JTextArea("Er. Minal Bansal ");
me.setEditable(false);
me.setBackground(Color.orange);
me.setFont(new Font("Pristina",Font.BOLD,30));
me.setForeground(Color.blue);

ta3=new JTextArea("\n \t Hey friends ! "+
"\n\t Hope you like my creation."+
"\n\t Enjoy !"+
"\n\t You can send me email on \t");
ta3.setEditable(false);
ta3.setBackground(Color.orange);
ta3.setFont(new Font("Papyrus",Font.BOLD,18));

tf3=new JTextField("minalbansal.hr@gmail.com");
tf3.setEditable(false);
tf3.setBackground(Color.orange);
tf3.setForeground(Color.red);
tf3.setFont(new Font("Rage Italic",Font.PLAIN,26));

p3=new JPanel();
p3.setBackground(Color.orange);
p3.add(three);
p3.add(me);
p3.add(ta3);
p3.add(tf3);

f3=new JFrame("About the developer...");
f3.setVisible(true);
f3.setSize(500,400);
f3.add(p3);
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
f3.setLocation(d.width/4,d.height/4);

} //cons
} //class

Download

0 comments:

Post a Comment