实现原理
扫雷实现第一步就是生成棋盘,第二步就要生成每个棋子,定义棋子属性是否是地雷,第三部就是给不是地雷的棋子赋值(附近多少地雷)。
直接贴代码
/**
* 扫雷棋盘
*/
public class DemoTest {
public static void main(String[] args) {
int[][] arrs = generate();
print(arrs);
}
private static void print(int[][] arrs) {
if (arrs == null || arrs.length == 0) {
return;
}
for (int i = 0; i < arrs.length; i++) {
for (int j = 0; j < arrs[i].length; j++) {
if (arrs[i][j] == -2) {
System.out.print("P ");
}else {
System.out.print(arrs[i][j] + " ");
}
}
//换行打印
System.out.println();
}
}
private static int[][] generate() {
Scanner in =new Scanner(System.in);
//设置棋盘的大小
System.out.println("请输入棋盘大小:");
int a = in.nextInt();
if (a <= 0) {
System.out.println("棋盘大小必须大于0");
return null;
}
//设置地雷的个数
System.out.println("请设置地雷的个数:");
int b = in.nextInt();
if (b < 0) {
System.out.println("地雷个数必须大于0");
return null;
}else if (b > a*a) {
System.out.println("地雷个数必须小于" + a*a);
return null;
}
int[][] arrs = new int[a][a];
//地雷标记位-2,其它标记为-1
Random r = new Random();
for (int i = 0; i < arrs.length; i++) {
for (int j = 0; j < arrs[i].length; j++) {
if (b > 0) {
boolean flag = r.nextBoolean();
if (flag) {
arrs[i][j] = -2;
b --;
}else {
arrs[i][j] = -1;
}
}else {
arrs[i][j] = -1;
}
}
}
for (int i = 0; i < arrs.length; i++) {
for (int j = 0; j < arrs[i].length; j++) {
if (i == 0) {
if (j == 0) {
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i+1][j] == -2) {
count ++;
}
if (arrs[i+1][j+1] == -2) {
count ++;
}
if (arrs[i][j+1] == -2) {
count ++;
}
arrs[i][j] = count;
}
}else if(j == arrs[i].length -1){
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i][j-1] == -2) {
count ++;
}
if (arrs[i+1][j-1] == -2) {
count ++;
}
if (arrs[i+1][j] == -2) {
count ++;
}
arrs[i][j] = count;
}
}else{
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i][j-1] == -2) {
count ++;
}
if (arrs[i+1][j-1] == -2) {
count ++;
}
if (arrs[i+1][j] == -2) {
count ++;
}
if (arrs[i+1][j+1] == -2) {
count ++;
}
if (arrs[i][j+1] == -2) {
count ++;
}
arrs[i][j] = count;
}
}
}else if (i == arrs.length -1){
if (j == 0) {
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i-1][j] == -2) {
count ++;
}
if (arrs[i-1][j+1] == -2) {
count ++;
}
if (arrs[i][j+1] == -2) {
count ++;
}
arrs[i][j] = count;
}
}else if(j == arrs[i].length -1){
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i][j-1] == -2) {
count ++;
}
if (arrs[i-1][j-1] == -2) {
count ++;
}
if (arrs[i-1][j] == -2) {
count ++;
}
arrs[i][j] = count;
}
}else{
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i][j-1] == -2) {
count ++;
}
if (arrs[i-1][j-1] == -2) {
count ++;
}
if (arrs[i-1][j] == -2) {
count ++;
}
if (arrs[i-1][j+1] == -2) {
count ++;
}
if (arrs[i][j+1] == -2) {
count ++;
}
arrs[i][j] = count;
}
}
}else {
if (j == 0) {
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i-1][j] == -2) {
count ++;
}
if (arrs[i-1][j+1] == -2) {
count ++;
}
if (arrs[i][j+1] == -2) {
count ++;
}
if (arrs[i+1][j+1] == -2) {
count ++;
}
if (arrs[i+1][j] == -2) {
count ++;
}
arrs[i][j] = count;
}
}else if(j == arrs[i].length -1){
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i-1][j] == -2) {
count ++;
}
if (arrs[i-1][j-1] == -2) {
count ++;
}
if (arrs[i][j-1] == -2) {
count ++;
}
if (arrs[i+1][j-1] == -2) {
count ++;
}
if (arrs[i+1][j] == -2) {
count ++;
}
arrs[i][j] = count;
}
}else{
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i][j-1] == -2) {
count ++;
}
if (arrs[i-1][j-1] == -2) {
count ++;
}
if (arrs[i-1][j] == -2) {
count ++;
}
if (arrs[i-1][j+1] == -2) {
count ++;
}
if (arrs[i][j+1] == -2) {
count ++;
}
if (arrs[i+1][j+1] == -2) {
count ++;
}
if (arrs[i+1][j] == -2) {
count ++;
}
if (arrs[i+1][j-1] == -2) {
count ++;
}
arrs[i][j] = count;
}
}
}
}
}
return arrs;
}
}