-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChess.py
More file actions
224 lines (156 loc) · 6.31 KB
/
Copy pathChess.py
File metadata and controls
224 lines (156 loc) · 6.31 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
import pygame
from Chess_Pieces.Pieces import *
from Board import Board
def DRAW_BOARD(SCREEN, SIZE, TILE_SIZE):
BLACK = (0, 0, 0)
GREY = (75, 75, 75)
WHITE = (255, 255, 255)
RED = (200, 0, 0)
BLUE = (0, 0, 200)
for i in range(0, SIZE, TILE_SIZE):
for j in range(0, SIZE, TILE_SIZE):
rect = pygame.Rect(i, j, TILE_SIZE, TILE_SIZE)
if i % (TILE_SIZE * 2) == j % (TILE_SIZE * 2):
pygame.draw.rect(SCREEN, WHITE, rect)
else:
pygame.draw.rect(SCREEN, GREY, rect)
pygame.draw.rect(SCREEN, BLACK, rect, 1)
for i in range(board.size):
for j in range(board.size):
rect = pygame.Rect(j * TILE_SIZE, i * TILE_SIZE, TILE_SIZE, TILE_SIZE)
if board.index[i][j].takeable:
pygame.draw.rect(SCREEN, RED, rect)
pygame.draw.rect(SCREEN, BLACK, rect, 1)
if board.index[i][j].empty and not board.index[i][j].enPassant:
pygame.draw.rect(SCREEN, BLUE, rect)
pygame.draw.rect(SCREEN, BLACK, rect, 1)
def DRAW_PIECES(SCREEN, TILE_SIZE):
for row in range(board.size):
for col in range(board.size):
image = board.index[row][col].img
image = pygame.transform.scale(image, (TILE_SIZE, TILE_SIZE))
SCREEN.blit(image, (col * TILE_SIZE, row * TILE_SIZE))
def DRAW_MOVES(row, col):
row = int(row)
col = int(col)
if not board.index[row][col].empty:
board.index[row][col].getMoves(board)
def main():
global board
global blackInCheck, whiteInCheck
global running
global printToTerminal
board = Board()
show = False
blackInCheck = False
whiteInCheck = False
prevRow = -1
prevCol = -1
printToTerminal = False
# if input(f'Do you want the game printed to the terminal? (y/n)') == 'y':
# printToTerminal = True
board.updateIndex()
windowIcon = pygame.image.load('Chess_Images/Thumbnail.png')
pygame.display.set_icon(windowIcon)
pygame.display.set_caption('Turn 1 \\ White\'s turn')
SIZE = 480
TILE_SIZE = int(SIZE / board.size)
SIZE = TILE_SIZE * board.size
SCREEN = pygame.display.set_mode((SIZE, SIZE))
running = True
while running:
DRAW_BOARD(SCREEN, SIZE, TILE_SIZE)
DRAW_PIECES(SCREEN, TILE_SIZE)
pygame.display.flip()
while True:
event = pygame.event.wait()
if event.type != pygame.MOUSEMOTION:
break
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
col, row = pos
row = int(row // TILE_SIZE)
col = int(col // TILE_SIZE)
clear = False
doEnPassant = False
try:
if row == prevRow and col == prevCol and not show:
clear = True
show = True
else:
show = False
except:
pass
if board.index[row][col].takeable:
if board.checkTurn((prevRow, prevCol)):
if board.index[row][col].empty and board.index[row][col].enPassant:
doEnPassant = True
board.index[row][col] = board.index[prevRow][prevCol]
board.index[prevRow][prevCol] = Empty(' ')
board.index[row][col].moved = True
board.turn += 1
clear = True
show = True
try:
board.index[row][col].castleAble = False
except:
pass
try:
board.index[row][col].doublemove = False
except:
pass
for tempRow in range(board.size):
for tempCol in range(board.size):
if board.index[tempRow][tempCol].empty:
board.index[tempRow][tempCol].enPassant = False
else:
if printToTerminal:
print(f'It is not you turn')
DRAW_PIECES(SCREEN, TILE_SIZE)
board.updateIndex()
if doEnPassant:
board.index[row][col].enPassant(board)
if board.index[row][col].piece == 'K' and board.index[row][col].moved and abs(prevCol - col) == 2:
rookPos = int((prevCol - col) / 2)
board.index[row][col].castle(board, rookPos)
if board.index[row][col].piece == 'P' and board.index[row][col].moved and abs(prevRow - row) == 2:
tempRow = row + int((prevRow - row) / 2)
board.index[tempRow][col].enPassant = True
for rows in board.index:
for pos in rows:
if pos.piece == 'P':
if pos.index[0] == 0 or pos.index[0] == board.size - 1:
pos.promote(SCREEN, SIZE)
board.updateIndex()
windowTitle = f'Turn {board.turn}'
if board.turn % 2 == 1:
windowTitle += ' \\ White\'s turn'
else:
windowTitle += ' \\ Black\'s turn'
board.checkForCheck()
if whiteInCheck:
if board.checkForMate():
windowTitle += ' \\ White is in mate'
else:
windowTitle += ' \\ White is in check'
elif blackInCheck:
if board.checkForMate():
windowTitle += ' \\ Black is in mate'
else:
windowTitle += ' \\ Black is in check'
pygame.display.set_caption(windowTitle)
prevRow = row
prevCol = col
board.clearBoard()
DRAW_MOVES(row, col)
if clear:
board.clearBoard()
if printToTerminal:
print(board)
main()
'''
TO-DO:
Update checkForCheck() and checForMate() to work with enPassant
'''