Java控制台五子棋编码学习

目录

主要经验

游戏描述

参考的是《疯狂Java实战演义》——杨恩雄(文字版)中第一章内容。

Java源代码

GobangGame.java

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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package practise.fiveChess;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class GobangGame {
private int WIN_COUNT = 5;
private int posX, posY;
Chessboard chessboard = new Chessboard();

public int getPosX() {
return posX;
}

public void setPosX(int posX) {
this.posX = posX;
}

public int getPosY() {
return posY;
}

public void setPosY(int posY) {
this.posY = posY;
<!-- more -->
}

public static void main(String[] args) {
//游戏初始化
boolean isOver = false;
System.out.println("游戏开始!");
GobangGame gobangGame = new GobangGame();
Chessboard chessboard = new Chessboard();
chessboard.initBorad();
chessboard.printBoard();
//执行游戏交互
do {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputStr = null;
// 判断输入是否合法
while ((inputStr = br.readLine()) != null && gobangGame.isValid(inputStr) && isOver == false) {
int posX = gobangGame.getPosX();
int posY = gobangGame.getPosY();
String colorDark = Chessman.BLACK.getChessman();
String colorWhite = Chessman.WHITE.getChessman();
// 放棋子,如果失败则重放
if (!chessboard.setBoardByMan(posX, posY, colorDark)
|| !chessboard.setBoardByComputer(colorWhite)) {
continue;
}
// 打印棋盘
chessboard.printBoard();
// 判断是否有赢家
if (gobangGame.isWin(chessboard, posX, posY, colorDark)
|| gobangGame.isWin(chessboard, posX, posY, colorWhite)) {
if (gobangGame.isWin(chessboard, posX, posY, colorDark) == true) {
System.out.println("比赛结束!" + colorDark + "获得胜利");
System.out.println("是否继续游戏?y/n");
// 用户确认是否继续游戏,继续则初始化棋盘,否则退出程序
if (br.readLine().contentEquals("y")) {
chessboard.initBorad();
System.out.println("游戏开始!");
chessboard.printBoard();
continue;
} else {
isOver = true;
break;
}
}
if (gobangGame.isWin(chessboard, posX, posY, colorWhite) == true) {
System.out.println("比赛结束!" + colorWhite + "获得胜利");
System.out.println("是否继续游戏?y/n");
// 用户确认是否继续游戏,继续则初始化棋盘,否则退出程序
if (br.readLine().contentEquals("y")) {
chessboard.initBorad();
System.out.println("游戏开始!");
chessboard.printBoard();
continue;
} else {
isOver = true;
break;
}
}
}
}

} catch (IOException e) {
e.printStackTrace();
}
} while (isOver == false);
System.out.println("Game Over!");
}

// 输入合法性检测
public boolean isValid(String str) {
String[] posStrArr = str.split(",");
try {
posX = Integer.parseInt(posStrArr[0]);
posY = Integer.parseInt(posStrArr[1]);
if (posX > chessboard.BOARD_SIZE || posY > chessboard.BOARD_SIZE || posX < 0 || posY < 0) {
System.out.println("输入不合法,请输入合法的坐标范围:0--" + (chessboard.BOARD_SIZE - 1));
return false;
}
} catch (NumberFormatException e) {
//chessboard.printBoard();
System.out.println("输入不合法,请重新按\"x,y\"的形式输入");
return false;
}
return true;
}
//是否继续游戏方法
public boolean isReplay(String enterStr) {
if (enterStr == "y" || enterStr == "Y") {
return true;
} else {
return false;
}
}

//以下方法基于输入棋子坐标,按不同方向(基于棋子坐标),东西|南北|东北-西南|西北-东南。
//东西
public boolean conetX(Chessboard chessboard, int posX, int posY, String chessColor) {
String[][] board;
board = chessboard.getBord();
int count = 1;
try {
int tmpY = posY - 1;
// 输入点不是起点所在列则判断
// 输入点左侧
while (posY >= tmpY && tmpY > 0) {
if (board[posX][tmpY] != chessColor) {
break;
} else {
count++;
tmpY--;
}
}
// 输入点右侧
tmpY = posY + 1;
while (posY <= tmpY && tmpY < chessboard.BOARD_SIZE) {
if (board[posX][tmpY] != chessColor) {
break;
} else {
count++;
tmpY++;
}
}
if (count >= WIN_COUNT) {
return true;
} else {
return false;
}
} catch (Exception e) {
System.out.println("异常错误:" + e.getMessage());
return false;
}
}

