Spaces:
Running
Running
Update engine.py
Browse files
engine.py
CHANGED
|
@@ -52,6 +52,7 @@ class GameState():
|
|
| 52 |
self.current_castling_rights = Castling_Rights(True,True,True,True)
|
| 53 |
self.castle_rights_log=[Castling_Rights(self.current_castling_rights.wks,self.current_castling_rights.wqs,self.current_castling_rights.bks,self.current_castling_rights.bqs)]
|
| 54 |
self.empassant_possible_log=[self.empassant_moves]
|
|
|
|
| 55 |
# when current castling rights modified it creates new object and pt it in log
|
| 56 |
|
| 57 |
|
|
@@ -150,18 +151,83 @@ class GameState():
|
|
| 150 |
print("this is our starting move ")
|
| 151 |
#if u move then it might be check to u so need to check these possiblities
|
| 152 |
#so we need to generate possible moves in next turn abd based on that we need to move
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
-
def is_valid_board(self,board):
|
| 155 |
-
# must be list of 8 rows
|
| 156 |
if len(board) != 8:
|
| 157 |
return False
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
if any(cell in [None, "", "-"] for cell in row):
|
| 164 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
return True
|
| 166 |
|
| 167 |
'''
|
|
|
|
| 52 |
self.current_castling_rights = Castling_Rights(True,True,True,True)
|
| 53 |
self.castle_rights_log=[Castling_Rights(self.current_castling_rights.wks,self.current_castling_rights.wqs,self.current_castling_rights.bks,self.current_castling_rights.bqs)]
|
| 54 |
self.empassant_possible_log=[self.empassant_moves]
|
| 55 |
+
self.update_state_variables()
|
| 56 |
# when current castling rights modified it creates new object and pt it in log
|
| 57 |
|
| 58 |
|
|
|
|
| 151 |
print("this is our starting move ")
|
| 152 |
#if u move then it might be check to u so need to check these possiblities
|
| 153 |
#so we need to generate possible moves in next turn abd based on that we need to move
|
| 154 |
+
def update_state_variables(self):
|
| 155 |
+
# Ensure move log is empty for a fresh board
|
| 156 |
+
self.moveLog = []
|
| 157 |
+
|
| 158 |
+
# Initialize game status flags
|
| 159 |
+
self.whiteToMove = True
|
| 160 |
+
self.checkmate = False
|
| 161 |
+
self.stalemate = False
|
| 162 |
+
self.in_check = False
|
| 163 |
+
self.pins = []
|
| 164 |
+
self.checks = []
|
| 165 |
+
self.protects = []
|
| 166 |
+
self.threatens = []
|
| 167 |
+
self.pieces_can_move_to = []
|
| 168 |
+
|
| 169 |
+
# Update king locations by scanning the board
|
| 170 |
+
self.white_king_location = None
|
| 171 |
+
self.black_king_location = None
|
| 172 |
+
for row in range(8):
|
| 173 |
+
for col in range(8):
|
| 174 |
+
if self.board[row][col] == 'wK':
|
| 175 |
+
self.white_king_location = (row, col)
|
| 176 |
+
if self.board[row][col] == 'bK':
|
| 177 |
+
self.black_king_location = (row, col)
|
| 178 |
+
|
| 179 |
+
# Set default king locations if not found (for standard board)
|
| 180 |
+
if not self.white_king_location:
|
| 181 |
+
self.white_king_location = (7, 4)
|
| 182 |
+
if not self.black_king_location:
|
| 183 |
+
self.black_king_location = (0, 4)
|
| 184 |
+
|
| 185 |
+
# Reset en passant
|
| 186 |
+
self.empassant_moves = ()
|
| 187 |
+
self.empassant_possible_log = [()]
|
| 188 |
+
|
| 189 |
+
# Set castling rights based on piece positions
|
| 190 |
+
self.current_castling_rights = Castling_Rights(False, False, False, False)
|
| 191 |
+
if self.white_king_location == (7, 4):
|
| 192 |
+
if self.board[7][0] == 'wR':
|
| 193 |
+
self.current_castling_rights.wqs = True
|
| 194 |
+
if self.board[7][7] == 'wR':
|
| 195 |
+
self.current_castling_rights.wks = True
|
| 196 |
+
if self.black_king_location == (0, 4):
|
| 197 |
+
if self.board[0][0] == 'bR':
|
| 198 |
+
self.current_castling_rights.bqs = True
|
| 199 |
+
if self.board[0][7] == 'bR':
|
| 200 |
+
self.current_castling_rights.bks = True
|
| 201 |
+
self.castle_rights_log = [Castling_Rights(
|
| 202 |
+
self.current_castling_rights.wks, self.current_castling_rights.wqs,
|
| 203 |
+
self.current_castling_rights.bks, self.current_castling_rights.bqs
|
| 204 |
+
)]
|
| 205 |
+
|
| 206 |
+
# Calculate pins, checks, and in_check
|
| 207 |
+
self.incheck,self.pins,self.checks = self.check_for_pins_and_checks()
|
| 208 |
|
| 209 |
+
def is_valid_board(self, board):
|
|
|
|
| 210 |
if len(board) != 8:
|
| 211 |
return False
|
| 212 |
+
valid_pieces = {'bR', 'bN', 'bB', 'bQ', 'bK', 'bp', 'wR', 'wN', 'wB', 'wQ', 'wK', 'wp', '--'}
|
| 213 |
+
white_kings = 0
|
| 214 |
+
black_kings = 0
|
| 215 |
+
for row in range(8):
|
| 216 |
+
if len(board[row]) != 8:
|
|
|
|
| 217 |
return False
|
| 218 |
+
for col in range(8):
|
| 219 |
+
cell = board[row][col]
|
| 220 |
+
if cell not in valid_pieces:
|
| 221 |
+
return False
|
| 222 |
+
if cell == 'wK':
|
| 223 |
+
white_kings += 1
|
| 224 |
+
if cell == 'bK':
|
| 225 |
+
black_kings += 1
|
| 226 |
+
# No pawns on promotion ranks
|
| 227 |
+
if row in (0, 7) and cell[1] == 'p':
|
| 228 |
+
return False
|
| 229 |
+
if white_kings != 1 or black_kings != 1:
|
| 230 |
+
return False
|
| 231 |
return True
|
| 232 |
|
| 233 |
'''
|