def act(): print("Act called") can_move = True can_switch = True # Cause the exception to happen before making a new state try: f.driver.find_element_by_name("chooseMove") except common.exceptions.NoSuchElementException: can_move = False except common.exceptions.ElementNotVisibleException: can_move = False try: f.driver.find_element_by_class_name("switchmenu") except common.exceptions.NoSuchElementException: can_switch = False except common.exceptions.ElementNotVisibleException: can_switch = False if can_switch and not can_move: time.sleep(1) try: f.driver.find_element_by_name("chooseMove") can_move = True except common.exceptions.NoSuchElementException: can_move = False except common.exceptions.ElementNotVisibleException: can_move = False if can_move and not can_switch: time.sleep(1) try: f.driver.find_element_by_class_name("switchmenu") can_switch = True except common.exceptions.NoSuchElementException: can_switch = False except common.exceptions.ElementNotVisibleException: can_switch = False print("Switch: " + str(can_switch) + " | Move: " + str(can_move)) if not can_switch and not can_move: raise CantMoveError("Can't move") f.update(can_switch and not can_move) current_state = s.State(f.own_team, f.opponent_team, f.own_mon_out, f.opponent_mon_out, can_move, can_switch) action = current_state.get_best_action() print(action.name) f.act(action.name, action.switch) print("Acted")
def calc_switch(mon): #switches according to the pokemon specified by the tree or otherwise #will be either the best, next best, or next next best depending on #what pokemon is expected to die or not if mon is None: try: print("Sorting pokemon to select switch option") #sort our team's pokemon by strongest move sortedRunnerUps = insertionSort_pokemon() justNames = [pokemon.name for pokemon in sortedRunnerUps] #get available switch options options = i.get_switch_options() # loop through sorted pokemon until we find one that is not on the field k = 0 print "options" print options print("Attempting to select an option") actNotSelected = True while actNotSelected: pokemon = justNames[k] print("now looking at" + pokemon) if pokemon in options: print "pokemon in options!" index = options.index(pokemon) i.act(options[index], True) break k = k + 1 except AttributeError: pass else: print("Unable to switch to sorted pokemon. Random_switch called") random_switch() else: #switch to pokemon specified by user options = i.get_switch_options() print("mon is not None. attempting to locate switch option.") # find the index where our pokemon is located index = options.index(mon) selection = options[index] print("IN CALC_SWITCH: pokemon to be acted upon is " + mon) i.act(mon, True)
def tree_battle(): switch_allowed = True move_allowed = True print("now in: TREE_BATTLE") try: i.driver.find_element_by_class_name("switchmenu") except common.exceptions.NoSuchElementException: switch_allowed = False try: i.driver.find_element_by_class_name("movemenu") except common.exceptions.NoSuchElementException: move_allowed = False if switch_allowed and move_allowed: try: print("in tree_battle: going to calc_switch_and_move") calc_switch_and_move() except ValueError: random_move() else: print("Decision tree did not work. Pokemon used a random move") random_move() elif switch_allowed: print("in tree_battle: only switches allowed. calculating switch") calc_switch(None) elif move_allowed: #find our pokemon's best damage move and use it print("in tree_battle: only moves allowed. calculating move") options = i.get_move_options() #account for u'Name' format when we call get_switch_options() ammendedOptions = [op.encode("ascii") for op in options] bestMove, bestDamage = calc_max_damage(i.own_mon_out.available_moves) moveIndex = ammendedOptions.index(bestMove) i.act(options[moveIndex]) else: print("Can't do anything")
def calc_switch_and_move(): print("We are in calc_switch_and_move") treeStatement = "" #keep track of where we go in tree try: # initialize team if we have not if i.own_team == []: i.get_own_team() print("Team initalized.") i.update() print("update completed.") options = i.get_move_options() ourPokemon = i.own_mon_out opponentPokemon = i.opponent_mon_out print("fetched our mon and opponent mon") ourMoves = ourPokemon.available_moves opponentMoves = opponentPokemon.available_moves print("fetched our mon and opponent mon moves") move, damage = calc_max_damage(ourMoves) print("max move calculated") if calc_danger_of_knockout(opponentMoves, ourPokemon, opponentPokemon): treeStatement = treeStatement + "Current pokemon in danger of knockout--> " print treeStatement if ourPokemon.health_percent == 1: treeStatement = treeStatement + "pokemon at full health-->Switch" print treeStatement calc_switch(None) else: treeStatement = treeStatement + "pokemon not at full health--> " heal = has_heal(options) if heal == False: treeStatement = treeStatement + "no healing move-->Attack" print treeStatement i.act(move) else: treeStatement = treeStatement + "healing move available-->Heal" print treeStatement i.act(heal) else: sortedPokemon = insertionSort_pokemon() justNames = [pokemon.name for pokemon in sortedPokemon] bestIndex, bestPokemon = available_pokemon(justNames, 1) bestPokemonObject = sortedPokemon.index(bestPokemon) print "best pokemon object" print bestPokemonObject nextBestIndex, nextBestPokemon = available_pokemon(justNames, 2) nextBestObject = sortedPokemon.index(nextBestPokemon) if (ourPokemon.name == bestPokemon): treeStatement = treeStatement \ + "our pokemon is the best out of available pokemon-->Attack" print treeStatement i.act(move) else: treeStatement = treeStatement \ + "pokemon is not the best out of available pokemon-->" print treeStatement if (calc_danger_of_knockout(opponentMoves, bestPokemonObject, opponentPokemon)): treeStatement = treeStatement \ + "best pokemon in danger of knockout-->" if (ourPokemon == nextBestObject): treeStatement = treeStatement \ + "our pokemon is the next best-->Attack" print treeStatement i.act(move) else: treeStatement = treeStatement \ + "our pokemone is not the next best-->" if (calc_danger_of_knockout(opponentMoves, nextBestObject, opponentPokemon)): treeStatement = treeStatement \ + "next best pokemon in danger of knockout-->Attack" print treeStatement i.act(move) else: calc_switch(nextBestPokemon) else: treeStatement = treeStatement \ + "best pokemon in not danger of knockout-->Switch" print treeStatement calc_switch(bestPokemon) except AttributeError: pass else: print("random move: we didn't get anywhere in the tree") random_move()
def random_move(): options = i.get_move_options() selection = options[random.randint(0, len(options) - 1)] i.act(selection)
def random_switch(): options = i.get_switch_options() selection = options[random.randint(0, len(options) - 1)] i.act(selection, True)