1 packagemap;2
3 import java.awt.*;4 import java.awt.event.*;5 import java.awt.geom.*;6
7 import javax.swing.*;8 importjavax.swing.border.Border;9
10 importjava.util.LinkedList;11 importjava.util.Queue;12 importjava.util.Random;13
14 public class MapFrame extendsJFrame {15 public introw,col,mine;16 public int[][] map;17 public boolean[][] visit;18 publicJButton[][] mine_button;19 public static final int[] dx=new int[] {-1,0,1,0,-1,-1,1,1};20 public static final int[] dy=new int[] {0,1,0,-1,-1,1,1,-1};21
22 public inttotal_visit;23
24 publicMapFrame(Setting setting) {25 this.row=setting.row; this.col=setting.col; this.mine=setting.mine;26 this.map=calc_map(setting);27 mine_button=newJButton[row][col];28 visit=new boolean[row][col];29 total_visit=0;30 int fwidth=row/9*400,fheight=col/9*400+70;31 setSize(fwidth,fheight);32 JPanel TimeJpanel=newJPanel();33 JPanel MapJpanel=newJPanel();34
35 JLabel TimeText=new JLabel("Time : 0");36 TimeJpanel.add(TimeText);37
38 myTimer timer =newmyTimer(TimeText);39 Timer timelistener =new Timer(1000,timer);40 timelistener.start(); //开始定时器
41
42 MapJpanel.setPreferredSize(new Dimension(row/9*400,col/9*400));43 MapJpanel.setLayout(newGridLayout(row,col));44 for (int i=0;i
53 setLayout(newBorderLayout());54 add(TimeJpanel,BorderLayout.NORTH);55 add(MapJpanel,BorderLayout.SOUTH);56 }57
58 class click_listener implementsActionListener {59 public intx,y;60 public click_listener(int x,inty) {61 this.x=x; this.y=y;62 }63
64 public voidactionPerformed(ActionEvent event) {65 if (visit[this.x][this.y]) return;66 int num=map[this.x][this.y];67 if (num!=-1) {68 mine_button[this.x][this.y].setBackground(Color.WHITE);69 if (num==0) whitebfs(this.x,this.y);70 else{71 if (!visit[this.x][this.y]) total_visit++; visit[this.x][this.y]=true;72 mine_button[this.x][this.y].setText(""+num);73 }74 if (total_visit+mine==row*col) {75 //游戏成功
76 int selection=JOptionPane.showConfirmDialog(null,"你成功排掉所有雷,是否重新开始?","游戏成功",77 JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);78 if (selection==JOptionPane.YES_OPTION) {79 //重新开始游戏
80 dispose();81 Map.NewMap();82 }83 }84 } else{85 //游戏失败
86 mine_button[this.x][this.y].setIcon(new ImageIcon("./Image/mine.jpg"));87 int selection=JOptionPane.showConfirmDialog(null,"你踩到雷了,是否重新开始?","游戏失败",88 JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);89 if (selection==JOptionPane.YES_OPTION) {90 //重新开始游戏
91 dispose();92 Map.NewMap();93 }94 }95 }96
97 public void whitebfs(int x,inty) {98 Queue q=new LinkedList<>();99 q.offer(x*col+y);100 while (q.size()>0) {101 int now=q.poll();102 x=now/col; y=now%col;103 if (!visit[x][y]) total_visit++; visit[x][y]=true;104 mine_button[x][y].setBackground(Color.WHITE);105 around(x,y);106 for (int i=0;i<4;i++) {107 int tx=x+dx[i],ty=y+dy[i];108 if (tx<0 || tx>=row || ty<0 || ty>=col) continue;109 if (visit[tx][ty]) continue;110 if (map[tx][ty]!=0) continue;111 q.offer(tx*col+ty);112 }113 }114 }115
116 public void around(int x,inty) {117 for (int i=0;i<8;i++) {118 int tx=x+dx[i],ty=y+dy[i];119 if (tx<0 || tx>=row || ty<0 || ty>=col) continue;120 if (map[tx][ty]>0) {121 if (!visit[tx][ty]) total_visit++; visit[tx][ty]=true;122 mine_button[tx][ty].setBackground(Color.WHITE);123 if (map[tx][ty]>0)mine_button[tx][ty].setText(""+map[tx][ty]);124 }125 }126 }127 }128
129 /**
130 * 计时器131 */
132 class myTimer implementsActionListener {133 JLabel text;134 longstart;135 publicmyTimer(JLabel text) {136 this.text=text;137 start=System.currentTimeMillis();138 }139 public voidactionPerformed(ActionEvent event) {140 text.setText("Time : "+(System.currentTimeMillis()-start)/1000);141 }142 }143
144 /**
145 * 计算map数字大小146 */
147 public int[][] calc_map(Setting setting) {148 int[][] map=new int[setting.row+2][setting.col+2];149 for (int i=0;i<=setting.row+1;i++)150 for (int j=0;j<=setting.col+1;j++)151 map[i][j]=0;152
153 int now_mine=0;154 Random rand=newRandom(System.currentTimeMillis());155 while (now_mine
164 for (int i=1;i<=setting.row;i++)165 for (int j=1;j<=setting.col;j++) {166 if (map[i][j]==-1) continue;167 for (int k=0;k<8;k++) map[i][j]+=(map[i+dx[k]][j+dy[k]]==-1 ? 1 : 0);168 }169
170 returnmap;171 }172 }