📄 board.java
字号:
import java.awt.Graphics;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class Board extends JPanel {
private ArrayList<BaseButton> btns = null;
Buttons bImgs;
private int userMine = 0;
private int remainMine = Game.mineNum;
private Boolean isEnd = false;
private Game parent;
public int getRemainMine(){
return remainMine;
}
public void setParent(Game g){
parent = g;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(btns != null){
for(BaseButton b : btns){
if(b != null)
b.draw(g);
}
}
}
public Board(){
bImgs = new Buttons();
btns = new ArrayList<BaseButton>();
this.addMouseListener(new MouseListener()
{
public void mousePressed(MouseEvent e){
if(isEnd)
return;
// JOptionPane.showMessageDialog(null,Integer.toString(calculatePos(e.getX(),e.getY()).getX()));
Position p = calculatePos(e.getX(),e.getY());
int i = getBtnIndex(p.getX(),p.getY());
// JOptionPane.showMessageDialog(null,Integer.toString(i));
BaseButton b = btns.get(i);
if(e.getButton() == MouseEvent.BUTTON1){
if(b.isMine == false){
b.type = ButtonType.UserEmpty;
b.setImage(bImgs);
detect(p.getX(),p.getY());
}
else{
//over
for(BaseButton bb : btns){
if(bb.isMine){
bb.type = ButtonType.Mine;
bb.setImage(bImgs);
}
}
b.type = ButtonType.Explosion;
b.setImage(bImgs);
// JOptionPane.showMessageDialog(null,"You lose");
isEnd = true;
}
}
else if(e.getButton() == MouseEvent.BUTTON3){
if(b.type == ButtonType.Empty){
b.type = ButtonType.UserMine;
b.setImage(bImgs);
userMine++;
parent.getNumberLabel().setText( "Mine Remaining:"+ Integer.toString(Game.mineNum - userMine) );
if(b.isMine){
remainMine--;
if(remainMine == 0){
JOptionPane.showMessageDialog(null,"Cong!!");
isEnd = true;
}
}
}
else if(b.type == ButtonType.UserMine){
b.type = ButtonType.Empty;
b.setImage(bImgs);
userMine--;
parent.getNumberLabel().setText( "Mine Remaining:"+ Integer.toString(Game.mineNum - userMine) );
if(b.isMine){
remainMine++;
}
}
}
repaint();
}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
}
);
}
private void detect(int x,int y){
int ox = 0;
int oy = 0;
Boolean flag = true;
for(int i = 0; i < 8;i++){
switch(i){
case 0:
ox = 1;oy = 1;break;
case 1:
ox = 1;oy = 0;break;
case 2:
ox = 1;oy = -1;break;
case 3:
ox = 0;oy = 1;break;
case 4:
ox = 0;oy = -1;break;
case 5:
ox = -1;oy = 1;break;
case 6:
ox = -1;oy = 0;break;
case 7:
ox = -1;oy = -1;break;
}
if(x+ox>=0&&y+oy>=0&&x+ox<Game.boardSize&&y+oy<Game.boardSize){
BaseButton bb = btns.get(getBtnIndex(x+ox,y+oy));
if(bb.type != ButtonType.UserEmpty){
if(bb.isMine == true){
flag = false;
}
}
}
}
if(flag){
for(int i = 0; i < 8;i++){
switch(i){
case 0:
ox = 1;oy = 1;break;
case 1:
ox = 1;oy = 0;break;
case 2:
ox = 1;oy = -1;break;
case 3:
ox = 0;oy = 1;break;
case 4:
ox = 0;oy = -1;break;
case 5:
ox = -1;oy = 1;break;
case 6:
ox = -1;oy = 0;break;
case 7:
ox = -1;oy = -1;break;
}
if(x+ox>=0&&y+oy>=0&&x+ox<Game.boardSize&&y+oy<Game.boardSize){
BaseButton bb = btns.get(getBtnIndex(x+ox,y+oy));
if(bb.type != ButtonType.UserEmpty){
bb.type = ButtonType.UserEmpty;
bb.setImage(bImgs);
detect(x+ox,y+oy);
}
}
}
}
}
public void initBoard(){
isEnd = false;
userMine = 0;
remainMine = Game.mineNum;
btns.clear();
for(int i = 0;i < Game.boardSize;i++){
for(int j = 0;j < Game.boardSize;j++){
BaseButton m = new BaseButton(new Position(i,j));
m.type = ButtonType.Empty;
m.setImage(bImgs);
btns.add(m);
}
}
Random r = new Random();
int count = 0;
do{
int x = r.nextInt(Game.boardSize);
int y = r.nextInt(Game.boardSize);
// Position p = new Position(x,y);
// boolean f = false;
// for(BaseButton bb : btns){
// if(bb.getPosition().equal(p)&&bb.isMine == true){
// f = true;
// }
// }
int i = this.getBtnIndex(x, y);
if(btns.get(i).isMine)
continue;
btns.get(getBtnIndex(x,y)).isMine = true;
/////////Test//////////
//btns.get(getBtnIndex(x,y)).type = ButtonType.UserMine;
//btns.get(getBtnIndex(x,y)).setImage(bImgs);
///////////////////////
initEmpty(x,y);
count++;
}while(count<Game.mineNum);
repaint();
}
private void initEmpty(int x,int y){
int ox = 0;
int oy = 0;
for(int i = 0; i < 8;i++){
switch(i){
case 0:
ox = 1;oy = 1;break;
case 1:
ox = 1;oy = 0;break;
case 2:
ox = 1;oy = -1;break;
case 3:
ox = 0;oy = 1;break;
case 4:
ox = 0;oy = -1;break;
case 5:
ox = -1;oy = 1;break;
case 6:
ox = -1;oy = 0;break;
case 7:
ox = -1;oy = -1;break;
}
if(x+ox>=0&&y+oy>=0&&x+ox<Game.boardSize&&y+oy<Game.boardSize){
BaseButton bb = btns.get(getBtnIndex(x+ox,y+oy));
if(bb.isMine != true){
bb.num++;
}
}
}
}
public int getBtnIndex(int x,int y){
return y*Game.boardSize+x;
}
private Position calculatePos(int x,int y){
Position p = new Position(y/Game.BTNSIZE,x/Game.BTNSIZE);
return p;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -