-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture_move.py
161 lines (143 loc) · 4.72 KB
/
capture_move.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/python
# -*- coding:utf-8 -*-
def get_board_state(s):
if len(s) == 16:
n = 4
elif len(s) == 36:
n = 6
res = []
for i in range(n):
res.append([s[i + j * n] for j in range(n)])
return res
def get_board_string(l):
a = [l[x][y] for y in range(len(l)) for x in range(len(l))]
return ''.join(a)
def make_move_capture(board_string, start, end):
if len(board_string) not in [16, 36] \
or not isinstance(board_string, str) \
or not isinstance(start, tuple) \
or not isinstance(end, tuple) \
or not isinstance(start[0], int) \
or not isinstance(start[1], int) \
or not isinstance(end[0], int) \
or not isinstance(end[1], int) \
or not len(start) == len(end) == 2:
return None
for bs in board_string:
if bs not in ['o', 'x', '.']:
return None
board_state = get_board_state(board_string)
start_x, start_y = start
end_x, end_y = end
if board_state[end_x][end_y] == '.' \
or board_state[start_x][start_y] == board_state[end_x][end_y]\
or start_x < 0 or start_y < 0 \
or end_x < 0 or end_y < 0 \
or start_x >= len(board_state) or start_y >= len(board_state) \
or end_x >= len(board_state) or end_y >= len(board_state):
return None
else:
n = len(board_state)
board_flag = [[0 for _ in range(n)] for _ in range(n)]
for x in range(n):
for y in range(n):
if board_state[x][y] == '.':
board_flag[x][y] = 1
if auto_run(start, board_flag, end, n):
board_state[end_x][end_y] = board_state[start_x][start_y]
board_state[start_x][start_y] = '.'
return get_board_string(board_state)
else:
return None
def auto_run(start, board_flag, end, n):
next_locations, is_captured = next_step(start, board_flag, end, n)
if is_captured:
return True
else:
for ns in next_locations:
board_flag[ns[0]][ns[1]] = 0
if auto_run(ns, board_flag, end, n):
return True
def next_step(now_location, board_flag, end, n):
# n: 4 or 6
x, y = now_location
next_locations = []
flag = True
is_captured = False
for i in range(x + 1, n):
if board_flag[i][y] != 1:
flag = False
if end == (i, y):
is_captured = True
break
if flag and n-1 != x:
next_locations.append((n - 1, y))
flag = True
for i in range(x - 1, -1, -1):
if board_flag[i][y] != 1:
flag = False
if end == (i, y):
is_captured = True
break
if flag and 0 != x:
next_locations.append((0, y))
flag = True
for j in range(y + 1, n):
if board_flag[x][j] != 1:
flag = False
if end == (x, j):
is_captured = True
break
if flag and n-1 != y:
next_locations.append((x, n - 1))
flag = True
for j in range(y - 1, -1, -1):
if board_flag[x][j] != 1:
flag = False
if end == (x, j):
is_captured = True
break
if flag and 0 != y:
next_locations.append((x, 0))
circle_track = init_circle_track(n)
if now_location in circle_track:
next_locations.append(circle_track[now_location])
next_locations = list(filter(lambda a: board_flag[a[0]][a[1]] == 1,
next_locations))
return next_locations, is_captured
def init_circle_track(n):
if n == 4:
tmp_dict = {}
tmp_dict[(1, 0)] = (0, 1)
tmp_dict[(0, 1)] = (1, 0)
tmp_dict[(2, 0)] = (3, 1)
tmp_dict[(3, 1)] = (2, 0)
tmp_dict[(1, 3)] = (0, 2)
tmp_dict[(0, 2)] = (1, 3)
tmp_dict[(2, 3)] = (3, 2)
tmp_dict[(3, 2)] = (2, 3)
return tmp_dict
if n == 6:
tmp_dict = {}
tmp_dict[(1, 0)] = (0, 1)
tmp_dict[(0, 1)] = (1, 0)
tmp_dict[(2, 0)] = (0, 2)
tmp_dict[(0, 2)] = (2, 0)
tmp_dict[(4, 0)] = (5, 1)
tmp_dict[(5, 1)] = (4, 0)
tmp_dict[(3, 0)] = (5, 2)
tmp_dict[(5, 2)] = (3, 0)
tmp_dict[(1, 5)] = (0, 4)
tmp_dict[(0, 4)] = (1, 5)
tmp_dict[(2, 5)] = (0, 3)
tmp_dict[(0, 3)] = (2, 5)
tmp_dict[(4, 5)] = (5, 4)
tmp_dict[(5, 4)] = (4, 5)
tmp_dict[(3, 5)] = (5, 3)
tmp_dict[(5, 3)] = (3, 5)
return tmp_dict
if __name__ == "__main__":
print(make_move_capture('xxxx........oooo', (0, 0), (0, 1)))
# None
print(make_move_capture('x.xx.x......oooo', (1, 1), (1, 3)))
# 'x.xx........oxoo'