Repository navigation
Expand file tree
/
Copy pathChess.py
More file actions
346 lines (311 loc) · 12.8 KB
/
Copy pathChess.py
File metadata and controls
346 lines (311 loc) · 12.8 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
# Chess
# 2021
# Updated in 2022
# Lucas Weber
# game of chess starting with white
# doesnt include advanced moves such as castling
# Importing
import pygame
import math
import numpy as np
from extra import draw_library
pygame.init()
# Colours
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
# important variables
COLUMNCOUNT = 8
ROWCOUNT = 8
SQUARESIZE = 75
size = (COLUMNCOUNT * SQUARESIZE, ROWCOUNT * SQUARESIZE)
screen = pygame.display.set_mode(size, 0, 32)
screen.fill(WHITE)
pygame.display.set_caption("Chess")
clock = pygame.time.Clock()
# Functions ************************************************************************************************************
# Creates a matrix of the board
def create_board(rows, columns):
board = np.zeros((rows, columns))
return board
# Sets the pieces on the board
def set_board(board, columns):
# Places the pieces
# Player 1: 10
# Player 2: 20
# Pawn: 1
# Rook: 2
# Knight: 3
# Bishop: 4
# Queen: 5
# King: 6
for c in range(columns):
board[0][c] = 10
board[1][c] = 10
board[-2][c] = 20
board[-1][c] = 20
board[1][c] += 1
board[-2][c] += 1
if c == 0 or c == columns - 1:
board[0][c] += 2
board[-1][c] += 2
elif c == 1 or c == columns - 2:
board[0][c] += 3
board[-1][c] += 3
elif c == 2 or c == columns - 3:
board[0][c] += 4
board[-1][c] += 4
elif c == 3:
board[0][c] += 5
board[-1][c] += 5
elif c == 4:
board[0][c] += 6
board[-1][c] += 6
return board
# Checks that kings are alive
def win_con(board, game):
if 16 not in board and -34 not in board:
return "2", False
elif 26 not in board and -24 not in board:
return "1", False
return None, game
# Selects piece
def symbol_select(turn, board, size, rows, columns):
selected = [-100, -100]
output = False
# Gets the mouse position
mouse_pos = pygame.mouse.get_pos()
# Divided the position by the size and rounds down to determine the row and column the player is hovering over
c = int(math.floor(mouse_pos[0] / size))
r = int(math.floor(mouse_pos[1] / size))
if (turn + 1) * 10 < board[r][c] < (turn + 2) * 10:
selected = [r, c]
board = temp_fill_spaces(board, selected, rows, columns)
output = True
return selected, output, board
# Places 1 or 2 in the matrix/board
def symbol_place(turn, board, size, selected, rows, columns):
output = True
# Gets the mouse position
mouse_pos = pygame.mouse.get_pos()
# Divided the position by the size and rounds down to determine the row and column the player is hovering over
c = int(math.floor(mouse_pos[0] / size))
r = int(math.floor(mouse_pos[1] / size))
# If that value is equal to 0
if board[r][c] == 0:
# After verifying that this spot is equal to zero we can make all the temp -50's back to original value
for cl in range(columns):
for rl in range(rows):
if board[rl][cl] < 0:
board[rl][cl] += 50
# Depending on the turn it will place 10 or 20 in that r and c as well as change to next players turn and print
# The players turn in the console
if turn == 0:
board[r][c], board[selected[0]][selected[1]] = board[selected[0]][selected[1]], board[r][c]
turn += 1
else:
board[r][c], board[selected[0]][selected[1]] = board[selected[0]][selected[1]], board[r][c]
turn -= 1
output = False
elif (turn + 2) * 10 < board[r][c] < (turn + 3) * 10 or turn * 10 < board[r][c] < (turn + 1) * 10:
# After verifying that this spot is another players piece we can make all the temp -50's back to original value
for cl in range(columns):
for rl in range(rows):
if board[rl][cl] < 0:
board[rl][cl] += 50
if turn == 0:
board[r][c], board[selected[0]][selected[1]] = board[selected[0]][selected[1]], 0
turn += 1
else:
board[r][c], board[selected[0]][selected[1]] = board[selected[0]][selected[1]], 0
turn -= 1
output = False
elif board[r][c] == board[selected[0]][selected[1]]:
for cl in range(columns):
for rl in range(rows):
if board[rl][cl] < 0:
board[rl][cl] += 50
output = False
return turn, board, output
# Mouse highlighting function
def mouse_highlight(screen, size, board, selected, p_selected, turn):
# Gets mouse pos
mouse_pos = pygame.mouse.get_pos()
# Divides location by the SQUARESIZE and rounds it down to find the column and row the mouse is hovering over
c = int(math.floor(mouse_pos[0] / size))
r = int(math.floor(mouse_pos[1] / size))
# Keeps track of the colour of the main square that follows cursor
if selected:
pos = (p_selected[1] * size, p_selected[0] * size)
colour = YELLOW
draw_library.draw_highlight(screen, size, pos, colour)
if board[r][c] == 0 or (turn + 2) * 10 < board[r][c] < (turn + 3) * 10 or turn * 10 < board[r][c] < \
(turn + 1) * 10:
colour = GREEN
else:
colour = RED
else:
if (turn + 1) * 10 < board[r][c] < (turn + 2) * 10:
colour = YELLOW
else:
colour = RED
# Multiplies the size back into c and r to get the position of the square on the screen
pos = (c * size, r * size)
# Draws the box
draw_library.draw_highlight(screen, size, pos, colour)
# Bishops valid move spaces algo
def bishop_moves(board, s_row, s_col, row, column):
pospos = True
posneg = True
negpos = True
negneg = True
for i in range(1, column):
if 0 <= s_row + i < row and 0 <= s_col + i < column and pospos:
board[s_row + i][s_col + i] += 50
if board[s_row + i][s_col + i] != 0:
pospos = False
if 0 <= s_row - i < row and 0 <= s_col - i < column and negneg:
board[s_row - i][s_col - i] += 50
if board[s_row - i][s_col - i] != 0:
negneg = False
if 0 <= s_row + i < row and 0 <= s_col - i < column and posneg:
board[s_row + i][s_col - i] += 50
if board[s_row + i][s_col - i] != 0:
posneg = False
if 0 <= s_row - i < row and 0 <= s_col + i < column and negpos:
board[s_row - i][s_col + i] += 50
if board[s_row - i][s_col + i] != 0:
negpos = False
return board
# Rooks valid move spaces algo
def rook_moves(board, s_row, s_col, row, column):
up = True
down = True
left = True
right = True
for i in range(1, column):
if 0 <= s_row + i < row and up:
board[s_row + i][s_col] += 50
if board[s_row + i][s_col] != 0:
up = False
if 0 <= s_row - i < row and down:
board[s_row - i][s_col] += 50
if board[s_row - i][s_col] != 0:
down = False
if 0 <= s_col - i < column and left:
board[s_row][s_col - i] += 50
if board[s_row][s_col - i] != 0:
left = False
if 0 <= s_col + i < column and right:
board[s_row][s_col + i] += 50
if board[s_row][s_col + i] != 0:
right = False
return board
# Valid spaces (adds 50 to all the negative numbers that the selected piece can move to)
def valid_spaces(board, og_board, selected, row, column):
s_row = selected[0]
s_col = selected[1]
selected_value = board[s_row][s_col] + 50
# Gets rid of the 10 or 20 to determine the piece it is
if selected_value - 10 < 10:
selected_value -= 10
else:
selected_value -= 20
# Checks pieces ***************************************************************************************************
# If its a pawn
if selected_value == 1:
board[s_row][s_col] += 50
if board[s_row][s_col] >= 20 and s_row - 1 >= 0:
if board[s_row - 1][s_col] + 50 == 0:
board[s_row - 1][s_col] += 50
if og_board[s_row][s_col] == board[s_row][s_col] and board[s_row][s_col] >= 20 and \
board[s_row - 2][s_col] + 50 == 0:
board[s_row - 2][s_col] += 50
if s_row - 1 >= 0 and s_col - 1 >= 0 and board[s_row - 1][s_col - 1] + 50 != 0:
board[s_row - 1][s_col - 1] += 50
if s_row - 1 >= 0 and s_col + 1 < column and board[s_row - 1][s_col + 1] + 50 != 0:
board[s_row - 1][s_col + 1] += 50
elif board[s_row][s_col] >= 10 and s_row + 1 < ROWCOUNT:
if board[s_row + 1][s_col] + 50 == 0:
board[s_row + 1][s_col] += 50
if og_board[s_row][s_col] == board[s_row][s_col] and board[s_row][s_col] >= 10 and \
board[s_row + 2][s_col] + 50 == 0:
board[s_row + 2][s_col] += 50
if s_row + 1 < row and s_col - 1 >= 0 and board[s_row + 1][s_col - 1] + 50 != 0:
board[s_row + 1][s_col - 1] += 50
if s_row + 1 < row and s_col + 1 < column and board[s_row + 1][s_col + 1] + 50 != 0:
board[s_row + 1][s_col + 1] += 50
# Elif its a rook
elif selected_value == 2:
board = rook_moves(board, s_row, s_col, row, column)
board[s_row][s_col] += 50
# Elif its a knight
elif selected_value == 3:
knight_moves = [[2,1],[2,-1],[1,2],[1,-2],[-2,1],[-2,-1],[-1,2],[-1,-2]]
for i in range(8):
if 0 <= s_row + knight_moves[i][0] < row and 0 <= s_col + knight_moves[i][1] < column:
board[s_row + knight_moves[i][0]][s_col + knight_moves[i][1]] += 50
board[s_row][s_col] += 50
# Elif its a bishop
elif selected_value == 4:
board = bishop_moves(board, s_row, s_col, row, column)
board[s_row][s_col] += 50
# Elif its a queen
elif selected_value == 5:
board = rook_moves(board, s_row, s_col, row, column)
board = bishop_moves(board, s_row, s_col, row, column)
board[s_row][s_col] += 50
# Elif its a king
elif selected_value == 6:
king_moves = [[1,1],[-1,-1],[1,-1],[-1,1],[1,0],[-1,0],[0,1],[0,-1]]
for i in range(8):
if 0 <= s_row + king_moves[i][0] < row and 0 <= s_col + king_moves[i][1] < column:
board[s_row + king_moves[i][0]][s_col + king_moves[i][1]] += 50
board[s_row][s_col] += 50
return board
# Temporarily fills spaces with negative numbers by subtracting 50 to all the values to then invalidate any moves
# Later adds 50 to all the spaces the specified piece can move
def temp_fill_spaces(board, selected, rows, columns):
for c in range(columns):
for r in range(rows):
board[r][c] -= 50
board = valid_spaces(board, og_board, selected, rows, columns)
return board
board = create_board(ROWCOUNT, COLUMNCOUNT)
board = set_board(board, COLUMNCOUNT)
og_board = board.copy()
game = True
turn = 0
piece_selected = False
selected = [-100, -100]
winner = None
while game:
# User Events ***************************************************
# Loop waiting for user input
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
game = False
if event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[0]:
if piece_selected:
# Calls the symbol place function when player clicks their mouse button
turn, board, piece_selected = symbol_place(turn, board, SQUARESIZE, selected, ROWCOUNT, COLUMNCOUNT)
else:
# Calls the symbol select function when player clicks their mouse button
selected, piece_selected, board = symbol_select(turn, board, SQUARESIZE, ROWCOUNT, COLUMNCOUNT)
# Drawing functions
draw_library.draw_board(screen, SQUARESIZE, ROWCOUNT, COLUMNCOUNT)
draw_library.draw_pieces(screen, SQUARESIZE, ROWCOUNT, COLUMNCOUNT, board)
mouse_highlight(screen, SQUARESIZE, board, piece_selected, selected, turn)
pygame.display.flip()
winner, game = win_con(board, game)
try:
print("Player " + winner + " WON!")
except:
print("Nobody won")