Example #1
0
class Viewer:
    def __init__(self, bot1, bot2, vbm):
        self.game = Game(bot1, bot2)
        self.vbm = vbm

    def step(self):
        inp_sh_p0, inp_sy_p0, inp_sh_p1, inp_sy_p1 = get_nn_input(self.game)

        proba0 = self.game.players[0].agent.compute_actions_proba(
            (inp_sh_p0, inp_sy_p0))
        proba1 = self.game.players[1].agent.compute_actions_proba(
            (inp_sh_p1, inp_sy_p1))

        self.vbm.flush()

        actions_list0 = self.game.players[0].agent.sample_actions(proba0)
        actions0 = compute_action_dict(actions_list0, self.game.players[0])
        actions_list1 = self.game.players[1].agent.sample_actions(proba1)
        actions1 = compute_action_dict(actions_list1, self.game.players[1])

        self.game._step(actions0, actions1)

    def view(self):
        inp_sh_p0, inp_sy_p0, _, _ = get_nn_input(self.game)
        inp = tf.concat([inp_sh_p0, inp_sy_p0], axis=0)[0]
        for i in range(7):
            plt.subplot(self.game.length + 1, 7, self.game.nb_step * 7 + i + 1)
            if i == 0:
                plt.text(-10,
                         2,
                         str(self.game.players[0].halite)[:4] + "-" +
                         str(self.game.players[1].halite)[:4],
                         fontsize=10,
                         color='black')  #, backgroundcolor = 'white')
            plt.imshow(inp[:, :, i])
            plt.xticks([])
            plt.yticks([])

    def play(self):
        self.view()
        for i in range(self.game.length):
            self.step()
            self.view()
        plt.show()
Example #2
0
def test__step():
    game = Game(wait_agent, wait_agent)

    p0 = game.players[0]
    p1 = game.players[1]

    s1 = Ship(15, 15, p0)  #collect
    game.set_halite(15, 15, 1)
    s1.halite = 10
    p0.add_ship(s1)
    s2 = Ship(16, 15, p1)  #move left
    s2.halite = 10
    p1.add_ship(s2)

    s3 = Ship(9, 7, p0)  #move up
    p0.add_ship(s3)

    s4 = Ship(10, 11, p1)  #collect
    game.set_halite(10, 11, 100)
    p1.add_ship(s4)

    s5 = Ship(16, 9, p0)  #move right and deposit
    s5.halite = 20
    p0.add_ship(s5)

    s6 = Ship(7, 7, p1)  #convert
    game.set_halite(7, 7, 20)
    s6.halite = 700
    p1.add_ship(s6)

    s7 = Ship(7, 8, p0)  #Move up
    p0.add_ship(s7)

    sy1 = Shipyard(17, 9, p0)
    p0.add_shipyard(sy1)

    sy2 = Shipyard(4, 5, p1)  #spawn
    p1.add_shipyard(sy2)

    actions0 = actions_dict()
    actions0[ShipMove.UP] = [s3, s7]
    actions0[ShipMove.RIGHT] = [s5]
    actions0[ShipMove.COLLECT] = [s1]

    actions1 = actions_dict()
    actions1[ShipyardMove.SPAWN] = [sy2]
    actions1[ShipMove.CONVERT] = [s6]
    actions1[ShipMove.LEFT] = [s2]
    actions1[ShipMove.COLLECT] = [s4]

    game._step(actions0, actions1)

    assert p0.ship_array[15][15] == []
    assert p1.ship_array[15][15] == []
    assert game.get_halite(15, 15) == 21 * 1.02

    assert p0.ship_array[7][9] == []
    assert p0.ship_array[6][9] == [s3]

    assert s4.halite == 25
    assert game.get_halite(10, 11) == 75 * 1.02

    assert s5.halite == 0
    assert p0.halite == 5020

    assert p1.halite == 5000 - 500 + 200
    assert p1.shipyard_array[7][7] == 0
    assert p0.ship_array[7][7] == [s7]
    assert game.get_halite(7, 7) == 0

    assert p1.ship_array[5][4] != []