This commit is contained in:
cutsweettea
2025-10-29 10:36:33 -04:00
parent 45d60d1657
commit bbd55f48a0
10 changed files with 147 additions and 32 deletions

View File

@@ -5,11 +5,7 @@ from abc import ABC, abstractmethod
ChessPieceT = TypeVar('ChessPieceT')
# my list of custom exceptions
class PieceOutOfBoundsError(Exception): pass
class StartEndPositionMismatch(Exception): pass
class ChessPiece:
class ChessPiece(ABC):
def __init__(self, piece_color: Player):
self.player = piece_color
@@ -36,17 +32,36 @@ class ChessPiece:
raise TypeError(f'each element in the board list bust be another list')
for v in arr:
if not isinstance(v, ChessPiece):
raise TypeError(f'each element in each row of the board must be of type ChessPiece')
if not isinstance(v, ChessPiece) and v != None:
raise TypeError(f'each element in each row of the board must be of type ChessPiece or must be None')
# create variables for the board dimensions, original spot and destination spot
board_dim = len(board)
board_orig: ChessPiece = board[move.to_row][move.to_col]
board_dest: ChessPiece = board[move.from_row][move.from_col]
within_bounds = board_dim <= move.to_col <= board_dim and board_dim <= move.to_row <= board_dim
different_position = move.from_col != move.to_col and move.from_row != move.to_row
at_position = board_orig == self
is_piece_class = isinstance(board_dest, ChessPiece)
taking_friendly_piece = board_dest.player != self.player
board_orig: ChessPiece | None = board[move.from_col][move.from_row]
board_dest: ChessPiece = board[move.to_col][move.to_row]
print(f'within_bounds={within_bounds}, different_position={different_position}, at_position={at_position}, is_piece_class={is_piece_class}, taking_friendly_piece={taking_friendly_piece}')
return within_bounds and different_position and at_position and is_piece_class and taking_friendly_piece
# check if move is within bounds by checking if y and x are within the board dimensions
move_col_valid = 0 <= move.to_col <= board_dim
move_row_valid = 0 <= move.to_row <= board_dim
within_bounds = move_col_valid and move_row_valid
# check if player is still at the original position they started at
different_position = move.from_col != move.to_col or move.from_row != move.to_row
# check if the current piece trying to be moved is in the same spot on the board
#print(f'{board_orig} => {self}')
#is_current_piece = board_orig is self
is_current_piece = True
# check if the destination is not empty, if so, do destination specific checks
is_piece_class = isinstance(board_dest, ChessPiece)
# setting all these values being checked within the conditional to true, cuz if they don't have to be ran, they should still be true to return the true result of all the booleans combined
not_taking_friendly_piece = True
if is_piece_class:
# determines whether an enemy piece is being taken
not_taking_friendly_piece = board_dest.player != self.player
print(f'within_bounds={within_bounds}, different_position={different_position}, is_current_piece={is_current_piece}, not_taking_friendly_piece={not_taking_friendly_piece}, ({within_bounds and different_position and is_current_piece and not_taking_friendly_piece})')
print(f'(other) is_piece_class={is_piece_class}, board_dim={board_dim}, move_col_valid={move_col_valid} ({move.from_col} -> {move.to_col}), move_row_valid={move_row_valid} ({move.from_row} -> {move.to_row})')
return within_bounds and different_position and is_current_piece and not_taking_friendly_piece