-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
262 lines (232 loc) · 9.05 KB
/
player.py
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
import pygame
from pieces import Queen, Rook, Pawn, King, Bishop, Knight
pygame.init()
class Player:
turn = 0
def __init__(self, board, color):
self.board = board
self.color = color
self.selected = None
self.king = self.board.kings[self.color]
self.opponent = None
self.legal_moves = {}
self.promoting_pawn = None
self.set_legal_moves()
self.get_legal_moves(None)
def set_opponent(self, other):
"""
sets the opponent of the player
:param other: A Player Object
"""
self.opponent = other
def get_status(self, check):
"""
Finds out whether the game should continue or end. If ended, finds the result.
:param check: Check Object
:return: A string containing current status of the game
"""
flag = True
insufficient = False
self.get_legal_moves(check)
if len(self.legal_moves) == 1:
if len(self.opponent.legal_moves) == 1:
insufficient = True
elif len(self.opponent.legal_moves) == 2:
insufficient = True
for piece in self.opponent.legal_moves:
if isinstance(piece, Queen) or isinstance(piece, Rook) or isinstance(piece, Pawn):
insufficient = False
elif len(self.legal_moves) == 2:
insufficient = True
remaining = None
for piece in self.legal_moves:
if isinstance(piece, Queen) or isinstance(piece, Rook) or isinstance(piece, Pawn):
insufficient = False
elif not isinstance(piece, King):
remaining = piece
if insufficient:
if len(self.opponent.legal_moves) == 2:
if not isinstance(remaining, Bishop):
insufficient = False
else:
opponent_remaining = None
for piece in self.opponent.legal_moves:
if isinstance(piece, Queen) or isinstance(piece, Rook) or isinstance(piece, Pawn):
insufficient = False
elif not isinstance(piece, King):
opponent_remaining = piece
if isinstance(opponent_remaining, Bishop):
if remaining.square.color != opponent_remaining.square.color:
insufficient = False
else:
insufficient = False
elif len(self.opponent.legal_moves) > 2:
insufficient = False
for piece in self.legal_moves:
if len(self.legal_moves[piece]) != 0:
flag = False
if check is not None:
if flag:
return 'Checkmate'
elif insufficient:
return 'Draw by insufficient material'
else:
return 'Continue'
elif flag:
return 'Stalemate'
elif insufficient:
return 'Draw by insufficient material'
else:
return 'Continue'
def set_legal_moves(self):
"""
Sets player pieces at object creation
"""
limit = (1, 3) if self.color == 'White' else (7, 9)
for i in range(limit[0], limit[1]):
for j in range(1, 9):
self.legal_moves[self.board.get_square(i, j).piece] = []
def get_legal_moves(self, check):
"""
Calculates the legal move for all pieces currently on the board
:param check: A Check object
"""
if check is not None and check.double_check():
self.legal_moves[self.king] = self.king.possible_moves(check)
return
captured = []
for piece in self.legal_moves:
if piece.square is None:
captured.append(piece)
continue
self.legal_moves[piece] = piece.possible_moves(check)
for piece in captured:
del self.legal_moves[piece]
def clear_legal_moves(self):
"""
Clears all the previous legal moves
"""
for piece in self.legal_moves:
self.legal_moves[piece] = []
if isinstance(piece, Pawn):
piece.en_passant = 0
def highlight_legal_moves(self, piece):
"""
Highlights the legal move of a selected piece
:param piece: A Piece Object
"""
for move in self.legal_moves[piece]:
move.highlighted = not move.highlighted
def select(self, piece):
"""
Selected the piece clicked
:param piece: A Piece Object
"""
self.selected = piece
if self.selected is not None and self.selected.color != self.color:
self.selected = None
if self.selected is not None:
# print(self.selected)
self.highlight_legal_moves(self.selected)
piece.square.selected_highlighted = True
def unselect(self):
"""
Removes the current selected piece
"""
self.selected.square.selected_highlighted = False
self.highlight_legal_moves(self.selected)
self.selected = None
def end_turn(self):
"""
Ends the turn for the current player
:return: returns the current status of the game
"""
Player.turn ^= 1
check = self.opponent.king.in_check(self.opponent.king.square)
if check is not None:
self.opponent.king.square.check_highlighted = True
status = self.opponent.get_status(check)
return status
def promotion(self, sq):
"""
Handles pawn promotion
:param sq: A Square Object
:return: returns status of the game after promotion
"""
if sq.column == self.promoting_pawn.square.column:
if self.color == 'White':
# promoted_piece = None
if sq.row == 8:
promoted_piece = self.promoting_pawn.promote(Queen)
elif sq.row == 7:
promoted_piece = self.promoting_pawn.promote(Rook)
elif sq.row == 6:
promoted_piece = self.promoting_pawn.promote(Bishop)
elif sq.row == 5:
promoted_piece = self.promoting_pawn.promote(Knight)
else:
return 'Continue'
if promoted_piece is not None:
del self.legal_moves[self.promoting_pawn]
self.legal_moves[promoted_piece] = []
self.promoting_pawn = None
self.board.promoting_pawn = None
return self.end_turn()
else:
# promoted_piece = None
if sq.row == 1:
promoted_piece = self.promoting_pawn.promote(Queen)
elif sq.row == 2:
promoted_piece = self.promoting_pawn.promote(Rook)
elif sq.row == 3:
promoted_piece = self.promoting_pawn.promote(Bishop)
elif sq.row == 4:
promoted_piece = self.promoting_pawn.promote(Knight)
else:
return 'Continue'
if promoted_piece is not None:
del self.legal_moves[self.promoting_pawn]
self.legal_moves[promoted_piece] = []
self.promoting_pawn = None
self.board.promoting_pawn = None
return self.end_turn()
else:
return 'Continue'
def play(self, x, y):
"""
Handling the players play
:param x: x-coordinate of click
:param y: y-coordinate of click
:return: the current status of the game
"""
sq = self.board.get_clicked_square(x, y)
if sq is None:
return 'Continue'
if self.promoting_pawn is not None:
return self.promotion(sq)
elif self.selected is not None:
if sq in self.legal_moves[self.selected]:
if self.king.square.check_highlighted:
self.king.square.check_highlighted = False
self.selected.square.selected_highlighted = False
self.promoting_pawn = self.selected.move(sq)
self.unselect()
self.clear_legal_moves()
if self.promoting_pawn is None:
return self.end_turn()
else:
self.board.promoting_pawn = self.promoting_pawn
return 'Continue'
elif sq.piece is not None:
if sq.piece == self.selected:
self.unselect()
elif sq.piece.color == self.color:
self.unselect()
self.select(sq.piece)
else:
self.unselect()
else:
self.unselect()
else:
self.select(sq.piece)
return 'Continue'