def __init__(self, black): '''Constructs a Knight piece with a color and associated movement functions.''' Piece.__init__(self, black) if self.black: self.symbol = 'bN' else: self.symbol = 'wN'
def __init__(self, black): '''Constructor for a King Piece. King always starts out of check.''' Piece.__init__(self, black) self.in_check = False if self.black: self.symbol = 'bK' else: self.symbol = 'wK'
def validate_move(self, start_column, start_row, end_column, end_row, board_array): ''' Returns True if movement of a queen is valid.''' valid_end_check = False # If no movement is made, return false. if start_column == end_column and start_row == end_row: return valid_end_check if Piece.straight_line_movement(self, start_column, start_row, end_column, end_row, board_array): return True if Piece.diagonal_line_movement(self, start_column, start_row, end_column, end_row, board_array): return True return valid_end_check
def validate_move(self, start_column, start_row, end_column, end_row, board_array): ''' Returns True if movement of a rook is valid.''' valid_end_check = False # If the rook does not move at all, return False: if start_column == end_column and start_row == end_row: return valid_end_check return Piece.straight_line_movement(self, start_column, start_row, end_column, end_row, board_array)
def importOpeningFromBSFile(self, aFileName): #Ugly hack parameters #breakConditionType = 0 # break just before black moves #breakConditionType = 1 # break just before any piece move #breakConditionType = 2 # break once a max nr of pieces has been imported breakConditionType = 3 # include only a certain number of moves maxNrPieces = 8 lastMoveNrToInclude = 8 checkOnlyPLM = True self.pieces = set() # Storage for the unordered set of bugs in play self.fileName = aFileName tmp = os.path.basename(aFileName) self.name = os.path.splitext(tmp)[0] #Read the file and import the opening fileObject = open(aFileName, 'r', encoding='utf-8', errors='ignore') currentMoveNr = 0 for line in fileObject: #If we should only process PLM games check if this #game is a PLM game, if not skip it and stop reading if "SU[" in line: self.gameType = findBetween(line, "SU[", "]").lower() #Skip all games that are not PLM if checkOnlyPLM and self.gameType != 'Hive-PLM'.lower(): break if self.findOutGameResult(line) == None: break #building the opening state if any(x in line for x in ["move", "dropb", "pdropb", "Move", "Dropb", "Pdropb"]): #building the opening state currentMoveNr += 1 parts = line.split() #print(line) if ("dropb" in line) or ("pdropb" in line) \ or ("Dropb" in line) or ("Pdropb" in line): #; P1[5 dropb bL1 M 12 /wM] #added pdropb and support for newer files where #actions/moves are capitalized in the .sgf files newPieceName = cleanPieceName(parts[3]) #print(parts[3] +" => "+ newPieceName) placement = parts[6][:-1] coordinates = parts[4] + ' ' + parts[5] else: #; P0[11 move W wA1 O 14 wS2-] newPieceName = cleanPieceName(parts[4]) placement = parts[7][:-1] coordinates = parts[5] + ' ' + parts[6] placement = placement.replace( "\\\\", "\\") #clean the placement from "//"='////' #print(newPieceName, placement) #New placement of piece or update of an already moved piece thePiece = self.getPieceByName(newPieceName) #if the piece was not found then add a new piece if thePiece == None: if 'w' in newPieceName: color = 'white' else: color = 'black' #Level = 0 for all pieces that have not #climbed on top of the hive level = 0 newPiece = Piece(newPieceName, color, coordinates, level) self.pieces.add(newPiece) if len(self.pieces) == 1: self.firstPlacedPosition = newPiece.position if len(self.pieces) == 2: self.secondPlacedPosition = newPiece.position else: #not a newly added piece, but a piece to move if self.shouldContinueMovingBased( thePiece, breakConditionType) == True: #print("Moving piece: " + newPieceName + " " + placement) thePiece.moveTo(coordinates) else: break #stop reading the file #check number of imported pieces is enabled #only used if break condition == 2 if self.shouldContinueNrBased(maxNrPieces, breakConditionType) == None: break if self.shouldContinueNrMoves(currentMoveNr, lastMoveNrToInclude, breakConditionType) == None: break #close the file - important fileObject.close() return self
def importFirstTwoPiecesFromBSFile(self, aFileName, checkOnlyPLM, onlyTournamentRule): self.pieces = set() # Storage for the unordered set of bugs in play self.fileName = aFileName tmp = os.path.basename(aFileName) self.name = os.path.splitext(tmp)[0] #read the file, extract who won, and the first 2 pieces placed fileObject = open(aFileName, 'r', encoding='utf-8', errors='ignore') currentMoveNr = 0 for line in fileObject: if "SU[" in line: self.gameType = findBetween(line, "SU[", "]").lower() #Skip all games that are not PLM if checkOnlyPLM and self.gameType != 'Hive-PLM'.lower(): break if self.findOutGameResult(line) == None: break if any(x in line for x in ["move", "dropb", "pdropb", "Move", "Dropb", "Pdropb"]): currentMoveNr += 1 parts = line.split() #print(line) if ("dropb" in line) or ("pdropb" in line) \ or ("Dropb" in line) or ("Pdropb" in line): #; P1[5 dropb bL1 M 12 /wM] #; P1[131 Pdropb wS1 K 9 /bP] #; P0[110 Dropb wG3 O 11 wM-] newPieceName = cleanPieceName(parts[3]) #print(parts[3] +" => "+ newPieceName) placement = parts[6][:-1] coordinates = parts[4] + ' ' + parts[5] else: #; P0[11 move W wA1 O 14 wS2-] newPieceName = cleanPieceName(parts[4]) placement = parts[7][:-1] coordinates = parts[5] + ' ' + parts[6] #clean the placement from "//"='////' placement = placement.replace("\\\\", "\\") #check that tournament rule is upheld #no queens as the first 2 pieces placed if (onlyTournamentRule == True) and ('Q' in newPieceName): break #check color if 'w' in newPieceName: color = 'white' else: color = 'black' #Level = 0 for all pieces that have not #climbed on top of the hive level = 0 newPiece = Piece(newPieceName, color, coordinates, level) self.pieces.add(newPiece) #Once we've found the first 2 pieces we stop reading the file if len(self.pieces) == 2: break #close the file - important fileObject.close() #Captures and filters away early resigns if len(self.pieces) != 2: return None return self
def __init__(self, black): Piece.__init__(self, black) if self.black: self.symbol = 'bR' else: self.symbol = 'wR'