Skip to content

Commit

Permalink
FIX/ENH: Cell.is_threatened()
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentAuriau committed Oct 27, 2023
1 parent c681c67 commit bf95966
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
43 changes: 43 additions & 0 deletions python/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,26 @@ def is_threatened(
if piece_to_check.is_white() != threaten_color:
return True

elif piece_to_check is None:
keep_going = True
x_to_check += i
y_to_check += j
while 0 <= x_to_check < 8 and 0 <= y_to_check < 8 and keep_going:
cell_to_check = board.get_cell(x_to_check, y_to_check)
piece_to_check = cell_to_check.get_piece()
if isinstance(piece_to_check, material.Rook) or isinstance(
piece_to_check, material.Queen
):
keep_going = False
if piece_to_check.is_white() != threaten_color:
return True
elif piece_to_check is not None:
keep_going = False
else:
x_to_check += i
y_to_check += j

"""
# Rook + Queen
# Going further
keep_going = True
Expand Down Expand Up @@ -237,6 +257,7 @@ def is_threatened(
elif piece_to_check is not None:
keep_going = False
y_to_check -= 1
"""

# King + Queen + Bishop + Pawn
# Checking direct surroundings
Expand Down Expand Up @@ -270,6 +291,27 @@ def is_threatened(
if piece_to_check.is_white() != threaten_color:
return True

elif piece_to_check is None:
print("def")
keep_going = True
x_to_check += i
y_to_check += j
while 0 <= x_to_check < 8 and 0 <= y_to_check < 8 and keep_going:
cell_to_check = board.get_cell(x_to_check, y_to_check)
piece_to_check = cell_to_check.get_piece()

if isinstance(piece_to_check, material.Bishop) or isinstance(
piece_to_check, material.Queen
):
keep_going = False
if piece_to_check.is_white() != threaten_color:
return True
elif piece_to_check is not None:
keep_going = False
x_to_check += i
y_to_check += j

"""
# Queen + Bishop
keep_going = True
x_to_check = self.x + 2
Expand Down Expand Up @@ -342,6 +384,7 @@ def is_threatened(
keep_going = False
x_to_check -= 1
y_to_check -= 1
"""

return False

Expand Down
6 changes: 0 additions & 6 deletions python/engine/move.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ def _is_castling(self):
###print("not castling becasuse king not moved")
return False
elif self.moved_piece.castling_done or self.moved_piece.has_moved:
###print("castling already done or king has already moved")
###print(self.moved_piece.castling_done)
###print(self.moved_piece.has_moved)
return False
Expand All @@ -144,10 +143,8 @@ def _is_castling(self):
if self.end.y == 6: # Castling in the right
rook_to_move = self.board.get_cell(self.start.x, 7).get_piece()
if not isinstance(rook_to_move, material.Rook):
###print("no rook to move")
return False
elif rook_to_move.has_moved:
###print("rook has already moved")
return False
else:
rook_starting_coordinates = (self.start.x, 7)
Expand All @@ -165,10 +162,8 @@ def _is_castling(self):
elif self.end.y == 2: # Castling on the left
rook_to_move = self.board.get_cell(self.start.x, 0).get_piece()
if not isinstance(rook_to_move, material.Rook):
###print('no rook to move')
return False
elif rook_to_move.has_moved:
###print('rook has already moved')
return False
else:
rook_starting_coordinates = (self.start.x, 0)
Expand Down Expand Up @@ -197,7 +192,6 @@ def _is_castling(self):
for cll in must_not_be_threatened_cells:
if cll.is_threatened(self.board, self.moved_piece.is_white()):
not_threatened_cells = False
###print("CELL THREATENED: ", cll.get_x(), cll.get_y())

# Verifies that both conditions are met
conditions_to_castling = [empty_cells_check, not_threatened_cells]
Expand Down

0 comments on commit bf95966

Please sign in to comment.