Question
Can anyone please help with the following part of my Knight and King Chess Puzzle: .......................................................... [Rule1] A knight may move two squares vertically and
Can anyone please help with the following part of my Knight and King Chess Puzzle:
..........................................................
[Rule1] A knight may move two squares vertically and one square horizontally or two squares horizontally and one square vertically. It can jump over other pieces.
[Rule2] The king moves one square in any direction, including diagonal, on the board.
As usual, as a result of any move, the piece that is moved either occupies a previously empty board location, or captures the other sides piece. In that case, the former piece occupies the latters position, while the latter piece is removed from the board. Clearly, we have the following:
[Rule3] A piece of side X (Black or White) cannot move to a location occupied by a piece of side X.
---------------------------------------------------------
code below:
def location2index(loc: str) -> tuple[int, int]: '''converts chess location to corresponding x and y coordinates''' def index2location(x: int, y: int) -> str: '''converts pair of coordinates to corresponding location'''
class Piece: pos_x : int pos_y : int side : bool #True for White and False for Black def __init__(self, pos_X : int, pos_Y : int, side_ : bool): '''sets initial values'''
Board = tuple[int, list[Piece]]
def is_piece_at(pos_X : int, pos_Y : int, B: Board) -> bool: '''checks if there is piece at coordinates pox_X, pos_Y of board B''' def piece_at(pos_X : int, pos_Y : int, B: Board) -> Piece: ''' returns the piece at coordinates pox_X, pos_Y of board B assumes some piece at coordinates pox_X, pos_Y of board B is present '''
class Knight(Piece): def __init__(self, pos_X : int, pos_Y : int, side_ : bool): '''sets initial values by calling the constructor of Piece''' def can_reach(self, pos_X : int, pos_Y : int, B: Board) -> bool: ''' checks if this rook can move to coordinates pos_X, pos_Y on board B according to rule [Rule1] and [Rule3] (see section Intro) Hint: use is_piece_at ''' def can_move_to(self, pos_X : int, pos_Y : int, B: Board) -> bool: ''' checks if this rook can move to coordinates pos_X, pos_Y on board B according to all chess rules Hints: - firstly, check [Rule1] and [Rule3] using can_reach - secondly, check if result of move is capture using is_piece_at - if yes, find the piece captured using piece_at - thirdly, construct new board resulting from move - finally, to check [Rule4], use is_check on new board ''' def move_to(self, pos_X : int, pos_Y : int, B: Board) -> Board: ''' returns new board resulting from move of this rook to coordinates pos_X, pos_Y on board B assumes this move is valid according to chess rules '''
class King(Piece): def __init__(self, pos_X : int, pos_Y : int, side_ : bool): '''sets initial values by calling the constructor of Piece''' def can_reach(self, pos_X : int, pos_Y : int, B: Board) -> bool: '''checks if this king can move to coordinates pos_X, pos_Y on board B according to rule [Rule2] and [Rule3]''' def can_move_to(self, pos_X : int, pos_Y : int, B: Board) -> bool: '''checks if this king can move to coordinates pos_X, pos_Y on board B according to all chess rules''' def move_to(self, pos_X : int, pos_Y : int, B: Board) -> Board: ''' returns new board resulting from move of this king to coordinates pos_X, pos_Y on board B assumes this move is valid according to chess rules '''
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started