JAVA扫雷简单实现

code format="java" package learn.js;

import java.util.*;

public class JiS { static boolean game = true;

static int t = 20;// 棋盘大小 static int COLS = t;// 行数，扫雷棋盘 static int ROWS = t;// 列数，扫雷棋盘

static int UNVISITED = 0;// 未访问过的 static int VISITED = 1;// 已访问过的

static int COVER = -2;// 遮罩标记 static int UNCOVER = -3;// 显示标记 static int FLAG = -4;// 玩家避难有雷标志

static int LEI = -1;

static int LEFT = 0;// 代表鼠标左键 static int RIGHT = 1;// 代表鼠标右键

static int[][] NUMPANEL = new int[COLS][ROWS];// 数字盘 static int[][] BJPANEL = new int[COLS][ROWS];// 标记盘 static int[][] LEIPANEL = new int[COLS][ROWS];// 雷盘 static int[][] HPANEL = new int[COLS + 2][ROWS + 2];// 辅助状态转换盘

Random ran = new Random;

public void initPanel(int numOfLei) {

for (int i = 0; i < numOfLei; i++) { int key = ran.nextInt(t * t); HPANEL[key / t + 1][key % t + 1] = LEI; }		for (int i = 1; i < ROWS + 1; i++) { for (int j = 1; j < COLS + 1; j++) { HPANEL[i][j] = update(i, j); NUMPANEL[i - 1][j - 1] = HPANEL[i][j]; BJPANEL[i - 1][j - 1] = COVER; LEIPANEL[i - 1][j - 1] = UNVISITED; }		}		// System.out.println(printlnArray(NUMPANEL)); System.out.println(printlnArray(BJPANEL)); }

/**	 * 标记盘对照方法 *	 * @param i	 * @return */	public String checkBJ(int i, int j, int key) { String rtn = ""; if (key == COVER) { rtn = "$"; } else if (key == FLAG) { rtn = "?"; } else {

if (HPANEL[i + 1][j + 1] == 0) { rtn = " "; } else if (HPANEL[i + 1][j + 1] == LEI) { rtn = "+"; } else { rtn = HPANEL[i + 1][j + 1] + ""; }		}		return rtn; }

/**	 * 简单雷盘对照方法 *	 * @param i	 * @return */	public String check(int i) { String rtn = ""; if (i == LEI) { rtn = "*"; } else if (i == COVER) { rtn = "$"; } else { rtn = i + ""; }		return rtn; }

public String printlnArray(int[][] a) { return printlnArray(a, 0); }

public String printlnArray(int[][] a, int type) { StringBuffer rtn = new StringBuffer; rtn.append("           "); rtn.append("|-"); for (int i = 1; i < a.length; i++) { rtn.append("--"); }		rtn.append("-|"); rtn.append("\n"); for (int i = 0; i < a.length; i++) { rtn.append("           "); rtn.append("|"); for (int j = 0; j < a[0].length; j++) { rtn.append(type == 0 ? check(a[i][j]) : checkBJ(i, j, a[i][j])); rtn.append(' '); }			rtn.append("|\n"); }		rtn.append("           "); rtn.append("|-"); for (int i = 1; i < a.length; i++) { rtn.append("--"); }		rtn.append("-|");

return rtn.toString; }

public int update(int i, int j) { int rtn = 0; if (HPANEL[i][j] == LEI) { rtn = LEI; } else { for (int k = i - 1; k <= i + 1; k++) { for (int l = j - 1; l <= j + 1; l++) { if (HPANEL[k][l] == LEI) { rtn++; }				}			}		}		return rtn; }

// Stack visitS;

public void tuo(int i, int j) { // visitS.pop; if (i > ROWS || i < 1 || j > COLS || j < 1) return; if (LEIPANEL[i - 1][j - 1] == VISITED) return; if (BJPANEL[i - 1][j - 1] == FLAG) return; if (HPANEL[i][j] > 0) { LEIPANEL[i - 1][j - 1] = VISITED; BJPANEL[i - 1][j - 1] = UNCOVER; return; }

LEIPANEL[i - 1][j - 1] = VISITED; BJPANEL[i - 1][j - 1] = UNCOVER; // 下 tuo(i, j + 1); // 上 tuo(i, j - 1); // 右 tuo(i + 1, j); // 右上 tuo(i + 1, j - 1); // 右下 tuo(i + 1, j + 1); // 左 tuo(i - 1, j); // 左下 tuo(i - 1, j + 1); // 左上 tuo(i - 1, j - 1);

}

public int updateBJ(int i, int j, int type) { int rtn = 0; if (type == LEFT) {

if (HPANEL[i][j] == LEI) { game = false;

} else { if (HPANEL[i][j] == 0 && LEIPANEL[i - 1][j - 1] == UNVISITED) { // visitS = new Stack; // visitS.push(new int[] { i, j }); tuo(i, j); }			}		} else { BJPANEL[i - 1][j - 1] = FLAG; }		return rtn; }

/**	 * @param args */	public static void main(String[] args) { // TODO Auto-generated method stub

JiS js = new JiS; js.initPanel(39); js.process; }

/**	 *	 */	public void process { Scanner s = new Scanner(System.in); l: while (game) { System.out.println("请输入要点击的行数>0&&<" + ROWS + ": "); int rows = s.nextInt; while ((rows) < 1 || rows > ROWS) { System.out.println("请输入要点击的行数>0&&<" + ROWS + ": "); rows = s.nextInt; }			rows -= 1; System.out.println("请输入要点击的列数>0&&<" + COLS + ": "); int cols = s.nextInt; while ((cols) < 1 || cols > COLS) { System.out.println("请输入要点击的列数>0&&<" + COLS + ": "); cols = s.nextInt; }			cols -= 1;

System.out.println("请输入要点击的类型左键" + LEFT + "右键" + RIGHT + ": "); int type = s.nextInt; while (type != LEFT && type != RIGHT) { System.out.println("请输入要点击的类型左键" + LEFT + "右键" + RIGHT + ": "); type = s.nextInt; }			if (BJPANEL[rows][cols] == VISITED) continue l; BJPANEL[rows][cols] = (type == LEFT ? UNCOVER : FLAG);

updateBJ(rows + 1, cols + 1, type);// 更新被点击坐标的周围标志

for (int i = 1; i < ROWS + 1; i++) { for (int j = 1; j < COLS + 1; j++) { HPANEL[i][j] = update(i, j); NUMPANEL[i - 1][j - 1] = HPANEL[i][j]; }			}			System.out.println(printlnArray(BJPANEL, 1)); // System.out.println(printlnArray(NUMPANEL)); }		System.out.println("GAME OVER"); }

}

code