<s> from Pawn import Pawn <EOL> from Rook import Rook <EOL> from King import King <EOL> from Bishop import Bishop <EOL> from Knight import Knight <EOL> from Queen import Queen <EOL> from Piece import Piece <EOL> from Coordinate import Coordinate as C <EOL> from termcolor import colored <EOL> import copy <EOL> from Move import Move <EOL> WHITE = True <EOL> BLACK = False <EOL> class Board : <EOL> def __init__ ( self , mateInOne = False , castleBoard = False , <EOL> pessant = False , promotion = False ) : <EOL> self . pieces = [ ] <EOL> self . history = [ ] <EOL> self . points = <NUM_LIT:0> <EOL> self . currentSide = WHITE <EOL> self . movesMade = <NUM_LIT:0> <EOL> self . checkmate = False <EOL> if not mateInOne and not castleBoard and not pessant and not promotion : <EOL> self . pieces . extend ( [ Rook ( self , BLACK , C ( <NUM_LIT:0> , <NUM_LIT:7> ) ) , <EOL> Knight ( self , BLACK , C ( <NUM_LIT:1> , <NUM_LIT:7> ) ) , <EOL> Bishop ( self , BLACK , C ( <NUM_LIT:2> , <NUM_LIT:7> ) ) , <EOL> Queen ( self , BLACK , C ( <NUM_LIT:3> , <NUM_LIT:7> ) ) , <EOL> King ( self , BLACK , C ( <NUM_LIT:4> , <NUM_LIT:7> ) ) , <EOL> Bishop ( self , BLACK , C ( <NUM_LIT:5> , <NUM_LIT:7> ) ) , <EOL> Knight ( self , BLACK , C ( <NUM_LIT:6> , <NUM_LIT:7> ) ) , <EOL> Rook ( self , BLACK , C ( <NUM_LIT:7> , <NUM_LIT:7> ) ) ] ) <EOL> for x in range ( <NUM_LIT:8> ) : <EOL> self . pieces . append ( Pawn ( self , BLACK , C ( x , <NUM_LIT:6> ) ) ) <EOL> for x in range ( <NUM_LIT:8> ) : <EOL> self . pieces . append ( Pawn ( self , WHITE , C ( x , <NUM_LIT:1> ) ) ) <EOL> self . pieces . extend ( [ Rook ( self , WHITE , C ( <NUM_LIT:0> , <NUM_LIT:0> ) ) , <EOL> Knight ( self , WHITE , C ( <NUM_LIT:1> , <NUM_LIT:0> ) ) , <EOL> Bishop ( self , WHITE , C ( <NUM_LIT:2> , <NUM_LIT:0> ) ) , <EOL> Queen ( self , WHITE , C ( <NUM_LIT:3> , <NUM_LIT:0> ) ) , <EOL> King ( self , WHITE , C ( <NUM_LIT:4> , <NUM_LIT:0> ) ) , <EOL> Bishop ( self , WHITE , C ( <NUM_LIT:5> , <NUM_LIT:0> ) ) , <EOL> Knight ( self , WHITE , C ( <NUM_LIT:6> , <NUM_LIT:0> ) ) , <EOL> Rook ( self , WHITE , C ( <NUM_LIT:7> , <NUM_LIT:0> ) ) ] ) <EOL> elif promotion : <EOL> pawnToPromote = Pawn ( self , WHITE , C ( <NUM_LIT:1> , <NUM_LIT:6> ) ) <EOL> pawnToPromote . movesMade = <NUM_LIT:1> <EOL> kingWhite = King ( self , WHITE , C ( <NUM_LIT:4> , <NUM_LIT:0> ) ) <EOL> kingBlack = King ( self , BLACK , C ( <NUM_LIT:3> , <NUM_LIT:2> ) ) <EOL> self . pieces . extend ( [ pawnToPromote , kingWhite , kingBlack ] ) <EOL> elif pessant : <EOL> pawn = Pawn ( self , WHITE , C ( <NUM_LIT:1> , <NUM_LIT:4> ) ) <EOL> pawn2 = Pawn ( self , BLACK , C ( <NUM_LIT:2> , <NUM_LIT:6> ) ) <EOL> kingWhite = King ( self , WHITE , C ( <NUM_LIT:4> , <NUM_LIT:0> ) ) <EOL> kingBlack = King ( self , BLACK , C ( <NUM_LIT:3> , <NUM_LIT:2> ) ) <EOL> self . pieces . extend ( [ pawn , pawn2 , kingWhite , kingBlack ] ) <EOL> self . history = [ ] <EOL> self . currentSide = BLACK <EOL> self . points = <NUM_LIT:0> <EOL> self . movesMade = <NUM_LIT:0> <EOL> self . checkmate = False <EOL> firstMove = Move ( pawn2 , C ( <NUM_LIT:2> , <NUM_LIT:4> ) ) <EOL> self . makeMove ( firstMove ) <EOL> self . currentSide = WHITE <EOL> return <EOL> def __str__ ( self ) : <EOL> return self . wrapStringRep ( self . makeStringRep ( self . pieces ) ) <EOL> def undoLastMove ( self ) : <EOL> lastMove , pieceTaken = self . history . pop ( ) <EOL> if lastMove . queensideCastle or lastMove . kingsideCastle : <EOL> king = lastMove . piece <EOL> rook = lastMove . specialMovePiece <EOL> self . movePieceToPosition ( king , lastMove . oldPos ) <EOL> self . movePieceToPosition ( rook , lastMove . rookMove . oldPos ) <EOL> king . movesMade -= <NUM_LIT:1> <EOL> rook . movesMade -= <NUM_LIT:1> <EOL> elif lastMove . pessant : <EOL> pawnMoved = lastMove . piece <EOL> pawnTaken = pieceTaken <EOL> self . pieces . append ( pawnTaken ) <EOL> self . movePieceToPosition ( pawnMoved , lastMove . oldPos ) <EOL> pawnMoved . movesMade -= <NUM_LIT:1> <EOL> if pawnTaken . side == WHITE : <EOL> self . points += <NUM_LIT:1> <EOL> if pawnTaken . side == BLACK : <EOL> self . points -= <NUM_LIT:1> <EOL> elif lastMove . promotion : <EOL> pawnPromoted = lastMove . piece <EOL> promotedPiece = self . pieceAtPosition ( lastMove . newPos ) <EOL> self . pieces . remove ( promotedPiece ) <EOL> self . pieces . append ( pawnPromoted ) <EOL> if pawnPromoted . side == WHITE : <EOL> self . points -= promotedPiece . value - <NUM_LIT:1> <EOL> elif pawnPromoted . side == BLACK : <EOL> self . points += promotedPiece . value - <NUM_LIT:1> <EOL> pawnPromoted . movesMade -= <NUM_LIT:1> <EOL> else : <EOL> pieceToMoveBack = lastMove . piece <EOL> self . movePieceToPosition ( pieceToMoveBack , lastMove . oldPos ) <EOL> if pieceTaken : <EOL> if pieceTaken . side == WHITE : <EOL> self . points += pieceTaken . value <EOL> if pieceTaken . side == BLACK : <EOL> self . points -= pieceTaken . value <EOL> self . addPieceToPosition ( pieceTaken , lastMove . newPos ) <EOL> self . pieces . append ( pieceTaken ) <EOL> pieceToMoveBack . movesMade -= <NUM_LIT:1> <EOL> self . currentSide = not self . currentSide <EOL> def isCheckmate ( self ) : <EOL> if len ( self . getAllMovesLegal ( self . currentSide ) ) == <NUM_LIT:0> : <EOL> for move in self . getAllMovesUnfiltered ( not self . currentSide ) : <EOL> pieceToTake = move . pieceToCapture <EOL> if pieceToTake and pieceToTake . stringRep == "<STR_LIT>" : <EOL> return True <EOL> return False <EOL> def isStalemate ( self ) : <EOL> if len ( self . getAllMovesLegal ( self . currentSide ) ) == <NUM_LIT:0> : <EOL> for move in self . getAllMovesUnfiltered ( not self . currentSide ) : <EOL> pieceToTake = move . pieceToCapture <EOL> if pieceToTake and pieceToTake . stringRep == "<STR_LIT>" : <EOL> return False <EOL> return True <EOL> return False <EOL> def getLastMove ( self ) : <EOL> if self . history : <EOL> return self . history [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> def getLastPieceMoved ( self ) : <EOL> if self . history : <EOL> return self . history [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] . piece <EOL> def addMoveToHistory ( self , move ) : <EOL> pieceTaken = None <EOL> if move . pessant : <EOL> pieceTaken = move . specialMovePiece <EOL> self . history . append ( [ move , pieceTaken ] ) <EOL> return <EOL> pieceTaken = move . pieceToCapture <EOL> if pieceTaken : <EOL> self . history . append ( [ move , pieceTaken ] ) <EOL> return <EOL> self . history . append ( [ move , None ] ) <EOL> def getCurrentSide ( self ) : <EOL> return self . currentSide <EOL> def makeStringRep ( self , pieces ) : <EOL> stringRep = '<STR_LIT>' <EOL> for y in range ( <NUM_LIT:7> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> for x in range ( <NUM_LIT:8> ) : <EOL> piece = None <EOL> for p in pieces : <EOL> if p . position == C ( x , y ) : <EOL> piece = p <EOL> break <EOL> pieceRep = '<STR_LIT>' <EOL> if piece : <EOL> side = piece . side <EOL> color = '<STR_LIT>' if side == WHITE else '<STR_LIT>' <EOL> pieceRep = colored ( piece . stringRep , color ) <EOL> else : <EOL> pieceRep = '<STR_LIT:x>' <EOL> stringRep += pieceRep + '<STR_LIT:U+0020>' <EOL> stringRep += '<STR_LIT:\n>' <EOL> stringRep = stringRep . strip ( ) <EOL> return stringRep <EOL> def wrapStringRep ( self , stringRep ) : <EOL> sRep = '<STR_LIT:\n>' . join ( <EOL> [ '<STR_LIT>' , '<STR_LIT:U+0020>' * <NUM_LIT> ] + <EOL> [ '<STR_LIT>' % ( <NUM_LIT:8> - r , s . strip ( ) , <NUM_LIT:8> - r ) <EOL> for r , s in enumerate ( stringRep . split ( '<STR_LIT:\n>' ) ) ] + <EOL> [ '<STR_LIT:U+0020>' * <NUM_LIT> , '<STR_LIT>' ] <EOL> ) . rstrip ( ) <EOL> return sRep <EOL> def rankOfPiece ( self , piece ) : <EOL> return str ( piece . position [ <NUM_LIT:1> ] + <NUM_LIT:1> ) <EOL> def fileOfPiece ( self , piece ) : <EOL> transTable = str . maketrans ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return str ( piece . position [ <NUM_LIT:0> ] ) . translate ( transTable ) <EOL> def getShortNotationOfMove ( self , move ) : <EOL> notation = "<STR_LIT>" <EOL> pieceToMove = move . piece <EOL> pieceToTake = move . pieceToCapture <EOL> if move . queensideCastle : <EOL> return "<STR_LIT>" <EOL> if move . kingsideCastle : <EOL> return "<STR_LIT>" <EOL> if pieceToMove . stringRep != '<STR_LIT:p>' : <EOL> notation += pieceToMove . stringRep <EOL> if pieceToTake is not None : <EOL> if pieceToMove . stringRep == '<STR_LIT:p>' : <EOL> notation += self . fileOfPiece ( pieceToMove ) <EOL> notation += '<STR_LIT:x>' <EOL> notation += self . positionToHumanCoord ( move . newPos ) <EOL> if move . promotion : <EOL> notation += "<STR_LIT:=>" + str ( move . specialMovePiece . stringRep ) <EOL> return notation <EOL> def getShortNotationOfMoveWithFile ( self , move ) : <EOL> notation = "<STR_LIT>" <EOL> pieceToMove = self . pieceAtPosition ( move . oldPos ) <EOL> pieceToTake = self . pieceAtPosition ( move . newPos ) <EOL> if pieceToMove . stringRep != '<STR_LIT:p>' : <EOL> notation += pieceToMove . stringRep <EOL> notation += self . fileOfPiece ( pieceToMove ) <EOL> if pieceToTake is not None : <EOL> notation += '<STR_LIT:x>' <EOL> notation += self . positionToHumanCoord ( move . newPos ) <EOL> return notation <EOL> def getShortNotationOfMoveWithRank ( self , move ) : <EOL> notation = "<STR_LIT>" <EOL> pieceToMove = self . pieceAtPosition ( move . oldPos ) <EOL> pieceToTake = self . pieceAtPosition ( move . newPos ) <EOL> if pieceToMove . stringRep != '<STR_LIT:p>' : <EOL> notation += pieceToMove . stringRep <EOL> notation += self . rankOfPiece ( pieceToMove ) <EOL> if pieceToTake is not None : <EOL> notation += '<STR_LIT:x>' <EOL> notation += self . positionToHumanCoord ( move . newPos ) <EOL> return notation <EOL> def getShortNotationOfMoveWithFileAndRank ( self , move ) : <EOL> notation = "<STR_LIT>" <EOL> pieceToMove = self . pieceAtPosition ( move . oldPos ) <EOL> pieceToTake = self . pieceAtPosition ( move . newPos ) <EOL> if pieceToMove . stringRep != '<STR_LIT:p>' : <EOL> notation += pieceToMove . stringRep <EOL> notation += self . fileOfPiece ( pieceToMove ) <EOL> notation += self . rankOfPiece ( pieceToMove ) <EOL> if pieceToTake is not None : <EOL> notation += '<STR_LIT:x>' <EOL> notation += self . positionToHumanCoord ( move . newPos ) <EOL> return notation <EOL> return <EOL> def humanCoordToPosition ( self , coord ) : <EOL> transTable = str . maketrans ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> coord = coord . translate ( transTable ) <EOL> coord = [ int ( c ) - <NUM_LIT:1> for c in coord ] <EOL> pos = C ( coord [ <NUM_LIT:0> ] , coord [ <NUM_LIT:1> ] ) <EOL> return pos <EOL> def positionToHumanCoord ( self , pos ) : <EOL> transTable = str . maketrans ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> notation = str ( pos [ <NUM_LIT:0> ] ) . translate ( transTable ) + str ( pos [ <NUM_LIT:1> ] + <NUM_LIT:1> ) <EOL> return notation <EOL> def isValidPos ( self , pos ) : <EOL> if <NUM_LIT:0> <= pos [ <NUM_LIT:0> ] <= <NUM_LIT:7> and <NUM_LIT:0> <= pos [ <NUM_LIT:1> ] <= <NUM_LIT:7> : <EOL> return True <EOL> else : <EOL> return False <EOL> def getSideOfMove ( self , move ) : <EOL> return move . piece . side <EOL> def getPositionOfPiece ( self , piece ) : <EOL> for y in range ( <NUM_LIT:8> ) : <EOL> for x in range ( <NUM_LIT:8> ) : <EOL> if self . boardArray [ y ] [ x ] is piece : <EOL> return C ( x , <NUM_LIT:7> - y ) <EOL> def pieceAtPosition ( self , pos ) : <EOL> for piece in self . pieces : <EOL> if piece . position == pos : <EOL> return piece <EOL> def movePieceToPosition ( self , piece , pos ) : <EOL> piece . position = pos <EOL> def addPieceToPosition ( self , piece , pos ) : <EOL> piece . position = pos <EOL> def clearPosition ( self , pos ) : <EOL> x , y = self . coordToLocationInArray ( pos ) <EOL> self . boardArray [ x ] [ y ] = None <EOL> def coordToLocationInArray ( self , pos ) : <EOL> return ( <NUM_LIT:7> - pos [ <NUM_LIT:1> ] , pos [ <NUM_LIT:0> ] ) <EOL> def locationInArrayToCoord ( self , loc ) : <EOL> return ( loc [ <NUM_LIT:1> ] , <NUM_LIT:7> - loc [ <NUM_LIT:0> ] ) <EOL> def makeMove ( self , move ) : <EOL> self . addMoveToHistory ( move ) <EOL> if move . kingsideCastle or move . queensideCastle : <EOL> kingToMove = move . piece <EOL> rookToMove = move . specialMovePiece <EOL> self . movePieceToPosition ( kingToMove , move . newPos ) <EOL> self . movePieceToPosition ( rookToMove , move . rookMovePos ) <EOL> kingToMove . movesMade += <NUM_LIT:1> <EOL> rookToMove . movesMade += <NUM_LIT:1> <EOL> elif move . pessant : <EOL> pawnToMove = move . piece <EOL> pawnToTake = move . specialMovePiece <EOL> pawnToMove . position = move . newPos <EOL> self . pieces . remove ( pawnToTake ) <EOL> pawnToMove . movesMade += <NUM_LIT:1> <EOL> elif move . promotion : <EOL> self . pieces . remove ( move . piece ) <EOL> self . pieces . append ( move . specialMovePiece ) <EOL> if move . piece . side == WHITE : <EOL> self . points += move . specialMovePiece . value - <NUM_LIT:1> <EOL> if move . piece . side == BLACK : <EOL> self . points -= move . specialMovePiece . value - <NUM_LIT:1> <EOL> else : <EOL> pieceToMove = move . piece <EOL> pieceToTake = move . pieceToCapture <EOL> if pieceToTake : <EOL> if pieceToTake . side == WHITE : <EOL> self . points -= pieceToTake . value <EOL> if pieceToTake . side == BLACK : <EOL> self . points += pieceToTake . value <EOL> self . pieces . remove ( pieceToTake ) <EOL> self . movePieceToPosition ( pieceToMove , move . newPos ) <EOL> pieceToMove . movesMade += <NUM_LIT:1> <EOL> self . movesMade += <NUM_LIT:1> <EOL> self . currentSide = not self . currentSide <EOL> def getPointValueOfSide ( self , side ) : <EOL> points = <NUM_LIT:0> <EOL> for piece in self . pieces : <EOL> if piece . side == side : <EOL> points += piece . value <EOL> return points <EOL> def getPointAdvantageOfSide ( self , side ) : <EOL> pointAdvantage = self . getPointValueOfSide ( side ) - self . getPointValueOfSide ( not side ) <EOL> return pointAdvantage <EOL> if side == WHITE : <EOL> return self . points <EOL> if side == BLACK : <EOL> return - self . points <EOL> def getAllMovesUnfiltered ( self , side , includeKing = True ) : <EOL> unfilteredMoves = [ ] <EOL> for piece in self . pieces : <EOL> if piece . side == side : <EOL> if includeKing or piece . stringRep != '<STR_LIT>' : <EOL> for move in piece . getPossibleMoves ( ) : <EOL> unfilteredMoves . append ( move ) <EOL> return unfilteredMoves <EOL> def testIfLegalBoard ( self , side ) : <EOL> for move in self . getAllMovesUnfiltered ( side ) : <EOL> pieceToTake = move . pieceToCapture <EOL> if pieceToTake and pieceToTake . stringRep == '<STR_LIT>' : <EOL> return False <EOL> return True <EOL> def moveIsLegal ( self , move ) : <EOL> side = move . piece . side <EOL> self . makeMove ( move ) <EOL> isLegal = self . testIfLegalBoard ( not side ) <EOL> self . undoLastMove ( ) <EOL> return isLegal <EOL> def getAllMovesLegal ( self , side ) : <EOL> unfilteredMoves = list ( self . getAllMovesUnfiltered ( side ) ) <EOL> legalMoves = [ ] <EOL> for move in unfilteredMoves : <EOL> if self . moveIsLegal ( move ) : <EOL> legalMoves . append ( move ) <EOL> return legalMoves </s>
