All for Joomla All for Webmasters

Puzzle Game in Java

Puzzle Game in Java with Source Code: We can develop Puzzle Game in java with the help of AWT/Swing with event handling. Let’s see the code of creating Puzzle Game in java.

 

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.JOptionPane;
  4. public class Puzzle extends Frame implements ActionListener{
  5. Button b1,b2,b3,b4,b5,b6,b7,b8,b9;
  6. Puzzle(){
  7.     super(“Puzzle – makeforsure”);
  8.     b1=new Button(“1”);
  9.     b1.setBounds(50,100,40,40);
  10.     b2=new Button(“2”);
  11.     b2.setBounds(100,100,40,40);
  12.     b3=new Button(“3”);
  13.     b3.setBounds(150,100,40,40);
  14.     b4=new Button(“4”);
  15.     b4.setBounds(50,150,40,40);
  16.     b5=new Button(“5”);
  17.     b5.setBounds(100,150,40,40);
  18.     b6=new Button(“6”);
  19.     b6.setBounds(150,150,40,40);
  20.     b7=new Button(“7”);
  21.     b7.setBounds(50,200,40,40);
  22.     b8=new Button(“”);
  23.     b8.setBounds(100,200,40,40);
  24.     b9=new Button(“8”);
  25.     b9.setBounds(150,200,40,40);
  26.     b1.addActionListener(this);
  27.     b2.addActionListener(this);
  28.     b3.addActionListener(this);
  29.     b4.addActionListener(this);
  30.     b5.addActionListener(this);
  31.     b6.addActionListener(this);
  32.     b7.addActionListener(this);
  33.     b8.addActionListener(this);
  34.     b9.addActionListener(this);
  35.     add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);add(b7);add(b8);add(b9);
  36.     setSize(400,400);
  37.     setLayout(null);
  38.     setVisible(true);
  39. }
  40. public void actionPerformed(ActionEvent e){
  41.     if(e.getSource()==b1){
  42.         String label=b1.getLabel();
  43.         if(b2.getLabel().equals(“”)){
  44.             b2.setLabel(label);
  45.             b1.setLabel(“”);
  46.         }
  47.         if(b4.getLabel().equals(“”)){
  48.             b4.setLabel(label);
  49.             b1.setLabel(“”);
  50.         }
  51.     }
  52.     if(e.getSource()==b2){
  53.         String label=b2.getLabel();
  54.         if(b1.getLabel().equals(“”)){
  55.             b1.setLabel(label);
  56.             b2.setLabel(“”);
  57.         }
  58.         if(b3.getLabel().equals(“”)){
  59.             b3.setLabel(label);
  60.             b2.setLabel(“”);
  61.         }
  62.         if(b5.getLabel().equals(“”)){
  63.             b5.setLabel(label);
  64.             b2.setLabel(“”);
  65.         }
  66.     }
  67.     if(e.getSource()==b3){
  68.         String label=b3.getLabel();
  69.         if(b2.getLabel().equals(“”)){
  70.             b2.setLabel(label);
  71.             b3.setLabel(“”);
  72.         }
  73.         if(b6.getLabel().equals(“”)){
  74.             b6.setLabel(label);
  75.             b3.setLabel(“”);
  76.         }
  77.     }
  78.     if(e.getSource()==b4){
  79.         String label=b4.getLabel();
  80.         if(b1.getLabel().equals(“”)){
  81.             b1.setLabel(label);
  82.             b4.setLabel(“”);
  83.         }
  84.         if(b7.getLabel().equals(“”)){
  85.             b7.setLabel(label);
  86.             b4.setLabel(“”);
  87.         }
  88.         if(b5.getLabel().equals(“”)){
  89.             b5.setLabel(label);
  90.             b4.setLabel(“”);
  91.         }
  92.     }
  93.     if(e.getSource()==b5){
  94.         String label=b5.getLabel();
  95.         if(b2.getLabel().equals(“”)){
  96.             b2.setLabel(label);
  97.             b5.setLabel(“”);
  98.         }
  99.         if(b6.getLabel().equals(“”)){
  100.             b6.setLabel(label);
  101.             b5.setLabel(“”);
  102.         }
  103.         if(b4.getLabel().equals(“”)){
  104.             b4.setLabel(label);
  105.             b5.setLabel(“”);
  106.         }
  107.         if(b8.getLabel().equals(“”)){
  108.             b8.setLabel(label);
  109.             b5.setLabel(“”);
  110.         }
  111.     }
  112.     if(e.getSource()==b6){
  113.         String label=b6.getLabel();
  114.         if(b9.getLabel().equals(“”)){
  115.             b9.setLabel(label);
  116.             b6.setLabel(“”);
  117.         }
  118.         if(b3.getLabel().equals(“”)){
  119.             b3.setLabel(label);
  120.             b6.setLabel(“”);
  121.         }
  122.         if(b5.getLabel().equals(“”)){
  123.             b5.setLabel(label);
  124.             b6.setLabel(“”);
  125.         }
  126.     }
  127.     if(e.getSource()==b7){
  128.         String label=b7.getLabel();
  129.         if(b4.getLabel().equals(“”)){
  130.             b4.setLabel(label);
  131.             b7.setLabel(“”);
  132.         }
  133.         if(b8.getLabel().equals(“”)){
  134.             b8.setLabel(label);
  135.             b7.setLabel(“”);
  136.         }
  137.     }
  138.     if(e.getSource()==b8){
  139.         String label=b8.getLabel();
  140.         if(b9.getLabel().equals(“”)){
  141.             b9.setLabel(label);
  142.             b8.setLabel(“”);
  143.         }
  144.         if(b7.getLabel().equals(“”)){
  145.             b7.setLabel(label);
  146.             b8.setLabel(“”);
  147.         }
  148.         if(b5.getLabel().equals(“”)){
  149.             b5.setLabel(label);
  150.             b8.setLabel(“”);
  151.         }
  152.     }
  153.     if(e.getSource()==b9){
  154.         String label=b9.getLabel();
  155.         if(b6.getLabel().equals(“”)){
  156.             b6.setLabel(label);
  157.             b9.setLabel(“”);
  158.         }
  159.         if(b8.getLabel().equals(“”)){
  160.             b8.setLabel(label);
  161.             b9.setLabel(“”);
  162.         }
  163.     }
  164.     //congrats code
  165.     if(b1.getLabel().equals(“1”)&&b2.getLabel().equals(“2”)&&b3.getLabel()
  166.             .equals(“3”)&&b4.getLabel().equals(“4”)&&b5.getLabel().equals(“5”)
  167.             &&b6.getLabel().equals(“6”)&&b7.getLabel().equals(“7”)&&b8.getLabel()
  168.             .equals(“8”)&&b9.getLabel().equals(“”)){
  169.             JOptionPane.showMessageDialog(this,“Congratulations! You won.”);
  170.     }
  171. }
  172. public static void main(String[] args) {
  173.     new Puzzle();
  174. }
  175. }

