?? minesquare.java
字號:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class MineSquare extends JPanel implements MouseListener
{
int rows=10,cols=10,雷數=10;
JButton buttonFace=null;
Block block[][];
TimeCount time;
CountMine count;
String grade=null;
RandomSetMine 隨機布雷者;
FindAroundMineNumber 確定周圍雷數者;
FindSafeArea 找出無雷連通區者;
DetermineDigArea 成片挖開者;
DetermineMineMarkIsRightOrWrong 確定雷標記是否正確者;
FindAroundMineMarkNumber 確定周圍雷標數目者;
DecideWinner 判斷是否勝利者;
Record 記錄對話框=null;
public MineSquare(String grade,int mineNumber,int rows, int cols,JButton b,JDialog dialog)
{
this.grade=grade;
this.rows=rows;
this.cols=cols;
雷數=mineNumber;
判斷是否勝利者=new DecideWinner();
記錄對話框=(Record)dialog;
block=new Block[rows+1][cols+1];
buttonFace=b;
time=new TimeCount();
count=new CountMine(雷數);
count.textShowMine.setText(""+雷數);
setLayout(new GridLayout(rows,cols));
for(int i=1;i<=rows;i++)
{for(int j=1;j<=cols;j++)
{
block[i][j]=new Block();
block[i][j].設置在雷區中的位置(i,j);
}
}
for(int i=1;i<=rows;i++)
{for(int j=1;j<=cols;j++)
{
add(block[i][j].label);
block[i][j].addMouseListener(this);
}
}
隨機布雷者=new RandomSetMine();
隨機布雷者.隨機布雷(雷數,block,rows,cols);
確定周圍雷數者=new FindAroundMineNumber();
for(int i=1;i<=rows;i++)
{for(int j=1;j<=cols;j++)
{
確定周圍雷數者.設置周圍的雷數(i,j,block,rows,cols);
}
}
}
public void gameOver()
{
time.interrupt();
buttonFace.setIcon(new ImageIcon("哭臉.gif"));
for(int i=1;i<=rows;i++)
{ for(int j=1;j<=cols;j++)
{
block[i][j].removeMouseListener(this);
if(block[i][j].判斷是否是雷()&&block[i][j].isOpaque()&&block[i][j].探雷標記!=0)
{
block[i][j].requestFocus();
block[i][j].setOpaque(false);
}
if(block[i][j].探雷標記==0&&!block[i][j].判斷是否是雷())
{
block[i][j].requestFocus();
block[i][j].探雷標記=-1;
block[i][j].repaint();
block[i][j].setIcon(new ImageIcon("探雷錯誤.gif"));
}
}
}
}
public void mousePressed(MouseEvent e)
{
boolean left=SwingUtilities.isLeftMouseButton(e);
boolean right=SwingUtilities.isRightMouseButton(e);
Block blockSource=(Block)e.getSource();
boolean 雷=blockSource.判斷是否是雷();
if(left==true&&blockSource.isOpaque()==true&&blockSource.獲取探雷標記()!=0)
{
try{time.start();
}
catch(Exception ee){
}
if(雷==true)
{
blockSource.requestFocus();
blockSource.setOpaque(false);
blockSource.探雷標記=-1;
blockSource.repaint();
blockSource.setIcon(new ImageIcon("觸雷.gif"));
gameOver();
}
else
{
buttonFace.setIcon(new ImageIcon("微笑臉.gif"));
if(blockSource.周圍無雷==false&&blockSource.獲取探雷標記()!=0)
{
blockSource.setOpaque(false);
blockSource.探雷標記=-1;
blockSource.repaint();
}
if(blockSource.周圍無雷==true&&blockSource.isOpaque())
{
blockSource.setOpaque(false);
Point blockPoint=(Point)blockSource.獲取在雷區中的位置();
int i=blockPoint.x;
int j=blockPoint.y;
找出無雷連通區者=new FindSafeArea();
Vector tree=找出無雷連通區者.確定無雷連通區(i,j,block,rows,cols);
for(int index=0;index<tree.size();index++)
{
Point p=(Point)tree.elementAt(index);
int m=p.x,n=p.y;
for(int k=Math.max(m-1,1);k<=Math.min(m+1,rows);k++)
{
for(int t=Math.max(n-1,1);t<=Math.min(n+1,cols);t++)
{
if(block[k][t].探雷標記==0)
{
gameOver();
}
else{
block[k][t].requestFocus();
block[k][t].setOpaque(false);
block[k][t].探雷標記=-1;
block[k][t].repaint();
}
}
}
}
tree.removeAllElements();
}
}
}
if(right)
{
if(blockSource.探雷標記==0&&blockSource.isOpaque())
{
count.countMineSub();
}
if(blockSource.探雷標記==1&&blockSource.isOpaque())
{
count.countMineAdd();
}
}
}
public void mouseEntered(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{
boolean success=判斷是否勝利者.判斷是否成功(雷數,block,rows,cols);
if(success==true)
{
time.interrupt();
記錄對話框.setGrade(grade);
int spendTime=Integer.parseInt(time.textShowTime.getText());
記錄對話框.setTime(spendTime);
File f=new File("英雄榜.txt");
try{
FileInputStream in=new FileInputStream(f);
ObjectInputStream object_in=new ObjectInputStream(in);
Hashtable hashtable=(Hashtable)object_in.readObject();
object_in.close();
in.close();
String temp=(String)hashtable.get(grade);
StringTokenizer fenxi=new StringTokenizer(temp,"#");
fenxi.nextToken();
int recordTime=Integer.parseInt(fenxi.nextToken());
if(spendTime<recordTime)
記錄對話框.setVisible(true);
}
catch(Exception eee)
{
}
}
}
public void mouseExited(MouseEvent e)
{}
public void mouseClicked(MouseEvent e)
{
if(SwingUtilities.isRightMouseButton(e))
{ Block blockSource=(Block)e.getSource();
Point blockPoint=(Point)blockSource.獲取在雷區中的位置();
int i=blockPoint.x;
int j=blockPoint.y;
確定周圍雷標數目者=new FindAroundMineMarkNumber();
int 周圍雷標數=確定周圍雷標數目者.獲取周圍雷標記的數目(i,j,block,rows,cols);
int 周圍雷數=blockSource.獲取周圍的雷數();
確定雷標記是否正確者=new DetermineMineMarkIsRightOrWrong();
boolean correct=確定雷標記是否正確者.判斷雷標記是否正確(i,j,block,rows,cols);
if(blockSource.isOpaque()==false&&周圍雷數>0&&correct)
{
成片挖開者=new DetermineDigArea();
Vector tree=成片挖開者.確定成片挖開區域(i,j,block,rows,cols);
for(int index=0;index<tree.size();index++)
{
Point p=(Point)tree.elementAt(index);
int m=p.x,n=p.y;
for(int k=Math.max(m-1,1);k<=Math.min(m+1,rows);k++)
{
for(int t=Math.max(n-1,1);t<=Math.min(n+1,cols);t++)
{
if(block[k][t].isOpaque()&&!block[k][t].判斷是否是雷()&&
block[k][t].探雷標記!=0)
{
block[k][t].requestFocus();
block[k][t].setOpaque(false);
block[k][t].探雷標記=-1;
block[k][t].repaint();
}
repaint();
}
}
}
tree.removeAllElements();
}
if(blockSource.isOpaque()==false&&周圍雷標數==周圍雷數&&!correct)
{
gameOver();
}
if((blockSource.isOpaque())==false&&(周圍雷標數!=周圍雷數))
{
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -