初学Java语言,大学专业课寒假作业便是做一款Java的扫雷游戏(可以显示排行榜),便抽空做了一个基础命令行版本的扫雷。

运行结果:

命令行扫雷

1.首先是埋雷的内部类。

原理为定义两个二维数组,一个是用户可以看到的界面(字符串)user,一个是雷的位置的数组mine。其中,在mine数组中值为-1的为地雷,循环遍历数组,在没有雷的位置周围有雷数组就加1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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]++;
}
}
}
}
}

2.游戏界面(内部类)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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("你赢了");
}
}

总的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* 命令行扫雷游戏(简易版排行榜)
* 作者:Codinglink
*/
import java.util.Scanner;

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();
}
}
}
}





class GoalList {
public void playGoalList() {

int temp;//临时交换变量
if (firstGoal[0] < secondGoal[0]) {
temp = firstGoal[0];
firstGoal[0] = secondGoal[0];
secondGoal[0] = temp;
}
if (firstGoal[0] < thirdGoal[0]) {
temp = firstGoal[0];
firstGoal[0] = thirdGoal[0];
thirdGoal[0] = temp;
}
if (secondGoal[0] < thirdGoal[0]) {
temp = secondGoal[0];
secondGoal[0] = thirdGoal[0];
thirdGoal[0] = temp;
}
System.out.println("最高分:" + firstGoal[0]);
System.out.println("第二名:" + secondGoal[0]);
System.out.println("第三名:" + thirdGoal[0]);
}
}


//选择1开始游戏
while (beginNum[0] == 1) {
paly1 start = new paly1();
start.play1start();
GoalList list=new GoalList();
list.playGoalList();
System.out.println("按1继续游戏");
beginNum[0]=kb.nextInt();

}

//选择2排行榜
if(beginNum[0]==2){
GoalList list=new GoalList();
list.playGoalList();
System.out.println("输入1开始游戏");
beginNum[0]=kb.nextInt();
if(beginNum[0]==1){
paly1 start = new paly1();
start.play1start();
}

}

//选择3退出游戏
if(beginNum[0]==3){
System.out.println("您已经退出游戏!!!");
}

if(beginNum[0]==2){
GoalList list=new GoalList();
list.playGoalList();
}
}