Messagehhjjjjhgffg

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

from typing import Union

from game_state import GameState


import asyncio
import random
import os
import time

uri = os.environ.get(
'GAME_CONNECTION_STRING') or "ws://127.0.0.1:3000/?
role=agent&agentId=agentId&name=defaultName"

actions = ["up", "down", "left", "right", "bomb", "detonate"]

MAP_SIZE_X=15
MAP_SIZE_Y=15

class Agent():

def __init__(self):
print("Inicializando Agente")
self._map = [['' for x in range(MAP_SIZE_X)] for y in range(MAP_SIZE_Y)]
self._my_army= {}
self._other_army={}
self._client = GameState(uri)

# any initialization code can go here


self._client.set_game_tick_callback(self._on_game_tick)

loop = asyncio.get_event_loop()
connection = loop.run_until_complete(self._client.connect())
tasks = [
asyncio.ensure_future(self._client._handle_messages(connection)),
]
loop.run_until_complete(asyncio.wait(tasks))

# returns coordinates of the first bomb placed by a unit


def _get_bomb_to_detonate(self, unit) -> Union[int, int] or None:
entities = self._client._state.get("entities")
bombs = list(filter(lambda entity: entity.get(
"unit_id") == unit and entity.get("type") == "b", entities))
bomb = next(iter(bombs or []), None)
if bomb != None:
return [bomb.get("x"), bomb.get("y")]
else:
return None

async def _on_game_tick(self, tick_number, game_state):


print("Versión 0.0.3")

# get my units
my_agent_id = game_state.get("connection").get("agent_id")
my_units = game_state.get("agents").get(my_agent_id).get("unit_ids")
self.update_data(game_state)
self.print_map()
# send each unit a random action
for unit_id in my_units:
#action = random.choice(actions)
#
#if action in ["up", "left", "right", "down"]:
# await self._client.send_move(action, unit_id)
#elif action == "bomb":
# await self._client.send_bomb(unit_id)
#elif action == "detonate":
# bomb_coordinates = self._get_bomb_to_detonate(unit_id)
# if bomb_coordinates != None:
# x, y = bomb_coordinates
# await self._client.send_detonate(x, y, unit_id)
#else:
# print(f"Unhandled action: {action} for unit {unit_id}")
print(f"Sin operaciones enviadas")

#actualiza la información interna del estado del mapa


def update_data(self, game_state):
my_agent_id = game_state.get("connection").get("agent_id")
entities = game_state.get("entities")
self._map = [['' for x in range(MAP_SIZE_X)] for y in range(MAP_SIZE_Y)]
for e in entities:
self._map[e['x']][e['y']]=e['type']

agents = game_state.get("agents")
army_1 = agents[list(agents.keys())[0]]
army_2 = agents[list(agents.keys())[1]]
if army_1['agent_id'] == my_agent_id:
self._my_army=army_1['unit_ids']
self._other_army=army_2['unit_ids']
else:
self._my_army=army_2['unit_ids']
self._other_army=army_1['unit_ids']
for unit in self._my_army:
info = self.get_unit_info(game_state, unit)
c = info['coordinates']
if info['hp']>0:
self._map[c[0]][c[1]]=(""+unit).upper()

for unit in self._other_army:


info = self.get_unit_info(game_state,unit)
c = info['coordinates']
if info['hp']>0:
self._map[c[0]][c[1]]=(""+unit).upper()

def get_unit_info(self,game_state,unit_id):
return game_state['unit_state'][unit_id]

# Método para pintar el map


def print_map(self):

for row in self._map:


s="|\t"
for e in row:
token = e if e!= '' else ' '
s=s+"\t"+token
s=s+"\t|"
print(s)
#print("|\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t
%s|"%
(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9],row[10],row[
11],row[12],row[13],row[14]))

def main():
for i in range(0,10):
while True:
try:
Agent()
except:
time.sleep(5)
continue
break

if __name__ == "__main__":
main()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy