Question: class ChessVar: ChessVar class represents an Atomic Chess game. def _ _ init _ _ ( self ) :
class ChessVar:
ChessVar class represents an Atomic Chess game.
def initself:
Initialize the game with the standard chess starting position, set the first turn to white.
self.board RNBQKBNR
PPPPPPPP
pppppppp
rnbqkbnr
self.turn 'white'
self.gamestate 'UNFINISHED'
def getgamestateself:
Get the current state of the game.
Returns: 'UNFINISHED', 'WHITEWON', or 'BLACKWON'
return self.gamestate
def getturnself:
Get the current turn color.
Returns: 'white' or 'black'
return self.turn
def makemoveself fromsquare, tosquare:
Makes a move on the Chessboard"""
fromrow, fromcol self.convertsquaretoindicesfromsquare
torow, tocol self.convertsquaretoindicestosquare
piece self.boardfromrowfromcol
if self.isvalidmovepiece fromrow, fromcol, torow, tocol:
self.boardtorowtocol piece
self.boardfromrowfromcol
self.switchturn
self.checkgamestate
return True
return False
def convertsquaretoindicesself square:
col ordsquare orda
row intsquare
return row, col
def isvalidmoveself piece, fromrow, fromcol, torow, tocol:
Determines if the move made was valid."""
if piece.lowerp:
return self.isvalidpawnmovepiece fromrow, fromcol, torow, tocol
elif piece.lowerr:
return self.isvalidrookmovefromrow, fromcol, torow, tocol
elif piece.lowern:
return self.isvalidknightmovefromrow, fromcol, torow, tocol
elif piece.lowerk:
return self.isvalidkingmovefromrow, fromcol, torow, tocol
return False
def isvalidpawnmoveself piece, fromrow, fromcol, torow, tocol:
Check if a pawn's move is valid.
Parameters:
piece: The pawn piece P or p
fromrow: The starting row of the pawn.
fromcol: The starting column of the pawn.
torow: The ending row of the pawn.
tocol: The ending column of the pawn.
Returns: True if the move is valid, False otherwise.
if piece P:
direction
startrow
else:
direction
startrow
if fromcol tocol:
if fromrow direction torow and self.boardtorowtocol:
return True
if fromrow startrow and fromrow direction torow and self.boardtorowtocol:
return True
elif absfromcol tocol and fromrow direction torow:
if self.boardtorowtocol:
return True
return False
def isvalidrookmoveself fromrow, fromcol, torow, tocol:
Check if a rook's move is valid.
Parameters:
fromrow: The starting row of the rook.
fromcol: The starting column of the rook.
torow: The ending row of the rook.
tocol: The ending column of the rook.
Returns: True if the move is valid, False otherwise.
if fromrow torow and fromcol tocol:
return False
steprow torow fromrow max abstorow fromrow if fromrow torow else
stepcol tocol fromcol max abstocol fromcol if fromcol tocol else
currentrow, currentcol fromrow steprow, fromcol stepcol
while currentrow torow or currentcol tocol:
if self.boardcurrentrowcurrentcol:
return False
currentrow steprow
currentcol stepcol
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
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
