-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
312 lines (287 loc) · 10.3 KB
/
Game.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
305
306
307
308
309
310
311
312
import java.io.*;
import java.util.*;
public class Game implements Serializable {
int rows, columns, imgs;
int board[][], iniboard[][];
int x1, y1;
boolean pressed;
List<Position> history;
public void createNewGame() {
createNewGame(8, 6, 6);
}
public void createNewGame(int imgs, int rows, int columns) {
// 实际数组四条边都是0不需要初始化
this.rows = rows;
this.columns = columns;
this.imgs = imgs;
this.board = new int[rows + 2][columns + 2];
this.iniboard = new int[rows + 2][columns + 2];
int half = columns * rows / 2;
List<Integer> list = new ArrayList<Integer>();
history = new ArrayList<Position>();
int cnt = 0;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= columns; j++) {
int newNum;
cnt++;
if (cnt <= half) {
newNum = (int) (Math.random() * 100) % imgs + 1;
list.add(newNum);
} else {
int z = (int) (Math.random() * 100) % list.size();
newNum = list.remove(z);
}
iniboard[i][j] = board[i][j] = newNum;
}
}
x1 = y1 = 0;
pressed = false;
}
public void reset() {
board = reloadInitializeArray(board);
x1 = y1 = 0;
pressed = false;
}
public static Game readGameFromStream(InputStream is) throws Exception {
try {
ObjectInputStream ois = new ObjectInputStream(is);
Game ret = (Game) ois.readObject();
ois.close();
return ret;
} catch (Exception e) {
throw e;
}
}
public void saveGameToStream(OutputStream os) throws IOException {
try {
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(this);
oos.close();
} catch (Exception e) {
throw e;
}
}
public Map<String, Object> canDelete(int x, int y) {
Map<String, Object> result = canDelete(x1, y1, x, y, board);
if (!pressed || board[x][y] != board[x1][y1])
result.put("result", false);
return result;
}
public void press(int x, int y) {
history.add(new Position(x, y, true));
if (!pressed || (x1 == x && y1 == y)) {
x1 = x;
y1 = y;
pressed = true;
return;
}
// 是第二次点击
Map<String, Object> res = canDelete(x, y);
if ((boolean) res.get("result") == true) {
board[x][y] = 0;
board[x1][y1] = 0;
}
x1 = 0;
y1 = 0;
pressed = false;
}
class GameCannotInterpretException extends Exception {
public GameCannotInterpretException() {
}
public GameCannotInterpretException(String message) {
super(message);
}
}
public static int[][] reloadInitializeArray(int[][] array) {
int rows = array.length;
int columns = array[0].length;
int[][] newArray = new int[rows][columns];
for (int x = 1; x <= rows - 2; x++) {
for (int y = 1; y <= columns - 2; y++) {
int selectRow = (int) (Math.random() * (rows - 2)) + 1;
int selectColumn = (int) (Math.random() * (columns - 2)) + 1;
// 表明该位置已经有图片了
while (newArray[selectRow][selectColumn] != 0) {
selectRow = (int) (Math.random() * (rows - 2)) + 1;
selectColumn = (int) (Math.random() * (columns - 2)) + 1;
}
newArray[selectRow][selectColumn] = array[x][y];
}
}
System.out.println();
for (int y = 0; y < rows; y++) {
for (int x = 0; x < columns; x++) {
System.out.print(newArray[y][x] + " ");
}
System.out.println();
}
return newArray;
}
public static boolean onLine(int x1, int y1, int x2, int y2, int[][] array) {
if (x1 == x2 && y1 == y2) {
// 不能是同一个点
return false;
}
if (x1 == x2 || y1 == y2) {
if (x1 == x2) {
int min = y1 < y2 ? y1 : y2;
int max = y1 > y2 ? y1 : y2;
if (max - min == 1) {
// 相邻
return true;
}
for (int i = min + 1; i < max; i++) {
if (array[x1][i] != 0) {
return false;
}
}
return true;
} else {
int min = x1 < x2 ? x1 : x2;
int max = x1 > x2 ? x1 : x2;
if (max - min == 1) {
return true;
}
for (int i = min + 1; i < max; i++) {
if (array[i][y1] != 0) {
return false;
}
}
return true;
}
} else {
// 不同轴
return false;
}
}
public static Position twoLine(int x1, int y1, int x2, int y2, int[][] array) {
// 交点是(x1,y2)
if (array[x1][y2] == 0) {
// 到(x1,y1)是否可达
boolean firstLine = onLine(x1, y2, x1, y1, array);
// 到(x2,y2)是否可达
boolean secondLine = onLine(x1, y2, x2, y2, array);
// 如果同时都可以走通那可以连线
if (firstLine && secondLine) {
Position position = new Position(x1, y2, true);
return position;
}
}
// 交点在(x2,y1)
if (array[x2][y1] == 0) {
boolean firstLine = onLine(x1, y1, x2, y1, array);
boolean secondLine = onLine(x2, y2, x2, y1, array);
if (firstLine && secondLine) {
Position position = new Position(x2, y1, true);
return position;
}
}
Position position = new Position(0, 0, false);
return position;
}
public static Map<String, Object> threeLines(int x1, int y1, int x2, int y2, int[][] array) {
Map<String, Object> map = new HashMap<>();
int rows = array.length;
int columns = array[0].length;
// 遍历棋盘,找到一个点到a点是一根线,到b点是两根线如果有就是可以连通
for (int x = 0; x < rows; x++) {
// 先列后行
for (int y = 0; y < columns; y++) {
// 先判断是不是空格
if (array[x][y] == 0) {
boolean flag = onLine(x, y, x1, y1, array);
if (flag) {
Position position = twoLine(x, y, x2, y2, array);
boolean result = position.isFlag();
// 这个点能够和被点击的两个点连通,同时返回一个中转点
if (result) {
List<Position> list = new ArrayList<>();
list.add(new Position(x1, y1, true));
list.add(new Position(x, y, true));
list.add(position);
list.add(new Position(x2, y2, true));
map.put("list", list);
map.put("result", true);
return map;
}
}
// 这个点可能到a点是两根线到b点是一根线
Position position = twoLine(x, y, x1, y1, array);
flag = position.isFlag();
if (flag) {
boolean result = onLine(x, y, x2, y2, array);
if (result) {
List<Position> list = new ArrayList<>();
list.add(new Position(x1, y1, true));
list.add(position);
list.add(new Position(x, y, true));
list.add(new Position(x2, y2, true));
map.put("list", list);
map.put("result", true);
return map;
}
}
}
}
}
map.put("result", false);
return map;
}
public static Map<String, Object> canDelete(int x1, int y1, int x2, int y2, int[][] array) {
boolean one = onLine(x1, y1, x2, y2, array);
Map<String, Object> map = new HashMap<>();
if (one) {
// System.out.println("one");
List<Position> list = new ArrayList<>();
list = Arrays.asList(new Position(x1, y1, true), new Position(x2, y2, true));
map.put("list", list);
map.put("result", true);
return map;
}
Position position = twoLine(x1, y1, x2, y2, array);
boolean two = position.isFlag();
if (two) {
// System.out.println("two");
List<Position> list = new ArrayList<>();
list.add(new Position(x1, y1, true));
list.add(position);
list.add(new Position(x2, y2, true));
map.put("list", list);
map.put("result", true);
return map;
}
return map = threeLines(x1, y1, x2, y2, array);
}
}
class DirectoryRestrictedFileSystemView extends javax.swing.filechooser.FileSystemView
{
private final File[] rootDirectories;
DirectoryRestrictedFileSystemView(File rootDirectory)
{
this.rootDirectories = new File[] {rootDirectory};
}
DirectoryRestrictedFileSystemView(File[] rootDirectories)
{
this.rootDirectories = rootDirectories;
}
@Override
public File createNewFolder(File containingDir) throws IOException
{
throw new UnsupportedOperationException("Unable to create directory");
}
@Override
public File[] getRoots()
{
return rootDirectories;
}
@Override
public boolean isRoot(File file)
{
for (File root : rootDirectories) {
if (root.equals(file)) {
return true;
}
}
return false;
}
}