扫雷算法

package com.common;

public class Constant {
public static final String MINE = "*";

public static int PANEL_SIZE = 10;

public static int MINE_SIZE = 10;

public static int LEVEL_1 = 1;

public static int LEVEL_2 = 3;

public static int LEVEL_3 = 9;
}


package com.sweep;

import java.util.Random;

import com.common.Constant;

public class Sweep {
private static Random ran = new Random();
private static String[][] PANEL = null;

private static final String MINE = Constant.MINE;

private static int PANEL_SIZE = Constant.MINE_SIZE;

private static int MINE_SIZE = Constant.MINE_SIZE;

public static void initMINE() {
int totalTimes = MINE_SIZE;
PANEL = new String[PANEL_SIZE][PANEL_SIZE];

while (totalTimes > 0) {
int row = ran.nextInt(PANEL_SIZE);
int erect = ran.nextInt(PANEL_SIZE);
if (null == PANEL[row][erect]) {
PANEL[row][erect] = MINE;
totalTimes--;
}
}
}

public static void fillNum() {
for (int i = 0; i < PANEL_SIZE; i++) {
for (int j = 0; j < PANEL_SIZE; j++) {
if (MINE.equals(PANEL[i][j])) {
if ((j - 1 >= 0)) {
if (checkNum(PANEL[i][j - 1])) {
PANEL[i][j - 1] = ""
+ (Integer.valueOf(PANEL[i][j - 1]) + 1);
} else if (!MINE.equals(PANEL[i][j - 1])) {
PANEL[i][j - 1] = "1";
}

}

if ((j + 1 < PANEL_SIZE)) {
if (checkNum(PANEL[i][j + 1])) {
PANEL[i][j + 1] = ""
+ (Integer.valueOf(PANEL[i][j + 1]) + 1);
} else if (!MINE.equals(PANEL[i][j + 1])) {
PANEL[i][j + 1] = "1";
}

}

if ((i - 1 >= 0) && (j - 1 >= 0)) {
if (checkNum(PANEL[i - 1][j - 1])) {
PANEL[i - 1][j - 1] = ""
+ (Integer.valueOf(PANEL[i - 1][j - 1]) + 1);
} else if (!MINE.equals(PANEL[i - 1][j - 1])) {
PANEL[i - 1][j - 1] = "1";
}

}

if ((i - 1 >= 0)) {
if (checkNum(PANEL[i - 1][j])) {
PANEL[i - 1][j] = ""
+ (Integer.valueOf(PANEL[i - 1][j]) + 1);
} else if (!MINE.equals(PANEL[i - 1][j])) {
PANEL[i - 1][j] = "1";
}

}

if ((i - 1 >= 0) && (j + 1 < PANEL_SIZE)) {
if (checkNum(PANEL[i - 1][j + 1])) {
PANEL[i - 1][j + 1] = ""
+ (Integer.valueOf(PANEL[i - 1][j + 1]) + 1);
} else if (!MINE.equals(PANEL[i - 1][j + 1])) {
PANEL[i - 1][j + 1] = "1";
}

}

if ((i + 1 < PANEL_SIZE) && (j - 1 >= 0)) {
if (checkNum(PANEL[i + 1][j - 1])) {
PANEL[i + 1][j - 1] = ""
+ (Integer.valueOf(PANEL[i + 1][j - 1]) + 1);
} else if (!MINE.equals(PANEL[i + 1][j - 1])) {
PANEL[i + 1][j - 1] = "1";
}

}

if ((i + 1 < PANEL_SIZE)) {
if (checkNum(PANEL[i + 1][j])) {
PANEL[i + 1][j] = ""
+ (Integer.valueOf(PANEL[i + 1][j]) + 1);
} else if (!MINE.equals(PANEL[i + 1][j])) {
PANEL[i + 1][j] = "1";
}

}

if ((i + 1 < PANEL_SIZE) && (j + 1 < PANEL_SIZE)) {
if (checkNum(PANEL[i + 1][j + 1])) {
PANEL[i + 1][j + 1] = ""
+ (Integer.valueOf(PANEL[i + 1][j + 1]) + 1);
} else if (!MINE.equals(PANEL[i + 1][j + 1])) {
PANEL[i + 1][j + 1] = "1";
}

}

}
}
}
}

public static void print() {
String flag = "|";
for (int i = 0; i < PANEL_SIZE; i++) {
for (int j = 0; j < PANEL_SIZE; j++) {
if (null != PANEL[i][j]) {
System.out.print(flag + PANEL[i][j]);
} else {
System.out.print(flag + " ");
}
}
System.out.print(flag);
System.out.println();
}
}

private static boolean checkNum(String value) {
if (null == value || MINE.equals(value))
return false;
return true;
}

public static void generate() {
initMINE();
fillNum();
print();
}

public static void main(String[] args) {
generate();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值