-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess.py
executable file
·321 lines (242 loc) · 8.51 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
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
import argparse
from enum import Enum
from pprint import pprint
import copy
class PieceType(Enum):
QUEEN = 1
ROOK = 2
BISHOP = 3
KING = 4
KNIGHT = 5
EMPTY = ' '
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--width", "-w", help="width of the board", type=int)
parser.add_argument("--length", "-l", help="length of the board", type=int)
parser.add_argument("--kings", "-k", help="number of kings", type=maximum_2, default=0)
parser.add_argument("--queens", "-q", help="number of queens", type=maximum_2, default=0)
parser.add_argument("--rooks", "-r", help="number of rooks", type=maximum_4, default=0)
parser.add_argument("--bishops", "-b", help="number of bishops", type=maximum_4, default=0)
parser.add_argument("--knights", "-n", help="number of bishops", type=maximum_4, default=0)
args = parser.parse_args()
return args
def maximum_2(n):
n_int = int(n)
if n_int < 0 or n_int > 2:
print("Value must be between 0 and 2")
exit(1)
return n_int
def maximum_4(n):
n_int = int(n)
if n_int < 0 or n_int > 4:
print("Value must be between 0 and 4")
exit(1)
return n_int
def contains_piece(board, row, col):
try:
if board[row][col] and board[row][col] != EMPTY:
return True
except:
print(f"failed for row {row}, col {col}")
pprint(board)
raise
return False
def is_empty_row(board, row_num):
for i in range(len(board[row_num])):
if board[row_num][i] and board[row_num][i] != EMPTY:
return False
return True
def is_empty_column(board, col_num):
for i in range(len(board)):
if board[i][col_num] and board[i][col_num] != EMPTY:
return False
return True
def empty_row(board, row_num):
for i in range(len(board[row_num])):
board[row_num][i] = EMPTY
def empty_column(board, col_num):
for i in range(len(board)):
board[i][col_num] = EMPTY
def is_empty_diagonals(board, row, col):
max_cols = len(board[0])
max_rows = len(board)
for n in range(-max_rows, max_rows):
if (row+n >= 0 and col+n >= 0 and row+n < max_rows and col+n < max_cols):
if contains_piece(board, row+n, col+n):
return False
if (row+n >= 0 and col-n >= 0 and row+n < max_rows and col-n < max_cols):
if contains_piece(board, row+n, col-n):
return False
for n in range(-max_cols, max_cols):
if (row+n >= 0 and col+n >= 0 and row+n < max_rows and col+n < max_cols):
if contains_piece(board, row+n, col+n):
return False
if (row-n >= 0 and col+n >= 0 and row-n < max_rows and col+n < max_cols):
if contains_piece(board, row-n, col+n):
return False
return True
def empty_diagonals(board, row, col):
max_cols = len(board[0])
max_rows = len(board)
for n in range(-max_rows, max_rows):
if (row+n >= 0 and col+n >= 0 and row+n < max_rows and col+n < max_cols):
board[row+n][col+n] = EMPTY
if (row+n >= 0 and col-n >= 0 and row+n < max_rows and col-n < max_cols):
board[row+n][col-n] = EMPTY
for n in range(-max_cols, max_cols):
if (row+n >= 0 and col+n >= 0 and row+n < max_rows and col+n < max_cols):
board[row+n][col+n] = EMPTY
if (row-n >= 0 and col+n >= 0 and row-n < max_rows and col+n < max_cols):
board[row-n][col+n] = EMPTY
def place_bishop(i, j, max_i, max_j, board):
if board[i][j]:
return False
if not is_empty_diagonals(board, i, j):
return False
empty_diagonals(board, i, j)
board[i][j] = 'B'
return True
def place_queen(i, j, max_i, max_j, board):
if (
board[i][j]
or not is_empty_row(board, i)
or not is_empty_column(board, j)
or not is_empty_diagonals(board, i, j)
):
return False
empty_row(board, i)
empty_column(board, j)
empty_diagonals(board, i, j)
board[i][j] = 'Q'
return True
def place_knight(i, j, max_i, max_j, board):
if (
board[i][j]
or (i+2 < max_i and j+1 < max_j and contains_piece(board, i+2, j+1))
or (i-2 >= 0 and j+1 < max_j and contains_piece(board, i-2, j+1))
or (i+2 < max_i and j-1 >= 0 and contains_piece(board, i+2, j-1))
or (i-2 >= 0 and j-1 >= 0 and contains_piece(board, i-2, j-1))
or (i+1 < max_i and j+2 < max_j and contains_piece(board, i+1, j+2))
or (i-1 >= 0 and j+2 < max_j and contains_piece(board, i-1, j+2))
or (i+1 < max_i and j-2 >= 0 and contains_piece(board, i+1, j-2))
or (i-1 >= 0 and j-2 >= 0 and contains_piece(board, i-1, j-2))
):
return False
if i+2 < max_i and j+1 < max_j:
board[i+2][j+1] = EMPTY
if i-2 >= 0 and j+1 < max_j:
board[i-2][j+1] = EMPTY
if i+2 < max_i and j-1 >= 0 :
board[i+2][j-1] = EMPTY
if i-2 >= 0 and j-1 >= 0 :
board[i-2][j-1] = EMPTY
if i+1 < max_i and j+2 < max_j:
board[i+1][j+2] = EMPTY
if i-1 >= 0 and j+2 < max_j:
board[i-1][j+2] = EMPTY
if i+1 < max_i and j-2 >= 0 :
board[i+1][j-2] = EMPTY
if i-1 >= 0 and j-2 >= 0 :
board[i-1][j-2] = EMPTY
board[i][j] = 'N'
return True
def place_rook(i, j, board):
if (
board[i][j]
or not is_empty_row(board, i)
or not is_empty_column(board, j)
):
return False
empty_row(board, i)
empty_column(board, j)
board[i][j] = 'R'
return True
def place_king(i, j, max_i, max_j, board):
if (
board[i][j]
or (i+1 < max_i and contains_piece(board, i+1, j))
or (i-1 >= 0 and contains_piece(board, i-1, j))
or (j+1 < max_j and contains_piece(board, i, j+1))
or (j-1 >= 0 and contains_piece(board, i, j-1))
or (i+1 < max_i and j+1 < max_j and contains_piece(board, i+1, j+1))
or (i-1 >= 0 and j+1 < max_j and contains_piece(board, i-1, j+1))
or (i+1 < max_i and j-1 >= 0 and contains_piece(board, i+1, j-1))
or (i-1 >= 0 and j-1 >= 0 and contains_piece(board, i-1, j-1))
):
return False
if i+1 < max_i:
board[i+1][j] = EMPTY
if i-1 >= 0:
board[i-1][j] = EMPTY
if j+1 < max_j:
board[i][j+1] = EMPTY
if j-1 >= 0:
board[i][j-1] = EMPTY
if i+1 < max_i and j+1 < max_j:
board[i+1][j+1] = EMPTY
if i-1 >= 0 and j+1 < max_j:
board[i-1][j+1] = EMPTY
if i+1 < max_i and j-1 >= 0:
board[i+1][j-1] = EMPTY
if i-1 >= 0 and j-1 >= 0:
board[i-1][j-1] = EMPTY
board[i][j] = 'K'
return True
def place_piece_at(x, y, width, length, board, piece_type):
match piece_type:
case PieceType.ROOK:
return place_rook(x, y, board)
case PieceType.KING:
return place_king(x, y, width, length, board)
case PieceType.KNIGHT:
return place_knight(x, y, width, length, board)
case PieceType.BISHOP:
return place_bishop(x, y, width, length, board)
case PieceType.QUEEN:
return place_queen(x, y, width, length, board)
case _:
return True
def place(board, piece, width, length, free_spaces):
new_boards = []
for x in range(width):
for y in range(length):
new_board = copy.deepcopy(board)
valid = place_piece_at(x, y, width, length, new_board, piece)
if valid:
new_boards.append(new_board)
return new_boards
def place_all(pieces, rows, columns):
free_spaces = rows * columns
empty_board = [ [ None ]*columns for i in range(rows)]
valid_boards = [ empty_board ]
for piece in pieces:
valid_boards_after_adding_piece = []
for board in valid_boards:
updated_boards = place(board, piece, rows, columns, free_spaces)
valid_boards_after_adding_piece += updated_boards
valid_boards = copy.deepcopy(valid_boards_after_adding_piece)
free_spaces -= 1
return valid_boards
def solve(rows, columns, num_kings, num_queens, num_bishops, num_rooks, num_knights):
pieces = []
pieces += [ PieceType.KING for _ in range(num_kings) ]
pieces += [ PieceType.ROOK for _ in range(num_rooks) ]
pieces += [ PieceType.BISHOP for _ in range(num_bishops) ]
pieces += [ PieceType.QUEEN for _ in range(num_queens) ]
pieces += [ PieceType.KNIGHT for _ in range(num_knights) ]
all_boards = place_all(pieces, rows, columns)
all_unique_boards = {str(x).replace("None", "' '") for x in all_boards}
pprint(all_unique_boards)
print(len(all_unique_boards))
if __name__ == '__main__':
args = parse_args()
columns, rows = args.width, args.length
num_kings = args.kings
num_queens = args.queens
num_rooks = args.rooks
num_bishops = args.bishops
num_knights = args.knights
if (num_kings+num_queens+num_bishops+num_rooks+num_knights) >= columns*rows:
print("No valid solutions because number of pieces is greater than or equal to the size of the board")
exit(1)
solve(rows, columns, num_kings, num_queens, num_bishops, num_rooks, num_knights)