-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfeature.py
More file actions
563 lines (484 loc) · 18.8 KB
/
feature.py
File metadata and controls
563 lines (484 loc) · 18.8 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
from time import time
def get_features(state):
t = time()
legal_features = get_legal_feature(state)
my_feature_4 = make_list(0)
my_feature_3 = make_list(0)
my_feature_2 = make_list(0)
enemy_feature_4 = make_list(0)
enemy_feature_3 = make_list(0)
enemy_feature_2 = make_list(0)
if state.check_turn():
me = state.black
enemy = state.white
else:
me = state.white
enemy = state.black
for i in range(15):
for j in range(15):
if me[i][j] == 0 and enemy[i][j] == 0:
if state.check_legal(i, j)[0]:
feature_4, feature_3, feature_2 = convert_me(state, i, j)
if feature_4:
my_feature_4[i][j] = 1
if feature_3:
my_feature_3[i][j] = 1
if feature_2:
my_feature_2[i][j] = 1
if feature_4:
enemy_feature_4[i][j] = 1
if feature_3:
enemy_feature_3[i][j] = 1
if feature_2:
enemy_feature_2[i][j] = 1
return legal_features, my_feature_4, my_feature_3, my_feature_2, enemy_feature_4, enemy_feature_3, enemy_feature_2
def convert_me(state, i, j):
feature_4 = False
feature_3 = False
feature_2 = False
if state.check_turn():
me = state.black
enemy = state.white
else:
me = state.white
enemy = state.black
for c in range(4):
a, b, left, lm, rm, right, l1, l2, r1, r2 = state.get_10set(i, j, c)
mid = lm + rm + 1
# 4를 체크 (놓으면 5)
if mid == 5:
feature_4 = True
elif not state.check_turn() and mid > 5:
feature_4 = True
# 3을 체크 (놓으면 4)
if mid == 4:
l_half, r_half = False, False
if state.check_turn():
if left >= 1:
l_half = True
if right >= 1:
r_half = True
if l1 == 0 or enemy[i - a * l1][j - b * l1] == 1:
l_half = True
if r1 == 0 or enemy[i + a * r1][j + b * r1] == 1:
r_half = True
if not (l_half and r_half):
feature_3 = True
else:
if mid + left == 4:
if enemy[i - a * l1][j - b * l1] == 0:
feature_3 = True
if mid + right == 4:
if enemy[i + a * r1][j + b * r1] == 0:
feature_3 = True
if not state.check_turn():
if mid + left > 4:
if enemy[i - a * l1][j - b * l1] == 0:
feature_3 = True
if mid + right > 4:
if enemy[i + a * r1][j + b * r1] == 0:
feature_3 = True
# 2를 체크 (놓으면 3)
if left >= 1 and right >= 1:
continue
elif l1 == 0 or r1 == 0:
continue
elif left >= 1 and left + mid == 3:
if enemy[i - a * l1][j - b * l1] == 1 or l2 == 0:
continue
elif enemy[i - a * l2][j - b * l2] == 1 or enemy[i + a * r1][j + b * r1] == 1:
continue
else:
try:
if i - a * l2 - a < 0 or j - b * l2 - b < 0:
pass
elif state.check_turn() and me[i - a * l2 - a][j - b * l2 - b] == 1:
continue
except IndexError:
pass
if state.check_legal(i - a * l1, j - b * l1)[0]:
feature_2 = True
else:
continue
elif right >= 1 and right + mid == 3:
if enemy[i + a * r1][j + b * r1] == 1 or r2 == 0:
continue
elif enemy[i + a * r2][j + b * r2] == 1 or enemy[i - a * l1][j - b * l1] == 1:
continue
else:
try:
if i + a * r2 + a < 0 or j + b * r2 + b < 0:
pass
elif state.check_turn() and me[i + a * r2 + a][j + b * r2 + b] == 1:
continue
except IndexError:
pass
if state.check_legal(i + a * r1, j + b * r1)[0]:
feature_2 = True
else:
continue
elif mid == 3 and left == right == 0:
if enemy[i - a * l1][j - b * l1] == 1 or enemy[i + a * r1][j + b * r1] == 1:
continue
left_half, right_half = False, False
if l2 == 0 or enemy[i - a * l2][j - b * l2] == 1:
left_half = True
else:
try:
if i - a * l2 - a < 0 or j - b * l2 - b < 0:
pass
elif state.check_turn() and me[i - a * l2 - a][j - b * l2 - b] == 1:
left_half = True
except IndexError:
pass
if r2 == 0 or enemy[i + a * r2][j + b * r2] == 1:
right_half = True
else:
try:
if i + a * r2 + a < 0 or j + b * r2 + b < 0:
pass
elif state.check_turn() and me[i + a * r2 + a][j + b * r2 + b] == 1:
right_half = True
except IndexError:
pass
if left_half == right_half == True:
continue
elif left_half == right_half == False:
if state.check_legal(i - a * l1, j - b * l1)[0] or state.check_legal(i + a * r1, j + b * r1)[0]:
feature_2 = True
else:
continue
elif left_half:
if state.check_legal(i + a * r1, j + b * r1)[0]:
feature_2 = True
else:
continue
elif right_half:
if state.check_legal(i - a * l1, j - b * l1)[0]:
feature_2 = True
else:
continue
return feature_4, feature_3, feature_2
def convert_enemy(state, i, j):
feature_4 = False
feature_3 = False
if state.check_turn():
me = state.black
enemy = state.white
else:
me = state.white
enemy = state.black
for c in range(4):
a, b, left, lm, rm, right, l1, l2, r1, r2 = state.get_e10set(i, j, c)
mid = lm + rm + 1
# 4를 체크
if mid == 4:
l_half, r_half = False, False
if left >= 1 and not state.check_turn():
l_half = True
elif l1 == 0:
l_half = True
elif me[i - a * l1][j - b * l1] == 1:
l_half = True
if right >= 1 and not state.check_turn():
r_half = True
elif r1 == 0:
r_half = True
elif me[i + a * r1][j + b * r1] == 1:
r_half = True
if not l_half or not r_half:
feature_4 = True
continue
else:
if mid + left == 4:
if me[i - a * l1][j - b * l1] == 0:
feature_4 = True
continue
elif mid + left > 4 and state.check_turn():
if me[i - a * l1][j - b * l1] == 0:
feature_4 = True
continue
if mid + right == 4:
if me[i + a * r1][j + b * r1] == 0:
feature_4 = True
continue
elif mid + right > 4 and state.check_turn():
if me[i + a * r1][j + b * r1] == 0:
feature_4 = True
continue
# 3을 체크
if left >= 1 and right >= 1:
continue
elif l1 == 0 or r1 == 0:
continue
elif left >= 1 and left + mid == 3:
if me[i - a * l1][j - b * l1] == 1 or l2 == 0:
continue
elif me[i - a * l2][j - b * l2] == 1 or me[i + a * r1][j + b * r1] == 1:
continue
else:
try:
if i - a * l2 - a < 0 or j - b * l2 - b < 0:
pass
elif enemy[i - a * l2 - a][j - b * l2 - b] == 1 and state.check_turn() == False:
continue
except IndexError:
pass
feature_3 = True
elif right >= 1 and right + mid == 3:
if me[i + a * r1][j + b * r1] == 1 or r2 == 0:
continue
elif me[i + a * r2][j + b * r2] == 1 or me[i - a * l1][j - b * l1] == 1:
continue
else:
try:
if i + a * r2 + a < 0 or j + b * r2 + b < 0:
pass
elif enemy[i + a * r2 + a][j + b * r2 + b] == 1 and state.check_turn() == False:
continue
except IndexError:
pass
feature_3 = True
elif mid == 3 and left == right == 0:
if me[i - a * l1][j - b * l1] == 1 or me[i + a * r1][j + b * r1] == 1:
continue
left_half, right_half = False, False
if l2 == 0 or me[i - a * l2][j - b * l2] == 1:
left_half = True
else:
try:
if i - a * l2 - a < 0 or j - b * l2 - b < 0:
pass
elif enemy[i - a * l2 - a][j - b * l2 - b] == 1 and state.check_turn() == False:
left_half = True
except IndexError:
pass
if r2 == 0 or me[i + a * r2][j + b * r2] == 1:
right_half = True
else:
try:
if i + a * r2 + a < 0 or j + b * r2 + b < 0:
pass
elif enemy[i + a * r2 + a][j + b * r2 + b] == 1 and state.check_turn() == False:
right_half = True
except IndexError:
pass
if left_half == right_half == True:
continue
feature_3 = True
return feature_4, feature_3
def convert_enemy2(state, i, j):
feature_4 = False
feature_3 = False
feature_2 = False
if state.check_turn():
me = state.white
enemy = state.black
else:
me = state.black
enemy = state.white
for c in range(4):
a, b, left, lm, rm, right, l1, l2, r1, r2 = state.get_10set(i, j, c)
mid = lm + rm + 1
# 4를 체크 (놓으면 5)
if mid == 5:
feature_4 = True
elif not state.check_turn() and mid > 5:
feature_4 = True
# 3을 체크 (놓으면 4)
if mid == 4:
l_half, r_half = False, False
if state.check_turn():
if left >= 1:
l_half = True
if right >= 1:
r_half = True
if l1 == 0 or enemy[i - a * l1][j - b * l1] == 1:
l_half = True
if r1 == 0 or enemy[i + a * r1][j + b * r1] == 1:
r_half = True
if not (l_half and r_half):
feature_3 = True
else:
if mid + left == 4:
if enemy[i - a * l1][j - b * l1] == 0:
feature_3 = True
if mid + right == 4:
if enemy[i + a * r1][j + b * r1] == 0:
feature_3 = True
if not state.check_turn():
if mid + left > 4:
if enemy[i - a * l1][j - b * l1] == 0:
feature_3 = True
if mid + right > 4:
if enemy[i + a * r1][j + b * r1] == 0:
feature_3 = True
# 2를 체크 (놓으면 3)
if left >= 1 and right >= 1:
continue
elif l1 == 0 or r1 == 0:
continue
elif left >= 1 and left + mid == 3:
if enemy[i - a * l1][j - b * l1] == 1 or l2 == 0:
continue
elif enemy[i - a * l2][j - b * l2] == 1 or enemy[i + a * r1][j + b * r1] == 1:
continue
else:
try:
if i - a * l2 - a < 0 or j - b * l2 - b < 0:
pass
elif state.check_turn() and me[i - a * l2 - a][j - b * l2 - b] == 1:
continue
except IndexError:
pass
if state.check_legal(i - a * l1, j - b * l1)[0]:
feature_2 = True
else:
continue
elif right >= 1 and right + mid == 3:
if enemy[i + a * r1][j + b * r1] == 1 or r2 == 0:
continue
elif enemy[i + a * r2][j + b * r2] == 1 or enemy[i - a * l1][j - b * l1] == 1:
continue
else:
try:
if i + a * r2 + a < 0 or j + b * r2 + b < 0:
pass
elif state.check_turn() and me[i + a * r2 + a][j + b * r2 + b] == 1:
continue
except IndexError:
pass
if state.check_legal(i + a * r1, j + b * r1)[0]:
feature_2 = True
else:
continue
elif mid == 3 and left == right == 0:
if enemy[i - a * l1][j - b * l1] == 1 or enemy[i + a * r1][j + b * r1] == 1:
continue
left_half, right_half = False, False
if l2 == 0 or enemy[i - a * l2][j - b * l2] == 1:
left_half = True
else:
try:
if i - a * l2 - a < 0 or j - b * l2 - b < 0:
pass
elif state.check_turn() and me[i - a * l2 - a][j - b * l2 - b] == 1:
left_half = True
except IndexError:
pass
if r2 == 0 or enemy[i + a * r2][j + b * r2] == 1:
right_half = True
else:
try:
if i + a * r2 + a < 0 or j + b * r2 + b < 0:
pass
elif state.check_turn() and me[i + a * r2 + a][j + b * r2 + b] == 1:
right_half = True
except IndexError:
pass
if left_half == right_half == True:
continue
elif left_half == right_half == False:
if state.check_legal(i - a * l1, j - b * l1)[0] or state.check_legal(i + a * r1, j + b * r1)[0]:
feature_2 = True
else:
continue
elif left_half:
if state.check_legal(i + a * r1, j + b * r1)[0]:
feature_2 = True
else:
continue
elif right_half:
if state.check_legal(i - a * l1, j - b * l1)[0]:
feature_2 = True
else:
continue
return feature_4, feature_3, feature_2
def make_list(n):
m = []
for i in range(15):
m.append([n]*15)
return m
def get_legal_feature(state):
legal_map = make_list(1)
for i in range(15):
for j in range(15):
if state.black[i][j] == 1 or state.white[i][j] == 1:
legal_map[i][j] = 0
if not state.check_turn():
return legal_map
illegal_list = state.referee()[1]
for i in illegal_list:
j = i[0]
r, c = j//15, j%15
legal_map[r][c] = 0
return legal_map
def get_connect_feature(state):
connect_map = make_list(0)
if state.check_turn():
me = state.black
enemy = state.white
else:
me = state.white
enemy = state.black
for i in range(15):
for j in range(15):
if me[i][j] == 1 or enemy[i][j] == 1:
continue
# left
if me[i][j-1] == 1 and j>0:
connect_map[i][j] += 1
elif me[i][j-2] == 1 and enemy[i][j-1] == 0 and j>1:
connect_map[i][j] += 1
elif me[i][j-3] == 1 and enemy[i][j-1] == 0 and enemy[i][j-2] == 0 and j>2:
connect_map[i][j] += 1
# right
if j<14 and me[i][j+1] == 1:
connect_map[i][j] += 1
elif j<13 and me[i][j+2] == 1 and enemy[i][j+1] == 0:
connect_map[i][j] += 1
elif j<12 and me[i][j+3] == 1 and enemy[i][j+1] == 0 and enemy[i][j+2] == 0:
connect_map[i][j] += 1
# up
if me[i-1][j] == 1 and i > 0:
connect_map[i][j] += 1
elif me[i-2][j] == 1 and enemy[i-1][j] == 0 and i > 1:
connect_map[i][j] += 1
elif me[i-3][j] == 1 and enemy[i-1][j] == 0 and enemy[i-2][j] == 0 and i > 2:
connect_map[i][j] += 1
# down
if i < 14 and me[i+1][j] == 1:
connect_map[i][j] += 1
elif i < 13 and me[i+2][j] == 1 and enemy[i+1][j] == 0:
connect_map[i][j] += 1
elif i < 12 and me[i+3][j] == 1 and enemy[i+1][j] == 0 and enemy[i+2][j] == 0:
connect_map[i][j] += 1
# lu
if me[i-1][j-1] == 1 and i > 0 and j > 0:
connect_map[i][j] += 1
elif me[i-2][j-2] == 1 and enemy[i-1][j-1] == 0 and i > 1 and j > 1:
connect_map[i][j] += 1
elif me[i-3][j-3] == 1 and enemy[i-1][j-1] == 0 and enemy[i-2][j-2] == 0 and i > 2 and j > 2:
connect_map[i][j] += 1
# ld
if i < 14 and me[i + 1][j-1] == 1 and j > 0:
connect_map[i][j] += 1
elif i < 13 and me[i + 2][j-2] == 1 and enemy[i + 1][j-1] == 0 and j > 1:
connect_map[i][j] += 1
elif i < 12 and me[i + 3][j-3] == 1 and enemy[i + 1][j-1] == 0 and enemy[i + 2][j-2] == 0 and j > 2:
connect_map[i][j] += 1
# ru
if j<14 and me[i-1][j+1] == 1 and i > 0:
connect_map[i][j] += 1
elif j<13 and me[i-2][j+2] == 1 and enemy[i-1][j+1] == 0 and i > 1:
connect_map[i][j] += 1
elif j<12 and me[i-3][j+3] == 1 and enemy[i-1][j+1] == 0 and enemy[i-2][j+2] == 0 and i > 2:
connect_map[i][j] += 1
# rd
if i < 14 and j < 14 and me[i+1][j+1] == 1:
connect_map[i][j] += 1
elif i < 13 and j < 13 and me[i+2][j+2] == 1 and enemy[i+1][j+1] == 0:
connect_map[i][j] += 1
elif i < 12 and j < 12 and me[i+3][j+3] == 1 and enemy[i+1][j+1] == 0 and enemy[i+2][j+2] == 0:
connect_map[i][j] += 1
return connect_map