声明:这几篇博客我都用扫雷为例用来巩固练习我学习的不同知识,这并不是写扫雷的最优方式,只是为了满足自己练习而写。
我们之前的一篇博客,写了扫雷,完成了扫雷的基本原理,扫雷,插旗都实现了,今天我们主要加入了等级控制,首先这是我上篇博客的链接:上篇扫雷链接
Game类
class Game {
// 定义12行12列,实际扫雷的区域只有10*10
static int ROW = 12;
static int COL = 12;
// 定义雷的个数
static int NUM = 20;
// 0为无炸弹,1为有炸弹
public static byte BOOM = '1';
public Game(int ROW, int COL, int NUM) {
this.ROW = ROW;
this.COL = COL;
this.NUM = NUM;
}
public static void setMines(int[][] mine_board, int row, int col) {
int i = 0;
while (i < NUM) {
Random random = new Random();//默认随机种子是系统时间
int x = random.nextInt(ROW - 2) + 1;
int y = random.nextInt(COL - 2) + 1;
if (mine_board[x][y] == BOOM) {
continue;
}
mine_board[x][y] = BOOM;
i++;
}
}
public static void showBoard(int[][] board, int row, int col) {
System.out.print(" ");
for (int i = 1; i <= row - 2; i++) {
System.out.printf("%4d", i);
}
System.out.println();
System.out.print(" ");
for (int k = 1; k <= col - 2; k++) {
System.out.print("----");
}
System.out.println();
for (int i = 1; i <= ROW - 2; i++) {
System.out.printf("%2d|", i);
for (int j = 1; j <= COL - 2; j++) {
System.out.printf(" %c |", board[i][j]);
}
System.out.println();
for (int k = 1; k <= COL - 2; k++) {
System.out.print("----");
}
System.out.println();
}
}
public static void flag(int[][] board, int a, int b) {
if (b == 0) {
System.out.print("请输入插旗坐标<x y> ");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
board[x][y] = '$';
} else {
while (true) {
System.out.print("请输入取消插旗坐标<x y> ");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
int j = sc.nextInt();
if (board[i][j] == '$') {
board[i][j] = '*';
break;
} else {
System.out.println("输入坐标有误!");
continue;
}
}
}
}
public static int countMines(int[][] board, int x, int y) {
return board[x - 1][y - 1] + board[x - 1][y]
+ board[x - 1][y + 1] + board[x][y - 1]
+ board[x][y + 1] + board[x + 1][y - 1]
+ board[x + 1][y] + board[x + 1][y + 1] - 8 * '0';
}
public static void game() {
int[][] show_board = new int[ROW][COL];
int[][] mine_board = new int[ROW][COL];
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
show_board[i][j] = '*';
mine_board[i][j] = '0';
}
}
setMines(mine_board, ROW, COL);
int tot = (ROW - 2) * (COL - 2) - NUM;
int clear = 0;
while (true) {
showBoard(show_board, ROW, COL);
System.out.print("请输扫雷入坐标<x y> ");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
if ((x == 0 && y == 0) || (x == 0 && y == 1)) {
flag(show_board, x, y);
continue;
}
if (!(x >= 1 && x <= ROW - 2 && y >= 1 && y <= COL - 2)) {
System.out.println("扫雷的位置有问题!");
continue;
}
if (show_board[x][y] != '*') {
System.out.println("扫雷的位置上已经排除!");
continue;
}
if (mine_board[x][y] == '1') {
System.out.println("不好意思,你被炸死了!");
break;
} else {
int count = countMines(mine_board, x, y);
show_board[x][y] = count + '0';
clear++;
}
if (clear >= tot) {
System.out.println("恭喜你,游戏胜利!");
break;
}
}
}
}
首先我们将上个博客代码中所有的方法打包成为一个新的Game类。
Menu方法和Level方法
public static void menu(){
System.out.println("####################");
System.out.println("##### 1.Play #####");
System.out.println("##### 2.Exit #####");
System.out.println("####################");
System.out.print("Please Select> ");
}
public static void Level(){
System.out.println("####################");
System.out.println("##### level1 #####");
System.out.println("##### level2 #####");
System.out.println("##### level3 #####");
System.out.println("####################");
System.out.print("Please Select> ");
}
这两个方法定义在minesweepplus类中,也就是包含main方法的类,主要用来打印我们需要选择的界面。
Main方法
public static void main(String[] args) {
boolean quit=true;
while(quit){
menu();
Scanner sc=new Scanner(System.in);
int select=sc.nextInt();
switch(select){
case 1:
Level();
Scanner scn=new Scanner(System.in);
int level=sc.nextInt();
switch(level){
case 1:
Game game1=new Game(7,7,5);
game1.game();
break;
case 2:
Game game2=new Game(9,9,10);
game2.game();
break;
case 3:
Game game3=new Game(12,12,20);
game3.game();
break;
default:
System.out.println("输入有误!");
break;
}
break;
case 2:
quit=false;
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
main方法中我们首先进入死循环,接着调用menu方法打印菜单进行选择。
case 1:
Level();
Scanner scn=new Scanner(System.in);
int level=sc.nextInt();
switch(level){
case 1:
Game game1=new Game(7,7,5);
game1.game();
break;
case 2:
Game game2=new Game(9,9,10);
game2.game();
break;
case 3:
Game game3=new Game(12,12,20);
game3.game();
break;
default:
System.out.println("输入有误!");
break;
}
break;
选一,调用level方法打印,根据提示进行选择,然后我们只需要根据不同选择new 一个Game对象然后用构造方法给定不同的值就完成了等级的控制。
总代码
import java.util.Random;
import java.util.Scanner;
class Game {
// 定义12行12列,实际扫雷的区域只有10*10
static int ROW = 12;
static int COL = 12;
// 定义雷的个数
static int NUM = 20;
// 0为无炸弹,1为有炸弹
public static byte BOOM = '1';
public Game(int ROW, int COL, int NUM) {
this.ROW = ROW;
this.COL = COL;
this.NUM = NUM;
}
public static void setMines(int[][] mine_board, int row, int col) {
int i = 0;
while (i < NUM) {
Random random = new Random();//默认随机种子是系统时间
int x = random.nextInt(ROW - 2) + 1;
int y = random.nextInt(COL - 2) + 1;
if (mine_board[x][y] == BOOM) {
continue;
}
mine_board[x][y] = BOOM;
i++;
}
}
public static void showBoard(int[][] board, int row, int col) {
System.out.print(" ");
for (int i = 1; i <= row - 2; i++) {
System.out.printf("%4d", i);
}
System.out.println();
System.out.print(" ");
for (int k = 1; k <= col - 2; k++) {
System.out.print("----");
}
System.out.println();
for (int i = 1; i <= ROW - 2; i++) {
System.out.printf("%2d|", i);
for (int j = 1; j <= COL - 2; j++) {
System.out.printf(" %c |", board[i][j]);
}
System.out.println();
for (int k = 1; k <= COL - 2; k++) {
System.out.print("----");
}
System.out.println();
}
}
public static void flag(int[][] board, int a, int b) {
if (b == 0) {
System.out.print("请输入插旗坐标<x y> ");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
board[x][y] = '$';
} else {
while (true) {
System.out.print("请输入取消插旗坐标<x y> ");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
int j = sc.nextInt();
if (board[i][j] == '$') {
board[i][j] = '*';
break;
} else {
System.out.println("输入坐标有误!");
continue;
}
}
}
}
public static int countMines(int[][] board, int x, int y) {
return board[x - 1][y - 1] + board[x - 1][y]
+ board[x - 1][y + 1] + board[x][y - 1]
+ board[x][y + 1] + board[x + 1][y - 1]
+ board[x + 1][y] + board[x + 1][y + 1] - 8 * '0';
}
public static void game() {
int[][] show_board = new int[ROW][COL];
int[][] mine_board = new int[ROW][COL];
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
show_board[i][j] = '*';
mine_board[i][j] = '0';
}
}
setMines(mine_board, ROW, COL);
int tot = (ROW - 2) * (COL - 2) - NUM;
int clear = 0;
while (true) {
showBoard(show_board, ROW, COL);
System.out.print("请输扫雷入坐标<x y> ");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
if ((x == 0 && y == 0) || (x == 0 && y == 1)) {
flag(show_board, x, y);
continue;
}
if (!(x >= 1 && x <= ROW - 2 && y >= 1 && y <= COL - 2)) {
System.out.println("扫雷的位置有问题!");
continue;
}
if (show_board[x][y] != '*') {
System.out.println("扫雷的位置上已经排除!");
continue;
}
if (mine_board[x][y] == '1') {
System.out.println("不好意思,你被炸死了!");
break;
} else {
int count = countMines(mine_board, x, y);
show_board[x][y] = count + '0';
clear++;
}
if (clear >= tot) {
System.out.println("恭喜你,游戏胜利!");
break;
}
}
}
}
public class MineSweepPlus {
public static void menu(){
System.out.println("####################");
System.out.println("##### 1.Play #####");
System.out.println("##### 2.Exit #####");
System.out.println("####################");
System.out.print("Please Select> ");
}
public static void Level(){
System.out.println("####################");
System.out.println("##### level1 #####");
System.out.println("##### level2 #####");
System.out.println("##### level3 #####");
System.out.println("####################");
System.out.print("Please Select> ");
}
public static void main(String[] args) {
boolean quit=true;
while(quit){
menu();
Scanner sc=new Scanner(System.in);
int select=sc.nextInt();
switch(select){
case 1:
Level();
Scanner scn=new Scanner(System.in);
int level=sc.nextInt();
switch(level){
case 1:
Game game1=new Game(7,7,5);
game1.game();
break;
case 2:
Game game2=new Game(9,9,10);
game2.game();
break;
case 3:
Game game3=new Game(12,12,20);
game3.game();
break;
default:
System.out.println("输入有误!");
break;
}
break;
case 2:
quit=false;
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
运行结果