class SetUpMine { int mineNum = 15;//雷的数量 int length = 9;//地图的大小 String[][] user = new String[length + 2][length + 2];//玩家看到的界面 int[][] mine = new int[length + 2][length + 2];//存储雷的位置 int mineCount = 0;//初始化地图时统计雷的数量 boolean b = false;//是否结束初始化地图
public void SetUp() { //标记序号 for (int i = 0; i <= length; i++) { //将序号数字1转换为1加上空格组成的字符串 user[i][0] = Integer.toString(i) + ' '; user[0][i] = Integer.toString(i) + ' '; mine[i][0] = i; mine[0][i] = i; } //埋雷 for (int i = 1; i <= length && !b; i++) { for (int j = 1; j <= length; j++) { mine[i][j] = Math.random() > 0.5 ? -1 : 0; if (mine[i][j] == -1) mineCount++; if (b = mineCount == mineNum) break; } } //用户看到的雷被隐藏的界面 for (int i = 1; i <= length; i++) { for (int j = 1; j <= length; j++) { user[i][j] = "* "; } }
/*标记九宫格内每个非雷的格子周围雷的数量 * i为行数; * j为列数; */ for (int i = 1; i <= length; i++) { for (int j = 1; j <= length; j++) { if (mine[i][j] != -1) { if (mine[i - 1][j - 1] == -1) mine[i][j]++; if (mine[i - 1][j] == -1) mine[i][j]++; if (mine[i - 1][j + 1] == -1) mine[i][j]++; if (mine[i][j - 1] == -1) mine[i][j]++; if (mine[i][j + 1] == -1) mine[i][j]++; if (mine[i + 1][j - 1] == -1) mine[i][j]++; if (mine[i + 1][j] == -1) mine[i][j]++; if (mine[i + 1][j + 1] == -1) mine[i][j]++; } } } } }
public class Main { public static void main(String[] args) { //排行榜记录排名 final int[] firstGoal = {0}; final int[] secondGoal = {0}; final int[] thirdGoal = {0};
class SetUpMine { int mineNum = 15;//雷的数量 int length = 9;//地图的大小 String[][] user = new String[length + 2][length + 2];//玩家看到的界面 int[][] mine = new int[length + 2][length + 2];//存储雷的位置 int mineCount = 0;//初始化地图时统计雷的数量 boolean b = false;//是否结束初始化地图
public void SetUp() { //标记序号 for (int i = 0; i <= length; i++) { //将序号数字1转换为1加上空格组成的字符串 user[i][0] = Integer.toString(i) + ' '; user[0][i] = Integer.toString(i) + ' '; mine[i][0] = i; mine[0][i] = i; } //埋雷 for (int i = 1; i <= length && !b; i++) { for (int j = 1; j <= length; j++) { mine[i][j] = Math.random() > 0.5 ? -1 : 0; if (mine[i][j] == -1) mineCount++; if (b = mineCount == mineNum) break; } } //用户看到的雷被隐藏的界面 for (int i = 1; i <= length; i++) { for (int j = 1; j <= length; j++) { user[i][j] = "* "; } }
/*标记九宫格内每个非雷的格子周围雷的数量 * i为行数; * j为列数; */ for (int i = 1; i <= length; i++) { for (int j = 1; j <= length; j++) { if (mine[i][j] != -1) { if (mine[i - 1][j - 1] == -1) mine[i][j]++; if (mine[i - 1][j] == -1) mine[i][j]++; if (mine[i - 1][j + 1] == -1) mine[i][j]++; if (mine[i][j - 1] == -1) mine[i][j]++; if (mine[i][j + 1] == -1) mine[i][j]++; if (mine[i + 1][j - 1] == -1) mine[i][j]++; if (mine[i + 1][j] == -1) mine[i][j]++; if (mine[i + 1][j + 1] == -1) mine[i][j]++; } } } } }
//用户开始界面 System.out.println("************命令行扫雷游戏************"); System.out.println("*************1.开始游戏***************"); System.out.println("*************2.排行榜***************"); System.out.println("*************3.退出游戏***************"); System.out.println("请输入您选择的序号,以回车键结束:"); Scanner kb = new Scanner(System.in); final int[] beginNum = {kb.nextInt()};
class paly1 { public void play1start() { SetUpMine set = new SetUpMine();//创建成员内部类SetUpMine(埋雷)的对象 set.SetUp();//埋雷
//显示用户所看到的地图 UserLook((SetUpMine) set);
int x = 0;//用户选择的纵坐标 int y = 0;//用户选择的横坐标 int userGoal = -1;//用户分数 while (set.mine[x][y] != -1) { userGoal++;//每次循环用户分数加1 System.out.println("请输入您要选择的位置坐标,以空格分隔,第一个为纵坐标,第二个为横坐标,以回车键确认:"); x = kb.nextInt(); y = kb.nextInt(); set.user[x][y] = Integer.toString(set.mine[x][y]) + ' '; UserLook((SetUpMine) set); } if (set.mine[x][y] == -1) { System.out.println("你输了!!!"); System.out.println("您的总分为:" + userGoal); thirdGoal[0] = userGoal; beginNum[0] = 2; } if(userGoal==set.length*set.length-set.mineNum){ System.out.println("你赢了"); } }
/** * 用户看到界面的隐藏符号*号和用户选择的坐标符号的替换 * @param set * 省去部分重复操作 * * for (int i = 0; i <= set.length; i++) { * for (int j = 0; j <= set.length; j++) { * * } * } * */ public void UserLook(SetUpMine set) { for (int i = 0; i <= set.length; i++) { for (int j = 0; j <= set.length; j++) { System.out.print(set.user[i][j]); if (j == set.length) System.out.println(); } } } }