📄 minepanel.java
字号:
}
}
/**
* 处理地雷按钮左键单击(当释放鼠标左键时激发),踩地雷按钮
* 如果中雷牺牲,就处理地雷爆炸方法
* 如果是“1”~“8”,就显示出来
* 如果是“0”,就处理连翻(递归算法)
*/
public void treadMineButton(MineButton mb) {
if (state != READY && state != PLAY)
return;
if (state == READY || state == PAUSE) {
timer.start();
state = PLAY;
}
if (mb.getIconStyle() != MineButton.ICON_NULL
&& mb.getIconStyle() != MineButton.ICON_MARK) {
return;
}
// 是地雷
if (mb.isMine()) {
mb.setIconStyle(MineButton.ICON_MINE_EXPLODED);
doLose();
return;
}
// 不是地雷
int aroundMines = countAroundMines(mb);
mb.setIconStyle(aroundMines);
// 处理连翻
if (aroundMines == 0) {
int h = mb.getRow();
int w = mb.getColumn();
if (h > 0) {
treadMineButton(mineButton[h-1][w]);
}
if (h < mineRows-1) {
treadMineButton(mineButton[h+1][w]);
}
if (w > 0) {
treadMineButton(mineButton[h][w-1]);
}
if (w < mineColumns-1) {
treadMineButton(mineButton[h][w+1]);
}
if (h > 0 && w > 0) {
treadMineButton(mineButton[h-1][w-1]);
}
if (h > 0 && w < mineColumns-1) {
treadMineButton(mineButton[h-1][w+1]);
}
if (h < mineRows-1 && w > 0) {
treadMineButton(mineButton[h+1][w-1]);
}
if (h < mineRows-1 && w < mineColumns-1) {
treadMineButton(mineButton[h+1][w+1]);
}
} // if (aroundMines == 0)
if (checkWinning()) {
doWin();
}
}
/**
* 处理扫雷胜利的方法
*/
private void doWin() {
state = WIN; // 胜利结束了!
timer.stop();
faceButton.setIconStyle(FaceButton.FACE_WIN);
// 把有地雷的按钮标记上旗帜
for (int i = 0; i < mineRows; i++) {
for (int j = 0; j < mineColumns; j++) {
if (mineButton[i][j].isMine()
&& mineButton[i][j].getIconStyle() != MineButton.ICON_FLAG) {
mineButton[i][j].setIconStyle(MineButton.ICON_FLAG);
}
}
}
remainMines.setNumber(0);
if (gameLevel == CUSTOM_LEVEL)
return;
if (JMine.mineProps.isRecordBreaker(gameLevel, totalTime.getNumber())) {
String title = "破纪录啦!";
String gameLevelStr = null;
if (gameLevel == LOW_LEVEL)
gameLevelStr = "初";
if (gameLevel == MIDDLE_LEVEL)
gameLevelStr = "中";
if (gameLevel == HIGH_LEVEL)
gameLevelStr = "高";
JLabel message1 = new JLabel("已破" + gameLevelStr + "级记录。");
JLabel message2 = new JLabel("请留尊姓大名。");
JTextField nameField = new JTextField("佚名");
//nameField.setText("佚名");
// 重新设置菜单的字体
Font font = new Font(message1.getFont().getName(), Font.PLAIN, 12);
message1.setFont(font);
message2.setFont(font);
nameField.setFont(font);
if (isSound) {
Toolkit.getDefaultToolkit().beep();
}
JOptionPane.showMessageDialog(
this, // 父组件
new Object[] {message1, message2, nameField}, // 显示的信息数组
title, // 对话框的标题
JOptionPane.INFORMATION_MESSAGE // 信息类型
);
JMine.mineProps.setRecord(gameLevel, nameField.getText(), totalTime.getNumber());
jmine.showTopListDialog();
}
}
/**
* 测试是否已经取得扫雷任务的胜利
*/
public boolean checkWinning() {
for (int i = 0; i < mineRows; i++) {
for (int j = 0; j < mineColumns; j++) {
// 检查是否所有没有地雷的按钮已全部被试探过
if (!mineButton[i][j].isMine()) {
int iconStyle = mineButton[i][j].getIconStyle();
if (iconStyle ==MineButton.ICON_NULL
|| iconStyle == MineButton.ICON_MARK
|| iconStyle == MineButton.ICON_FLAG) {
return false;
}
}
}
}
return true;
}
/**
* 翻开所有地雷按钮
*/
private void openAllMines() {
for (int i = 0; i < mineRows; i++) {
for (int j = 0; j < mineColumns; j++) {
switch (mineButton[i][j].getIconStyle()) {
case MineButton.ICON_MARK: // drop down
case MineButton.ICON_NULL:
if (mineButton[i][j].isMine()) {
mineButton[i][j].setIconStyle(MineButton.ICON_MINE_BLACK);
}
else {
mineButton[i][j].setIconStyle(countAroundMines(mineButton[i][j]));
}
break;
case MineButton.ICON_FLAG:
if (!mineButton[i][j].isMine()) {
mineButton[i][j].setIconStyle(MineButton.ICON_MINE_ERROR);
}
break;
default: // ICON_M0~M8 ICON_MINE_BLACK ICON_MINE_ERROR ICON_MINE_BURST
break;
} // switch
} // for (j)
} // for (i)
} // openAllMines()
/**
* 对踩爆地雷的处理
*/
private void doLose() {
state = LOSE; // 失败结束——被地雷炸死了!
timer.stop();
faceButton.setIconStyle(FaceButton.FACE_LOSE);
if (isSound) {
Toolkit.getDefaultToolkit().beep();
}
// 掀开所有地雷或标记错误的地雷
for (int i = 0; i < mineRows; i++) {
for (int j = 0; j < mineColumns; j++) {
int iconStyle = mineButton[i][j].getIconStyle();
boolean isMine = mineButton[i][j].isMine();
if ((iconStyle == MineButton.ICON_NULL
|| iconStyle == MineButton.ICON_MARK)
&& isMine) {
mineButton[i][j].setIconStyle(MineButton.ICON_MINE_BLACK);
}
if (iconStyle == MineButton.ICON_FLAG && !isMine) {
mineButton[i][j].setIconStyle(MineButton.ICON_MINE_ERROR);
}
} // for (j)
} // for (i)
} // openAllMines()
/**
* @param mb 地雷按钮
* @return 地雷按钮周围的地雷总数
*/
private int countAroundMines(MineButton mb) {
int h = mb.getRow();
int w = mb.getColumn();
int aroundMines = 0;
if (h > 0) {
aroundMines += (mineButton[h-1][w].isMine() ? 1 : 0);
}
if (h < mineRows-1) {
aroundMines += (mineButton[h+1][w].isMine() ? 1 : 0);
}
if (w > 0) {
aroundMines += (mineButton[h][w-1].isMine() ? 1 : 0);
}
if (w < mineColumns-1) {
aroundMines += (mineButton[h][w+1].isMine() ? 1 : 0);
}
if (h > 0 && w > 0) {
aroundMines += (mineButton[h-1][w-1].isMine() ? 1 : 0);
}
if (h > 0 && w < mineColumns-1) {
aroundMines += (mineButton[h-1][w+1].isMine() ? 1 : 0);
}
if (h < mineRows-1 && w > 0) {
aroundMines += (mineButton[h+1][w-1].isMine() ? 1 : 0);
}
if (h < mineRows-1 && w < mineColumns-1) {
aroundMines += (mineButton[h+1][w+1].isMine() ? 1 : 0);
}
return aroundMines;
}
/**
* @return 地雷按钮周围的红旗总数
*/
private int countAroundFlags(MineButton mb) {
int h = mb.getRow();
int w = mb.getColumn();
int aroundFlags = 0;
if (h > 0) {
aroundFlags += (mineButton[h-1][w].getIconStyle() == MineButton.ICON_FLAG ? 1 : 0);
}
if (h < mineRows-1) {
aroundFlags += (mineButton[h+1][w].getIconStyle() == MineButton.ICON_FLAG ? 1 : 0);
}
if (w > 0) {
aroundFlags += (mineButton[h][w-1].getIconStyle() == MineButton.ICON_FLAG ? 1 : 0);
}
if (w < mineColumns-1) {
aroundFlags += (mineButton[h][w+1].getIconStyle() == MineButton.ICON_FLAG ? 1 : 0);
}
if (h > 0 && w > 0) {
aroundFlags += (mineButton[h-1][w-1].getIconStyle() == MineButton.ICON_FLAG ? 1 : 0);
}
if (h > 0 && w < mineColumns-1) {
aroundFlags += (mineButton[h-1][w+1].getIconStyle() == MineButton.ICON_FLAG ? 1 : 0);
}
if (h < mineRows-1 && w > 0) {
aroundFlags += (mineButton[h+1][w-1].getIconStyle() == MineButton.ICON_FLAG ? 1 : 0);
}
if (h < mineRows-1 && w < mineColumns-1) {
aroundFlags += (mineButton[h+1][w+1].getIconStyle() == MineButton.ICON_FLAG ? 1 : 0);
}
return aroundFlags;
}
/**
* 使指定地雷按钮周围的按钮边框为凹入型
* @param mb 地雷按钮
*/
public void doDownAround(MineButton mb) {
int h = mb.getRow();
int w = mb.getColumn();
Vector vAroundMines = new Vector(8);
if (h > 0) {
vAroundMines.add(mineButton[h-1][w]);
}
if (h < mineRows-1) {
vAroundMines.add(mineButton[h+1][w]);
}
if (w > 0) {
vAroundMines.add(mineButton[h][w-1]);
}
if (w < mineColumns-1) {
vAroundMines.add(mineButton[h][w+1]);
}
if (h > 0 && w > 0) {
vAroundMines.add(mineButton[h-1][w-1]);
}
if (h > 0 && w < mineColumns-1) {
vAroundMines.add(mineButton[h-1][w+1]);
}
if (h < mineRows-1 && w > 0) {
vAroundMines.add(mineButton[h+1][w-1]);
}
if (h < mineRows-1 && w < mineColumns-1) {
vAroundMines.add(mineButton[h+1][w+1]);
}
for (int i = 0; i < vAroundMines.size(); i++) {
MineButton mb2 = (MineButton) vAroundMines.elementAt(i);
if (mb2.getIconStyle() == MineButton.ICON_NULL
|| mb2.getIconStyle() == MineButton.ICON_MARK) {
mb2.setBorderDown();
}
}
vAroundMines.clear();
}
/**
* 使指定地雷按钮周围的按钮边框为凸出型
*/
public void doUpAround(MineButton mb) {
int h = mb.getRow();
int w = mb.getColumn();
Vector vAroundMines = new Vector(8);
if (h > 0) {
vAroundMines.add(mineButton[h-1][w]);
}
if (h < mineRows-1) {
vAroundMines.add(mineButton[h+1][w]);
}
if (w > 0) {
vAroundMines.add(mineButton[h][w-1]);
}
if (w < mineColumns-1) {
vAroundMines.add(mineButton[h][w+1]);
}
if (h > 0 && w > 0) {
vAroundMines.add(mineButton[h-1][w-1]);
}
if (h > 0 && w < mineColumns-1) {
vAroundMines.add(mineButton[h-1][w+1]);
}
if (h < mineRows-1 && w > 0) {
vAroundMines.add(mineButton[h+1][w-1]);
}
if (h < mineRows-1 && w < mineColumns-1) {
vAroundMines.add(mineButton[h+1][w+1]);
}
for (int i = 0; i < vAroundMines.size(); i++) {
MineButton mb2 = (MineButton) vAroundMines.elementAt(i);
if (mb2.getIconStyle() == MineButton.ICON_NULL
|| mb2.getIconStyle() == MineButton.ICON_MARK) {
mb2.setBorderUp();
}
}
vAroundMines.clear();
}
/**
* 处理左右双钮同时释放的鼠标事件
*/
public void doDoubleDownReleased(MineButton mb) {
int iconStyle = mb.getIconStyle();
int h = mb.getRow();
int w = mb.getColumn();
Vector vAroundMines = new Vector(8);
if (h > 0) {
vAroundMines.add(mineButton[h-1][w]);
}
if (h < mineRows-1) {
vAroundMines.add(mineButton[h+1][w]);
}
if (w > 0) {
vAroundMines.add(mineButton[h][w-1]);
}
if (w < mineColumns-1) {
vAroundMines.add(mineButton[h][w+1]);
}
if (h > 0 && w > 0) {
vAroundMines.add(mineButton[h-1][w-1]);
}
if (h > 0 && w < mineColumns-1) {
vAroundMines.add(mineButton[h-1][w+1]);
}
if (h < mineRows-1 && w > 0) {
vAroundMines.add(mineButton[h+1][w-1]);
}
if (h < mineRows-1 && w < mineColumns-1) {
vAroundMines.add(mineButton[h+1][w+1]);
}
if (iconStyle >= MineButton.ICON_M0 && iconStyle <= MineButton.ICON_M8
&& countAroundMines(mb) == countAroundFlags(mb)) {
for (int i = 0; i < vAroundMines.size(); i++) {
// 按下周围的各个按钮
treadMineButton((MineButton) vAroundMines.elementAt(i));
}
}
else {
if (mb.getIconStyle() == MineButton.ICON_NULL
|| mb.getIconStyle() == MineButton.ICON_MARK) {
mb.setBorderUp();// 凸起本按钮
}
doUpAround(mb); // 凸起周围的各个按钮
}
vAroundMines.clear();
} // doDoubleDownReleased()
/**
* 时钟暂停计时
*/
public void pause() {
if (state == PLAY) {
timer.stop();
state = PAUSE;
}
}
/**
* 时钟继续计时
*/
public void resume() {
if (state == PAUSE) {
timer.start();
state = PLAY;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -