-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
467 lines (405 loc) · 18.6 KB
/
main.py
File metadata and controls
467 lines (405 loc) · 18.6 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import numpy as np
import Movement
import Pawn
import ChessAI
import time
import King
import Queen
import Rook as rookie
import Bishop as bish
import Knight as knight
# DICTIONARY TO MAP UNICODE OBJECTS
chess = {
'b_checker': u'\u25FB',
'b_pawn': u'\u265F',
'b_rook': u'\u265C',
'b_knight': u'\u265E',
'b_bishop': u'\u265D',
'b_king': u'\u265A',
'b_queen': u'\u265B',
'w_checker': u'\u25FC',
'w_pawn': u'\u2659',
'w_rook': u'\u2656',
'w_knight': u'\u2658',
'w_bishop': u'\u2657',
'w_king': u'\u2654',
'w_queen': u'\u2655'
}
# FUNCTION TO PRINT CHECKERS WITHOUT ANY PIECE ON BOARD
def getchecks(user):
bw_row = [chess['b_checker'], chess['w_checker']] * 4
# print(bw_row)
bw_checkers = []
for i in range(8):
bw_checkers.append(bw_row if i % 2 == 0 else bw_row[::-1])
# print(bw_checkers)
bw_checkers = np.array(bw_checkers)
# print(bw_checkers)
wb_checkers = bw_checkers[::-1]
# print(wb_checkers)
board = []
for _ in range(4):
board.append(['0'] * 8)
return wb_checkers
# FUNCTION TO PRINT CHEKCERS
def get_checkers():
bw_row = [chess['b_checker'], chess['w_checker']] * 4
# print(bw_row)
bw_checkers = []
for i in range(8):
bw_checkers.append(bw_row if i % 2 == 0 else bw_row[::-1])
# print(bw_checkers)
bw_checkers = np.array(bw_checkers)
# print(bw_checkers)
wb_checkers = bw_checkers[::-1]
# print(wb_checkers)
return {'W': wb_checkers, 'B': bw_checkers}
# FUNCTION TO GET BOARD
def get_board():
def get_army(user):
u = user.lower()
guard = [chess[u + '_rook'], chess[u + '_knight'], chess[u + '_bishop']]
rear = guard + [chess[u + '_queen'], chess[u + '_king']] + guard[::-1]
front = [chess[u + '_pawn']] * 8
# print(front)
if user == 'W':
return [rear, front]
else:
return [front, rear]
board = []
board += get_army('W')
# board = [squad for squad in get_army('B')]
# board=[]
for _ in range(4):
board.append(['0'] * 8)
# board += get_army('B')
board += get_army('B')
return np.array(board)
# FUNCTION TO PRINT BOARD
def print_board(board, checkers, user):
count = 8
alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
chks = checkers[user]
temp = board.copy() # if user == 'W' else board.copy()[::-1]
for i, row in enumerate(temp):
for j, c in enumerate(row):
if c == '0':
print('', chks[i][j], end='')
else:
print('', temp[i][j], end='')
print(' ', count)
count -= 1
# print('')
print('', *alphabets)
# print(i, j)
# time.sleep(5)
# print('', chks[i][j] if c == '0' else c, end='', flush = True)
def board_state_change(temp_board, board, temp_input, temp_output):
temp_piece = board[temp_input[0]][temp_input[1]]
board[temp_input[0]][temp_input[1]] = temp_board[temp_input[0]][temp_input[1]]
board[temp_output[0]][temp_output[1]] = temp_piece
return board
def check_legal_move(board, temp_input):
temp_piece = board[temp_input[0]][temp_input[1]]
if temp_piece == chess['b_checker'] or temp_piece == chess['w_checker']:
return True
return False
# MAIN
if __name__ == "__main__":
checkers = get_checkers()
print("WELCOME TO CHESS")
print("Select a color you want to play")
print("for White, press W, \n", "for Black, press B, \n")
user = input("enter the color you want to play: ")
while user != 'W' and user != 'B':
print("invalid input, enter again: ")
user = input("enter the color you want to play: ")
board = get_board()
print_board(board, checkers, user)
# with only checkers
temp_board = getchecks(user)
running = True
PlayerOne = True
PlayerTwo = True
check = False
check_mate = False
if user == 'W':
PlayerOne = True
PlayerTwo = False
turn = PlayerOne
AI = 'B'
else:
PlayerOne = False
PlayerTwo = True
turn = PlayerTwo
AI = 'W'
print("GAME IS STARTING", '\n')
if PlayerOne:
while running:
time.sleep(1)
# USER HAS THE FIRST MOVE
if turn == PlayerOne:
print("IT IS YOUR TURN")
print("enter p for pawn, b for bishop, q for queen, k for king, r for rook, kn for king")
piece = input("enter the piece you want to move: ")
while piece != "p" and piece != "b" and piece != "q" and piece != "k" and piece != "r" and piece != "kn":
print("INVALID INPUT, ENTER AGAIN:")
piece = input("enter the piece you want to move: ")
if piece == "r":
obj_rook = rookie.Rook()
elif piece == "b":
obj_bishop = bish.Bishop()
elif piece == 'k':
obj_knight = knight.Knight()
elif piece == 'kn':
obj_king = King.King()
elif piece == 'q':
obj_queen = Queen.Queen()
# INPUT COORDINATES
print("enter current coordinates of selected piece: ")
input_coordinates = list()
x = int(input("enter row: "))
while x < 1 or x > 8:
print("invalid input, enter again, A NUMBER BETWEEEN 1-8")
x = int(input("enter row: "))
y = input("enter column: ")
while y < 'a' or y > 'h':
print("invalid input, enter again, AN ALPHABET BETWEEEN A-G")
y = input("enter column: ")
input_coordinates.append(x)
input_coordinates.append(y)
# OUTPUT COORDINATES
output_coordinates = list()
print("enter output coordinates")
x = int(input("enter row: "))
while x < 1 or x > 8:
print("invalid input, enter again, A NUMBER BETWEEEN 1-8")
x = int(input("enter row: "))
y = input("enter column: ")
while y < 'a' or y > 'h':
print("invalid input, enter again, AN ALPHABET BETWEEEN A-G")
y = input("enter column: ")
output_coordinates.append(x)
output_coordinates.append(y)
# call to move class constructor
M = Movement.Move(input_coordinates, output_coordinates)
# converting coordinates to iteratables form
temp_input = M.moving_input_coordinates()
temp_output = M.moving_output_coordinates()
# to make sure that input coordinates do not contain checkers
while check_legal_move(board, temp_input):
print("THIS IS AN INVALID MOVE")
print("enter current coordinates of selected piece: ")
input_coordinates = list()
x = int(input("enter row: "))
y = input("enter column: ")
input_coordinates.append(x)
input_coordinates.append(y)
output_coordinates = list()
print("enter new coordinates")
x = int(input("enter row: "))
y = input("enter column: ")
output_coordinates.append(x)
output_coordinates.append(y)
# call to move class constructor
M = Movement.Move(input_coordinates, output_coordinates)
# converting coordinates to iteratables form
temp_input = M.moving_input_coordinates()
temp_output = M.moving_output_coordinates()
# new board printed after changing of pieces
print('\n')
if piece == "r":
obj_rook.setCoordinates(temp_input, temp_output)
legal, attack, check = obj_rook.ValidMove(board, user)
elif piece == "b":
obj_bishop.setCoordinates(temp_input, temp_output)
legal, attack, check = obj_bishop.ValidMove(board, user)
elif piece == "k":
obj_knight.setCoordinates(temp_input, temp_output)
legal, attack, check = obj_knight.ValidMove(board, user)
elif piece == 'p':
p = Pawn.Pawn(user, temp_input, temp_output)
legal = p.validate_move(board)
elif piece == "kn":
obj_king.setCoordinates(temp_input, temp_output)
legal, attack, check = obj_king.ValidMove(board, user)
elif piece == "q":
obj_queen.setCoordinates(temp_input, temp_output)
legal, attack, check = obj_queen.ValidMove(board, user)
if legal:
# new board printed after changing of pieces
if piece == 'p':
print("Pawn", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'b':
print("Bishop", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'q':
print("Queen", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'k':
print("Knight", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'kn':
print("King", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'r':
print("Rook", output_coordinates[0], output_coordinates[1], '\n')
new_board = board_state_change(temp_board, board, temp_input, temp_output)
print_board(new_board, checkers, user)
turn = PlayerTwo
else:
print("\nInvalid Move\n")
temp_output = temp_input
if turn == PlayerTwo:
# ENTER AI GENERATION CODE
print("HI, I AM AI, IT IS MY TURN", '\n')
bot = ChessAI.Bot(AI, board)
temp_input, temp_output, piece = bot.generatemove(board)
new_board = board_state_change(temp_board, board, temp_input, temp_output)
print_board(new_board, checkers, user)
m = Movement.Move(temp_input, temp_output)
output_coordinates = m.covert_output_coordinates(temp_output)
if piece == 'p':
print("Pawn", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'b':
print("Bishop", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'q':
print("Queen", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'k':
print("Knight", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'kn':
print("King", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'r':
print("Rook", output_coordinates[0], output_coordinates[1], '\n')
turn = PlayerOne
time.sleep(1)
elif not PlayerOne:
while running:
if turn == PlayerTwo:
# ENTER AI GENERATION CODE
print("HI, I AM AI, IT IS MY TURN", '\n')
bot = ChessAI.Bot(AI, board)
temp_input, temp_output, piece = bot.generatemove(board)
new_board = board_state_change(temp_board, board, temp_input, temp_output)
print_board(new_board, checkers, user)
m = Movement.Move(temp_input, temp_output)
output_coordinates = m.covert_output_coordinates(temp_output)
if piece == 'p':
print("Pawn", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'b':
print("Bishop", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'q':
print("Queen", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'k':
print("Knight", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'kn':
print("King", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'r':
print("Rook", output_coordinates[0], output_coordinates[1], '\n')
turn = PlayerOne
time.sleep(1)
# USER HAS THE FIRST MOVE
if turn == PlayerOne:
print("IT IS YOUR TURN")
print("enter p for pawn, b for bishop, q for queen, k for king, r for rook, kn for king")
piece = input("enter the piece you want to move: ")
while piece != "p" and piece != "b" and piece != "q" and piece != "k" and piece != "r" and piece != "kn":
print("INVALID INPUT, ENTER AGAIN:")
piece = input("enter the piece you want to move: ")
if piece == "r":
obj_rook = rookie.Rook()
elif piece == "b":
obj_bishop = bish.Bishop()
elif piece == 'k':
obj_knight = knight.Knight()
elif piece == 'kn':
obj_king = King.King()
elif piece == 'q':
obj_queen = Queen.Queen()
# INPUT COORDINATES
print("enter current coordinates of selected piece: ")
input_coordinates = list()
x = int(input("enter row: "))
while x < 1 or x > 8:
print("invalid input, enter again, A NUMBER BETWEEEN 1-8")
x = int(input("enter row: "))
y = input("enter column: ")
while y < 'a' or y > 'h':
print("invalid input, enter again, AN ALPHABET BETWEEEN A-G")
y = input("enter column: ")
input_coordinates.append(x)
input_coordinates.append(y)
# OUTPUT COORDINATES
output_coordinates = list()
print("enter output coordinates")
x = int(input("enter row: "))
while x < 1 or x > 8:
print("invalid input, enter again, A NUMBER BETWEEEN 1-8")
x = int(input("enter row: "))
y = input("enter column: ")
while y < 'a' or y > 'h':
print("invalid input, enter again, AN ALPHABET BETWEEEN A-G")
y = input("enter column: ")
output_coordinates.append(x)
output_coordinates.append(y)
# call to move class constructor
M = Movement.Move(input_coordinates, output_coordinates)
# converting coordinates to iteratables form
temp_input = M.moving_input_coordinates()
temp_output = M.moving_output_coordinates()
# to make sure that input coordinates do not contain checkers
while check_legal_move(board, temp_input):
print("THIS IS AN INVALID MOVE")
print("enter current coordinates of selected piece: ")
input_coordinates = list()
x = int(input("enter row: "))
y = input("enter column: ")
input_coordinates.append(x)
input_coordinates.append(y)
output_coordinates = list()
print("enter new coordinates")
x = int(input("enter row: "))
y = input("enter column: ")
output_coordinates.append(x)
output_coordinates.append(y)
# call to move class constructor
M = Movement.Move(input_coordinates, output_coordinates)
# converting coordinates to iteratables form
temp_input = M.moving_input_coordinates()
temp_output = M.moving_output_coordinates()
# new board printed after changing of pieces
print('\n')
if piece == "r":
obj_rook.setCoordinates(temp_input, temp_output)
legal, attack, check = obj_rook.ValidMove(board, user)
elif piece == "b":
obj_bishop.setCoordinates(temp_input, temp_output)
legal, attack, check = obj_bishop.ValidMove(board, user)
elif piece == "k":
obj_knight.setCoordinates(temp_input, temp_output)
legal, attack, check = obj_knight.ValidMove(board, user)
elif piece == 'p':
p = Pawn.Pawn(user, temp_input, temp_output)
legal = p.validate_move(board)
elif piece == "kn":
obj_king.setCoordinates(temp_input, temp_output)
legal, attack, check = obj_king.ValidMove(board, user)
elif piece == "q":
obj_queen.setCoordinates(temp_input, temp_output)
legal, attack, check = obj_queen.ValidMove(board, user)
if legal:
# new board printed after changing of pieces
if piece == 'p':
print("Pawn", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'b':
print("Bishop", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'q':
print("Queen", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'k':
print("Knight", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'kn':
print("King", output_coordinates[0], output_coordinates[1], '\n')
elif piece == 'r':
print("Rook", output_coordinates[0], output_coordinates[1], '\n')
new_board = board_state_change(temp_board, board, temp_input, temp_output)
print_board(new_board, checkers, user)
turn = PlayerTwo
else:
print("\nInvalid Move\n")
temp_output = temp_input