Another puzzle  source code :

  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. public class puzzle extends JFrame implements ActionListener{
  5. JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,next;
  6. puzzle(){
  7. super(“Puzzle Game – makeforsure”);
  8.  b1=new JButton(“1”);
  9.  b2=new JButton(” “);
  10.  b3=new JButton(“3”);
  11.  b4=new JButton(“4”);
  12.  b5=new JButton(“5”);
  13.  b6=new JButton(“6”);
  14.  b7=new JButton(“7”);
  15.  b8=new JButton(“8”);
  16.  b9=new JButton(“2”);
  17.  next=new JButton(“next”);
  18. b1.setBounds(10,30,50,40);
  19. b2.setBounds(70,30,50,40);
  20. b3.setBounds(130,30,50,40);
  21. b4.setBounds(10,80,50,40);
  22. b5.setBounds(70,80,50,40);
  23. b6.setBounds(130,80,50,40);
  24. b7.setBounds(10,130,50,40);
  25. b8.setBounds(70,130,50,40);
  26. b9.setBounds(130,130,50,40);
  27. next.setBounds(70,200,100,40);
  28. add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);add(b7);add(b8);add(b9); add(next);
  29. b1.addActionListener(this);
  30. b2.addActionListener(this);
  31. b3.addActionListener(this);
  32. b4.addActionListener(this);
  33. b5.addActionListener(this);
  34. b6.addActionListener(this);
  35. b7.addActionListener(this);
  36. b8.addActionListener(this);
  37. b9.addActionListener(this);
  38. next.addActionListener(this);
  39. next.setBackground(Color.black);
  40. next.setForeground(Color.green);
  41. setSize(250,300);
  42. setLayout(null);
  43. setVisible(true);
  44. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  45. }//end of constructor
  46. public void actionPerformed(ActionEvent e){
  47. if(e.getSource()==next){
  48. String s=b4.getLabel();
  49. b4.setLabel(b9.getLabel());
  50. b9.setLabel(s);
  51. s=b1.getLabel();
  52. b1.setLabel(b5.getLabel());
  53. b5.setLabel(s);
  54. s=b2.getLabel();
  55. b2.setLabel(b7.getLabel());
  56. b7.setLabel(s);
  57. }
  58. if(e.getSource()==b1){
  59. String s=b1.getLabel();
  60. if(b2.getLabel().equals(” “)){ b2.setLabel(s); b1.setLabel(” “);}
  61. else if(b4.getLabel().equals(” “)){ b4.setLabel(s); b1.setLabel(” “);}
  62.  }//end of if
  63. if(e.getSource()==b3){
  64. String s=b3.getLabel();
  65. if(b2.getLabel().equals(” “)){ b2.setLabel(s); b3.setLabel(” “);}
  66. else if(b6.getLabel().equals(” “)){ b6.setLabel(s); b3.setLabel(” “);}
  67.  }//end of if
  68. if(e.getSource()==b2){
  69. String s=b2.getLabel();
  70. if(b1.getLabel().equals(” “)){ b1.setLabel(s); b2.setLabel(” “);}
  71. else if(b3.getLabel().equals(” “)){ b3.setLabel(s); b2.setLabel(” “);}
  72. else if(b5.getLabel().equals(” “)){ b5.setLabel(s); b2.setLabel(” “);}
  73.  }//end of if
  74. if(e.getSource()==b4){
  75. String s=b4.getLabel();
  76. if(b1.getLabel().equals(” “)){ b1.setLabel(s); b4.setLabel(” “);}
  77. else if(b7.getLabel().equals(” “)){ b7.setLabel(s); b4.setLabel(” “);}
  78. else if(b5.getLabel().equals(” “)){ b5.setLabel(s); b4.setLabel(” “);}
  79.  }//end of if
  80. if(e.getSource()==b5){
  81. String s=b5.getLabel();
  82. if(b2.getLabel().equals(” “)){ b2.setLabel(s); b5.setLabel(” “);}
  83. else if(b4.getLabel().equals(” “)){ b4.setLabel(s); b5.setLabel(” “);}
  84. else if(b6.getLabel().equals(” “)){ b6.setLabel(s); b5.setLabel(” “);}
  85. else if(b8.getLabel().equals(” “)){ b8.setLabel(s); b5.setLabel(” “);}
  86.  }//end of if
  87. if(e.getSource()==b6){
  88. String s=b6.getLabel();
  89. if(b9.getLabel().equals(” “)){ b9.setLabel(s); b6.setLabel(” “);}
  90. else if(b3.getLabel().equals(” “)){ b3.setLabel(s); b6.setLabel(” “);}
  91. else if(b5.getLabel().equals(” “)){ b5.setLabel(s); b6.setLabel(” “);}
  92.  }//end of if
  93. if(e.getSource()==b7){
  94. String s=b7.getLabel();
  95. if(b4.getLabel().equals(” “)){ b4.setLabel(s); b7.setLabel(” “);}
  96. else if(b8.getLabel().equals(” “)){ b8.setLabel(s); b7.setLabel(” “);}
  97.  }//end of if
  98. if(e.getSource()==b8){
  99. String s=b8.getLabel();
  100. if(b7.getLabel().equals(” “)){ b7.setLabel(s); b8.setLabel(” “);}
  101. else if(b9.getLabel().equals(” “)){ b9.setLabel(s); b8.setLabel(” “);}
  102. else if(b5.getLabel().equals(” “)){ b5.setLabel(s); b8.setLabel(” “);}
  103.  }//end of if
  104. if(e.getSource()==b9){
  105. String s=b9.getLabel();
  106. if(b6.getLabel().equals(” “)){ b6.setLabel(s); b9.setLabel(” “);}
  107. else if(b8.getLabel().equals(” “)){ b8.setLabel(s); b9.setLabel(” “);}
  108. if(b1.getLabel().equals(“1”)&&b2.getLabel().equals(“2”)&&b3.getLabel()
  109. .equals(“3”)&&b4.getLabel().equals(“4”)&&b5.getLabel().equals(“5”)
  110. &&b6.getLabel().equals(“6”)&&b7.getLabel().equals(“7”)&&b8.getLabel()
  111. .equals(“8”)&&b9.getLabel().equals(” “)){
  112. JOptionPane.showMessageDialog(puzzle.this,“!!!you won!!!”);
  113. }
  114.  }//end of if
  115. }//end of actionPerformed
  116. public static void main(String[] args){
  117. new puzzle();
  118. }//end of main
  119. }//end of class
PinIt
submit to reddit

Leave a Reply

Top