-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChess.py
188 lines (129 loc) · 4.9 KB
/
Chess.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
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
from collections import deque
import itertools
import heapq
import time
# Board:
# -1 --> NOT MOVEABLE
# 0 --> EMPTY
# 1 --> RED
# 2 --> BLUE
# 3 --> GOLDEN
# CONFIGURATION
INITIAL_BOARD = [1, 2, -1, 2, 3, 0, -1, 3, -1, 0, 1, 0, 3, 0, 1, 0, -1, 3, -1, 0, 3, 2, -1, 2, 1]
WINNING_BOARD = [0, 1, -1, 2, 0, 0, -1, 2, -1, 0, 1, 0, 0, 0, 1, 0, -1, 2, -1, 0, 0, 1, -1, 2, 0]
# SPEEDUP OR SPEEDUP_EXP, NOT both!
SPEEDUP = False # May be turned on for speed, Heuristic: Out of Position
SPEEDUP_EXP = True # Non-admissible heuristic, very fast, probably NOT optimal
#
assert len(WINNING_BOARD) == len(INITIAL_BOARD)
assert len(WINNING_BOARD) in [9, 12, 16, 20, 25]
print("Starting..")
LEN_X = 1
LEN_Y = 1
if len(WINNING_BOARD) == 9:
LEN_X = 3
LEN_Y = 3
elif len(WINNING_BOARD) == 12:
LEN_X = 3
LEN_Y = 4
elif len(WINNING_BOARD) == 16:
LEN_X = 4
LEN_Y = 4
elif len(WINNING_BOARD) == 20:
LEN_X = 4
LEN_Y = 5
elif len(WINNING_BOARD) == 25:
LEN_X = 5
LEN_Y = 5
LEN = LEN_X * LEN_Y
class ChessBoard:
def __init__(self, board, parent, depth):
self.board = board
self.parent = parent
self.depth = depth + 1
if SPEEDUP:
self.value = self.generate_value() + self.depth
elif SPEEDUP_EXP:
self.value = self.generate_value()
def is_winner(self):
return [0 if x == 3 else x for x in self.board] == WINNING_BOARD
def generate_value(self):
counter = 0
for i in range(LEN):
if WINNING_BOARD[i] == 1 or WINNING_BOARD[i] == 2:
if WINNING_BOARD[i] != self.board[i]:
counter += 1
return counter
def get_valid_moves(board):
moves = []
for i, x in enumerate(board.board):
if x != 0 and x != -1:
if i + 2 * LEN_X - 1 < LEN and i % LEN_X - 1 >= 0 and board.board[i + 2 * LEN_X - 1] == 0:
moves.append((i, i + 2 * LEN_X - 1))
if i + 2 * LEN_X + 1 < LEN and i % LEN_X + 1 < LEN_X and board.board[i + 2 * LEN_X + 1] == 0:
moves.append((i, i + 2 * LEN_X + 1))
if i - 2 * LEN_X + 1 >= 0 and i % LEN_X + 1 < LEN_X and board.board[i - 2 * LEN_X + 1] == 0:
moves.append((i, i - 2 * LEN_X + 1))
if i - 2 * LEN_X - 1 >= 0 and i % LEN_X - 1 >= 0 and board.board[i - 2 * LEN_X - 1] == 0:
moves.append((i, i - 2 * LEN_X - 1))
if i % LEN_X - 2 >= 0 and i - LEN_X - 2 >= 0 and board.board[i - LEN_X - 2] == 0:
moves.append((i, i - LEN_X - 2))
if i % LEN_X - 2 >= 0 and i + LEN_X - 2 < LEN and board.board[i + LEN_X - 2] == 0:
moves.append((i, i + LEN_X - 2))
if i % LEN_X + 2 < LEN_X and i - LEN_X + 2 >= 0 and board.board[i - LEN_X + 2] == 0:
moves.append((i, i - LEN_X + 2))
if i % LEN_X + 2 < LEN_X and i + LEN_X + 2 < LEN and board.board[i + LEN_X + 2] == 0:
moves.append((i, i + LEN_X + 2))
return moves
def get_board(board, move): # move -> tuple (index_before, index_after)
new_board = board.board[:]
new_board[move[1]] = new_board[move[0]]
new_board[move[0]] = 0
return ChessBoard(new_board, board, board.depth)
def list_to_string(l):
return "".join(str(l))
def bfs(node):
heap = []
queue = []
counter = itertools.count()
if SPEEDUP or SPEEDUP_EXP:
heapq.heappush(heap, [node.value, next(counter), node])
else:
queue = deque([node])
visited = set()
k = 0
while True: # Since solvability is guaranteed this is equivalent: while queue:
if k % 100000 == 0:
if SPEEDUP or SPEEDUP_EXP:
print(len(heap))
else:
print(len(queue))
if SPEEDUP or SPEEDUP_EXP:
pop = heapq.heappop(heap)[2]
else:
pop = queue.popleft()
if pop.is_winner():
return pop
for x in get_valid_moves(pop):
node_ = get_board(pop, x)
if list_to_string(node_.board) not in visited:
visited.add(list_to_string(node_.board))
if SPEEDUP or SPEEDUP_EXP:
heapq.heappush(heap, [node_.value, next(counter), node_])
else:
queue.append(node_)
k += 1
def board_print(board):
print("\n")
for i in range(LEN_Y):
print(board[i * LEN_X:(i + 1) * LEN_X])
start_time = time.time()
root = ChessBoard(INITIAL_BOARD, None, 0)
winner = bfs(root)
a = 0
while winner is not None:
a += 1
board_print(winner.board)
winner = winner.parent
print("--- %s seconds ---" % (time.time() - start_time))
print("Win in {} Moves. If SPEEDUP_EXP == True, this may NOT be optimal.".format(a - 1))