-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBoard.py
More file actions
196 lines (156 loc) · 4.82 KB
/
Copy pathBoard.py
File metadata and controls
196 lines (156 loc) · 4.82 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
import pygame
from data.classes.Square import Square
from data.classes.pieces.Rook import Rook
from data.classes.pieces.Bishop import Bishop
from data.classes.pieces.Knight import Knight
from data.classes.pieces.Queen import Queen
from data.classes.pieces.King import King
from data.classes.pieces.Pawn import Pawn
class Board:
def __init__(self, width, height):
self.width = width
self.height = height
self.square_width = width // 8
self.square_height = height // 8
self.selected_piece = None
self.turn = 'white'
self.config = [
['bR', 'bN', 'bB', 'bQ', 'bK', 'bB', 'bN', 'bR'],
['b ', 'b ', 'b ', 'b ', 'b ', 'b ', 'b ', 'b '],
['','','','','','','',''],
['','','','','','','',''],
['','','','','','','',''],
['','','','','','','',''],
['w ', 'w ', 'w ', 'w ', 'w ', 'w ', 'w ', 'w '],
['wR', 'wN', 'wB', 'wQ', 'wK', 'wB', 'wN', 'wR'],
]
self.squares = self.generate_squares()
self.setup_board()
def generate_squares(self):
output = []
for y in range(8):
for x in range(8):
output.append(
Square(
x,
y,
self.square_width,
self.square_height
)
)
return output
def setup_board(self):
for y, row in enumerate(self.config):
for x, piece in enumerate(row):
if piece != '':
square = self.get_square_from_pos((x, y))
if piece[1] == 'R':
square.occupying_piece = Rook(
(x, y),
'white' if piece[0] == 'w' else 'black',
self
)
elif piece[1] == 'N':
square.occupying_piece = Knight(
(x, y),
'white' if piece[0] == 'w' else 'black',
self
)
elif piece[1] == 'B':
square.occupying_piece = Bishop(
(x, y),
'white' if piece[0] == 'w' else 'black',
self
)
elif piece[1] == 'Q':
square.occupying_piece = Queen(
(x, y),
'white' if piece[0] == 'w' else 'black',
self
)
elif piece[1] == 'K':
square.occupying_piece = King(
(x, y),
'white' if piece[0] == 'w' else 'black',
self
)
elif piece[1] == ' ':
square.occupying_piece = Pawn(
(x, y),
'white' if piece[0] == 'w' else 'black',
self
)
def handle_click(self, mx, my):
x = mx // self.square_width
y = my // self.square_height
clicked_square = self.get_square_from_pos((x, y))
if self.selected_piece is None:
if clicked_square.occupying_piece is not None:
if clicked_square.occupying_piece.color == self.turn:
self.selected_piece = clicked_square.occupying_piece
elif self.selected_piece.move(self, clicked_square):
self.turn = 'white' if self.turn == 'black' else 'black'
elif clicked_square.occupying_piece is not None:
if clicked_square.occupying_piece.color == self.turn:
self.selected_piece = clicked_square.occupying_piece
def is_in_check(self, color, board_change=None): # board_change = [(x1, y1), (x2, y2)]
output = False
king_pos = None
changing_piece = None
old_square = None
new_square = None
new_square_old_piece = None
if board_change is not None:
for square in self.squares:
if square.pos == board_change[0]:
changing_piece = square.occupying_piece
old_square = square
old_square.occupying_piece = None
for square in self.squares:
if square.pos == board_change[1]:
new_square = square
new_square_old_piece = new_square.occupying_piece
new_square.occupying_piece = changing_piece
pieces = [
i.occupying_piece for i in self.squares if i.occupying_piece is not None
]
if changing_piece is not None:
if changing_piece.notation == 'K':
king_pos = new_square.pos
if king_pos == None:
for piece in pieces:
if piece.notation == 'K':
if piece.color == color:
king_pos = piece.pos
for piece in pieces:
if piece.color != color:
for square in piece.attacking_squares(self):
if square.pos == king_pos:
output = True
if board_change is not None:
old_square.occupying_piece = changing_piece
new_square.occupying_piece = new_square_old_piece
return output
def is_in_checkmate(self, color):
output = False
for piece in [i.occupying_piece for i in self.squares]:
if piece != None:
if piece.notation == 'K' and piece.color == color:
king = piece
if king.get_valid_moves(self) == []:
if self.is_in_check(color):
output = True
return output
def get_square_from_pos(self, pos):
for square in self.squares:
if (square.x, square.y) == (pos[0], pos[1]):
return square
def get_piece_from_pos(self, pos):
return self.get_square_from_pos(pos).occupying_piece
def draw(self, display):
if self.selected_piece is not None:
self.get_square_from_pos(self.selected_piece.pos).highlight = True
for square in self.selected_piece.get_valid_moves(self):
square.highlight = True
for square in self.squares:
square.draw(display)