def load_sudoku(sudoku): """ Create a list of fields with all relevant information based on a sudoku. The sudoku needs to be in the form found in Sudokus.py """ y = 0 id_num = 0 fields = [] for row in sudoku: x = 0 for number in row: # Create a field field = Field() # Set coordinates, 3x3 box and and id field.x = x field.y = y field.box = set_box_number(x, y) field.id = id_num field.number = number # If the field has a number, that is the only possibility if field.number != 0: field.possible = {field.number} fields.append(field) # Step up all index x += 1 id_num += 1 y += 1 return fields
def confusion_attack(def_poke): confusion_move = Move() confusion_move.i_pow = 40 confusion_move.str_type = "confusion" i_dmg = attack(def_poke, def_poke, confusion_move, Field()) return i_dmg
def __init__(self, l_new_players=[]): self.firstRun = True self.field = Field(l_new_players[0], l_new_players[1]) self.l_players = l_new_players self.b_gameover = False for player in self.l_players: other_player = self.get_other_player(player) player.i_turn_readiness = NOT_READY player.send_data(FOUND_BATTLE) player.send_pokes(other_player.team) player.send_data( SELECT_POKE + json.dumps({"availpoke": player.get_available_pokes()}))
def __init__(self, bzrc): self.bzrc = bzrc self.field = Field(-400, 400, -400, 400) self.field.setupMap4Ls() #self.field.setupMapRotatedBoxes() self.field.fastCalculate() self.constants = self.bzrc.get_constants() self.commands = [] self.prevError = defaultdict(list) self.vizualize = True self.gotFlag = False self.prevLocation = [0, 0] self.stuckTimer = time.time() self.stuck = False
def copy_fields(fields): """ Create a copy of the fields in the list called "fields" """ fields_c = [] for field in fields: # copy all values in the fields and append them to copy list f = Field() f.x = field.x f.y = field.y f.box = field.box f.id = field.id f.number = field.number f.possible = field.possible fields_c.append(f) return fields_c
from FieldClass import Field f = Field(-400,400,-400,400) ''' f.setAttractionWeight(1) f.setAttractionRadius(5) f.setAttractionSpread(1132) f.setRepulsionWeight(.5) f.setRepulsionRadius(2) f.setRepulsionSpread(50) f.setTangentialWeight(2) f.setTangentialRadius(10) f.setTangentialSpread(12) ''' ''' f.clearGoals() f.clearObstacles() f.addGoal(370,0) # green flag f.addObstacle(-100,-100) ''' f.setupMap4Ls() #f.setupMapRotatedBoxes() #f.calculateFields() f.fastCalculate() print f.getFast(297, 50), f.getFast(300, 50), f.getFast(303, 50)
class Battle(object): def __init__(self, l_new_players=[]): self.firstRun = True self.field = Field(l_new_players[0], l_new_players[1]) self.l_players = l_new_players self.b_gameover = False for player in self.l_players: other_player = self.get_other_player(player) player.i_turn_readiness = NOT_READY player.send_data(FOUND_BATTLE) player.send_pokes(other_player.team) player.send_data( SELECT_POKE + json.dumps({"availpoke": player.get_available_pokes()})) def get_other_player(self, player): i_player_idx = self.l_players.index(player) return self.l_players[(i_player_idx + 1) % 2] def hazard_dmg(self, player): atk_poke = player.active_poke other_player = self.get_other_player(player) def_poke = other_player.active_poke if atk_poke == None or def_poke == None: return self.send_players_pokes() # entry hazards if self.field.count_entry_hazards(player, "spikes") == 1: atk_poke.i_hp -= atk_poke.get_usable_stats().i_hp * 1 / 8 elif self.field.count_entry_hazards(player, "spikes") == 2: atk_poke.i_hp -= atk_poke.get_usable_stats().i_hp * 1 / 6 elif self.field.count_entry_hazards(player, "spikes") == 3: atk_poke.i_hp -= atk_poke.get_usable_stats().i_hp * 1 / 4 if self.field.count_entry_hazards(player, "stealth-rock") >= 1: atk_poke.i_hp -= atk_poke.get_usable_stats().i_hp * 0.125 * Type( "Rock").getAtkEff(atk_poke.type_1, atk_poke.type_2) if self.field.count_entry_hazards(player, "sticky-web") >= 1: atk_poke.modifier_stats.i_spe -= 1 if self.field.count_entry_hazards(player, "toxic-spikes") == 1: atk_poke.str_status = "poison" elif self.field.count_entry_hazards(player, "toxic-spikes") == 2: atk_poke.str_status = "toxic" self.send_players_pokes() def recieved_data(self, player, dic_data): other_player = self.get_other_player(player) if dic_data["battlestate"] == "chat": print(dic_data) player.send_data(DISPLAY_TEXT + "You : " + dic_data["chat"]) other_player.send_data(DISPLAY_TEXT + "Other : " + dic_data["chat"]) elif dic_data["battlestate"] == "pokes": pass elif dic_data["battlestate"] == "selectpoke": self.hazard_dmg(player) pass elif dic_data["battlestate"] == "selectmove": pass def everyone_ready(self): b_ready = True for player in self.l_players: b_ready = b_ready and player.i_turn_readiness return b_ready def send_players_pokes(self): for player in self.l_players: if player.b_active_poke_is_new: player.b_active_poke_is_new = False # send updated info to players for player in self.l_players: atk_poke = player.active_poke other_player = self.get_other_player(player) def_poke = other_player.active_poke player.send_data(DISPLAY_POKES + json.dumps({ "player": ME, "pokeidx": player.i_active_poke_idx, "poke": atk_poke.to_dic() })) player.send_data(DISPLAY_POKES + json.dumps({ "player": OTHER, "pokeidx": other_player.i_active_poke_idx, "poke": def_poke.to_dic() })) def send_delay(self): for player in self.l_players: player.send_data(DISPLAY_DELAY) def send_broadcast(self, str_msg): for player in self.l_players: player.send_data(DISPLAY_TEXT + str_msg) def send_move(self, player, move): other_player = self.get_other_player(player) player.send_data(DISPLAY_MOVE + json.dumps({ "player": ME, "move": move.to_dic() })) other_player.send_data(DISPLAY_MOVE + json.dumps({ "player": OTHER, "move": move.to_dic() })) def send_field(self): for player in self.l_players: other_player = self.get_other_player(player) player.send_data( DISPLAY_FIELD + json.dumps(self.field.to_dic(player, other_player))) def send_ad_hoc_text(self, player, str_text): other_player = self.get_other_player(player) player.send_data(DISPLAY_AD_HOC_TEXT + json.dumps({ "player": ME, "move": str_text })) other_player.send_data(DISPLAY_AD_HOC_TEXT + json.dumps({ "player": OTHER, "move": str_text })) def player_poke_fainted(self, player): atk_poke = player.active_poke other_player = self.get_other_player(player) def_poke = other_player.active_poke player.i_turn_readiness = NOT_READY player.i_active_move_idx = -1 if def_poke.b_destiny_bonded: def_poke.b_destiny_bonded = False def_poke.i_hp = 0 def_poke.is_usable() self.player_poke_fainted(other_player) if atk_poke.b_grudge: def_poke.get_last_move().i_pp = 0 if len(player.get_available_pokes()) <= 0: self.b_gameover = True player.send_data(DISPLAY_LOSE) other_player.send_data(DISPLAY_WIN) # self.send_delay() return True return False def run(self): # Log.info("battle running") # print(self.l_players[0].i_turn_readiness,self.l_players[1].i_turn_readiness) if (not self.everyone_ready() or self.b_gameover): return if self.field.i_weather_counter > 0: self.field.i_weather_counter -= 1 if self.field.i_weather_counter == 0: self.field.weather = Weather.CLEAR_SKIES self.send_players_pokes() for player in self.l_players: player.pre_turn() # calculate damages l_move_queue = [] # add moves to queue according to speed for player in self.l_players: atk_poke = player.active_poke other_player = self.get_other_player(player) def_poke = other_player.active_poke if (player.i_active_move_idx == -1): continue if len(l_move_queue) >= 1: if atk_poke.get_moves( )[player.i_active_move_idx].i_priority > def_poke.get_moves()[ other_player.i_active_move_idx].i_priority: l_move_queue.insert(0, player) elif atk_poke.get_moves( )[player.i_active_move_idx].i_priority < def_poke.get_moves()[ other_player.i_active_move_idx].i_priority: l_move_queue.append(player) elif atk_poke.get_usable_stats( ).i_spe > l_move_queue[0].active_poke.get_usable_stats().i_spe: l_move_queue.insert(0, player) else: l_move_queue.append(player) else: l_move_queue.append(player) b_last = False # move according to queue for player in l_move_queue: atk_poke = player.active_poke other_player = self.get_other_player(player) def_poke = other_player.active_poke #print("susu",atk_poke.is_usable()) if not atk_poke.is_usable(): continue other_player = self.get_other_player(player) cur_move = atk_poke.get_moves()[player.i_active_move_idx] if not cur_move.use_move(): if cur_move.i_pp <= 0: self.send_broadcast(cur_move.str_name.capitalize() + " has no PP left!") if cur_move.i_disable_idx > 0: self.send_broadcast(cur_move.str_name.capitalize() + " is disabled!") continue if cur_move.str_name in ["copycat", "mirror-move"]: cur_move = def_poke.get_last_move() print(atk_poke, "used move", cur_move) # check if move hit b_hit = accuracy(atk_poke, def_poke, cur_move) b_para_immo = False if atk_poke.str_status == "paralyze": if randint(0, 99) < 25: b_para_immo = True if atk_poke.str_status == "freeze": if randint(0, 99) < 20: # thawed atk_poke.str_status = "none" self.send_broadcast(atk_poke.str_name.capitalize() + " thawed out!") if atk_poke.str_status == "sleep": if atk_poke.i_sleep_counter <= 0: # woke atk_poke.str_status = "none" self.send_broadcast(atk_poke.str_name.capitalize() + " woke up!") atk_poke.i_sleep_counter -= 1 if atk_poke.str_status == "confuse": if atk_poke.i_confusion_counter <= 0: # snapped out of confusion atk_poke.str_status = "none" self.send_broadcast(atk_poke.str_name.capitalize() + " snapped out of confusion!") atk_poke.i_confusion_counter -= 1 # check if moving is possible if b_para_immo: # paralysed, can't move self.send_broadcast(atk_poke.str_name.capitalize() + " is paralyzed!") self.send_broadcast("It can't move!") elif atk_poke.str_status == "freeze": # frozen, can't move self.send_broadcast(atk_poke.str_name.capitalize() + " is frozen solid!") self.send_ad_hoc_text(player, "frozen") elif atk_poke.str_status == "sleep": # asleep, can't move self.send_broadcast(atk_poke.str_name.capitalize() + " is fast asleep!") elif atk_poke.str_status == "confuse" and randint(0, 99) < 33: # confused, hurt it self self.send_broadcast(atk_poke.str_name.capitalize() + " hurt itself in confusion!") # calculate confusion damage i_dmg = confusion_attack(atk_poke) self.send_broadcast( atk_poke.str_name.capitalize() + " lost " + str(i_dmg / atk_poke.get_usable_stats().i_hp * 100) + "% HP.") atk_poke.i_hp -= i_dmg elif b_hit: # woah, the move hit self.send_broadcast(atk_poke.str_name.capitalize() + " used " + cur_move.str_name + ".") # electrify if def_poke.forced_move_type != None: cur_move = copy.deepcopy(cur_move) cur_move.type = def_poke.forced_move_type def_poke.forced_move_type = None # some moves hit more than one time i_hits = multi_hit(cur_move) for i in range(i_hits): # calculate damage self.send_move(player, cur_move) i_dmg = attack(atk_poke, def_poke, cur_move, self.field, player, other_player, l_move_queue.index(player) == 1) if not move_ad_hoc_during(atk_poke, def_poke, cur_move, self.field, player, other_player, l_move_queue.index(player) == 1): self.send_broadcast("It failed!") self.send_ad_hoc_text(player, "failed") else: if other_player.b_active_poke_is_new: self.hazard_dmg(other_player) elif player.i_turn_readiness == NOT_READY: return self.send_field() # move ad hoc if def_poke.b_protected and cur_move.flag_protect: # protected self.send_broadcast(def_poke.str_name.capitalize() + " protected itself.") self.send_ad_hoc_text(other_player, "protected") continue # if the move is not status, tell everyone the damage if cur_move.str_cat != "status": i_tmp_eff = cur_move.type.getAtkEff( def_poke.type_1, def_poke.type_2) if i_tmp_eff == 0: self.send_broadcast("It had no effect!") elif i_tmp_eff == 0.25: self.send_broadcast( "It is very not very effective!") elif i_tmp_eff == 0.5: self.send_broadcast("It is not very effective.") elif i_tmp_eff == 2: self.send_broadcast("It is super effective!") elif i_tmp_eff == 4: self.send_broadcast("It is super super effective!") # self.send_broadcast(str(def_poke.i_hp) + " - " + str(i_dmg) + " = " + str(def_poke.i_hp - i_dmg)) self.send_broadcast( def_poke.str_name.capitalize() + " lost " + str(i_dmg / def_poke.get_usable_stats().i_hp * 100) + "% HP.") # actually take damage def_poke.i_hp -= i_dmg player.i_active_move_idx = -1 # is it dead??? if (def_poke.i_hp <= 0): def_poke.i_hp = 0 def_poke.b_fainted = True i_dmg -= def_poke.i_hp # send updated pokes self.send_players_pokes() # recoil damage i_recoil_dmg = cur_move.get_recoil_ratio() * i_dmg if cur_move.str_name == "struggle": i_recoil_dmg = atk_poke.get_usable_stats().i_hp * 1 / 4 if cur_move.str_name == "shadow-end": i_recoil_dmg = atk_poke.i_hp * 1 / 2 atk_poke.i_hp -= i_recoil_dmg # is it dead??? if (atk_poke.i_hp <= 0): atk_poke.i_hp = 0 atk_poke.b_fainted = True # healing health i_heal_hp = cur_move.get_heal_hp(i_dmg, atk_poke) atk_poke.i_hp = min(atk_poke.i_hp + i_heal_hp, atk_poke.get_usable_stats().i_hp) # send updated pokes self.send_players_pokes() if not def_poke.is_usable(): break # implement status effect str_eff = status_effect(atk_poke, def_poke, cur_move, Field) if str_eff != 'none': self.send_broadcast(def_poke.str_name.capitalize() + " is " + str_eff + ".") self.send_players_pokes() # implement stat change str_eff = stat_change(atk_poke, def_poke, cur_move) if str_eff != 'none': self.send_broadcast("Its " + str(atk_poke.get_usable_stats()) + " stat changed.") self.send_players_pokes() else: # the move missed self.send_broadcast("It missed.") self.send_ad_hoc_text(player, "missed") i_recoil_dmg = 0 if cur_move.str_name in ["jump-kick", "high-jump-kick"]: i_recoil_dmg = atk_poke.get_usable_stats().i_hp // 2 elif cur_move.str_name in ["belly-drum"]: i_recoil_dmg = atk_poke.get_usable_stats().i_hp // 2 atk_poke.i_hp -= i_recoil_dmg # is it dead??? if (atk_poke.i_hp <= 0): atk_poke.i_hp = 0 atk_poke.b_fainted = True # make a new line to look more organised # self.send_broadcast("") # send updated pokes self.send_players_pokes() # the moving poke is dead !?!? if not def_poke.is_usable(): if self.player_poke_fainted(other_player): return continue # after turn heal / damage if self.firstRun == False: self.send_broadcast("") self.firstRun = False for player in l_move_queue: atk_poke = player.active_poke other_player = self.get_other_player(player) def_poke = other_player.active_poke if not atk_poke.is_usable(): continue if atk_poke.b_aqua_ring: atk_poke.i_hp += int(atk_poke.get_usable_stats().i_hp / 16) atk_poke.i_hp = min(atk_poke.i_hp, atk_poke.get_usable_stats().i_hp) self.send_players_pokes() #atk_poke.i_hp += int(atk_poke.get_usable_stats().i_hp / 13) #self.send_broadcast("It regained 7.69230769231% hp.") # status effect damage if atk_poke.str_status == "burn": atk_poke.i_hp -= int(atk_poke.get_usable_stats().i_hp / 16) self.send_ad_hoc_text(player, "burn") elif atk_poke.str_status == "poison": atk_poke.i_hp -= int(atk_poke.get_usable_stats().i_hp / 8) self.send_ad_hoc_text(player, "poison") elif atk_poke.str_status == "toxic": atk_poke.i_hp -= int(atk_poke.get_usable_stats().i_hp / 16 * atk_poke.i_toxic_idx) self.send_ad_hoc_text(player, "toxic") atk_poke.i_toxic_idx += 1 atk_poke.i_hp = min(atk_poke.i_hp, atk_poke.get_usable_stats().i_hp) # is it dead??? if (atk_poke.i_hp <= 0): atk_poke.i_hp = 0 atk_poke.b_fainted = True self.send_players_pokes() # the moving poke is dead !?!? if not atk_poke.is_usable(): if self.player_poke_fainted(player): return continue #self.send_delay() # check if pokes are dead for player in self.l_players: atk_poke = player.active_poke other_player = self.get_other_player(player) def_poke = other_player.active_poke if not atk_poke.is_usable(): if self.player_poke_fainted(player): return player.send_data( SELECT_POKE + json.dumps({"availpoke": player.get_available_pokes()})) # after turn move reset for player in l_move_queue: atk_poke = player.active_poke other_player = self.get_other_player(player) def_poke = other_player.active_poke if not atk_poke.is_usable(): continue move_ad_hoc_after_turn(atk_poke, def_poke, self.field, player, other_player) # if a poke is dead, then no turn 4 u for player in self.l_players: atk_poke = player.active_poke if not atk_poke.is_usable(): return if (not self.everyone_ready() or self.b_gameover): return # send updated info to players for player in self.l_players: atk_poke = player.active_poke other_player = self.get_other_player(player) def_poke = other_player.active_poke player.i_turn_readiness = NOT_READY #player.i_active_move_idx = -1 #player.send_data(DISPLAY_TEXT + "You selected pokeman number " + str(player.i_active_poke_idx)) #player.send_data(DISPLAY_TEXT + "Your opponent selected pokeman number " + str(other_player.i_active_poke_idx)) self.send_players_pokes() if atk_poke.is_usable(): player.send_data( SELECT_POKE_OR_MOVE + json.dumps({ "availpoke": player.get_available_pokes(), "availmove": atk_poke.get_move_dic() })) elif atk_poke.is_trapped(): player.send_data( SELECT_MOVE + json.dumps({"availmove": atk_poke.get_move_dic()})) else: player.send_data( SELECT_POKE + json.dumps({"availpoke": player.get_available_pokes()})) return
class Agent(object): def __init__(self, bzrc): self.bzrc = bzrc self.field = Field(-400, 400, -400, 400) self.field.setupMap4Ls() #self.field.setupMapRotatedBoxes() self.field.fastCalculate() self.constants = self.bzrc.get_constants() self.commands = [] self.prevError = defaultdict(list) self.vizualize = True self.gotFlag = False self.prevLocation = [0, 0] self.stuckTimer = time.time() self.stuck = False def tickAll(self, step): '''Some time has passed; decide what to do next''' # Get information from the BZRC server mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.mytanks = mytanks self.othertanks = othertanks self.flags = flags self.shots = shots self.enemies = [tank for tank in othertanks if tank.color != self.constants['team']] # Reset my set of commands (we don't want to run old commands) self.commands = [] # Check to see if the flag has been captured for bot in mytanks: if not self.gotFlag and bot.flag is not '-': self.gotFlag = True self.flipFieldToHome() if self.gotFlag and bot.flag is '-': self.gotFlag = False self.flipFieldToEnemy() # Decide what to do with each of my tanks if (time.time() - self.stuckTimer) > 3: if bot.x == self.prevLocation[0] and bot.y == self.prevLocation[1]: print 'stuck' self.stuck = True else: self.stuck = False self.prevLocation = [bot.x, bot.y] self.stuckTimer = time.time() if self.stuck: command = Command(bot.index, 1, 1, True) self.commands.append(command) else: for bot in mytanks: values = self.calculatePD(bot, step) command = Command(bot.index, values[0], values[1], True) self.commands.append(command) # Send the commands to the server results = self.bzrc.do_commands(self.commands) def tickOne(self, step): '''Some time has passed; decide what to do next''' # Get information from the BZRC server mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.mytanks = mytanks self.othertanks = othertanks self.flags = flags self.shots = shots self.enemies = [tank for tank in othertanks if tank.color != self.constants['team']] # Reset my set of commands (we don't want to run old commands) self.commands = [] # Visualize potential field if self.vizualize: self.field.drawFast(True, True, True) self.vizualize = False print 'Done with visualization' # Which tank do you want to control bot = mytanks[0] #print bot.flag # Check to see if the flag has been captured if not self.gotFlag and bot.flag is not '-': print 'set home' self.gotFlag = True self.flipFieldToHome() if self.gotFlag and bot.flag is '-': self.gotFlag = False self.flipFieldToEnemy() # Decide what to do with one tank of my tanks values = self.calculatePD(bot, step) if (time.time() - self.stuckTimer) > 3: if bot.x == self.prevLocation[0] and bot.y == self.prevLocation[1]: print 'stuck' self.stuck = True else: self.stuck = False self.prevLocation = [bot.x, bot.y] self.stuckTimer = time.time() if self.stuck: command = Command(bot.index, 1, 1, True) self.commands.append(command) else: command = Command(bot.index, values[0], values[1], True) self.commands.append(command) # Send the commands to the server results = self.bzrc.do_commands(self.commands) def calculatePD(self, bot, step): # Get the potential field magnitudes in the x and y at the current position pf = self.field.getFast(bot.x, bot.y) # Tune these or set to zero to manipulate the PD controller kProportion = .5 kDerivative = .002 # Calculate values used in the PD controller formula angleReference = numpy.arctan2(pf[1], pf[0]) angle = angleReference - bot.angle errorAtStep = numpy.arctan2(math.sin(angle),math.cos(angle)) errorPrevStep = 0.0 # Previous error is zero if this is the first calculation. # Update the previous error to the last saved error if there is one if bot.index in self.prevError: errorPrevStep = self.prevError[bot.index] # This is the PD formula angularAtStep = (kProportion * errorAtStep) + (kDerivative * ((errorAtStep - errorPrevStep) / step)) #print angularAtStep if angularAtStep > 1: angularAtStep = 1 if angularAtStep < -1: angularAtStep = -1 #print errorAtStep, angularAtStep, pf # Calculates the speed based off the magnitude of the potential field vector speedAtStep = 1 - .7 * math.fabs(angularAtStep) #math.sqrt(pf[0]**2 + pf[1]**2) #print speedAtStep # Save the new error to use in the next iteration self.prevError[bot.index] = errorAtStep return [speedAtStep, angularAtStep] def flipFieldToHome(self): self.field.goToHome(True) #self.field.drawFast(True, True, True) def flipFieldToEnemy(self): self.field.goToHome(False)
poke1.base_stats.i_spd = 236 poke1.base_stats.i_spe = 259 poke1.usable_stats = poke1.base_stats poke1.type_1 = Type("psychic") poke1.type_2 = None poke1.str_ability = "contrary" #poke1.str_status = 'burn' poke2 = Pokeman() poke2.base_stats.i_atk = 212 poke2.base_stats.i_def = 236 poke2.base_stats.i_hp = 341 poke2.base_stats.i_spa = 236 poke2.base_stats.i_spd = 236 poke2.base_stats.i_spe = 259 poke2.usable_stats = poke2.base_stats poke2.type_1 = Type("psychic") poke2.type_2 = None poke2.str_ability = "competitive" move = Move("feather-dance") field = Field() #field.terrain = Terrain.ELECTRIC print(attack(poke1, poke2, move, field)) print(str_prv_mov) #move = Move("surf") #print(attack(poke2, poke1, move, field)) #print(str_prv_mov)
print(np_action) #act_value = self.model.predict(np_action) #if act_value > max_act_value: # best_actions = [a,] # max_act_value = act_value #elif act_value == max_act_value: # best_actions.append(a) #return random.choice(best_actions) size = 10 barriar_rate = 0.1 maze_1 = Maze(size, barriar_rate) maze, start_point, goal_point = maze_1.generate_maze() maze_field = Field(maze, start_point, goal_point) maze_field.display() print("start end point") print(start_point, goal_point) action = maze_field.get_actions(state=start_point) print("action") print(action) #myAgent = AgentClass(2,3) state = np.array(start_point) #print(state) print("state, action 0") print(np.array( [[state,action[0]]] ) )