/**
* 南北统计
*/
public boolean conetY(Chessboard chessboard, int posX, int posY, String chessColor) {
String[][] board;
board = chessboard.getBord();
int count = 1;
try {
int tmpX = posX - 1;
// 输入点上方,如果输入的是原点则不计
while (tmpX >= 0) {
if (board[tmpX][posY] != chessColor) {
break;
} else {
count++;
tmpX--;
}
}
// 输入点下方
tmpX = posX + 1;
while (posX < tmpX && tmpX < chessboard.BOARD_SIZE) {
if (board[tmpX][posY] != chessColor) {
break;
} else {
count++;
tmpX++;
}
}
// 累加后是否符合要求
if (count >= WIN_COUNT) {
return true;
} else {
return false;
}
} catch (Exception e) {
System.out.println("异常错误:" + e.getMessage());
return false;
}

}

/**
* 东北\西南斜线方向
*/
public boolean conetEN(Chessboard chessboard, int posX, int posY, String chessColor) {
String[][] board;
board = chessboard.getBord();
int count = 1;

try {
int tmpX1 = posX - 1;
int tmpY1 = posY - 1;

// 西北线
while (tmpX1 < posX && tmpX1 >= 0 && tmpY1 < posY && tmpY1 >= 0) {
if (board[tmpX1][tmpY1] != chessColor) {
break;
} else {
count++;
tmpY1--;
tmpX1--;
}
}
// 东南线
int tmpX2 = posX + 1;
int tmpY2 = posY + 1;
while (tmpX2 > posX && tmpX2 < chessboard.BOARD_SIZE && tmpY2 > posY && tmpY2 < chessboard.BOARD_SIZE) {
if (board[tmpX2][tmpY2] != chessColor) {
break;
} else {
count++;
tmpY2++;
tmpX2++;
}
}
if (count >= WIN_COUNT) {
return true;
} else {
return false;
}

} catch (Exception e) {
System.out.println("异常错误:" + e.getMessage());
return false;
}
}

/**
* 东北西南斜线方向
*/
public boolean conetES(Chessboard chessboard, int posX, int posY, String chessColor) {
String[][] board;
board = chessboard.getBord();
int count = 1;
count = 1;
try {
int tmpX1 = posX - 1;
int tmpY1 = posY + 1;
// 东北线
while (tmpX1 < posX && tmpX1 >= 0 && tmpY1 >= posY && tmpY1 < chessboard.BOARD_SIZE) {
if (board[tmpX1][tmpY1] != chessColor) {
break;
} else {
count++;
tmpY1++;
tmpX1--;
}
}
// 西南线
int tmpX2 = posX + 1;
int tmpY2 = posY - 1;
while (tmpX2 < chessboard.BOARD_SIZE && tmpX2 > posX && tmpY2 < posY && tmpY2 >= 0) {
if (board[tmpX2][tmpY2] != chessColor) {
break;
} else {
count++;
tmpY2--;
tmpX2++;
}
}
if (count >= WIN_COUNT) {
return true;
} else {
return false;
}

} catch (Exception e) {
System.out.println("异常错误:" + e.getMessage());
return false;
}
}

// 判断所输入打棋子是否能够赢得比赛
public boolean isWin(Chessboard chessboard, int posX, int posY, String chessColor) {
boolean isWinx = this.conetX(chessboard, posX, posY, chessColor);
boolean isWiny = this.conetY(chessboard, posX, posY, chessColor);
boolean isWinEN = this.conetEN(chessboard, posX, posY, chessColor);
boolean isWinES = this.conetES(chessboard, posX, posY, chessColor);
if (isWinx || isWiny || isWinEN || isWinES) {
return true;
} else {
return false;
}
}
}

Chessman.java

1
2
3
4
5
6
7
8
9
10
public enum Chessman {
BLACK("●"), WHITE("○");
private String chessman;
private Chessman(String chessman) {
this.chessman = chessman;
}
public String getChessman() {
return this.chessman;
}
}

Chessboard.java

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
package practise.fiveChess;

public class Chessboard {

static int BOARD_SIZE=22;
String[][] board=new String[BOARD_SIZE][BOARD_SIZE];
//初始化棋盘
public void initBorad(){
for (Integer i = 0; i < BOARD_SIZE; i++) {
for (Integer j = 0; j < BOARD_SIZE; j++) {
board[i][j]="╂";
}
}
}
//打印棋盘
public void printBoard(){
for (int i = 0; i < BOARD_SIZE; i++) {
//System.out.print(i);
for (int j = 0; j < BOARD_SIZE; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
}

//人执黑棋下棋落子
public boolean setBoardByMan(int posX,int posY,String color){
if(board[posX][posY]!="╂"){
System.out.println("输入位置已有棋子,请重新输入");
return false;
}else{
board[posX][posY]="●";
return true;
}
}
//电脑执白棋
public boolean setBoardByComputer(String color){
int posX,posY;
posX=(int)((Math.random())*BOARD_SIZE);
posY=(int)((Math.random())*BOARD_SIZE);
if(board[posX][posY]!="╂"){
setBoardByComputer(color);
return false;
}else{
board[posX][posY]="○";
return true;
}

}
//返回棋盘,二维数组
public String[][] getBord(){
return board;
}

}