-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerBlack.java
More file actions
227 lines (197 loc) · 7.2 KB
/
Copy pathPlayerBlack.java
File metadata and controls
227 lines (197 loc) · 7.2 KB
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
package Objects;
import java.awt.*;
import Data.Data;
import Util.Util;
public class PlayerBlack extends Player {
int[][] chessPositionArray;
public PlayerBlack() {
this.name = "黑方";
this.color = Color.black;
}
@Override
public void writeChess() {
if (Data.gameEnd == 0) {
// 当前鼠标坐标
int x = Data.mousePointer.x;
int y = Data.mousePointer.y;
// 转换成棋盘坐标
row = (Util.round(x)) / 40;
column = (Util.round(y)) / 40;
// 检查当前位置是否已有棋子
for (Chess chess : Data.chessArray) {
if (chess.equal(new Chess(row, column, Color.black))) {
return;
}
}
// 向棋盘数组添加棋子
Data.chessArray.add(new Chess(row, column, Color.black));
// 根据当前模式交换下棋方
if (Data.pattern == 0) {
Data.gameState = 1;
} else if (Data.pattern == 1) {
Data.gameState = 2;
}
}
ifWin();
}
@Override
public void repentanceChess() {
if (Data.chessArray.size() > 2) {
Data.chessArray.remove(Data.chessArray.size() - 1);
if (Data.pattern == 0) {
Data.gameState = 1;
} else if (Data.pattern == 1) {
Data.gameState = 2;
}
}
}
@Override
public void ifWin() {
// 游戏结束不判断
if (Data.gameEnd == 0) {
// 当棋子总数大于等于9时才判断
if (Data.chessArray.size() >= 1) {
// 初始化变量
// 利用Util的静态方法把棋子对象转化为一个二维数组,方便进行胜负判断
chessPositionArray = Util.getChessPositionArray(Data.chessArray);
int positionX = row;
int positionY = column;
// 其中0 代表空位 1代表黑棋 2代表白棋
/* 1 2 3 4 5 x →
1 [0][0][0][0][0]
2 [0][1][1][1][1]
3 [0][0][2][2][0]
4 [0][2][0][0][0]
5 [0][0][0][0][0]
y
↓
当前进行横向右方判断的方法
当黑棋手在(1,2)坐标下棋时(1,2)坐标数字变为1
判断坐标(1,2)右边相邻坐标(2,2)的数字是否为1
如果为1,计数器加一 再进行对坐标(3,2)的判断
如果为2,则结束判断,
对计数器的数值进行判断,如果计数器大于等于4 则 相当于横方向有五个相同颜色的棋子相连
游戏结束;
下面是代码
int x = 1;
int y = 2;
int posX = x;
int posY = y;
int count = 0;
while (chessPositionArray[x][y]
== chessPositionArray[posX + 1][posY]) {
count++;
posX++;
}
if (count>=4){
游戏结束
}
需要判断八个方向,如下
↖ ↑ ↗
← 棋 →
↙ ↓ ↘
*/
// 检查同一行右方向是否有相同颜色棋子
if (!(positionX == 14)) {
while (chessPositionArray[row][column] == chessPositionArray[positionX + 1][positionY]) {
count++;
positionX++;
if (positionX == 14) {
break;
}
}
}
// 检查同一行左方向
positionX = row;
if (!(positionX == 0)) {
while (chessPositionArray[row][column] == chessPositionArray[positionX - 1][positionY]) {
count++;
positionX--;
if (positionX == 0) {
break;
}
}
}
winOrLose();
count = 0;
// 检查同一列下方向
if (!(positionY == 14)) {
while (chessPositionArray[row][column] == chessPositionArray[positionX][positionY + 1]) {
count++;
positionY++;
if (positionY == 14) {
break;
}
}
}
// 检查同一列上方向
positionY = column;
if (!(positionY == 0)) {
while (chessPositionArray[row][column] == chessPositionArray[row][positionY - 1]) {
count++;
positionY--;
if (positionY == 0) {
break;
}
}
}
winOrLose();
count = 0;
// 检查右下方向
positionX = row;
positionY = column;
if (!(positionX == 14 || positionY == 14))
while (chessPositionArray[row][column] == chessPositionArray[positionX + 1][positionY + 1]) {
count++;
positionX++;
positionY++;
if (positionX == 14 || positionY == 14) {
break;
}
}
// 检查方左上方向
positionX = row;
positionY = column;
if (!(positionX == 0 || positionY == 0)) {
while (chessPositionArray[row][column] == chessPositionArray[positionX - 1][positionY - 1]) {
count++;
positionX--;
positionY--;
if (positionX == 0 || positionY == 0) {
break;
}
}
}
winOrLose();
count = 0;
// 检查右上方向
positionX = row;
positionY = column;
if (!(positionX == 14 || positionY == 0)) {
while (chessPositionArray[row][column] == chessPositionArray[positionX + 1][positionY - 1]) {
count++;
positionX++;
positionY--;
if (positionX == 14 || positionY == 0) {
break;
}
}
}
// 检查左下方向
positionX = row;
positionY = column;
if (!(positionX == 0 || positionY == 14)) {
while (chessPositionArray[row][column] == chessPositionArray[positionX - 1][positionY + 1]) {
count++;
positionX--;
positionY++;
if (positionX == 0 || positionY == 14) {
break;
}
}
}
winOrLose();
}
}
}
}