Code.GeekInterview.com
 
 

Java Code Base

 
Code Samples Java
 

Java puzzles


Code ResourceAuthor: Sindhukumar.R  

Difficulty Level:

Published: 8th Nov 2006   Read: 4000 times  

Filed in: Java
Add Comment


 

 

Sponsored Links


 

 

It contains puzzle game and a die game. First compile the "well.java" file then run using "java well" to view the result.

 


Sample Code
  1.  
  2. import java.util.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import javax.swing.border.*;
  7. import javax.swing.event.*;
  8. import java.net.*;
  9. import javax.swing.event.MenuEvent.*;
  10.  
  11. public class well extends JFrame implements ActionListener{
  12.    private static JButton[] ba;
  13.    private static JButton[] bb;
  14.    private static JInternalFrame f2,f1,f3,f4,f5,f6,f7,f8;
  15.    private static javax .swing.JScrollPane scroll;
  16.    private static javax.swing.JEditorPane editor;
  17.    private JPanel p1,p2,p3,p4,p5,p6,p7,p8,p9;
  18.    private static JButton b1,b2,dv,die,ctime,cpoint,etime;
  19.    private static JTextField point;
  20.    static int value,no_click,total;
  21.    static String c;
  22.    static int a,bvalue,v;
  23.    static int sstime,smtime,estime,emtime,tt;
  24.    
  25.    public well()
  26. {
  27. super("--------JGamez--------");
  28. setDefaultLookAndFeelDecorated(false);
  29. setBounds(10,10, 900, 500);
  30. getContentPane().add(d);
  31.  
  32. f1=new JInternalFrame("1",true,true,true,true);
  33. f1.setTitle("welcome");
  34. f1.setBorder(new MatteBorder(10,10,10,10,new Color(255,100,150)));
  35. f1.setBounds(10,10,350,350);
  36. f1.getContentPane().setLayout(new GridLayout(9,9));
  37. ba=new JButton[81];
  38. for(int i=0;i<=80;i++)
  39. {
  40.     ba[i]=new JButton(" ");
  41.     ba[i].setBorder(new MatteBorder(3,3,3,3,Color.BLUE));
  42.     ba[i].setText("  ");
  43.     ba[i].setBackground(Color.white);
  44.     ba[0].setText("Start");
  45.     f1.getContentPane().add(ba[i]);
  46. }
  47. ba[80].setBackground(Color.red);
  48. ba[80].setText("Goal");
  49. f1.setResizable(false);
  50. f1.setIconifiable(true);
  51. f1.setDefaultCloseOperation(HIDE_ON_CLOSE);
  52. d.add(f1,0);
  53. f1.setVisible(false);
  54.  
  55. f2=new JInternalFrame("1",true,true);
  56. f2.setTitle("Remote Control");
  57. f2.getContentPane().setLayout(new GridLayout(1,1));
  58. JMenuBar jmb=new JMenuBar();
  59. JMenu file=new JMenu("File");
  60. JMenu item1= new JMenu("New");
  61. JMenuItem item2= new JMenuItem("Exit");
  62. JMenuItem item10=new JMenuItem("Die Game");
  63. JMenuItem item11=new JMenuItem("Number Game");
  64. {
  65.     public void actionPerformed(ActionEvent e)
  66.     {
  67.          well.die.setEnabled(true);
  68.          well.etime.setText("   ");
  69.          well.ctime.setText("  ");
  70.          well.cpoint.setText("point:  ");
  71.          well.f1.setVisible(true);
  72.          well.b1.setEnabled(true);
  73.          well.b2.setEnabled(true);
  74.          well.f1.show();
  75.          well.f5.setVisible(false);
  76.          well.f6.setVisible(true);
  77.          int i=0;
  78.          while(i<=80)
  79.          {
  80.             if(i>=10&&i<=20){
  81.                 if(i==11||i==12||i==13)
  82.                     ba[i].setBackground(Color.red);
  83.                 else
  84.                     ba[i].setBackground(Color.white);}
  85.                    
  86.             else if(i>=21&&i<=31){
  87.                 if(i==23||i==26||i==29)
  88.                     ba[i].setBackground(Color.red);
  89.                 else
  90.                     ba[i].setBackground(Color.white); }
  91.             else if(i>=32&&i<=45){
  92.                 if(i==34||i==37||i==41)
  93.                     ba[i].setBackground(Color.red);
  94.                 else
  95.                     ba[i].setBackground(Color.white); }
  96.                    
  97.             else if(i>=45&&i<=60){
  98.                 if(i==46||i==49||i==58||i==52)
  99.                     ba[i].setBackground(Color.red);
  100.                 else
  101.                     ba[i].setBackground(Color.white); }
  102.             else if(i>=61&&i<=78){
  103.                 if(i==66||i==69||i==75)
  104.                     ba[i].setBackground(Color.red);
  105.                 else
  106.                     ba[i].setBackground(Color.white); }
  107.             else
  108.                 ba[i].setBackground(Color.white);
  109.              i++;
  110.          }
  111.          
  112.              
  113.           new well(0,0);      
  114.          }
  115. };
  116. {
  117.     public void actionPerformed(ActionEvent e)
  118.     {
  119.          smtime=new Date().getMinutes();
  120.          sstime=new Date().getSeconds();
  121.  
  122.          well.ctime.setText(" StartedTime:"+String.valueOf(new Date().getHours())+":"+String.valueOf(new Date().getMinutes())+":"+String.valueOf(new Date().getSeconds()));
  123.        
  124.          well.etime.setText("        ");
  125.          well.cpoint.setText("point: ");
  126.          well.b1.setEnabled(false);
  127.          well.b2.setEnabled(false);
  128.          well.f5.setVisible(true);
  129.          well.f1.setVisible(false);
  130.          well.die.setEnabled(false);
  131.          well.f6.setVisible(true);
  132.          well.f5.show();
  133.          no_click=0;
  134.          total=0;
  135.          for(int j=0;j<=15;j++)
  136.          {
  137.          bvalue=(int)(Math.random()*75);
  138.         bb[j].setText(String.valueOf(bvalue));
  139.        
  140.        
  141.       }
  142.     }
  143. };
  144. item10.addActionListener(al);
  145. item11.addActionListener(ab);
  146. item1.add(item10);
  147. item1.add(item11);
  148. file.add(item1);
  149. file.add(item2);
  150. JMenu Help=new JMenu("Help");
  151. JMenuItem item3= new JMenuItem("About Game");
  152. {
  153.     public void actionPerformed(ActionEvent e)
  154.     {
  155.         well.f7.setVisible(true);
  156.         well.f7.show();
  157.     }
  158. };
  159. JMenuItem item4= new JMenuItem("About Developer");
  160. {
  161.     public void actionPerformed(ActionEvent e)
  162.     {
  163.         JOptionPane.showMessageDialog(null,"(c)copyright 2006 by NeXT solutionsn owner: R.Sindhukumar","JGamez",JOptionPane.INFORMATION_MESSAGE);
  164.     }
  165. };
  166. item3.addActionListener(ag);
  167. item4.addActionListener(ad);
  168. Help.add(item3);
  169. Help.add(item4);
  170. jmb.add(file);
  171. jmb.add(Help);
  172. f2.getContentPane().add(jmb);
  173. f2.setBounds(370,100,350,50);
  174. f2.setResizable(false);
  175. f2.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  176. die=new JButton("Throw the dice");
  177. die.setEnabled(false);
  178. die.setBackground(Color.cyan);
  179. die.addActionListener(this);
  180. f2.getContentPane().add(die);
  181. d.add(f2,1);
  182. f2.show();
  183.  
  184.  
  185. JInternalFrame f3=new JInternalFrame("1",true,true);
  186. f3.getContentPane().setLayout(new GridLayout(1,1));
  187. b1=new JButton("HIDE THE Die GAME WINDOW");
  188. b2=new JButton("SHOW THE Die GAME WINDOW");
  189. b1.addActionListener(this);
  190. b2.addActionListener(this);
  191. f3.getContentPane().add(b1);
  192. f3.getContentPane().add(b2);
  193. f3.setBounds(370,10,420,50);
  194. f3.setResizable(false);
  195. f3.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  196. d.add(f3,3);
  197. f3.show();
  198.  
  199. f4=new JInternalFrame("1",true,true);
  200. f4.getContentPane().setLayout(new GridLayout(1,1));
  201. dv=new JButton(" ");
  202. dv.setBackground(Color.white);
  203. f4.getContentPane().add(dv);
  204. f4.setBounds(370,150,350,50);
  205. f4.setResizable(false);
  206. f4.setTitle("Dice Value");
  207. f4.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  208. d.add(f4,4);
  209. f4.show();
  210.  
  211. f5=new JInternalFrame("1",true,true,true,true);
  212. f5.setTitle("Number Game");
  213. f5.setBorder(new MatteBorder(3,3,3,3,Color.red));
  214. f5.setBounds(10,10,350,350);
  215. f5.getContentPane().setLayout(new GridLayout(4,4));
  216. f5.setDefaultCloseOperation(HIDE_ON_CLOSE);
  217. bb=new JButton[16];
  218. {
  219.    public void actionPerformed(ActionEvent e)
  220.   {
  221.    
  222.      String val=e.getActionCommand();
  223.       no_click++;
  224.      if(no_click<=4)
  225.      {
  226.       int s=Integer.parseInt(val);
  227.       total=total+s;    
  228.       JOptionPane.showMessageDialog(null,String.valueOf(total));
  229.       if(total==100&&no_click==4)
  230.       {
  231.         well.etime.setText(" Finished Time:"+String.valueOf(new Date().getHours())+":"+String.valueOf(new         Date().getMinutes())+":"+String.valueOf(new Date().getSeconds()));
  232.  
  233.         JOptionPane.showMessageDialog(null,"Congratulations");
  234.          emtime=new Date().getMinutes();
  235.         if(emtime!=0){
  236.          tt=(emtime*60)-(smtime*60);        
  237.          if(tt<=10){
  238.              JOptionPane.showMessageDialog(null,"Excellent work");
  239.              String stt=String.valueOf(100);
  240.              well.cpoint.setText("point: "+stt);}
  241.          else if(tt>=11&&tt<=20){
  242.              JOptionPane.showMessageDialog(null,"Very Good work");
  243.              String stt=String.valueOf(85);
  244.              well.cpoint.setText("point: "+stt);}
  245.          else if(tt>=21&&tt<=25){
  246.              JOptionPane.showMessageDialog(null,"Good work");
  247.              String stt=String.valueOf(65);
  248.              well.cpoint.setText("point: "+stt);}
  249.          else if(tt>=26&&tt<=35){
  250.              JOptionPane.showMessageDialog(null,"Too much time");
  251.              String stt=String.valueOf(35);
  252.              well.cpoint.setText("point: "+stt);}
  253.          else
  254.          {
  255.              JOptionPane.showMessageDialog(null,"Not Good ");
  256.              String stt=String.valueOf(0);
  257.              well.cpoint.setText("point: "+stt);}
  258.          }
  259.          
  260.          else if(emtime==0)
  261.           {
  262.              tt=estime-sstime;
  263.              if(tt<=10){
  264.              JOptionPane.showMessageDialog(null,"Excellent work");
  265.              String stt=String.valueOf(100);
  266.              well.cpoint.setText("point: "+stt);}
  267.          else if(tt>=11&&tt<=20){
  268.              JOptionPane.showMessageDialog(null,"Very Good work");
  269.              String stt=String.valueOf(85);
  270.              well.cpoint.setText("point: "+stt);}
  271.          else if(tt>=21&&tt<=25){
  272.              JOptionPane.showMessageDialog(null,"Good work");
  273.              String stt=String.valueOf(65);
  274.              well.cpoint.setText("point: "+stt);}
  275.          else if(tt>=26&&tt<=35){
  276.              JOptionPane.showMessageDialog(null,"Too much time");
  277.              String stt=String.valueOf(35);
  278.              well.cpoint.setText("point: "+stt);}
  279.          else
  280.          {
  281.              JOptionPane.showMessageDialog(null,"Not Good ");
  282.              String stt=String.valueOf(0);
  283.              well.cpoint.setText("point: "+stt);}
  284.          }
  285.          }    
  286.          }
  287.      
  288.      else
  289.      {
  290.          if(total==100&&no_click>=4)
  291.          {
  292.          JOptionPane.showMessageDialog(null,"Sorry! You are already won");
  293.          JOptionPane.showMessageDialog(null,"To Play again Click File->new->number game");
  294.          }
  295.          else
  296.          {
  297.              well.etime.setText(" Finished Time:"+String.valueOf(new Date().getHours())+":"+String.valueOf(new         Date().getMinutes())+":"+String.valueOf(new Date().getSeconds()));
  298.              JOptionPane.showMessageDialog(null,"Bad luck! You are failed");
  299.          }
  300.      }
  301.      
  302.    
  303.        
  304.   }
  305. };
  306. for(int j=0;j<=15;j++)
  307. {
  308.     bb[j]=new JButton(" ");
  309.     bb[j].setBorder(new CompoundBorder(new MatteBorder(10,10,10,10,Color.orange),new BevelBorder(BevelBorder.RAISED,Color.MAGENTA,Color.pink)));
  310.     bb[j].setText("  ");
  311.     bb[j].setBackground(Color.yellow);
  312.     bvalue=(int)(Math.random()*75);
  313.     bb[j].setText(String.valueOf(bvalue));
  314.     bb[j].addActionListener(ngl);
  315.     f5.getContentPane().add(bb[j]);
  316.     d.add(f5,0);
  317. }
  318. f5.setVisible(false);
  319.  
  320. f6=new JInternalFrame("Your Points-Only for Number Game",false,false,true,true);
  321. f6.setBounds(370,200,350, 75);
  322. f6.getContentPane().setLayout(new GridLayout(2,2));
  323. ctime=new JButton();
  324. ctime.setText("  ");
  325. cpoint=new JButton();
  326. etime=new JButton();
  327. f6.getContentPane().add(cpoint);
  328. f6.getContentPane().add(ctime);
  329. f6.getContentPane().add(etime);
  330. f6.show();
  331. d.add(f6,0);
  332.  
  333. f7=new JInternalFrame("About Game",false,true,true,true);
  334. f7.setBounds(10,10, 350,350);
  335. f7.setDefaultCloseOperation(HIDE_ON_CLOSE);
  336. f7.setBackground(new Color(170,172,100));
  337. editor=new JEditorPane();
  338. editor.setContentType("text/html");
  339. editor.setText("<font color='#800000'><b>Number Game</b></font><font color='#800000'><b><pre>Number Game contains 16 boxes. Each box contains a Number. You want to select 4 numbers from the boxes by clicking them and the total of the numbers must be 100 ...............</pre></b></font><p align=center><font color='#800000'><b>Die Game</b></font><pre>Die Game is the game in which you want to move around thecells based on value get by throwing the die and it containssome Red cells on whichyou can't go</pre>.............<font color='#800000'><b>Thank you</b></font>");
  340. editor.setEditable(false);
  341. scroll=new JScrollPane(editor);
  342. f7.getContentPane().add(scroll);
  343. f7.setDefaultCloseOperation(HIDE_ON_CLOSE);
  344. f7.setVisible(false);
  345. d.add(f7,0);
  346.  
  347. f8=new JInternalFrame("welcome",false,true,true,true);
  348. f8.setBounds(10,10,350,350);
  349. editor=new JEditorPane();
  350. editor.setContentType("text/html");
  351. editor.setText("<font size=12 color=blue>Welcome Friends!</font><b><font size=8 color=#ff0ce4>JGamez contains two games--die game and Number gameTry it...</font></b>");
  352. editor.setEditable(false);
  353. scroll=new JScrollPane(editor);
  354. f8.getContentPane().add(scroll);
  355. f8.setDefaultCloseOperation(HIDE_ON_CLOSE);
  356. f8.setVisible(true);
  357. f8.show();
  358. d.add(f8,0);
  359.  
  360. }
  361.                public well(int value1)
  362.                {                        
  363.                                      
  364.                                      
  365.                                        a=a+value1;
  366.                                        if(a<=80)
  367.                                       {
  368.                                           if(a==11||a==12||a==13||a==23||a==26||a==29||a==34||a==37||a==41||a==46||a==49||a==58||a==52||a==66||a==69||a==75)
  369.                                           {
  370.                                               JOptionPane.showMessageDialog(null,"sorry! you can't proceedn you landed on red cell");
  371.                                               JOptionPane.showMessageDialog(well.f1,"Window will close now ");
  372.                                               well.f1.setVisible(false);
  373.                                               well.b1.setEnabled(false);
  374.                                               well.b2.setEnabled(false);
  375.                                           }
  376.                                           else
  377.                                           {
  378.                                        ba[a].setBackground(Color.green);
  379.                                        for(int i=a-1;i>=0;i--)
  380.                                        {
  381.                                            ba[i].setBackground(Color.pink);
  382.                                        }
  383.                                        }
  384.                                        }
  385.                                       else
  386.                                        {
  387.                                         ba[0].setBackground(Color.red);
  388.                                         ba[0].setText("Game over");
  389.                                         repaint();
  390.                                        }
  391.                 }
  392.                public well(int value1,int a1)
  393.                  {
  394.                    a=a1;
  395.                    new well(value1);
  396.                  }
  397.  
  398. public void actionPerformed(ActionEvent evt)
  399. {
  400.     if(evt.getSource()==b1)
  401.     {
  402.         well.f1.setVisible(false);
  403.        
  404.     }
  405. else if(evt.getSource()==die)
  406.  {
  407.                    
  408.                     if(ba[80].getBackground()!=Color.green){
  409.                      value=(int)(Math.random()*6);
  410.                      value=value+1;
  411.                      c=String.valueOf(value);
  412.                        well.dv.setText(c);
  413.                      
  414.                      new well(value);
  415.                    }
  416.                    else
  417.                    {
  418.                        JOptionPane.showMessageDialog(null,"your are successfull winner","winnner notice",JOptionPane.INFORMATION_MESSAGE);
  419.                    }}
  420.     else{
  421.         well.f1.show();
  422.         well.f1.setVisible(true);
  423.         well.f1.repaint();      
  424.     }  
  425. }
  426. public static void main(String args[])
  427. {
  428. well a=new well();
  429. a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  430. a.setVisible(true);
  431. }
  432. }
  433.  
Copyright GeekInterview.com


Next Article: Simple Java Mail application


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. srinivasaraobora130576
2. iamdvr97636
3. venki_madesh35132
4. Raju18459
5. parmod kumar duhan15743
6. Kiran.jakkaraju14037
7. chowsys7448
8. Venkateswara Rao6469
9. ashish.cns6342
10. Vamshidhar Matam5424

Active Coders

Refined Tags