Question: class ChessVar: ChessVar class represents an Atomic Chess game. def _ _ init _ _ ( self ) :

class ChessVar:
"""
ChessVar class represents an Atomic Chess game.
"""
def __init__(self):
"""
Initialize the game with the standard chess starting position, set the first turn to white.
"""
self.__board =[['R','N','B','Q','K','B','N','R'],
['P','P','P','P','P','P','P','P'],
['','','','','','','',''],
['','','','','','','',''],
['','','','','','','',''],
['','','','','','','',''],
['p','p','p','p','p','p','p','p'],
['r','n','b','q','k','b','n','r']]
self.__turn = 'white'
self.__game_state = 'UNFINISHED'
def get_game_state(self):
"""
Get the current state of the game.
Returns: 'UNFINISHED', 'WHITE_WON', or 'BLACK_WON'
"""
return self.__game_state
def get_turn(self):
"""
Get the current turn color.
Returns: 'white' or 'black'
"""
return self.__turn
def make_move(self, from_square, to_square):
"""Makes a move on the Chessboard"""
from_row, from_col = self.__convert_square_to_indices(from_square)
to_row, to_col = self.__convert_square_to_indices(to_square)
piece = self.__board[from_row][from_col]
if self.__is_valid_move(piece, from_row, from_col, to_row, to_col):
self.__board[to_row][to_col]= piece
self.__board[from_row][from_col]=''
self.__switch_turn()
self._check_game_state()
return True
return False
def __convert_square_to_indices(self, square):
col = ord(square[0])- ord('a')
row =8- int(square[1])
return row, col
def __is_valid_move(self, piece, from_row, from_col, to_row, to_col):
"""Determines if the move made was valid."""
if piece.lower()=='p':
return self._is_valid_pawn_move(piece, from_row, from_col, to_row, to_col)
elif piece.lower()=='r':
return self._is_valid_rook_move(from_row, from_col, to_row, to_col)
elif piece.lower()=='n':
return self._is_valid_knight_move(from_row, from_col, to_row, to_col)
elif piece.lower()=='k':
return self._is_valid_king_move(from_row, from_col, to_row, to_col)
return False
def _is_valid_pawn_move(self, piece, from_row, from_col, to_row, to_col):
"""
Check if a pawn's move is valid.
Parameters:
- piece: The pawn piece ('P' or 'p').
- from_row: The starting row of the pawn.
- from_col: The starting column of the pawn.
- to_row: The ending row of the pawn.
- to_col: The ending column of the pawn.
Returns: True if the move is valid, False otherwise.
"""
if piece =='P':
direction =-1
start_row =6
else:
direction =1
start_row =1
if from_col == to_col:
if from_row + direction == to_row and self.__board[to_row][to_col]=='':
return True
if from_row == start_row and from_row +2* direction == to_row and self.__board[to_row][to_col]=='':
return True
elif abs(from_col - to_col)==1 and from_row + direction == to_row:
if self.__board[to_row][to_col]!='':
return True
return False
def _is_valid_rook_move(self, from_row, from_col, to_row, to_col):
"""Check if a rook's move is valid.
Parameters:
- from_row: The starting row of the rook.
- from_col: The starting column of the rook.
- to_row: The ending row of the rook.
- to_col: The ending column of the rook.
Returns: True if the move is valid, False otherwise.
"""
if from_row != to_row and from_col != to_col:
return False
step_row =(to_row - from_row)// max(1, abs(to_row - from_row)) if from_row != to_row else 0
step_col =(to_col - from_col)// max(1, abs(to_col - from_col)) if from_col != to_col else 0
current_row, current_col = from_row + step_row, from_col + step_col
while current_row != to_row or current_col != to_col:
if self.__board[current_row][current_col]!='':
return False
current_row += step_row
current_col += step_col
return True
This is my code for the tests i am not passing. Chess game is Atomic chess. Needs to follow those rules for each piece. Needs to return true, not false like it currently is
 class ChessVar: """ ChessVar class represents an Atomic Chess game. """

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!