1
+ import pygame
2
+
3
+ from data .classes .Square import Square
4
+ from data .classes .pieces .Rook import Rook
5
+ from data .classes .pieces .Bishop import Bishop
6
+ from data .classes .pieces .Knight import Knight
7
+ from data .classes .pieces .Queen import Queen
8
+ from data .classes .pieces .King import King
9
+ from data .classes .pieces .Pawn import Pawn
10
+
11
+
12
+ # Game state checker
13
+ class Board :
14
+ def __init__ (self , width , height ):
15
+ self .width = width
16
+ self .height = height
17
+ self .tile_width = width // 8
18
+ self .tile_height = height // 8
19
+ self .selected_piece = None
20
+ self .turn = 'white'
21
+
22
+ # try making it chess.board.fen()
23
+ self .config = [
24
+ ['bR' , 'bN' , 'bB' , 'bQ' , 'bK' , 'bB' , 'bN' , 'bR' ],
25
+ ['bP' , 'bP' , 'bP' , 'bP' , 'bP' , 'bP' , 'bP' , 'bP' ],
26
+ ['' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ],
27
+ ['' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ],
28
+ ['' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ],
29
+ ['' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ],
30
+ ['wP' , 'wP' , 'wP' , 'wP' , 'wP' , 'wP' , 'wP' , 'wP' ],
31
+ ['wR' , 'wN' , 'wB' , 'wQ' , 'wK' , 'wB' , 'wN' , 'wR' ],
32
+ ]
33
+
34
+ self .squares = self .generate_squares ()
35
+
36
+ self .setup_board ()
37
+
38
+
39
+ def generate_squares (self ):
40
+ output = []
41
+ for y in range (8 ):
42
+ for x in range (8 ):
43
+ output .append (
44
+ Square (x , y , self .tile_width , self .tile_height )
45
+ )
46
+ return output
47
+
48
+
49
+ def get_square_from_pos (self , pos ):
50
+ for square in self .squares :
51
+ if (square .x , square .y ) == (pos [0 ], pos [1 ]):
52
+ return square
53
+
54
+
55
+ def get_piece_from_pos (self , pos ):
56
+ return self .get_square_from_pos (pos ).occupying_piece
57
+
58
+
59
+ def setup_board (self ):
60
+ # iterating 2d list
61
+ for y , row in enumerate (self .config ):
62
+ for x , piece in enumerate (row ):
63
+ if piece != '' :
64
+ square = self .get_square_from_pos ((x , y ))
65
+
66
+ # looking inside contents, what piece does it have
67
+ if piece [1 ] == 'R' :
68
+ square .occupying_piece = Rook (
69
+ (x , y ), 'white' if piece [0 ] == 'w' else 'black' , self
70
+ )
71
+ # as you notice above, we put `self` as argument, or means our class Board
72
+
73
+ elif piece [1 ] == 'N' :
74
+ square .occupying_piece = Knight (
75
+ (x , y ), 'white' if piece [0 ] == 'w' else 'black' , self
76
+ )
77
+
78
+ elif piece [1 ] == 'B' :
79
+ square .occupying_piece = Bishop (
80
+ (x , y ), 'white' if piece [0 ] == 'w' else 'black' , self
81
+ )
82
+
83
+ elif piece [1 ] == 'Q' :
84
+ square .occupying_piece = Queen (
85
+ (x , y ), 'white' if piece [0 ] == 'w' else 'black' , self
86
+ )
87
+
88
+ elif piece [1 ] == 'K' :
89
+ square .occupying_piece = King (
90
+ (x , y ), 'white' if piece [0 ] == 'w' else 'black' , self
91
+ )
92
+
93
+ elif piece [1 ] == 'P' :
94
+ square .occupying_piece = Pawn (
95
+ (x , y ), 'white' if piece [0 ] == 'w' else 'black' , self
96
+ )
97
+
98
+
99
+ def handle_click (self , mx , my ):
100
+ x = mx // self .tile_width
101
+ y = my // self .tile_height
102
+ clicked_square = self .get_square_from_pos ((x , y ))
103
+
104
+ if self .selected_piece is None :
105
+ if clicked_square .occupying_piece is not None :
106
+ if clicked_square .occupying_piece .color == self .turn :
107
+ self .selected_piece = clicked_square .occupying_piece
108
+
109
+ elif self .selected_piece .move (self , clicked_square ):
110
+ self .turn = 'white' if self .turn == 'black' else 'black'
111
+
112
+ elif clicked_square .occupying_piece is not None :
113
+ if clicked_square .occupying_piece .color == self .turn :
114
+ self .selected_piece = clicked_square .occupying_piece
115
+
116
+
117
+ def is_in_check (self , color , board_change = None ): # board_change = [(x1, y1), (x2, y2)]
118
+ output = False
119
+ king_pos = None
120
+
121
+ changing_piece = None
122
+ old_square = None
123
+ new_square = None
124
+ new_square_old_piece = None
125
+
126
+ if board_change is not None :
127
+ for square in self .squares :
128
+ if square .pos == board_change [0 ]:
129
+ changing_piece = square .occupying_piece
130
+ old_square = square
131
+ old_square .occupying_piece = None
132
+ for square in self .squares :
133
+ if square .pos == board_change [1 ]:
134
+ new_square = square
135
+ new_square_old_piece = new_square .occupying_piece
136
+ new_square .occupying_piece = changing_piece
137
+
138
+ pieces = [
139
+ i .occupying_piece for i in self .squares if i .occupying_piece is not None
140
+ ]
141
+
142
+ if changing_piece is not None :
143
+ if changing_piece .notation == 'K' :
144
+ king_pos = new_square .pos
145
+ if king_pos == None :
146
+ for piece in pieces :
147
+ if piece .notation == 'K' and piece .color == color :
148
+ king_pos = piece .pos
149
+ for piece in pieces :
150
+ if piece .color != color :
151
+ for square in piece .attacking_squares (self ):
152
+ if square .pos == king_pos :
153
+ output = True
154
+
155
+ if board_change is not None :
156
+ old_square .occupying_piece = changing_piece
157
+ new_square .occupying_piece = new_square_old_piece
158
+
159
+ return output
160
+
161
+
162
+ def is_in_checkmate (self , color ):
163
+ output = False
164
+
165
+ for piece in [i .occupying_piece for i in self .squares ]:
166
+ if piece != None :
167
+ if piece .notation == 'K' and piece .color == color :
168
+ king = piece
169
+
170
+ if king .get_valid_moves (self ) == []:
171
+ if self .is_in_check (color ):
172
+ output = True
173
+
174
+ return output
175
+
176
+
177
+ def draw (self , display ):
178
+ if self .selected_piece is not None :
179
+ self .get_square_from_pos (self .selected_piece .pos ).highlight = True
180
+ for square in self .selected_piece .get_valid_moves (self ):
181
+ square .highlight = True
182
+
183
+ for square in self .squares :
184
+ square .draw (display )
0 commit comments