/**
*
*/
package saolei;
import java.util.Scanner;
/**
* @author tomtrije
*
*/
public class MineSweeper {
// 唯一实例
private static MineSweeper gameMain;
//
private boolean gameStart, noLongThenMax;// 游戏开始否(true为已经开始),是否允许周边雷数超过周边最大雷数
private int xL, yL, MaxMine, mineNumOfAll;// 水平长,垂直长(长宽),周边最大雷数,全局最大雷数
private int nowTag;// 已标记个数
private int[][] coordinate;// 记录长指令的坐标值,第二维数组下标0为X,1为Y,第一维表示长度
private MineArea[][] mineArr;// 雷块对象
/**
* 判断是否胜利,在排雷后调用
*/
public void winCk() {
int ckSum = 0;
for (int x = 0; x < this.xL; x++) {
for (int y = 0; y < this.yL; y++) {
if (mineArr[x][y].isCk() == true) {
ckSum++;
}
}
}
if ((ckSum + mineNumOfAll) == this.xL * this.yL) {
win();
}
}
/**
* 胜利结果
*/
public void win() {
this.gameStart = false;
System.out.println("恭喜你,你赢了,是否要继续玩?(Y/N)");
String s = null;
do {
Scanner scr = new Scanner(System.in);
s = scr.next();
if (s.length() == 1) {
if (s.equalsIgnoreCase("y")) {
break;
} else if (s.equalsIgnoreCase("n")) {
System.out.println("Bye!");
System.exit(0);
}
}
} while (s.length() != 1);
}
/**
* 初始化数据
*/
public void initialize() {
mineArr = new MineArea[xL][yL];
for (int x = 0; x < xL; x++) {
for (int y = 0; y < yL; y++) {
mineArr[x][y] = new Empty(x + 1, y + 1);
}
}
interMine();
}
/**
* 埋雷
*/
public void interMine() {
int sum = 0;
while (sum < mineNumOfAll) {
int x = (int) (Math.random() * xL);
int y = (int) (Math.random() * yL);
if (mineArr[x][y].getClass() != Mine.class) {
if (noLongThenMax == true) {// 设置最高数开则执行,否则跳过
if (indexOverMax(x, y) == true) {// true表示没有超过限制
mineArr[x][y] = new Mine(x + 1, y + 1);
changeToInfoMine(x, y);
sum++;
}
} else {// 跳过
mineArr[x][y] = new Mine(x + 1, y + 1);
changeToInfoMine(x, y);
sum++;
}
}
}
}
/**
* 埋雷时设置周边雷块index值
*/
public void changeToInfoMine(int x, int y) {
if (x - 1 >= 0) {
if (y - 1 >= 0) {
addIndex(x - 1, y - 1);
}
addIndex(x - 1, y);
if (y + 1 < yL) {
addIndex(x - 1, y + 1);
}
}
if (x + 1 < xL) {
if (y - 1 >= 0) {
addIndex(x + 1, y - 1);
}
addIndex(x + 1, y);
if (y + 1 < yL) {
addIndex(x + 1, y + 1);
}
}
if (y + 1 < yL) {
addIndex(x, y + 1);
}
if (y - 1 >= 0) {
addIndex(x, y - 1);
}
}
/**
* 设置index+1,不是该类型转换为该类型
*/
public void addIndex(int x, int y) {
if (mineArr[x][y].getClass() == InfoMine.class) {
mineArr[x][y].setIndex(1);// 并不是设置为1,而是加1
} else if (mineArr[x][y].getClass() == Empty.class) {
mineArr[x][y] = new InfoMine(x + 1, y + 1);
mineArr[x][y].setIndex(1);
}
}
/**
* 判断当前雷块周边,是否有index值达到上限的
*/
public boolean indexOverMax(int x, int y) {
if (x - 1 >= 0) {
if (y - 1 >= 0) {
if (mineArr[x - 1][y - 1].getIndex() >= MaxMine) {
return false;
}
}
if (mineArr[x - 1][y].getIndex() >= MaxMine) {
return false;
}
if (y + 1 < yL) {
if (mineArr[x - 1][y + 1].getIndex() >= MaxMine) {
return false;
}
}
}
if (x + 1 < xL) {
if (y - 1 >= 0) {
if (mineArr[x + 1][y - 1].getIndex() >= MaxMine) {
return false;
}
}
if (mineArr[x + 1][y].getIndex() >= MaxMine) {
return false;
}
if (y + 1 < yL) {
if (mineArr[x + 1][y + 1].getIndex() >= MaxMine) {
return false;
}
}
}
if (y + 1 < yL) {
if (mineArr[x][y + 1].getIndex() >= MaxMine) {
return false;
}
}
if (y - 1 >= 0) {
if (mineArr[x][y - 1].getIndex() >= MaxMine) {
return false;
}
}
return true;
}
/**
* 游戏界面刷新与显示
*/
public void showMine() {
System.out.print(" ");
for (int y = 0; y < yL; y++) {
System.out.print(" " + MineCon.arr[y % 10]);
}
System.out.println();
for (int x = 0; x < xL; x++) {
System.out.print(MineCon.arr[x % 10]);
for (int y = 0; y < yL; y++) {
System.out.print(" " + mineArr[x][y].getShow());
}
System.out.println();
}
}
/**
* 测试显示雷位
*/
public void showLeiWei() {
System.out.print(" ");
for (int i = 1; i <= yL; i++) {
System.out.print(" " + MineCon.arr[(i - 1) % 10]);
}
System.out.println();
for (int i = 1; i <= xL; i++) {
System.out.print(MineCon.arr[(i - 1) % 10]);
for (int j = 1; j <= yL; j++) {
if (mineArr[i - 1][j - 1].getIndex() < 0) {
System.out.print(" " + "@");
} else if (mineArr[i - 1][j - 1].getIndex() > 0) {
System.out
.print(" "
+ MineCon.arr[(mineArr[i - 1][j - 1]
.getIndex() - 1) % 10]);
} else {
System.out.print(" " + "口");
}
}
System.out.println();
}
}
/**
* 指令读取
*/
public String inputShell() {
String s;
Scanner scr;
while (true) {
System.out.println("请输入指令:");
scr = new Scanner(System.in);
s = scr.nextLine();
int shellId = judgeShell(s);
if (shellId > 0) {
return s;
}
}
}
/**
* 指令确认
*/
public int judgeShell(String shell) {
int shellId = -1;
if (shell.indexOf("remove") == 0) {
if (segShell(shell) == false) {
return -1;
}
shellId = 1;
}
if (shell.indexOf("tag") == 0) {
if (segShell(shell) == false) {
return -1;
}
shellId = 2;
}
if (shell.indexOf("both") == 0) {
if (segShell(shell) == false) {
return -1;
}
shellId = 3;
}
if (shell.length() == 9 && shell.equalsIgnoreCase("removeall")) {
shellId = 4;
}
if (shell.length() == 4 && shell.equalsIgnoreCase("help")) {
shellId = 5;
}
if (shell.length() == 3 && shell.equalsIgnoreCase("set")) {
shellId = 6;
}
if (shell.length() == 4 && shell.equalsIgnoreCase("exit")) {
System.out.println("Bye!");
System.exit(0);
}
return shellId;
}
/**
* 指令分割
*/
public boolean segShell(String shell) {
String[] arr = shell.split(" ");
coordinate = new int[(arr.length - 1) / 2][2];
try {
for (int i = 0; i < coordinate.length; i++) {
coordinate[i][0] = Integer.parseInt(arr[i * 2 + 1]);
coordinate[i][1] = Integer.parseInt(arr[i * 2 + 2]);
}
} catch (Exception e) {
System.out.println("输入的指令正确,但参数错误!");
return false;
}
return true;
}
/**
* 指令执行
*/
public void execShell(int shellId) {
switch (shellId) {
case 1:
for (int i = 0; i < coordinate.length; i++) {
// 排雷工作
removeMine(coordinate[i][0], coordinate[i][1]);
}
break;
case 2:
for (int i = 0; i < coordinate.length; i++) {
// 标记工作
tagMine(coordinate[i][0], coordinate[i][1]);
}
break;
case 3:
for (int i = 0; i < coordinate.length; i++) {
// 多位片区排雷
both(coordinate[i][0], coordinate[i][1]);
}
break;
case 4:
removeAllMine();
break;
case 5:
help();
break;
case 6:
set();
break;
default:
System.out.println("系统出现错误,请联系作者!");
}
}
/**
* 排雷
*/
public void removeMine(int y, int x) {
if (mineArr[x - 1][y - 1].remove() < 0) {
gameOver();
} else if (mineArr[x - 1][y - 1].remove() > 0) {
ckB(x, y);
}
}
/**
* 标记
*/
public void tagMine(int y, int x) {
if (mineArr[x - 1][y - 1].tagOn()) {
this.nowTag++;
} else {
this.nowTag--;
}
}
/**
* 片区排雷
*/
public void both(int y, int x) {
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 3; i++) {
if(x - 2 + j>=0&&x - 2 + j<xL&&y - 2 + i>=0&&y - 2 + i<yL){
mineArr[x - 2 + j][y - 2 + i].setCkFromBoth(true);
if (mineArr[x - 2 + j][y - 2 + i].remove() <0) {
gameOver();
}
if(mineArr[x-2+j][y-2+i].getIndex()==0){
ckB(x-1+j,y-1+i);
}
mineArr[x - 2 + j][y - 2 + i].setCkFromBoth(false);
}
}
}
}
/**
* 空位排雷,排除所有连接空位,以带index值雷块为结束
*/
public void ckB(int x, int y) {
评论0