-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpieceplacer.py
More file actions
297 lines (270 loc) · 10.4 KB
/
pieceplacer.py
File metadata and controls
297 lines (270 loc) · 10.4 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
import sys
emptychar = "."
threatened = "T"
king = "K"
knight = "N"
pawn = "P"
queen = "Q"
bishop = "B"
rook = "R"
# check to see if the spot has been taken or threatened
def is_available(space, x, y):
# if the space is out of bounds, then it's definitely not available
# also, it should be an emptychar
return 0 <= x < 8 and 0 <= y < 8 and (board[x][y] == emptychar)
def disturbs_space(space, x, y):
# the first two checks are to make sure the index is in-bounds. if index out of bounds, just let the caller know
# that it's not threatening things out of bounds. it's not even really a lie, and simplifies several checks.
# if it's neither empty, nor threatened, then it is a piece
return 0 <= x < 8 and 0 <= y < 8 and (board[x][y] != emptychar and board[x][y] != threatened)
# note that we define pawns to be side-agnostic: they can attack one square in any diagonal direction
def place_pawn(board, i, j):
placed = False
if is_available(board, i, j):
available = True
# ensure that this space doesn't threaten an existing piece,
# if a location is neither empty, nor threatened, then a piece is in that location
if disturbs_space(board, i+1, j+1) or \
disturbs_space(board, i-1, j+1) or \
disturbs_space(board, i+1, j-1) or \
disturbs_space(board, i-1, j-1):
available = False
# check to see if the spot is available
if available:
board[i][j] = "P"
placed = True
# mark all threatened spaces
if is_available(board, i+1, j+1):
board[i+1][j+1] = "T"
if is_available(board, i-1, j+1):
board[i-1][j+1] = "T"
if is_available(board, i+1, j-1):
board[i+1][j-1] = "T"
if is_available(board, i-1, j-1):
board[i-1][j-1] = "T"
return placed
def place_king(board, i, j):
placed = False
if is_available(board, i, j):
available = True
# ensure that this space doesn't threaten an existing piece,
# if a location is neither empty, nor threatened, then a piece is in that location
if disturbs_space(board, i+1, j+1) or \
disturbs_space(board, i-1, j+1) or \
disturbs_space(board, i+1, j-1) or \
disturbs_space(board, i-1, j-1) or \
disturbs_space(board, i, j+1) or \
disturbs_space(board, i, j-1) or \
disturbs_space(board, i+1, j) or \
disturbs_space(board, i-1, j):
available = False
# check to see if the spot is available
if available:
board[i][j] = "K"
placed = True
# mark all threatened spaces
if is_available(board, i+1, j+1):
board[i+1][j+1] = "T"
if is_available(board, i-1, j+1):
board[i-1][j+1] = "T"
if is_available(board, i+1, j-1):
board[i+1][j-1] = "T"
if is_available(board, i-1, j-1):
board[i-1][j-1] = "T"
if is_available(board, i, j+1):
board[i-1][j-1] = "T"
if is_available(board, i, j-1):
board[i-1][j-1] = "T"
if is_available(board, i+1, j):
board[i-1][j-1] = "T"
if is_available(board, i-1, j):
board[i-1][j-1] = "T"
return placed
def place_knight(board, i, j):
placed = False
if is_available(board, i, j):
available = True
# ensure that this space doesn't threaten an existing piece,
# if a location is neither empty, nor threatened, then a piece is in that location
if disturbs_space(board, i+2, j+1) or \
disturbs_space(board, i+2, j-1) or \
disturbs_space(board, i-2, j+1) or \
disturbs_space(board, i-2, j-1) or \
disturbs_space(board, i+1, j+2) or \
disturbs_space(board, i-1, j+2) or \
disturbs_space(board, i+1, j-2) or \
disturbs_space(board, i-1, j-2):
available = False
# check to see if the spot is available
if available:
board[i][j] = "N"
placed = True
# mark all threatened spaces
if is_available(board, i+2, j+1):
board[i+2][j+1] = "T"
if is_available(board, i+2, j-1):
board[i+2][j-1] = "T"
if is_available(board, i-2, j+1):
board[i-2][j+1] = "T"
if is_available(board, i-2, j-1):
board[i-2][j-1] = "T"
if is_available(board, i+1, j+2):
board[i+1][j+2] = "T"
if is_available(board, i-1, j+2):
board[i-1][j+2] = "T"
if is_available(board, i+1, j-2):
board[i+1][j-2] = "T"
if is_available(board, i-1, j-2):
board[i-1][j-2] = "T"
return placed
def place_rook(board, i, j):
placed = False
# check to see if the spot is used or threatened
if is_available(board, i, j):
available = True
# ensure that this doesn't threaten an already-placed piece:
for x in range(8):
# if it's neither empty, nor threatened, then it is a piece
if disturbs_space(board, i, x) or disturbs_space(board, x, j):
# can't use this spot!
available = False
break
if available:
placed = True
board[i][j] = "R"
# mark all threatened spaces
for k in range(8):
if is_available(board, k, j):
board[k][j] = "T"
if is_available(board, i, k):
board[i][k] = "T"
return placed
def place_bishop(board, i, j):
placed = False
# check to see if the spot is used or threatened
if is_available(board, i, j):
available = True
# ensure that this doesn't threaten an already-placed piece:
for c in range(8):
# if it's neither empty, nor threatened, then it is a piece
if disturbs_space(board, i-c, j-c) or \
disturbs_space(board, i+c, j+c) or \
disturbs_space(board, i-c, j+c) or \
disturbs_space(board, i+c, j-c):
# can't use this spot!
available = False
break
if available:
placed = True
board[i][j] = "B"
# mark all threatened spaces
for c in range(8):
# if it's neither empty, nor threatened, then it is a piece
if is_available(board, i-c, j-c):
board[i-c][j-c] = "T"
if is_available(board, i-c, j+c):
board[i-c][j+c] = "T"
if is_available(board, i+c, j-c):
board[i+c][j-c] = "T"
if is_available(board, i+c, j+c):
board[i+c][j+c] = "T"
return placed
def place_queen(board, i, j):
placed = False
# check to see if the spot is used or threatened
if is_available(board, i, j):
available = True
# ensure that this doesn't threaten an already-placed piece:
for c in range(8):
# if it's neither empty, nor threatened, then it is a piece
if disturbs_space(board, i-c, j-c) or \
disturbs_space(board, i+c, j+c) or \
disturbs_space(board, i-c, j+c) or \
disturbs_space(board, i+c, j-c) or \
disturbs_space(board, i, c) or \
disturbs_space(board, c, j):
# can't use this spot!
available = False
break
if available:
placed = True
board[i][j] = "Q"
# mark all threatened spaces
for c in range(8):
# if it's neither empty, nor threatened, then it is a piece
if is_available(board, i-c, j-c):
board[i-c][j-c] = "T"
if is_available(board, i-c, j+c):
board[i-c][j+c] = "T"
if is_available(board, i+c, j-c):
board[i+c][j-c] = "T"
if is_available(board, i+c, j+c):
board[i+c][j+c] = "T"
if is_available(board, c, j):
board[c][j] = "T"
if is_available(board, i, c):
board[i][c] = "T"
return placed
def place_piece(board, piece, x, y):
if piece == "pawn":
return place_pawn(board, x, y)
elif piece == "rook":
return place_rook(board, x, y)
elif piece == "bishop":
return place_bishop(board, x, y)
elif piece == "knight":
return place_knight(board, x, y)
elif piece == "king":
return place_king(board, x, y)
elif piece == "queen":
return place_queen(board, x, y)
# initialize the board with + in each empty space
def init_board(board):
for i in range(8):
board.append([])
for j in range(8):
board[i].append(emptychar)
def print_board(board):
for i in range(8):
print
for j in range(8):
if board[i][j] == threatened:
print emptychar,
else:
print board[i][j],
def guess_placement(board, pieces, x, y):
piece = pieces.pop()
# base case, try to place the last piece then return
if len(pieces) == 0:
return place_piece(board, piece, x, y)
# otherwise, place the next piece and go back to the setup_board state
elif place_piece(board, piece, x, y):
return setup_board(board, pieces)
else:
return False
def setup_board(board, pieces):
for x in range(8):
for y in range(8):
copyboard = [list(p) for p in board]
copypieces = list(pieces)
# try putting our first piece in this location
if guess_placement(board, pieces, x, y):
# success!
return True
else:
# failure! revert the board and try again
del board[:]
for p in copyboard:
board.append(p)
# board = [list(p) for p in copyboard]
del pieces[:]
for p in copypieces:
pieces.append(p)
return False
if __name__ == "__main__":
# get the list of space-separated pieces
pieces = sys.argv[1].split(" ")
board = []
init_board(board)
ret = setup_board(board, pieces)
print_board(board)