def interactive(ply): g = Game() mode, end = g.game_type() end = int(end) if mode: # mode is true means new game player_x = Player("x") player_y = Player("y") b = Board(player_x, player_y) ai = Ai(ply) remain = ["PlayerX King", "PlayerX Rook", "PlayerY King"] g.ask_piece(b, player_x, player_y, remain) b.display() File.prompt("Who am I, PlayerX or PlayerY?") localPlayer = input("Player [x/y]: ") if re.match(r"[Xx]", localPlayer): localPlayer = "x" else: localPlayer = "y" # if local player is playerX, PlayerX is our ai moves # PlayerY is opponents moves inputted by us if localPlayer == "x": for i in range(0, end): # b.ai_move(player_x) ai.move(b, player_x) b.display() File.debug(ai.value(b)) File.debug(ai.number_of_states) ai.opponent_move(player_y, b) b.display() else: for i in range(0, end): ai.opponent_move(player_x, b) b.display() # b.ai_move(player_y) ai.move(b, player_y) b.display() File.debug(ai.value(b)) File.debug(ai.number_of_states) else: player_x = Player("x") player_y = Player("y") b = Board(player_x, player_y) File.test_file(b, g, player_x, player_y) ai = Ai(ply) # AI random moves test: for i in range(0, end): ai.move(b, player_x) b.display() File.debug(ai.value(b)) File.debug(ai.number_of_states) ai.move(b, player_y) b.display() File.debug(ai.value(b)) File.debug(ai.number_of_states)
def test2(ply): player_x = Player("x") player_y = Player("y") king_x = Piece(player_x, "K", 3, 5) rook_x = Piece(player_x, "R", 5, 7) king_y = Piece(player_y, "K", 4, 3) player_x.add_piece(rook_x) player_x.add_piece(king_x) player_y.add_piece(king_y) b = Board(player_x, player_y) b.display() ai = Ai(ply) for i in range(35): ai.move(b, player_x) b.display() ai.move(b, player_y) b.display() # row, col = input('Row Col:').split() # b.player_move(player_y, king_y, row, col) # b.display() # ai.move(b, player_x) # b.display() # b.player_move(player_y, 'K', 5, 5) # ai.create_tree(b, player_x) # ai.move(b, player_x) # b.display() # ai.create_tree(b, player_y) # ai.display_tree(ai.root_node) # print(ai.value(b)) # ai.bfs() print(ai.number_of_states)
async def agent_loop(server_address="localhost:8000", agent_name="student"): async with websockets.connect( f"ws://{server_address}/player") as websocket: # Receive information about static game properties await websocket.send(json.dumps({"cmd": "join", "name": agent_name})) msg = await websocket.recv() game_properties = json.loads(msg) # You can create your own map representation or use the game representation: mapa = Map(size=game_properties["size"], mapa=game_properties["map"]) agent = Ai() agent.map = mapa agent.xmap = len(mapa.map) agent.ymap = len(mapa.map[0]) mapa_inicial = [] for i in range(agent.xmap): mapa_inicial = mapa_inicial + [[0] * agent.ymap] for i in range(agent.xmap): for j in range(agent.ymap): if agent.map.map[i][j] != 2: mapa_inicial[i][j] = agent.map.map[i][j] else: agent.map.map[i][j] = 0 i = 0 while True: try: state = json.loads( await websocket.recv() ) # receive game state, this must be called timely or your game will get out of sync with the server agent.update(state, mapa) agent.update_map(state, mapa_inicial) if agent.action != "Perseguir" and agent.action != "Fugir": if agent.cb: if len(agent.bombs) == 0: agent.cb = False agent.action = "Fugir" agent.think() else: if agent.enemysAlive() and agent.enemysInRange(): agent.action = "Hunt" agent.think() else: #if agent.existItem() and not (agent.level ==3 or agent.level ==8): if agent.existItem() and agent.needItem(): agent.action = "GetItem" agent.think() else: if (agent.level == 4 or agent.level == 3 or agent.level == 2): if not agent.enemysAlive(): if len(agent.exit) != 0 and ( agent.powersTest[agent.level - 1] or (not agent.powersTest[agent.level - 1] and agent.needItem())): agent.action = "Exit" agent.think() else: agent.action = "Wall" agent.think() else: agent.next = agent.getEnemy() agent.action = "Assassin" agent.think() elif agent.level == 1: if len(agent.exit) != 0 and ( agent.powersTest[agent.level - 1] or (not agent.powersTest[agent.level - 1] and agent.needItem())): if not agent.enemysAlive(): agent.action = "Exit" agent.think() else: agent.next = agent.getStrongEnemy() if agent.next is None: agent.action = "Porco" agent.think() else: agent.action = "Assassin" agent.think() else: agent.action = "Wall" agent.think() else: if not agent.enemysAlive(): if (agent.level == 15): return 0 if len(agent.exit) != 0 and ( agent.powersTest[agent.level - 1] or (not agent.powersTest[agent.level - 1] and agent.needItem())): agent.action = "Exit" agent.think() else: agent.action = "Wall" agent.think() else: agent.next = agent.getStrongEnemy() if agent.next is None: agent.action = "Porco" agent.think() else: agent.action = "Assassin" agent.think() #print(agent.action) #print(agent.moves) #print(agent.powersTest) if agent.action == "Perseguir": if agent.map2[agent.pos[0]][ agent.pos[1]] == 2 or agent.map2[agent.pos[0]][ agent.pos[1]] == 3 and not agent.override: agent.moves = [] await websocket.send( json.dumps({ "cmd": "key", "key": "B" }) ) # send key command to server - you must implement this send in the AI agent agent.cb = True agent.action = "" elif len(agent.moves) != 0: key = agent.move() if key == "B" and not agent.wallInRange: agent.action = "" else: await websocket.send( json.dumps({ "cmd": "key", "key": key }) ) # send key command to server - you must implement this send in the AI agent if key == "B": agent.cb = True agent.action = "" if len(agent.moves) == 0: agent.action = "" else: agent.override = False agent.action = "" elif agent.action == "Fugir": #if agent.map2[agent.pos[0]][agent.pos[1]] == 2 or agent.map2[agent.pos[0]][agent.pos[1]] == 3: # agent.moves.queue.clear() # agent.think() if len(agent.moves) != 0: if agent.moveIsSafe(): if len(agent.moves) != 0: key = agent.move() if key == 'A' and not agent.isSafe(): agent.moves = [] agent.action = "" else: await websocket.send( json.dumps({ "cmd": "key", "key": key }) ) # send key command to server - you must implement this send in the AI agent if len(agent.moves) == 0: agent.action = "" agent.cb = False else: agent.action = "" else: agent.moves = [] agent.think() agent.cb = False else: agent.action = "" if len(agent.bombs) == 0: agent.cb = False except websockets.exceptions.ConnectionClosedOK: print("Server has cleanly disconnected us") return