Friday 14 June 2013

program in java or Game in java - Sudoku with awt

/*Simple java game and easy to understand*/
/**
 * @(#)Sudoku.java
 *
 *
 * @anant mahale
 * @version 1.00 2013/6/2
 */

import java.awt.*;
import java.awt.event.*;
class Game extends Frame implements ActionListener
{
    private TextField txt_a1,txt_a2,txt_a3,txt_a4,txt_a5,txt_b1,txt_b2,txt_b3,txt_b4,txt_b5,txt_b6,txt_b7,txt_c1,txt_c2,txt_c3,txt_c4,txt_c5,txt_d1,txt_d2,txt_d3,txt_d4,txt_d5,txt_d6,txt_e1,txt_e2,txt_e3,txt_e4,txt_f1,txt_f2,txt_f3,txt_f4,txt_f5,txt_f6,txt_g1,txt_g2,txt_h1,txt_h2,txt_h3,txt_h4,txt_h5,txt_h6,txt_h7,txt_i1,txt_i2,txt_i3,txt_i4,txt_i5;
    private Label lbl_a1,lbl_a2,lbl_a3,lbl_a4,lbl_b1,lbl_b2,lbl_c1,lbl_c2,lbl_c3,lbl_c4,lbl_d1,lbl_d2,lbl_d3,lbl_e1,lbl_e2,lbl_e3,lbl_e4,lbl_e5,lbl_f1,lbl_f2,lbl_f3,lbl_g1,lbl_g2,lbl_g3,lbl_g4,lbl_g5,lbl_g6,lbl_g7,lbl_h1,lbl_h2,lbl_i1,lbl_i2,lbl_i3,lbl_i4;
    private Button btn_check;
    public Game()
    {
        setLayout(null);
        setTitle("Sudoku");
        setLocation(100,100);
        setSize(450,500);
        setResizable(false);
       
        txt_a1=new TextField();
        txt_a1.setForeground(Color.red);
        txt_a1.setBounds(50,50,30,30);
        add(txt_a1);
       
        txt_a2=new TextField();
        txt_a2.setForeground(Color.red);
        txt_a2.setBounds(90,50,30,30);
        add(txt_a2);
       
        txt_a3=new TextField();
        txt_a3.setForeground(Color.red);
        txt_a3.setBounds(130,50,30,30);
        add(txt_a3);
       
        txt_a4=new TextField();
        txt_a4.setForeground(Color.red);
        txt_a4.setBounds(50,90,30,30);
        add(txt_a4);
       
        txt_a5=new TextField();
        txt_a5.setForeground(Color.red);
        txt_a5.setBounds(90,90,30,30);
        add(txt_a5);
       
        lbl_a1=new Label("1");
        lbl_a1.setBounds(130,90,30,30);
        add(lbl_a1);
       
        lbl_a2=new Label("8");
        lbl_a2.setBounds(50,130,30,30);
        add(lbl_a2);
       
        lbl_a3=new Label("2");
        lbl_a3.setBounds(90,130,30,30);
        add(lbl_a3);
       
        lbl_a4=new Label("4");
        lbl_a4.setBounds(130,130,30,30);
        add(lbl_a4);
       
        lbl_b1=new Label("9");
        lbl_b1.setBounds(170,50,30,30);
        add(lbl_b1);
       
        txt_b1=new TextField();
        txt_b1.setForeground(Color.red);
        txt_b1.setBounds(210,50,30,30);
        add(txt_b1);
       
        txt_b2=new TextField();
        txt_b2.setForeground(Color.red);
        txt_b2.setBounds(250,50,30,30);
        add(txt_b2);
       
        txt_b3=new TextField();
        txt_b3.setForeground(Color.red);
        txt_b3.setBounds(170,90,30,30);
        add(txt_b3);
       
        txt_b4=new TextField();
        txt_b4.setForeground(Color.red);
        txt_b4.setBounds(210,90,30,30);
        add(txt_b4);
       
        txt_b5=new TextField();
        txt_b5.setForeground(Color.red);
        txt_b5.setBounds(250,90,30,30);
        add(txt_b5);
       
        txt_b6=new TextField();
        txt_b6.setForeground(Color.red);
        txt_b6.setBounds(170,130,30,30);
        add(txt_b6);
       
        lbl_b2=new Label("6");
        lbl_b2.setBounds(210,130,30,30);
        add(lbl_b2);
       
        txt_b7=new TextField();
        txt_b7.setForeground(Color.red);
        txt_b7.setBounds(250,130,30,30);
        add(txt_b7);
       
        txt_c1=new TextField();
        txt_c1.setForeground(Color.red);
        txt_c1.setBounds(290,50,30,30);
        add(txt_c1);
       
        txt_c2=new TextField();
        txt_c2.setForeground(Color.red);
        txt_c2.setBounds(330,50,30,30);
        add(txt_c2);
       
        txt_c3=new TextField();
        txt_c3.setForeground(Color.red);
        txt_c3.setBounds(370,50,30,30);
        add(txt_c3);
       
        lbl_c1=new Label("5");
        lbl_c1.setBounds(290,90,30,30);
        add(lbl_c1);
       
        lbl_c2=new Label("4");
        lbl_c2.setBounds(330,90,30,30);
        add(lbl_c2);
       
        lbl_c3=new Label("6");
        lbl_c3.setBounds(370,90,30,30);
        add(lbl_c3);
       
        lbl_c4=new Label("1");
        lbl_c4.setBounds(290,130,30,30);
        add(lbl_c4);
       
        txt_c4=new TextField();
        txt_c4.setForeground(Color.red);
        txt_c4.setBounds(330,130,30,30);
        add(txt_c4);
       
        txt_c5=new TextField();
        txt_c5.setForeground(Color.red);
        txt_c5.setBounds(370,130,30,30);
        add(txt_c5);
       
        txt_d1=new TextField();
        txt_d1.setForeground(Color.red);
        txt_d1.setBounds(50,170,30,30);
        add(txt_d1);
       
        txt_d2=new TextField();
        txt_d2.setForeground(Color.red);
        txt_d2.setBounds(90,170,30,30);
        add(txt_d2);
       
        txt_d3=new TextField();
        txt_d3.setForeground(Color.red);
        txt_d3.setBounds(130,170,30,30);
        add(txt_d3);
       
        txt_d4=new TextField();
        txt_d4.setForeground(Color.red);
        txt_d4.setBounds(50,210,30,30);
        add(txt_d4);
       
        txt_d5=new TextField();
        txt_d5.setForeground(Color.red);
        txt_d5.setBounds(90,210,30,30);
        add(txt_d5);
       
        lbl_d1=new Label("2");
        lbl_d1.setBounds(130,210,30,30);
        add(lbl_d1);
       
        lbl_d2=new Label("3");
        lbl_d2.setBounds(50,250,30,30);
        add(lbl_d2);
       
        txt_d6=new TextField();
        txt_d6.setForeground(Color.red);
        txt_d6.setBounds(90,250,30,30);
        add(txt_d6);
       
        lbl_d3=new Label("9");
        lbl_d3.setBounds(130,250,30,30);
        add(lbl_d3);
       
        lbl_e1=new Label("5");
        lbl_e1.setBounds(170,170,30,30);
        add(lbl_e1);
       
        lbl_e2=new Label("2");
        lbl_e2.setBounds(210,170,30,30);
        add(lbl_e2);
       
        txt_e1=new TextField();
        txt_e1.setForeground(Color.red);
        txt_e1.setBounds(250,170,30,30);
        add(txt_e1);
       
        txt_e2=new TextField();
        txt_e2.setForeground(Color.red);
        txt_e2.setBounds(170,210,30,30);
        add(txt_e2);
       
        lbl_e3=new Label("4");
        lbl_e3.setBounds(210,210,30,30);
        add(lbl_e3);
       
        txt_e3=new TextField();
        txt_e3.setForeground(Color.red);
        txt_e3.setBounds(250,210,30,30);
        add(txt_e3);
       
        txt_e4=new TextField();
        txt_e4.setForeground(Color.red);
        txt_e4.setBounds(170,250,30,30);
        add(txt_e4);
       
        lbl_e4=new Label("8");
        lbl_e4.setBounds(210,250,30,30);
        add(lbl_e4);
       
        lbl_e5=new Label("7");
        lbl_e5.setBounds(250,250,30,30);
        add(lbl_e5);
       
        lbl_f1=new Label("3");
        lbl_f1.setBounds(290,170,30,30);
        add(lbl_f1);
       
        txt_f1=new TextField();
        txt_f1.setForeground(Color.red);
        txt_f1.setBounds(330,170,30,30);
        add(txt_f1);
       
        lbl_f2=new Label("8");
        lbl_f2.setBounds(370,170,30,30);
        add(lbl_f2);
       
        lbl_f3=new Label("6");
        lbl_f3.setBounds(290,210,30,30);
        add(lbl_f3);
       
        txt_f2=new TextField();
        txt_f2.setForeground(Color.red);
        txt_f2.setBounds(330,210,30,30);
        add(txt_f2);
       
        txt_f3=new TextField();
        txt_f3.setForeground(Color.red);
        txt_f3.setBounds(370,210,30,30);
        add(txt_f3);
       
        txt_f4=new TextField();
        txt_f4.setForeground(Color.red);
        txt_f4.setBounds(290,250,30,30);
        add(txt_f4);
       
        txt_f5=new TextField();
        txt_f5.setForeground(Color.red);
        txt_f5.setBounds(330,250,30,30);
        add(txt_f5);
       
        txt_f6=new TextField();
        txt_f6.setForeground(Color.red);
        txt_f6.setBounds(330+40,250,30,30);
        add(txt_f6);
       
        txt_g1=new TextField();
        txt_g1.setForeground(Color.red);
        txt_g1.setBounds(50,290,30,30);
        add(txt_g1);
       
        txt_g2=new TextField();
        txt_g2.setForeground(Color.red);
        txt_g2.setBounds(90,290,30,30);
        add(txt_g2);
       
        lbl_g1=new Label("3");
        lbl_g1.setBounds(130,290,30,30);
        add(lbl_g1);
       
        lbl_g2=new Label("9");
        lbl_g2.setBounds(50,330,30,30);
        add(lbl_g2);
       
        lbl_g3=new Label("4");
        lbl_g3.setBounds(90,330,30,30);
        add(lbl_g3);
       
        lbl_g4=new Label("8");
        lbl_g4.setBounds(130,330,30,30);
        add(lbl_g4);
       
        lbl_g5=new Label("2");
        lbl_g5.setBounds(50,370,30,30);
        add(lbl_g5);
       
        lbl_g6=new Label("1");
        lbl_g6.setBounds(50+40,370,30,30);
        add(lbl_g6);
       
        lbl_g7=new Label("6");
        lbl_g7.setBounds(50+40+40,370,30,30);
        add(lbl_g7);
       
        txt_h1=new TextField();
        txt_h1.setForeground(Color.red);
        txt_h1.setBounds(170,290,30,30);
        add(txt_h1);
       
        lbl_h1=new Label("9");
        lbl_h1.setBounds(170+40,290,30,30);
        add(lbl_h1);
       
        txt_h2=new TextField();
        txt_h2.setForeground(Color.red);
        txt_h2.setBounds(170+40+40,290,30,30);
        add(txt_h2);
       
        txt_h3=new TextField();
        txt_h3.setForeground(Color.red);
        txt_h3.setBounds(170,290+40,30,30);
        add(txt_h3);
       
        txt_h4=new TextField();
        txt_h4.setForeground(Color.red);
        txt_h4.setBounds(170+40,290+40,30,30);
        add(txt_h4);
       
        txt_h5=new TextField();
        txt_h5.setForeground(Color.red);
        txt_h5.setBounds(170+40+40,290+40,30,30);
        add(txt_h5);
       
        txt_h6=new TextField();
        txt_h6.setForeground(Color.red);
        txt_h6.setBounds(170,290+40+40,30,30);
        add(txt_h6);
       
        txt_h7=new TextField();
        txt_h7.setForeground(Color.red);
        txt_h7.setBounds(170+40,290+40+40,30,30);
        add(txt_h7);
       
        lbl_h2=new Label("3");
        lbl_h2.setBounds(170+40+40,290+40+40,30,30);
        add(lbl_h2);
       
        lbl_i1=new Label("2");
        lbl_i1.setBounds(290,290,30,30);
        add(lbl_i1);
       
        lbl_i2=new Label("8");
        lbl_i2.setBounds(330,290,30,30);
        add(lbl_i2);
       
        lbl_i3=new Label("1");
        lbl_i3.setBounds(370,290,30,30);
        add(lbl_i3);
       
        lbl_i4=new Label("7");
        lbl_i4.setBounds(290,290+40,30,30);
        add(lbl_i4);
       
        txt_i1=new TextField();
        txt_i1.setForeground(Color.red);
        txt_i1.setBounds(290+40,290+40,30,30);
        add(txt_i1);
       
        txt_i2=new TextField();
        txt_i2.setForeground(Color.red);
        txt_i2.setBounds(290+40+40,290+40,30,30);
        add(txt_i2);
       
        txt_i3=new TextField();
        txt_i3.setForeground(Color.red);
        txt_i3.setBounds(290,290+40+40,30,30);
        add(txt_i3);
       
        txt_i4=new TextField();
        txt_i4.setForeground(Color.red);
        txt_i4.setBounds(290+40,290+40+40,30,30);
        add(txt_i4);
       
        txt_i5=new TextField();
        txt_i5.setForeground(Color.red);
        txt_i5.setBounds(290+40+40,290+40+40,30,30);
        add(txt_i5);
       
        btn_check=new Button("Check");
        btn_check.setBounds(100,450,220,30);
        btn_check.addActionListener(this);
        add(btn_check);       
       
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }           
        });       
    }
    public void paint(Graphics g)
    {
        g.setColor(Color.red);
        g.drawLine(45,45,405,45);  // 1st horizontal line
        g.drawLine(45,405,45,45);    // 1st vertical line
        g.drawLine(45,165,405,165);    // 2nd horizontal line
        g.drawLine(165,405,165,45);    // 2nd vertical line
        g.drawLine(285,405,285,45);    // 3rd vertical line
        g.drawLine(405,405,405,45);    // 4th vertical line
        g.drawLine(45,285,405,285);
        g.drawLine(45,405,405,405); //last vertical line       
    }   
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==btn_check)
        {
            int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0;
            String str_a1=txt_a1.getText();
            String str_a2=txt_a2.getText();
            String str_a3=txt_a3.getText();
            String str_a4=txt_a4.getText();
            String str_a5=txt_a5.getText();
            String str_b1=txt_b1.getText();
            String str_b2=txt_b2.getText();
            String str_b3=txt_b3.getText();
            String str_b4=txt_b4.getText();
            String str_b5=txt_b5.getText();
            String str_b6=txt_b6.getText();
            String str_b7=txt_b7.getText();
            String str_c1=txt_c1.getText();
            String str_c2=txt_c2.getText();
            String str_c3=txt_c3.getText();
            String str_c4=txt_c4.getText();
            String str_c5=txt_c5.getText();
            String str_d1=txt_d1.getText();
            String str_d2=txt_d2.getText();
            String str_d3=txt_d3.getText();
            String str_d4=txt_d4.getText();
            String str_d5=txt_d5.getText();
            String str_d6=txt_d6.getText();
            String str_e1=txt_e1.getText();
            String str_e2=txt_e2.getText();
            String str_e3=txt_e3.getText();
            String str_e4=txt_e4.getText();
            String str_f1=txt_f1.getText();
            String str_f2=txt_f2.getText();
            String str_f3=txt_f3.getText();
            String str_f4=txt_f4.getText();
            String str_f5=txt_f5.getText();
            String str_f6=txt_f6.getText();
            String str_g1=txt_g1.getText();
            String str_g2=txt_g2.getText();
            String str_h1=txt_h1.getText();
            String str_h2=txt_h2.getText();
            String str_h3=txt_h3.getText();
            String str_h4=txt_h4.getText();
            String str_h5=txt_h5.getText();
            String str_h6=txt_h6.getText();
            String str_h7=txt_h7.getText();
            String str_i1=txt_i1.getText();
            String str_i2=txt_i2.getText();
            String str_i3=txt_i3.getText();
            String str_i4=txt_i4.getText();
            String str_i5=txt_i5.getText();
           
            if(str_a1.equals("6")&&str_a2.equals("3")&&str_a3.equals("5")&&str_a4.equals("7")&&str_a5.equals("9"))
            {
                a=1;
                System.out.println("you r right");
            }
            else
            {
                a=2;
                System.out.println("you r wrong");
            }
            if(str_b1.equals("1")&&str_b2.equals("4")&&str_b3.equals("2")&&str_b4.equals("3")&&str_b5.equals("8")&&str_b6.equals("7")&&str_b7.equals("5"))
            {
                b=1;
                System.out.println("you r right");
            }
            else
            {
                b=2;
                System.out.println("you r wrong");
            }
            if(str_c1.equals("8")&&str_c2.equals("2")&&str_c3.equals("7")&&str_c4.equals("3")&&str_c5.equals("9"))
            {
                c=1;
                System.out.println("you r right");
            }
            else
            {
                c=2;
                System.out.println("you r wrong");
            }
            if(str_d1.equals("4")&&str_d2.equals("6")&&str_d3.equals("7")&&str_d4.equals("1")&&str_d5.equals("8")&&str_d6.equals("5"))
            {
                d=1;
                System.out.println("you r right");
            }
            else
            {
                d=2;
                System.out.println("you r wrong");
            }
            if(str_e1.equals("1")&&str_e2.equals("3")&&str_e3.equals("9")&&str_e4.equals("6"))
            {
                e=1;
                System.out.println("you r right");
            }
            else
            {
                e=2;
                System.out.println("you r wrong");
            }
            if(str_f1.equals("9")&&str_f2.equals("7")&&str_f3.equals("5")&&str_f4.equals("4")&&str_f5.equals("1")&&str_f6.equals("2"))
            {
                f=1;
                System.out.println("you r right");
            }
            else
            {
                f=2;
                System.out.println("you r wrong");
            }
            if(str_g1.equals("5")&&str_g2.equals("7"))
            {
                g=1;
                System.out.println("you r right");
            }
            else
            {
                g=2;
                System.out.println("you r wrong");
            }
            if(str_h1.equals("4")&&str_h2.equals("6")&&str_h3.equals("1")&&str_h4.equals("5")&&str_h5.equals("2")&&str_h6.equals("8")&&str_h7.equals("7"))
            {
                h=1;
                System.out.println("you r right");
            }
            else
            {
                h=2;
                System.out.println("you r wrong");
            }
            if(str_i1.equals("6")&&str_i2.equals("3")&&str_i3.equals("9")&&str_i4.equals("5")&&str_i5.equals("4"))
            {
                i=1;
                System.out.println("you r right");
            }
            else
            {
                i=2;
                System.out.println("you r wrong");
            }
           
            if(a==1&&b==1&&c==1&&d==1&&e==1&&f==1&&g==1&&h==1&&i==1)
            {
                System.out.println("all conditions are satisfy");
                new WinDialog().setVisible(true);
            }
            else
            {
                txt_a1.setText("");
                txt_a2.setText("");
                txt_a3.setText("");
                txt_a4.setText("");
                txt_a5.setText("");
                txt_b1.setText("");
                txt_b2.setText("");
                txt_b3.setText("");
                txt_b4.setText("");
                txt_b5.setText("");
                txt_b6.setText("");
                txt_b7.setText("");
                txt_c1.setText("");
                txt_c2.setText("");
                txt_c3.setText("");
                txt_c4.setText("");
                txt_c5.setText("");
                txt_d1.setText("");
                txt_d2.setText("");
                txt_d3.setText("");
                txt_d4.setText("");
                txt_d5.setText("");
                txt_d6.setText("");
                txt_e1.setText("");
                txt_e2.setText("");
                txt_e3.setText("");
                txt_e4.setText("");
                txt_f1.setText("");
                txt_f2.setText("");
                txt_f3.setText("");
                txt_f4.setText("");
                txt_f5.setText("");
                txt_f6.setText("");
                txt_g1.setText("");
                txt_g2.setText("");
                txt_h1.setText("");
                txt_h2.setText("");
                txt_h3.setText("");
                txt_h4.setText("");
                txt_h5.setText("");
                txt_h6.setText("");
                txt_h7.setText("");
                txt_i1.setText("");
                txt_i2.setText("");
                txt_i3.setText("");
                txt_i4.setText("");
                txt_i5.setText("");   
                System.out.println("you fail in game..... ");
                new LoseDialog().setVisible(true);
            }           
        }
    }
}
class WinDialog extends  Dialog implements  ActionListener
{
    private Label lbl_msg;
    private Button btn_yes,btn_no;
    private Panel north_panel,south_panel;
    public WinDialog()
    {
        super(new Game(),"Dialog Demo",true);
        north_panel=new Panel();
        south_panel=new Panel();
       
        setTitle("Congratulations...!!!");
        setSize(200,200);
        setLocation(50,80);
        lbl_msg=new Label("Congrats you win the game");
        btn_yes=new Button("Yes");
        btn_no=new Button("No");
       
        north_panel.add(lbl_msg);
        south_panel.add(btn_yes);
        south_panel.add(btn_no);
        add(north_panel,"North");
        add(south_panel,"South");
        btn_yes.addActionListener(this);
        btn_no.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==btn_yes)
        {
            System.exit(0);
        }
        else
            dispose();
    }
}
class LoseDialog extends  Dialog implements  ActionListener
{
    private Label lbl_msg;
    private Button btn_yes;
    private Panel north_panel,south_panel;
    public LoseDialog()
    {
        super(new Game(),"Dialog Demo",true);
        north_panel=new Panel();
        south_panel=new Panel();
       
        setTitle("you fail ........ ");
        setSize(200,200);
        setLocation(50,80);
        lbl_msg=new Label("you fail ..... so try again");
        btn_yes=new Button("Yes");
       
        north_panel.add(lbl_msg);
        south_panel.add(btn_yes);
        add(north_panel,"North");
        add(south_panel,"South");
        btn_yes.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==btn_yes)
        {
            dispose();
        }   
    }
}
class Sudoku
{
    public static void main(String []args)
    {
        new Game().setVisible(true);
    }
}

No comments: