0.前言
今天一同學(xué)讓幫忙寫(xiě)一個(gè)打印掃雷代碼某兩個(gè) 狀態(tài)的程序,感覺(jué)還挺有意思的,特此 整理如下:
1.問(wèn)題描述
默認(rèn)10 × 10的掃雷板,默認(rèn)數(shù)值是0,讀取一個(gè)描述“雷”坐標(biāo)的txt文件,文件第一行表示“雷”的個(gè)數(shù)n,接下來(lái)n行標(biāo)明n個(gè)“雷”的橫縱坐標(biāo)

“雷”周?chē)藗€(gè)方向(左、左上、上、右上、右、右下、下、左下)中的任何一個(gè)方向的空間都標(biāo)明了周?chē)袄椎膫€(gè)數(shù)”,“雷”用
-1表示,每個(gè)沒(méi)有地雷的空間周?chē)嘁粋€(gè)“雷”那么該空間的數(shù)字+1
輸出放置“雷”后的整個(gè)掃雷板,以及改變每個(gè)沒(méi)有地雷的空間后的掃雷板

2.分析
讀取文件,切割字符串后,可得某個(gè)“地雷”的坐標(biāo)
第一個(gè)狀態(tài):直接向掃雷板中“放置地雷”即可;
第二個(gè)狀態(tài):遍歷每個(gè)地雷,判斷地雷周?chē)藗€(gè)方向(左、左上、上、右上、右、右下、下、左下)是否能“改變”(是否越界,是否已經(jīng)放置地雷)
如果可以“改變”,自增即可
3.代碼實(shí)現(xiàn)
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
/**
* @author Stone6762
*
*/
public class Main {
/**
* 讀取文件
*
* @param file
* @return
*/
public static String txt2String(File file) {
StringBuilder result = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));// 構(gòu)造一個(gè)BufferedReader類(lèi)來(lái)讀取文件
String s = null;
while ((s = br.readLine()) != null) {// 使用readLine方法,一次讀一行
result.append(s + System.lineSeparator());
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
}
/**
* 打印數(shù)組
*
* @param array
*/
public static void print(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
//默認(rèn)占兩位,不足補(bǔ)空格,放置出現(xiàn)兩位數(shù)或者-1,對(duì)不齊的情況
String format = String.format("% 2d", array[i][j]);
System.out.print("[" + format + "]");
}
System.out.println();
}
}
/**
* 改變炸彈周?chē)臄?shù)據(jù)
*
* @param array
* @param x
* 炸彈橫坐標(biāo)
* @param y
* 炸彈縱坐標(biāo)
*/
public static void changBoomNeighbor(int[][] array, int x, int y) {
// 該炸彈左、左上、上、右上、右、右下、下、左下 八個(gè)地方,非炸彈的位置+1
// 左上
int temp_x = x - 1;
int temp_y = y - 1;
if (isCanChange(array, temp_x, temp_y)) {
array[temp_x][temp_y]++;
}
// 上
temp_x = x;
temp_y = y - 1;
if (isCanChange(array, temp_x, temp_y)) {
array[temp_x][temp_y]++;
}
// 右上
temp_x = x + 1;
temp_y = y - 1;
if (isCanChange(array, temp_x, temp_y)) {
array[temp_x][temp_y]++;
}
// 左
temp_x = x - 1;
temp_y = y;
if (isCanChange(array, temp_x, temp_y)) {
array[temp_x][temp_y]++;
}
// 右
temp_x = x + 1;
temp_y = y;
if (isCanChange(array, temp_x, temp_y)) {
array[temp_x][temp_y]++;
}
// 左下
temp_x = x - 1;
temp_y = y + 1;
if (isCanChange(array, temp_x, temp_y)) {
array[temp_x][temp_y]++;
}
// 下
temp_x = x;
temp_y = y + 1;
if (isCanChange(array, temp_x, temp_y)) {
array[temp_x][temp_y]++;
}
// 右下
temp_x = x + 1;
temp_y = y + 1;
if (isCanChange(array, temp_x, temp_y)) {
array[temp_x][temp_y]++;
}
}
/**
* 判斷某個(gè)地方是否可以被改變
*
* @param array
* @param x
* @param y
* @return
*/
public static boolean isCanChange(int[][] array, int x, int y) {
if (x < 0 || x > 9) {
return false;
}
if (y < 0 || y > 9) {
return false;
}
if (array[x][y] == -1) {
return false;
}
return true;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("請(qǐng)輸入文件名如:D:/ct/index.txt (路徑名不能有中文)");
while (scan.hasNext()) {
int[][] array = new int[10][10];
String fileName = scan.next();
File file = new File(fileName);
String txt_str = txt2String(file);
String[] txt_datas = txt_str.split(System.lineSeparator());
int num = Integer.parseInt(txt_datas[0]);// 有多少地雷
int[][] boom = new int[num][2];
for (int i = 1; i < txt_datas.length; i++) {
String[] temp_strs = txt_datas[i].split(" ");
int x = Integer.parseInt(temp_strs[0]);
int y = Integer.parseInt(temp_strs[1]);
boom[i - 1][0] = x;
boom[i - 1][1] = y;
array[x][y] = -1;
}
// 放置炸彈后的情況
print(array);
for (int i = 0; i < boom.length; i++) {
changBoomNeighbor(array, boom[i][0], boom[i][1]);
}
System.out.println("------改變周邊后-------");
// 處理炸彈 改變周邊后的情況
print(array);
}
}
}