def free(self, key) -> Piece: '''Removes item from captured list, provided that it exists in the list''' for p in range(len(self._captured)): if Piece.getChar(self._captured[p]).lower() == key.lower(): return self._captured.pop(p) else: raise IllegalDrop("Invalid piece was attempted to be dropped")
def findEscapeMoves(self, board, opponent): '''Traverses board to find escape moves for box Drive piece by checking kings possible moves and its teammates moves as well in order to prevent a check ''' king_moves = {} # filter king moves that lead to check for c in BoxDrive.possibleMoves(board, self.king_loc): board_copy = deepcopy(board) self.move(self.king_loc, c, board_copy, False, False) if not self.check(board_copy, c): king_moves[c] = self.king_loc bannedMoves = set() team_moves = {} drop_moves = set() in_check = self.check(board) # traverse through board and check possible moves for all squares (empty,opponent,teammate) except your king for i in range(board.BOARD_SIZE): for j in range(board.BOARD_SIZE): # traverse through board if not board.isEmptyAt((i, j)): if board[(i, j)].player != self.player: bannedMoves.update( type(board[(i, j)]).possibleMoves(board, (i, j))) elif (i, j) != self.king_loc: teammate_moves = type(board[(i, j)]).possibleMoves( board, (i, j)) # simulate each move and check if it leads to check -> if not, add to results for move in teammate_moves: board_copy = deepcopy(board) board_copy[(i, j)].move((i, j), move, board_copy, False) if not self.check(board_copy, self.king_loc): team_moves[move] = (i, j) else: player_copy = deepcopy(self) opponent_copy = deepcopy(opponent) # simulate dropping every piece at every possible location and validate for check at each spot for c in range(len(self.captured)): player_copy = deepcopy(self) board_copy = deepcopy(board) try: player_copy.drop(Piece.getChar(self.captured[c]), board_copy, (i, j), opponent_copy) except (IllegalMove, IllegalDrop): pass if not self.check(board_copy, self.king_loc): drop_moves.add((self.captured[c], i, j)) for banned in bannedMoves: # filter out banned moves if banned in king_moves: del king_moves[banned] return (drop_moves, { **king_moves, **team_moves, })
def _stringifyBoard(self): """ Utility function for printing the board """ s = '' for row in range(len(self._board) - 1, -1, -1): s += '' + str(row + 1) + ' |' for col in range(0, len(self._board[row])): s += self._stringifySquare(Piece.getChar( self._board[row][col])) s += os.linesep s += ' a b c d e' + os.linesep return s