Пример #1
0
def testManualGameImageOutput1():
    print("\n== init manual setting game ==")
    height = 10
    width = 10
    game = Game(height, width)

    obstacles = [{"x": 1, "y": 1}, {"x": 2, "y": 2}, {"x": 3, "y": 3}]
    targets = [{"x": 4, "y": 4}, {"x": 5, "y": 5}, {"x": 6, "y": 6}]
    game.setObstacles(obstacles)
    game.setTargets(targets)
    #game.setScore(100, 10, -0.01, -100)

    game.printGodMap()

    agents = {
        0: Agent(0, 7, 6, height, width, r=3),  # id, x, y, height, width
    }
    game.setAgents(agents)

    game.printConsoleMap()

    print("\n== 1st round ==")
    game.runOneRound([Command(0, -1, -1)])  # (6, 5)
    game.printConsoleMap()

    print("\n== 2st round ==")
    game.runOneRound([Command(0, -1, 0)])  # (5, 5)
    game.printConsoleMap()

    print("\n== 3st round ==")
    game.runOneRound([Command(0, -1, 0)])  #(4, 5)
    game.printConsoleMap()
Пример #2
0
def manualGame():
    print("\n== init manual setting game ==")
    height = 20
    width = 20
    game = Game(height, width)

    obstacles = [{"x": 1, "y": 1}, {"x": 2, "y": 2}, {"x": 3, "y": 3}]
    targets = [{"x": 10, "y": 10}, {"x": 12, "y": 12}, {"x": 13, "y": 13}]
    game.setObstacles(obstacles)
    game.setTargets(targets)

    agents = {
        0: Agent(0, 0, 0, height, width),  # id, x, y, height, width
        1: Agent(1, 14, 14, height, width),
        2: Agent(2, 15, 15, height, width),
    }
    game.setAgents(agents)
    game.setScore(100, 20, 10, -0.0005, 0, -20)

    game.printGodInfo()
    print("Score: " + str(game.outputScore()))

    print("\n== 1st round ==")
    commands = []
    commands.append(Command(0, 0, 1))  # id, dx, dy
    commands.append(Command(1, -1, 1))
    commands.append(Command(2, 1, -1))

    game.runOneRound(commands)

    game.printConsoleInfo()
    print("Score: " + str(game.outputScore()))

    print("\n== 2ed round ==")
    commands = []
    commands.append(Command(0, 1, 1))
    commands.append(Command(1, -1, 1))
    commands.append(Command(2, 1, -1))

    game.runOneRound(commands)

    game.printConsoleInfo()
    print("Score: " + str(game.outputScore()))

    print("\n== 3ed round ==")
    commands = []
    commands.append(Command(0, 1, 1))
    commands.append(Command(1, -1, 1))
    commands.append(Command(2, -1, 0))

    game.runOneRound(commands)

    game.printConsoleInfo()
    print("Score: " + str(game.outputScore()))
Пример #3
0
def testManualGameImageOutput2():
    print("\n== init manual setting game ==")
    height = 20
    width = 20
    mode = {0: False, 1: False, 2: False}
    #now target = {0:{},1:{},2:{}}
    belongs = {0: [], 1: [], 2: []}
    game = Game(height, width)

    game.setRandomMap(0, 50, 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)
    #agents[id].x,game.consolemap.targets
    game.printConsoleMap()
    game.runOneRound([Command(0, 1, 1), Command(1, -1, 1), Command(2, -1, -1)])
    for item in game.consolemap.targets:
        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)
    print(belongs)
    for i in range(3):
        if mode[i] == False:
            target_find = belongs[i][0]
            for target_list in belongs[i]:
                if target_agent_len(target_list, agents[i]) < target_agent_len(
                        target_find, agents[i]):
                    target_find = target_list
            print(target_find)
            mode[i] = True

    game.printConsoleMap()
    '''for i in range(0, 10):
Пример #4
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")