java Puzzle Game

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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.

Puzzle Game using AWT

Puzzle game in AWT

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

Puzzle Game using Swing

Puzzle game in swing

  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 - JavaTpoint");  
  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.   
  19. b1.setBounds(10,30,50,40);  
  20. b2.setBounds(70,30,50,40);  
  21. b3.setBounds(130,30,50,40);  
  22. b4.setBounds(10,80,50,40);  
  23. b5.setBounds(70,80,50,40);  
  24. b6.setBounds(130,80,50,40);  
  25. b7.setBounds(10,130,50,40);  
  26. b8.setBounds(70,130,50,40);  
  27. b9.setBounds(130,130,50,40);  
  28. next.setBounds(70,200,100,40);  
  29.     
  30. add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);add(b7);add(b8);add(b9); add(next);  
  31. b1.addActionListener(this);  
  32. b2.addActionListener(this);  
  33. b3.addActionListener(this);  
  34. b4.addActionListener(this);  
  35. b5.addActionListener(this);  
  36. b6.addActionListener(this);  
  37. b7.addActionListener(this);  
  38. b8.addActionListener(this);  
  39. b9.addActionListener(this);  
  40. next.addActionListener(this);  
  41.   
  42. next.setBackground(Color.black);  
  43. next.setForeground(Color.green);  
  44. setSize(250,300);  
  45. setLayout(null);  
  46. setVisible(true);  
  47. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  48. }//end of constructor  
  49.   
  50. public void actionPerformed(ActionEvent e){  
  51. if(e.getSource()==next){  
  52. String s=b4.getLabel();  
  53. b4.setLabel(b9.getLabel());  
  54. b9.setLabel(s);  
  55. s=b1.getLabel();  
  56. b1.setLabel(b5.getLabel());  
  57. b5.setLabel(s);  
  58. s=b2.getLabel();  
  59. b2.setLabel(b7.getLabel());  
  60. b7.setLabel(s);  
  61. }  
  62. if(e.getSource()==b1){  
  63. String s=b1.getLabel();  
  64. if(b2.getLabel().equals(" ")){ b2.setLabel(s); b1.setLabel(" ");}  
  65. else if(b4.getLabel().equals(" ")){ b4.setLabel(s); b1.setLabel(" ");}  
  66.  }//end of if  
  67.   
  68. if(e.getSource()==b3){  
  69. String s=b3.getLabel();  
  70. if(b2.getLabel().equals(" ")){ b2.setLabel(s); b3.setLabel(" ");}  
  71. else if(b6.getLabel().equals(" ")){ b6.setLabel(s); b3.setLabel(" ");}  
  72.  }//end of if  
  73.   
  74. if(e.getSource()==b2){  
  75. String s=b2.getLabel();  
  76. if(b1.getLabel().equals(" ")){ b1.setLabel(s); b2.setLabel(" ");}  
  77. else if(b3.getLabel().equals(" ")){ b3.setLabel(s); b2.setLabel(" ");}  
  78. else if(b5.getLabel().equals(" ")){ b5.setLabel(s); b2.setLabel(" ");}  
  79.  }//end of if  
  80.   
  81. if(e.getSource()==b4){  
  82. String s=b4.getLabel();  
  83. if(b1.getLabel().equals(" ")){ b1.setLabel(s); b4.setLabel(" ");}  
  84. else if(b7.getLabel().equals(" ")){ b7.setLabel(s); b4.setLabel(" ");}  
  85. else if(b5.getLabel().equals(" ")){ b5.setLabel(s); b4.setLabel(" ");}  
  86.  }//end of if  
  87.   
  88. if(e.getSource()==b5){  
  89. String s=b5.getLabel();  
  90. if(b2.getLabel().equals(" ")){ b2.setLabel(s); b5.setLabel(" ");}  
  91. else if(b4.getLabel().equals(" ")){ b4.setLabel(s); b5.setLabel(" ");}  
  92. else if(b6.getLabel().equals(" ")){ b6.setLabel(s); b5.setLabel(" ");}  
  93. else if(b8.getLabel().equals(" ")){ b8.setLabel(s); b5.setLabel(" ");}  
  94.  }//end of if  
  95.   
  96. if(e.getSource()==b6){  
  97.   
  98. String s=b6.getLabel();  
  99. if(b9.getLabel().equals(" ")){ b9.setLabel(s); b6.setLabel(" ");}  
  100. else if(b3.getLabel().equals(" ")){ b3.setLabel(s); b6.setLabel(" ");}  
  101. else if(b5.getLabel().equals(" ")){ b5.setLabel(s); b6.setLabel(" ");}  
  102.   
  103.  }//end of if  
  104.   
  105. if(e.getSource()==b7){  
  106. String s=b7.getLabel();  
  107. if(b4.getLabel().equals(" ")){ b4.setLabel(s); b7.setLabel(" ");}  
  108. else if(b8.getLabel().equals(" ")){ b8.setLabel(s); b7.setLabel(" ");}  
  109.   
  110.  }//end of if  
  111.   
  112. if(e.getSource()==b8){  
  113. String s=b8.getLabel();  
  114. if(b7.getLabel().equals(" ")){ b7.setLabel(s); b8.setLabel(" ");}  
  115. else if(b9.getLabel().equals(" ")){ b9.setLabel(s); b8.setLabel(" ");}  
  116. else if(b5.getLabel().equals(" ")){ b5.setLabel(s); b8.setLabel(" ");}  
  117.   
  118.  }//end of if  
  119.   
  120. if(e.getSource()==b9){  
  121. String s=b9.getLabel();  
  122. if(b6.getLabel().equals(" ")){ b6.setLabel(s); b9.setLabel(" ");}  
  123. else if(b8.getLabel().equals(" ")){ b8.setLabel(s); b9.setLabel(" ");}  
  124.   
  125. if(b1.getLabel().equals("1")&&b2.getLabel().equals("2")&&b3.getLabel()  
  126. .equals("3")&&b4.getLabel().equals("4")&&b5.getLabel().equals("5")  
  127. &&b6.getLabel().equals("6")&&b7.getLabel().equals("7")&&b8.getLabel()  
  128. .equals("8")&&b9.getLabel().equals(" ")){   
  129. JOptionPane.showMessageDialog(puzzle.this,"!!!you won!!!");  
  130. }  
  131.  }//end of if  
  132.   
  133. }//end of actionPerformed  
  134.    
  135.   
  136. public static void main(String[] args){  
  137. new puzzle();  
  138. }//end of main  
  139.   
  140. }//end of class