def run(): # Initialize a 10x10 stage. No other stage sizes are # coded yet, but these constructor arguments exist in # case we want to later on board = Board() # Initialize the A-Star algorithm class with our # current graph and reference to the board because # I'm lazy. It shouldn't have a dependency on a board, # but it's been 8 hours already and I'm tired. astar = AStar(board.graph, board) # Render the board to show where we stand board.render() inp = get_input() while inp[0].lower() not in "qe": if inp[0].lower() in "h": # Players is babies and wants helps! with open(os.path.join(resource_root, "maze", "help.txt"), "rb") as help_file: print(help_file.read()) board.render() inp = raw_input("Next move? (enter h or help for help)").strip() if inp == "": inp = " " continue # Try to move the player. Run, Forrest! Run! if inp[0].lower() == "u": (x, y) = board.player board.move_entity("player", x, y - 1) elif inp[0].lower() == "d": (x, y) = board.player board.move_entity("player", x, y + 1) elif inp[0].lower() == "l": (x, y) = board.player board.move_entity("player", x - 1, y) elif inp[0].lower() == "r": (x, y) = board.player board.move_entity("player", x + 1, y) # The enemy is a greedy bastard and moves every chance he can path = astar.get_path(astar.search(board.enemy, board.player)[0], board.enemy, board.player) board.move_entity("enemy", *path[1]) board.render(path=path[2:-1]) if board.enemy == board.player: print("You lost!") exit() if board.player == board.goal: print("You escaped death! Huzzah!") exit() inp = get_input() print("Scaredy-cat!")
def move(self): if self.next_step_x == None and self.next_step_y == None: if self.player.x != self.player_last_position[ 0] or self.player.y != self.player_last_position[1]: self.player_last_position = (self.player.x, self.player.y) if self.player.next_step_x == None and self.player.next_step_y == None: self.path = AStar.get_path(self) if self.next_step_x == None and self.next_step_y == None: if len(self.path) > 0: l = self.path.pop() self.next_step_x = l.x self.next_step_y = l.y else: if self.x > self.next_step_x: self.x -= 4 self.direction = "W" elif self.x < self.next_step_x: self.x += 4 self.direction = "E" if self.y > self.next_step_y: self.y -= 4 self.direction = "N" elif self.y < self.next_step_y: self.y += 4 self.direction = "S" if self.x == self.next_step_x and self.y == self.next_step_y: self.next_step_x = None self.next_step_y = None
def move(self): if self.next_step_x == None and self.next_step_y == None: if self.player.x != self.player_last_position[0] or self.player.y != self.player_last_position[1]: self.player_last_position = (self.player.x, self.player.y) if self.player.next_step_x == None and self.player.next_step_y == None: self.path = AStar.get_path(self) if self.next_step_x == None and self.next_step_y == None: if len(self.path) > 0: l = self.path.pop() self.next_step_x = l.x self.next_step_y = l.y else: if self.x > self.next_step_x: self.x -= 4 self.direction = "W" elif self.x < self.next_step_x: self.x += 4 self.direction = "E" if self.y > self.next_step_y: self.y -= 4 self.direction = "N" elif self.y < self.next_step_y: self.y += 4 self.direction = "S" if self.x == self.next_step_x and self.y == self.next_step_y: self.next_step_x = None self.next_step_y = None
def calculate(self): global escapeLocation opponentLocation = self.battlefieldInfo.opponents[0].location # Find path to desired position # If escape location is specified then bot is running away # otherwise it tries to go to the opponent. endLocation = None if escapeLocation is None: endLocation = opponentLocation else: endLocation = escapeLocation # Find a path astar = AStar(self.battlefieldInfo, endLocation) (pathToFollow, nodes) = astar.get_path() moveDirection = MoveDirection.NoMove action = BotAction.NoAction fireDirection = MoveDirection.Up nextMoveLocation = None # Calculate move direction based on first step of a shortest path to opponent if pathToFollow is not None and len(pathToFollow) > 0: nextMoveLocation = pathToFollow[0] moveDirection = self._get_move_direction( self.battlefieldInfo.bot.location, nextMoveLocation) if escapeLocation is not None: # If you hit escape location in next move you have to go to the opponent next round if nextMoveLocation.x == escapeLocation.x and nextMoveLocation.y == escapeLocation.y: escapeLocation = None else: # If you're following opponent then you need to calculate some action, probably... didAction = False # Check whether way to opponent is a straight line (for new step) without any obstacles. # If that's true then make fire a missile. if self._is_straight_line(pathToFollow) and len(pathToFollow): action = BotAction.FireMissile fireDirection = self._get_fire_direction(pathToFollow) didAction = True # Plant a bomb if bot is close to Opponent # Close mean in range of a bomb that bot can plant if len(pathToFollow ) <= self.battlefieldInfo.config.bombBlastRadius: action = BotAction.DropBomb didAction = True # If action is performed, then bot want to escape to a safe area next time if didAction is True: # Find an escape position escapeLocationFinder = EscapeLocation( self.battlefieldInfo, nextMoveLocation, opponentLocation, nodes) escapeLocation = escapeLocationFinder.calculateEscapeLocation( ) if escapeLocation is not None: print 'Escaping to: ({}, {})'.format( escapeLocation.x, escapeLocation.y) else: print 'Cannot find escape location' botMove = BotMove(moveDirection, action, fireDirection) botMove.print_debug() # Return structure that contains info to be returned by the server and info # what is the next location of the bot, so it can decide what next. nextMoveInfo = NextMoveInfo(botMove, nextMoveLocation) return nextMoveInfo