forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece.py
233 lines (170 loc) · 9.55 KB
/
piece.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
class Piece:
def __init__(self ,color) -> None:
self.color = color
self.has_moved = False
#print("Piece class constructor called")
""" each class of piece will have its own symbol and this method will be overriden in every subclass"""
def symbol(self) -> str:
return '?'
""" checks move is acceptable or not"""
def is_valid_move(self ,start_position:tuple[int ,int],end_position:tuple[int ,int],board:list[list['Piece']]) -> bool:
raise NotImplementedError("method should be overriden in base classes")
def get_color(self):
return self.color
class Pawn(Piece):
def __init__(self ,color) -> None:
super().__init__(color)
def symbol(self) -> str:
return 'P' if self.color == 'white' else 'p'
def is_valid_move(self ,start_position:tuple[int ,int] ,end_position:tuple[int ,int] ,board:list[list['Piece']]) -> bool:
""" pawn moves one step forward and two step forward is only available when it is the first move of that pawn
if pawn is in (i,j) it can move to (i-1 ,j) for black (assumption black is at bottom) and (i+1 ,j) for white
edge case if it is at the first row or at the last row :- it cannot move further
"""
row_initial ,col_initial = start_position[0] ,start_position[1]
row_final ,col_final = end_position[0] ,end_position[1]
""" edge case - avoid pawn to going off the board """
if not (0 <= row_final < 8 and 0 <= col_final < 8):
return False
""" Handling logic for white pawn"""
if self.color == 'white':
#One square forward
if col_initial == col_final and row_final == row_initial + 1 and board[row_final][col_final] is None:
return True
# Two squares forward on the first move
elif col_initial == col_final and row_final == row_initial + 2 and not self.has_moved and board[row_initial + 1][col_initial] is None and board[row_final][col_final] is None:
return True
# Diagonal capture
elif abs(col_initial - col_final) == 1 and row_final == row_initial + 1 and board[row_final][col_final] is not None and board[row_final][col_final].color != self.color:
return True
else:
return False
else:
#One square forward
if col_initial == col_final and row_final == row_initial - 1 and board[row_final][col_final] is None:
return True
# Two squares forward on the first move
elif col_initial == col_final and row_final == row_initial - 2 and not self.has_moved and board[row_initial - 1][col_initial] is None and board[row_final][col_final] is None:
return True
#Diagonal capture
elif abs(col_initial - col_final) == 1 and row_final == row_initial - 1 and board[row_final][col_final] is not None and board[row_final][col_final].color != self.color:
return True
else:
return False
class Rook(Piece):
def __init__(self ,color):
super().__init__(color)
def symbol(self) -> str:
return 'R' if self.color == 'white' else 'r'
def is_valid_move(self ,start_position:tuple[int ,int] ,end_position:tuple[int ,int] ,board:list[list['Piece']]) -> bool:
""" rooks can move horizontally and vertically
means (i,j) - (i-k,j) or (i+k,j) for k = 1 to 7
or (i,j) - (i,j-k) or (i,j+k) for k = 1 to 7
"""
row_initial ,col_initial = start_position[0] ,start_position[1]
row_final ,col_final = end_position[0] ,end_position[1]
# Ensure the move stays on the board
if not (0 <= row_final < 8 and 0 <= col_final < 8):
return False
# Rooks only move either horizontally or vertically, not diagonally
if row_initial != row_final and col_initial != col_final:
return False
# Check for horizontal movement
if row_initial == row_final:
step = 1 if col_final > col_initial else -1
for col in range(col_initial + step, col_final, step):
if board[row_initial][col] is not None: # Check for obstacles in the path
return False
return True # The move is valid if it reaches here
# Check for vertical movement
elif col_initial == col_final:
step = 1 if row_final > row_initial else -1
for row in range(row_initial + step, row_final, step):
if board[row][col_initial] is not None: # Check for obstacles in the path
return False
return True # The move is valid if it reaches here
return False # If none of the conditions matched, it's not a valid move
class Knight(Piece):
def __init__(self ,color):
super().__init__(color)
def symbol(self) -> str:
return 'KN' if self.color == 'white' else 'kn'
def is_valid_move(self ,start_position:tuple[int ,int] ,end_position:tuple[int ,int] ,board:list[list['Piece']]) -> bool:
""" """
row_initial ,col_initial = start_position[0] ,start_position[1]
row_final ,col_final = end_position[0] ,end_position[1]
#print(f"Knight at {start_position} trying to move to {end_position}")
row_diff = abs(row_final - row_initial)
col_diff = abs(col_final - col_initial)
print(row_diff,col_diff)
# Check for L-shaped movement: (2,1) or (1,2)
if (row_diff == 2 and col_diff == 1) or (row_diff == 1 and col_diff == 2):
# Check if the destination is empty or occupied by an opponent's piece
destination_piece = board[row_final][col_final]
if destination_piece is None or destination_piece.color != self.color:
return True
return False
class Bishop(Piece):
def __init__(self ,color):
super().__init__(color)
def symbol(self) -> str:
return 'B' if self.color == 'white' else 'b'
def is_valid_move(self ,start_position:tuple[int ,int] ,end_position:tuple[int ,int] ,board:list[list['Piece']]) -> bool:
row_initial ,col_initial = start_position[0] ,start_position[1]
row_final ,col_final = end_position[0] ,end_position[1]
if not (0 <= row_final < 8) or (0 <= col_final < 8):
return False
# Check if the move is diagonal
if abs(row_final - row_initial) != abs(col_final - col_initial):
return False
# Check if the path is clear
row_step = 1 if row_final > row_initial else -1
col_step = 1 if col_final > col_initial else -1
current_row = row_initial + row_step
current_col = col_initial + col_step
while current_row != row_final and current_col != col_final:
if board[current_row][current_col] is not None:
return False # Blocked by another piece
current_row += row_step
current_col += col_step
return True # Valid move
class Queen(Piece):
def __init__(self, color):
super().__init__(color)
def symbol(self) -> str:
return 'Q' if self.color == 'white' else 'q'
def is_valid_move(self, start_position: tuple[int, int], end_position: tuple[int, int], board: list[list['Piece']]) -> bool:
row_initial ,col_initial = start_position[0] ,start_position[1]
row_final ,col_final = end_position[0] ,end_position[1]
# Check if the end position is within the board boundaries
if not (0 <= row_final < 8) or not (0 <= col_final < 8):
return False
# Check for movement in straight lines (row or column) or diagonally
if row_initial == row_final or col_initial == col_final or abs(row_final - row_initial) == abs(col_final - col_initial):
# Check for obstacles in the path
step_row = (row_final - row_initial) // max(1, abs(row_final - row_initial)) if row_initial != row_final else 0
step_col = (col_final - col_initial) // max(1, abs(col_final - col_initial)) if col_initial != col_final else 0
current_row = row_initial + step_row
current_col = col_initial + step_col
while (current_row, current_col) != (row_final, col_final):
if board[current_row][current_col] is not None:
return False # There's an obstacle in the path
current_row += step_row
current_col += step_col
return True
return False
class King(Piece):
def __init__(self, color):
super().__init__(color)
def symbol(self) -> str:
return 'K' if self.color == 'white' else 'k'
def is_valid_move(self, start_position: tuple[int, int], end_position: tuple[int, int], board: list[list['Piece']]) -> bool:
row_initial ,col_initial = start_position[0] ,start_position[1]
row_final ,col_final = end_position[0] ,end_position[1]
# Check if the end position is within the board boundaries
if not (0 <= row_final < 8) or not (0 <= col_final < 8):
return False
# Check if the move is one square away in any direction
if max(abs(row_final - row_initial), abs(col_final - col_initial)) > 1:
return False
return True