-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI_play_AI.py
139 lines (123 loc) · 4.28 KB
/
AI_play_AI.py
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
import numpy as np
from main import AI as AI
from main1 import AI as AI1
def display_chessboard(chessboard):
colors = ['·', '◯', '●']
print('\t', end='')
for i in range(chessboard[0].size):
print(i, end='\t')
print()
start = 0
for e in chessboard:
print(start, end=':\t')
for j in e:
# if (j == )
print(colors[int(j)], end='\t')
start += 1
print()
# line_processor use for duplicated process of lines in function evaluator
def line_processor(line, color):
colors = ['-', 'o', 'x'] # o表示自己, x表示对手, 所以这个数组是给白棋用的
if color == -1:
colors = ['-', 'x', 'o']
result = line.tolist()
result.insert(0, -color)
result.append(-color)
return [colors[int(c)] for c in result]
def go_bang(chessboard, position, color):
assert chessboard[position[0], position[1]] == 0
chessboard[position[0], position[1]] = color
# 测试用
def AI_go(chessboard, arti):
arti.go(chessboard)
# print(ai.candidate_list)
go_bang(chessboard, arti.candidate_list[-1], arti.color)
def who_win(chessboard):
length = len(chessboard)
for i in range(length):
if kmp_match(line_processor(chessboard[i, :], 1), 'ooooo') > 0 or \
kmp_match(line_processor(chessboard[:, i], 1), 'ooooo') > 0:
print("White win, Game over")
display_chessboard(chessboard)
return True
if kmp_match(line_processor(chessboard[i, :], -1), 'ooooo') > 0 or \
kmp_match(line_processor(chessboard[:, i], -1), 'ooooo') > 0:
print("Black win, Game over")
display_chessboard(chessboard)
return True
h = [i for i in range(length)]
v = h[::-1]
for i in range(5, length + 1):
if i < length:
if kmp_match(line_processor(chessboard[h[:i - length], v[length - i:]], 1), 'ooooo') > 0 or \
kmp_match(line_processor(chessboard[h[:i - length], h[length - i:]], 1), 'ooooo') > 0:
print("White win, Game over")
display_chessboard(chessboard)
return True
if kmp_match(line_processor(chessboard[h[:i - length], v[length - i:]], -1), 'ooooo') > 0 or \
kmp_match(line_processor(chessboard[h[:i - length], h[length - i:]], -1), 'ooooo') > 0:
print("Black win, Game over")
display_chessboard(chessboard)
return True
return False
# -1 black, 1 white
def play(chessboard, chess, inChess):
x, y = inChess
if x < 0 or x > 14 or y < 0 or y > 14:
print('越界了重新下')
play(chessboard, chess)
return
if chessboard[x][y] != 0:
print('当前位置已经满了,请重新下')
play(chessboard, chess)
return
if chess == -1:
chessboard[x][y] = -1
if chess == 1:
chessboard[x][y] = 1
# KMP
def kmp_match(s, p):
m = len(s) # string
n = len(p) # pattern
count = 0
cur = 0 # 起始指针cur
table = partial_table(p)
while cur <= m - n:
for i in range(n):
if s[i + cur] != p[i]:
cur += max(i - table[i - 1], 1) # 有了部分匹配表,我们不只是单纯的1位1位往右移,可以一次移动多位
break
else:
count = count + 1
cur += 1
return count
# 部分匹配表
def partial_table(p):
prefix = set()
ret = [0]
for i in range(1, len(p)):
prefix.add(p[:i])
postfix = {p[j:i + 1] for j in range(1, i + 1)}
ret.append(len((prefix & postfix or {''}).pop()))
return ret
if __name__ == '__main__':
cb = np.zeros([15, 15], dtype=int)
# cb[14, 5] = COLOR_BLACK
# cb[5, 5] = COLOR_BLACK
# no = np.where(cb == COLOR_NONE)
# played = np.where(cb != COLOR_NONE)
# played_list = list(zip(played[0], played[1]))
#
display_chessboard(cb)
# ai = aii(15, COLOR_BLACK, 100)
ai = AI(15, 1, 100)
ai2 = AI1(15, -1, 100)
# ai2 = AI(15, COLOR_WHITE, 100)
x = 0
y = 0
while np.where(cb == 0)[0].size > 0 and not who_win(chessboard=cb):
AI_go(cb, ai2)
display_chessboard(cb)
# tempt = input("Enter to continue")
AI_go(cb, ai)
display_chessboard(cb)