コード例 #1
0
ファイル: test_game_alg.py プロジェクト: yoshonabee/Wall-Es
def testManualGameImageOutput2():
    print("\n== init manual setting game ==")
    height = 50
    width = 50
    crash = 0

    game = Game(height, width)

    game.setRandomMap(8, 300, 200)  # numbers of agents, targets, obstacles

    game.printGodMap()
    #agents = game.godmap.agents
    '''agents = {
        0: Agent(0, 0, 0, height, width, r=5), # id, x, y, height, width
        1: Agent(1, width-1 , 0, height, width, r=5), # id, x, y, height, width
        2: Agent(2, 0 , height-1 , height, width, r=5), # id, x, y, height, width
        #3: Agent(3, width - 1, height- 1, height, width, r = 5)
    }'''

    #game.setAgents(agents)
    ##########
    game.runOneRoundwithoutMovement()
    game.printConsoleMap()
    round = 1
    while (game.algNext(crash, round)):
        round += 1
コード例 #2
0
def newGame():
    global game

    height = int(request.args.get("height"))
    width = int(request.args.get("width"))
    t = (height + width) / 2
    game = Game(height, width)
    game.setRandomMap(5, int(t * 0.4)**2, int(t * 0.3)**2)
    game.setScore()
    game.printGodMap()
    game.runOneRoundwithoutMovement()
    game.printConsoleMap()
    return game.jsonMap()
コード例 #3
0
def testManualGameImageOutput2():
    print("\n== init manual setting game ==")
    height = 40
    width = 40
    crash = 0

    mode = {
        0: False,
        1: False,
        2: False
    }  #agents' mode True if agent has target rightnow
    found_target = []
    now_target = {0: [], 1: [], 2: []}
    belongs = {0: [], 1: [], 2: []}
    cmd = []

    game = Game(height, width)

    game.setRandomMap(0, 200, 0)  # numbers of agents, targets, obstacles
    #game.setScore(100, 10, -0.01, -100)

    game.printGodMap()

    agents = {
        0: Agent(0, 0, 0, height, width, r=5),  # id, x, y, height, width
        1: Agent(1, width - 1, 0, height, width,
                 r=5),  # id, x, y, height, width
        2: Agent(2, int(width / 2), height - 1, height, width,
                 r=5),  # id, x, y, height, width
    }

    game.setAgents(agents)
    ##########
    game.runOneRoundwithoutMovement()
    game.printConsoleMap()
    round = 1

    while (game.consolemap.targets != []
           or haveunseenspace(game.consolemap.areas, height, width)):
        print("====the %d round" % round)

        found_target = game.consolemap.targets
        #print("found:",found_target)
        #print("agent mode",mode)

        for item in found_target:  # cluster the target
            index = 0
            if target_agent_len(item, agents[index]) > target_agent_len(
                    item, agents[1]):
                index = 1
            if target_agent_len(item, agents[index]) > target_agent_len(
                    item, agents[2]):
                index = 2
            belongs[index].append(item)

        cmd = []  # store the new command for agents

        for i in agents:

            if mode[i] == False:  # assign a target to agent
                now_target[i] = []
                for target_list in belongs[i]:
                    if now_target[i] == [] and target_list != []:
                        now_target[i] = target_list
                        mode[i] = True
                    else:
                        if target_agent_len(target_list,
                                            agents[i]) < target_agent_len(
                                                now_target[i], agents[i]):
                            now_target[i] = target_list

            no_target_command = {
                0: {
                    "dx": 0,
                    "dy": 0
                },
                1: {
                    "dx": 1,
                    "dy": -1
                },
                2: {
                    "dx": -1,
                    "dy": -1
                },
                3: {
                    "dx": -1,
                    "dy": 1
                },
                4: {
                    "dx": 1,
                    "dy": 1
                },
            }

            if mode[i] == False:  # assign the cammand to agent
                direction = no_target_walk(game.getmap(), agents[i])
                cmd.append(
                    Command(agents[i].id, no_target_command[direction]["dx"],
                            no_target_command[direction]["dy"]))
                print("agent %d goes" % agents[i].id,
                      no_target_command[direction]["dx"], "and",
                      no_target_command[direction]["dy"])
            elif mode[i] == True:
                cmd.append(walk(now_target[i], agents[i]))
                mode[i] = False

        game.runOneRound(cmd)

        for i in agents:  #calculate crash time
            for j in range(i + 1, len(agents)):
                if agents[i].x == agents[j].x and agents[i].y == agents[j].y:
                    crash += 1
        print(found_target)

        game.printConsoleMap()
        belongs = {0: [], 1: [], 2: []}
        round += 1
    print("crush time: %d" % crash)
    print("finish")