MineSquare.java
上传用户:penghbff
上传日期:2007-04-16
资源大小:32k
文件大小:9k
源码类别:

射击游戏

开发平台:

Java

  1. import java.awt.event.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.util.*;
  5. import java.io.*;
  6. public class MineSquare extends JPanel implements MouseListener
  7.   int rows=10,cols=10,雷数=10;
  8.   JButton buttonFace=null;
  9.   Block block[][];             
  10.   TimeCount time;              
  11.   CountMine count;            
  12.   String grade=null;          
  13.   RandomSetMine 随机布雷者;
  14.   FindAroundMineNumber 确定周围雷数者;
  15.   FindSafeArea  找出无雷连通区者;
  16.   DetermineDigArea 成片挖开者;
  17.   DetermineMineMarkIsRightOrWrong 确定雷标记是否正确者;
  18.   FindAroundMineMarkNumber 确定周围雷标数目者;
  19.   DecideWinner 判断是否胜利者; 
  20.   Record 记录对话框=null;
  21.   public MineSquare(String grade,int mineNumber,int rows, int cols,JButton b,JDialog dialog)
  22.    { 
  23.      this.grade=grade;
  24.      this.rows=rows;
  25.      this.cols=cols;
  26.      雷数=mineNumber;
  27.      判断是否胜利者=new DecideWinner();
  28.      记录对话框=(Record)dialog;
  29.      block=new Block[rows+1][cols+1];
  30.      buttonFace=b;
  31.      time=new TimeCount();
  32.      count=new CountMine(雷数);
  33.      count.textShowMine.setText(""+雷数);
  34.      setLayout(new GridLayout(rows,cols));
  35.      for(int i=1;i<=rows;i++)
  36.        {for(int j=1;j<=cols;j++)
  37.          { 
  38.            block[i][j]=new Block();
  39.            block[i][j].设置在雷区中的位置(i,j);
  40.          }
  41.        }
  42.      for(int i=1;i<=rows;i++)
  43.        {for(int j=1;j<=cols;j++)
  44.          { 
  45.            add(block[i][j].label);
  46.            block[i][j].addMouseListener(this);
  47.          }
  48.        }
  49.      随机布雷者=new RandomSetMine();
  50.      随机布雷者.随机布雷(雷数,block,rows,cols);
  51.      确定周围雷数者=new FindAroundMineNumber();
  52.      for(int i=1;i<=rows;i++)
  53.        {for(int j=1;j<=cols;j++)
  54.           {        
  55.            确定周围雷数者.设置周围的雷数(i,j,block,rows,cols);
  56.           }
  57.        }
  58.    }
  59.  public void gameOver()
  60.   {
  61.       time.interrupt();
  62.       buttonFace.setIcon(new ImageIcon("哭脸.gif"));
  63.       for(int i=1;i<=rows;i++)
  64.        { for(int j=1;j<=cols;j++)
  65.           {
  66.             block[i][j].removeMouseListener(this);
  67.             if(block[i][j].判断是否是雷()&&block[i][j].isOpaque()&&block[i][j].探雷标记!=0)
  68.                { 
  69.                   block[i][j].requestFocus();
  70.                   block[i][j].setOpaque(false);  
  71.                }
  72.             if(block[i][j].探雷标记==0&&!block[i][j].判断是否是雷())
  73.                { 
  74.                  block[i][j].requestFocus();
  75.                  block[i][j].探雷标记=-1;   
  76.                  block[i][j].repaint();
  77.                  block[i][j].setIcon(new ImageIcon("探雷错误.gif")); 
  78.                }
  79.           }
  80.        }
  81.   } 
  82.  public void mousePressed(MouseEvent e)
  83.   { 
  84.     boolean left=SwingUtilities.isLeftMouseButton(e);
  85.     boolean right=SwingUtilities.isRightMouseButton(e);
  86.     Block blockSource=(Block)e.getSource();
  87.     boolean 雷=blockSource.判断是否是雷();
  88.    if(left==true&&blockSource.isOpaque()==true&&blockSource.获取探雷标记()!=0)
  89.    {   
  90.        try{time.start();  
  91.        }                  
  92.        catch(Exception ee){   
  93.        }
  94.        if(雷==true)
  95.           {  
  96.            blockSource.requestFocus();
  97.            blockSource.setOpaque(false);   
  98.            blockSource.探雷标记=-1;
  99.            blockSource.repaint();
  100.            blockSource.setIcon(new ImageIcon("触雷.gif"));
  101.            gameOver();                    
  102.           }
  103.        else
  104.           {  
  105.             buttonFace.setIcon(new ImageIcon("微笑脸.gif"));
  106.             if(blockSource.周围无雷==false&&blockSource.获取探雷标记()!=0)  
  107.               {
  108.                blockSource.setOpaque(false);  
  109.                blockSource.探雷标记=-1;
  110.                blockSource.repaint();
  111.               }
  112.            
  113.             if(blockSource.周围无雷==true&&blockSource.isOpaque())
  114.               { 
  115.                blockSource.setOpaque(false);     
  116.                Point blockPoint=(Point)blockSource.获取在雷区中的位置();
  117.                int i=blockPoint.x;
  118.                int j=blockPoint.y;
  119.                找出无雷连通区者=new FindSafeArea();
  120.                Vector tree=找出无雷连通区者.确定无雷连通区(i,j,block,rows,cols);
  121.                for(int index=0;index<tree.size();index++)
  122.                  {  
  123.                   Point p=(Point)tree.elementAt(index);
  124.                   int m=p.x,n=p.y;
  125.                   for(int k=Math.max(m-1,1);k<=Math.min(m+1,rows);k++)
  126.                     { 
  127.                       for(int t=Math.max(n-1,1);t<=Math.min(n+1,cols);t++)
  128.                       {
  129.                         if(block[k][t].探雷标记==0)
  130.                             {  
  131.                                gameOver();
  132.                             }
  133.                         else{  
  134.                              block[k][t].requestFocus();
  135.                              block[k][t].setOpaque(false);  
  136.                              block[k][t].探雷标记=-1;
  137.                              block[k][t].repaint();
  138.                             }
  139.                       } 
  140.                     }
  141.                  }
  142.                tree.removeAllElements();                   
  143.               }
  144.           }
  145.     }
  146.    if(right)
  147.     { 
  148.       if(blockSource.探雷标记==0&&blockSource.isOpaque())
  149.          {                                             
  150.            count.countMineSub();                      
  151.          }
  152.       if(blockSource.探雷标记==1&&blockSource.isOpaque())
  153.          {                                           
  154.            count.countMineAdd();                    
  155.          }
  156.     }
  157.  }
  158.  public void mouseEntered(MouseEvent e)
  159.   {}
  160.  public void mouseReleased(MouseEvent e)
  161.   { 
  162.     boolean success=判断是否胜利者.判断是否成功(雷数,block,rows,cols);
  163.     if(success==true)
  164.     { 
  165.            time.interrupt();                        
  166.            记录对话框.setGrade(grade);             
  167.            int spendTime=Integer.parseInt(time.textShowTime.getText());  
  168.            记录对话框.setTime(spendTime);
  169.           
  170.            File f=new File("英雄榜.txt");
  171.            try{
  172.                 FileInputStream in=new FileInputStream(f);
  173.                 ObjectInputStream object_in=new ObjectInputStream(in);
  174.                 Hashtable hashtable=(Hashtable)object_in.readObject();  
  175.                 object_in.close();
  176.                 in.close(); 
  177.                 String temp=(String)hashtable.get(grade);            
  178.                 StringTokenizer fenxi=new StringTokenizer(temp,"#"); 
  179.                 fenxi.nextToken();
  180.                 int recordTime=Integer.parseInt(fenxi.nextToken());  
  181.                  if(spendTime<recordTime)                           
  182.                    记录对话框.setVisible(true);                     
  183.                }
  184.             catch(Exception eee)
  185.                {
  186.                }
  187.     } 
  188.   }
  189.  public void mouseExited(MouseEvent e)
  190.   {}
  191.  public void mouseClicked(MouseEvent e)
  192.   {  
  193.     if(SwingUtilities.isRightMouseButton(e)) 
  194.     { Block blockSource=(Block)e.getSource();
  195.       Point blockPoint=(Point)blockSource.获取在雷区中的位置();
  196.                int i=blockPoint.x;
  197.                int j=blockPoint.y;
  198.       确定周围雷标数目者=new FindAroundMineMarkNumber();
  199.       int 周围雷标数=确定周围雷标数目者.获取周围雷标记的数目(i,j,block,rows,cols);
  200.       int 周围雷数=blockSource.获取周围的雷数();
  201.       确定雷标记是否正确者=new DetermineMineMarkIsRightOrWrong();
  202.       boolean correct=确定雷标记是否正确者.判断雷标记是否正确(i,j,block,rows,cols);
  203.       if(blockSource.isOpaque()==false&&周围雷数>0&&correct)
  204.        {  
  205.           成片挖开者=new DetermineDigArea();
  206.           Vector tree=成片挖开者.确定成片挖开区域(i,j,block,rows,cols);
  207.           for(int index=0;index<tree.size();index++)
  208.              {
  209.                 Point p=(Point)tree.elementAt(index);
  210.                 int m=p.x,n=p.y;
  211.                 for(int k=Math.max(m-1,1);k<=Math.min(m+1,rows);k++)
  212.                    {
  213.                     
  214.                      for(int t=Math.max(n-1,1);t<=Math.min(n+1,cols);t++)
  215.                       { 
  216.                          if(block[k][t].isOpaque()&&!block[k][t].判断是否是雷()&&
  217.                             block[k][t].探雷标记!=0)
  218.                           {
  219.                             block[k][t].requestFocus();
  220.                             block[k][t].setOpaque(false);  
  221.                             block[k][t].探雷标记=-1;
  222.                             block[k][t].repaint();
  223.                           }
  224.                         repaint();
  225.                       } 
  226.                     }
  227.               }
  228.            tree.removeAllElements();
  229.        } 
  230.        if(blockSource.isOpaque()==false&&周围雷标数==周围雷数&&!correct)
  231.        { 
  232.          gameOver();                  
  233.        }
  234.       if((blockSource.isOpaque())==false&&(周围雷标数!=周围雷数))
  235.        {
  236.        }
  237.    }
  238.   }
  239. }