-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
170 lines (143 loc) · 5.13 KB
/
Copy pathPlayer.java
File metadata and controls
170 lines (143 loc) · 5.13 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
package objects;
import data.Game;
import util.Util;
import javax.swing.*;
import java.awt.*;
public class Player {
public String name;
public Color color;
public int count;
public int row;
public int column;
public Player() {
}
public void writeChess() {
}
public void repentanceChess() {
}
public void surrender() {
JOptionPane.showMessageDialog(new JFrame(), name + "赢得游戏");
Game.ifGameEnd = true;
}
public void ifWin() {
// 游戏结束不判断
if (!Game.ifGameEnd) {
// 当棋子总数大于等于9时才判断
if (Game.chessArray.size() >= 1) {
// 初始化变量
int[][] chessPositionArray = Util.getChessPositionArray(Game.chessArray);
int positionX = row;
int positionY = column;
// 检查同一行右方向是否有相同颜色棋子
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();
}
}
}
public synchronized void winOrLose() {
if (!Game.ifGameEnd) {
if (count >= 4) {
JOptionPane.showMessageDialog(new JFrame(), this.name + "赢得游戏");
Game.ifGameEnd = true;
}
}
count = 0;
}
}