Ejemplo n.º 1
0
    def test_lizard(self):
        state = setup_state()
        state.player.lizards = 1
        state.opponent.lizards = 1
        cmd = Cmd.LIZARD

        nstate = next_state(state, cmd, cmd)
        for prev, cur in zip([state.player, state.opponent],
                             [nstate.player, nstate.opponent]):
            assert cur.y == prev.y
            assert cur.x - prev.x == prev.speed
            assert cur.speed == prev.speed

        for player in [state.player, state.opponent]:
            for x in range(1, player.speed):
                state.map[player.x + x, player.y] = Block.MUD

        nstate = next_state(state, cmd, cmd)
        for prev, cur in zip([state.player, state.opponent],
                             [nstate.player, nstate.opponent]):
            assert cur.y == prev.y
            assert cur.x - prev.x == prev.speed
            assert cur.speed == prev.speed

        for player in [state.player, state.opponent]:
            state.map[player.x + player.speed, player.y] = Block.MUD

        nstate = next_state(state, cmd, cmd)
        for prev, cur in zip([state.player, state.opponent],
                             [nstate.player, nstate.opponent]):
            assert cur.y == prev.y
            assert cur.x - prev.x == prev.speed
            assert cur.speed == prev_speed(prev.speed)
Ejemplo n.º 2
0
    def test_decel(self):
        state = setup_state()
        cmd = Cmd.DECEL

        nstate = next_state(state, cmd, cmd)
        for prev, cur in zip([state.player, state.opponent],
                             [nstate.player, nstate.opponent]):
            assert cur.y == prev.y
            assert cur.x - prev.x == prev_speed(prev.speed)
            assert cur.speed == prev_speed(prev.speed)

        for player in [state.player, state.opponent]:
            state.map[player.x + 1, player.y] = Block.MUD

        nstate = next_state(state, Cmd.DECEL, Cmd.DECEL)
        for prev, cur in zip([state.player, state.opponent],
                             [nstate.player, nstate.opponent]):
            assert cur.y == prev.y
            assert cur.x - prev.x == prev_speed(prev.speed)
            assert cur.speed == prev_speed(prev_speed(prev.speed))
Ejemplo n.º 3
0
    def test_hit_oil_spill(self):
        state = setup_state()
        cmd = Cmd.NOP
        block = Block.OIL_SPILL

        for player in [state.player, state.opponent]:
            state.map[player.x + 1, player.y] = block

        nstate = next_state(state, cmd, cmd)
        for prev, cur in zip([state.player, state.opponent],
                             [nstate.player, nstate.opponent]):
            assert cur.y == prev.y
            assert cur.x - prev.x == prev.speed
            assert cur.speed == prev_speed(prev.speed)
            assert cur.score - prev.score == -4
            assert nstate.map[prev.x + 1, prev.y] == block
            assert prev.damage == 0
            assert cur.damage == 1
Ejemplo n.º 4
0
    def test_right(self):
        state = setup_state()
        state.opponent.y = 3
        cmd = Cmd.RIGHT

        nstate = next_state(state, cmd, cmd)
        for prev, cur in zip([state.player, state.opponent],
                             [nstate.player, nstate.opponent]):
            assert cur.y == prev.y + 1
            assert cur.x - prev.x == prev.speed - 1
            assert cur.speed == prev.speed

        for player in [state.player, state.opponent]:
            state.map[player.x, player.y + 1] = Block.MUD

        nstate = next_state(state, cmd, cmd)
        for prev, cur in zip([state.player, state.opponent],
                             [nstate.player, nstate.opponent]):
            assert cur.y == prev.y + 1
            assert cur.x - prev.x == prev.speed - 1
            assert cur.speed == prev_speed(prev.speed)
Ejemplo n.º 5
0
def calc_opp_cmd(cmd, from_state, to_state):
    # no way to know what they were going to do
    if cmd == Cmd.EMP:
        return None

    cmd = ns_filter(cmd)

    x, y = from_state.opponent.x, from_state.opponent.y

    # if their boost counter was one their effective speed was actually 9
    if from_state.opponent.boost_counter == 1:
        from_state.opponent.speed = max_speed(from_state.opponent.damage)
    speed = from_state.opponent.speed

    fx, fy = to_state.opponent.x, to_state.opponent.y
    fspeed = to_state.opponent.speed

    x_off = fx - x
    y_off = fy - y

    # go through all the valid actions that they could've taken and check if
    # the next state matches their actual state
    for opp_cmd in valid_actions(from_state.switch()):
        nstate = next_state(from_state, cmd, opp_cmd)
        if ((nstate.opponent.x, nstate.opponent.y,
             nstate.opponent.speed) == (fx, fy, fspeed)):
            return opp_cmd

    # above didn't work, so try figure out what they did based on some
    # rudimentary rules

    if y_off:
        return Cmd.LEFT if y_off < 0 else Cmd.RIGHT

    if x_off == y_off == 0 and fspeed == speed:
        return Cmd.FIX

    if x_off > speed:
        if x_off <= next_speed(speed):
            return Cmd.ACCEL
        else:
            return Cmd.BOOST

    if x_off == prev_speed(speed):
        return Cmd.DECEL

    if x_off == speed:
        _y = fy
        _speed = speed
        start_x = x if y_off else x + 1

        for _x in range(start_x, fx + 1):
            block = from_state.map[_x, _y]

            if block == Block.MUD:
                _speed = prev_speed(_speed)
            elif block == Block.OIL_SPILL:
                _speed = prev_speed(_speed)
            elif block == Block.WALL:
                _speed = Speed.SPEED_1.value

        _speed = max(Speed.SPEED_1.value, _speed)
        return Cmd.LIZARD if _speed < fspeed else Cmd.NOP

    return None
Ejemplo n.º 6
0
 def prev_speed(self, min_stop=False):
     if min_stop:
         if self.speed > Speed.SPEED_1.value:
             self.speed = prev_speed(self.speed, self.damage)
     else:
         self.speed = prev_speed(self.speed, self.damage)