Exemplo n.º 1
0
    def test_add_squid(self):
        l.info('test_add_squid')

        sea = Sea(options)

        for i in range(5, 9):
            self.assertTrue(len(sea.list_things_at((i, 2))) == 1)
            self.assertTrue(len(sea.list_things_at((i, 5))) == 1)

        for i in range(0, 50):
            self.assertTrue(isinstance(
                sea.list_things_at((i, 3))[0], Obstacle))
Exemplo n.º 2
0
def run_trial(wss=None, steps=None, seed=None):
    steps = int(steps) if steps else 10
    l.debug('Running mom_and_calf in', str(steps), 'steps with seed', seed)

    random.seed(seed)

    options = OPTIONS
    options.wss = wss
    sea = Sea(options)

    mom = Mom(options.objectives)
    calf = Calf(options.objectives)

    sea.add_thing(mom, mom_start_pos)
    sea.add_thing(calf, calf_start_pos)

    sea.run(steps)

    for status in ['energy']:
        l.debug('----- ' + status + '------')

        U, pi = mom.q_agent.Q_to_U_and_pi()[status]
        l.debug('mom - pi:', pi, ', U:', U)

        U, pi = calf.q_agent.Q_to_U_and_pi()[status]
        l.debug('calf - pi:', pi, ', U:', U)

    return ({'name': mom.__name__, 'iterations': mom.q_agent.iterations, 'U_and_pi': mom.q_agent.Q_to_U_and_pi()},
            {'name': calf.__name__, 'iterations': calf.q_agent.iterations, 'U_and_pi': calf.q_agent.Q_to_U_and_pi()})
Exemplo n.º 3
0
    def start_game(self):
        self.player1.sea = Sea.blank()
        self.player2.sea = Sea.blank()

        try:
            self.player1.sea = Sea.blank()
            self.player1.sea = Sea(self.player1.place_ships())
        except ValidationError as e:
            self.player2.games_won += 1
            self.print_game(self.player2, e.message)
            return False

        try:
            self.player2.sea = Sea(self.player2.place_ships())
        except ValidationError as e:
            self.player1.games_won += 1
            self.print_game(self.player1, e.message)
            return False

        return True
Exemplo n.º 4
0
    def test_singing_cachalot(self):
        l.info('test_singing_cachalot')

        e = Sea(options)
        a = Agent(None, 'cachelot')
        e.add_thing(a, (1, 1))

        # song at time=1 will be heard by other agents at time=2
        e.execute_ns_action(a, 'sing', 1)
        self.assertTrue(len(e.list_ns_artifacts_at(2)) == 1)
Exemplo n.º 5
0
def run(wss=None, steps=None, seed=None):
    l.debug('Running random_mom_and_calf in', str(steps), 'steps with seed', seed)
    steps = int(steps) if steps else 10

    random.seed(seed)

    options = OPTIONS
    options.wss = wss
    sea = Sea(options)

    mom = Agent(mom_program, 'mom')
    calf = Agent(calf_program, 'calf')

    sea.add_thing(mom, mom_start_pos)
    sea.add_thing(calf, calf_start_pos)

    sea.run(steps)
Exemplo n.º 6
0
def run(wss=None, steps=None, seed=None):
    steps = int(steps) if steps else 10
    l.debug('Running random_mom_and_calf in', str(steps), 'steps with seed',
            seed)

    random.seed(seed)

    options = OPTIONS
    options.wss = wss
    sea = Sea(options)

    mom = Mom()
    calf = Calf()

    sea.add_thing(mom, mom_start_pos)
    sea.add_thing(calf, calf_start_pos)

    sea.run(steps)
Exemplo n.º 7
0
 def test_get_tack(self, heading, expected_tack):
     sea = Sea(0.0)
     sailboat = Sailboat(heading)
     sailboat.set_sail(sea)
     self.assertEqual(sailboat.get_tack(), expected_tack)
Exemplo n.º 8
0
 def test_get_wind_heading_after_launch(self):
     sea = Sea(0.0)
     sailboat = Sailboat(0.0)
     sailboat.set_sail(sea)
     self.assertEqual(sailboat.get_wind_direction(), 0.0)
Exemplo n.º 9
0
 def test_get_wind_heading_exception(self):
     sea = Sea(0.0)
     sailboat = Sailboat(0.0)
     self.assertRaises(ValueError, sailboat.get_wind_direction)
Exemplo n.º 10
0
    def test_moving_cachalot(self):
        l.info('test_moving_cachalot')

        e = Sea(options)
        a = Agent(None, 'cachelot')
        e.add_thing(a, (1, 1))

        self.assertTrue(a.location == (1, 1))
        e.execute_action(a, 'dive_and_forward', 1)
        self.assertTrue(a.location == (2, 2))

        # Should hit the wall
        e.execute_action(a, 'dive_and_forward', 1)
        self.assertTrue(a.location == (3, 2))

        e.execute_action(a, 'forward', 1)
        self.assertTrue(a.location == (4, 2))

        e.execute_action(a, 'up_and_forward', 1)
        self.assertTrue(a.location == (5, 1))

        # check that the world is torus, should get back to the same location
        for _ in range(0, 50):
            e.execute_action(a, 'forward', 1)

        self.assertTrue(a.location == (5, 1))
Exemplo n.º 11
0
from sea import Sea
from ship import Ship
from command import Command

print("Welcome to BattleShips Game!\n")
print("Commands information:\n")
print(
    "place: place ship to sea. example place v,1,3\nshow ships: show ships on the sea.\nexit battleships: exit the game\n")

# create commands for battleships game
commands = Command()

# create sea size of 5x5
seaOfGame = Sea()

isFinished = False

# loop continue when isFinished equal to false
while isFinished == False:
    command = input()
    # show ships command for show all ships on the sea
    if command == commands.command[0]:
        seaOfGame.showShips()
    # exit battleships command for exit game
    elif command == commands.command[1]:
        isFinished = True
    # size 2 for split " ", and size 3 for split of first split's for "," example that place v,1,3
    elif len(command.split(" ")) == 2 and len(command.split(" ")[1].split(",")) == 3:
        # first element command name
        commandName = command.split(" ")[0]
        # second element position type