Beispiel #1
0
    def test_simple_game(self):
        layout = """
        ##########
        #        #
        #0  ..  1#
        ##########
        """
        server = SimpleServer(
            layout_string=layout,
            rounds=5,
            players=2,
            bind_addrs=("ipc:///tmp/pelita-testplayer1-%s" % uuid.uuid4(),
                        "ipc:///tmp/pelita-testplayer2-%s" % uuid.uuid4()))

        for bind_address in server.bind_addresses:
            assert bind_address.startswith("ipc://")

        client1_address = server.bind_addresses[0]
        client2_address = server.bind_addresses[1]

        client1 = SimpleClient(SimpleTeam("team1", RandomPlayer()),
                               address=client1_address)
        client2 = SimpleClient(SimpleTeam("team2", RandomPlayer()),
                               address=client2_address)

        client1.autoplay_process()
        client2.autoplay_process()
        server.run()
        server.shutdown()
Beispiel #2
0
    def test_simple_failing_bots(self):
        layout = """
        ##########
        #        #
        #0  ..  1#
        ##########
        """
        server = SimpleServer(layout_string=layout, rounds=5, players=2)

        for bind_address in server.bind_addresses:
            assert bind_address.startswith("tcp://")

        client1_address = server.bind_addresses[0].replace("*", "localhost")
        client2_address = server.bind_addresses[1].replace("*", "localhost")

        class FailingPlayer:
            def _set_initial(self, dummy, dummy2):
                pass

            def _set_index(self, dummy):
                pass

            def _get_move(self, universe, game_state):
                pass

        client1 = SimpleClient(SimpleTeam("team1", SteppingPlayer("^>>v<")),
                               address=client1_address)
        client2 = SimpleClient(SimpleTeam("team2", FailingPlayer()),
                               address=client2_address)

        client1.autoplay_process()
        client2.autoplay_process()
        server.run()
        server.shutdown()
Beispiel #3
0
    def test_simple_failing_bots(self):
        layout = """
        ##########
        #        #
        #0  ..  1#
        ##########
        """
        server = SimpleServer(layout_string=layout, rounds=5, players=2)

        for bind_address in server.bind_addresses:
            assert bind_address.startswith("tcp://")

        client1_address = server.bind_addresses[0].replace("*", "localhost")
        client2_address = server.bind_addresses[1].replace("*", "localhost")

        class FailingPlayer:
            def _set_initial(self, dummy, dummy2):
                pass
            def _set_index(self, dummy):
                pass
            def _get_move(self, universe, game_state):
                pass

        client1 = SimpleClient(SimpleTeam("team1", SteppingPlayer("^>>v<")), address=client1_address)
        client2 = SimpleClient(SimpleTeam("team2", FailingPlayer()), address=client2_address)

        client1.autoplay_process()
        client2.autoplay_process()
        server.run()
        server.shutdown()
Beispiel #4
0
    def test_failing_bots_do_not_crash_server(self):
        layout = """
        ##########
        #        #
        #0  ..  1#
        ##########
        """
        server = SimpleServer(layout_string=layout, rounds=5, players=2, timeout_length=0.3)

        for bind_address in server.bind_addresses:
            assert bind_address.startswith("tcp://")

        client1_address = server.bind_addresses[0].replace("*", "localhost")
        client2_address = server.bind_addresses[1].replace("*", "localhost")

        class ThisIsAnExpectedException(Exception):
            pass

        class FailingPlayer(AbstractPlayer):
            def get_move(self):
                raise ThisIsAnExpectedException()
        old_timeout = pelita.simplesetup.DEAD_CONNECTION_TIMEOUT
        pelita.simplesetup.DEAD_CONNECTION_TIMEOUT = 0.3

        client1 = SimpleClient(SimpleTeam("team1", FailingPlayer()), address=client1_address)
        client2 = SimpleClient(SimpleTeam("team2", FailingPlayer()), address=client2_address)

        client1.autoplay_process()
        client2.autoplay_process()
        server.run()
        server.shutdown()

        pelita.simplesetup.DEAD_CONNECTION_TIMEOUT = old_timeout
Beispiel #5
0
    def test_simple_game(self):
        layout = """
        ##########
        #        #
        #0  ..  1#
        ##########
        """
        server = SimpleServer(
            layout_string=layout,
            rounds=5,
            players=2,
            bind_addrs=(
                "ipc:///tmp/pelita-testplayer1-%s" % uuid.uuid4(),
                "ipc:///tmp/pelita-testplayer2-%s" % uuid.uuid4(),
            ),
        )

        for bind_address in server.bind_addresses:
            self.assertTrue(bind_address.startswith("ipc://"))

        client1_address = server.bind_addresses[0]
        client2_address = server.bind_addresses[1]

        client1 = SimpleClient(SimpleTeam("team1", RandomPlayer()), address=client1_address)
        client2 = SimpleClient(SimpleTeam("team2", RandomPlayer()), address=client2_address)

        client1.autoplay_process()
        client2.autoplay_process()
        server.run()
        server.shutdown()
Beispiel #6
0
    def test_failing_bots_do_not_crash_server_in_set_initial(self):
        layout = """
        ##########
        #        #
        #0  ..  1#
        ##########
        """
        server = SimpleServer(layout_string=layout,
                              rounds=5,
                              players=2,
                              timeout_length=0.3)

        for bind_address in server.bind_addresses:
            assert bind_address.startswith("tcp://")

        client1_address = server.bind_addresses[0].replace("*", "localhost")
        client2_address = server.bind_addresses[1].replace("*", "localhost")

        class ThisIsAnExpectedException(Exception):
            pass

        class FailingPlayer(AbstractPlayer):
            def set_initial(self):
                raise ThisIsAnExpectedException()

            def get_move(self):
                raise ThisIsAnExpectedException()

        old_timeout = pelita.simplesetup.DEAD_CONNECTION_TIMEOUT
        pelita.simplesetup.DEAD_CONNECTION_TIMEOUT = 0.3

        client1 = SimpleClient(SimpleTeam("team1", FailingPlayer()),
                               address=client1_address)
        client2 = SimpleClient(SimpleTeam("team2", FailingPlayer()),
                               address=client2_address)

        client1.autoplay_process()
        client2.autoplay_process()
        server.run()
        server.shutdown()

        pelita.simplesetup.DEAD_CONNECTION_TIMEOUT = old_timeout
Beispiel #7
0
    def test_simple_remote_game(self):
        layout = """
        ##########
        #        #
        #0  ..  1#
        ##########
        """
        server = SimpleServer(layout_string=layout, rounds=5, players=2)

        for bind_address in server.bind_addresses:
            assert bind_address.startswith("tcp://")

        client1_address = server.bind_addresses[0].replace("*", "localhost")
        client2_address = server.bind_addresses[1].replace("*", "localhost")

        client1 = SimpleClient(SimpleTeam("team1", SteppingPlayer("^>>v<")),
                               address=client1_address)
        client2 = SimpleClient(SimpleTeam("team2", SteppingPlayer("^<<v>")),
                               address=client2_address)

        client1.autoplay_process()
        client2.autoplay_process()
        server.run()
        server.shutdown()
    def test_simple_remote_game(self):
        layout = """
        ##########
        #        #
        #0      1#
        ##########
        """
        client1 = SimpleClient(SimpleTeam("team1", RandomPlayer()), local=False)
        client2 = SimpleClient(SimpleTeam("team2", RandomPlayer()), local=False)
        server = SimpleServer(layout_string=layout, rounds=5, players=2, local=False)

        self.assertEqual(server.host, "")
        self.assertEqual(server.port, 50007)
        self.assertTrue(server.server.is_alive)

        client1.autoplay_background()
        client2.autoplay_background()
        server.run_simple(AsciiViewer)

        self.assertFalse(server.server.is_alive)
Beispiel #9
0
    def test_simple_remote_game(self):
        layout = """
        ##########
        #        #
        #0  ..  1#
        ##########
        """
        server = SimpleServer(layout_string=layout, rounds=5, players=2)

        for bind_address in server.bind_addresses:
            assert bind_address.startswith("tcp://")

        client1_address = server.bind_addresses[0].replace("*", "localhost")
        client2_address = server.bind_addresses[1].replace("*", "localhost")

        client1 = SimpleClient(SimpleTeam("team1", SteppingPlayer("^>>v<")), address=client1_address)
        client2 = SimpleClient(SimpleTeam("team2", SteppingPlayer("^<<v>")), address=client2_address)

        client1.autoplay_process()
        client2.autoplay_process()
        server.run()
        server.shutdown()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This file demonstrates setting up a server and two clients using local connections.
"""

from pelita.simplesetup import SimpleClient, SimpleServer
from pelita.player import SimpleTeam, StoppingPlayer
from players import RandomPlayer, BFSPlayer, NQRandomPlayer, BasicDefensePlayer

client = SimpleClient(SimpleTeam("the good ones", NQRandomPlayer(),
                                 BFSPlayer()),
                      address="ipc:///tmp/pelita-client1")
client.autoplay_process()

client2 = SimpleClient(SimpleTeam("the bad ones", BFSPlayer(),
                                  BasicDefensePlayer()),
                       address="ipc:///tmp/pelita-client2")
client2.autoplay_process()

layout = """
    ##################################
    #...   #      .#     #  #       3#
    # ## #   # ###    #  #     #####1#
    #.   # #    # .   # ##           #
    #.#    #  .    #    .  # #########
    # ## # ## ####    # ##.   . .   .#
    #.. .  .  #. . #. #  # ## #####  #
    # ## #### #.## #     #  .  . . ..#
    #..  ..   # #  #  #    ##### #####
    ##### #####    #  #  # #   ..  ..#
Beispiel #11
0
    #.. .  .  #. . #. #  # ## #####  #
    # ## #### #.## #     #  .  . . ..#
    #..  ..   # #  #  #    ##### #####
    ##### #####    #  #  # #   ..  ..#
    #.. . .  .  #     # ##.# #### ## #
    #  ##### ## #  # .# . .#  .  . ..#
    #.   . .   .## #    #### ## # ## #
    ######### #  .    #    .  #    #.#
    #           ## #   . #    # #   .#
    #0#####     #  #    ### #   # ## #
    #2       #  #     #.      #   ...#
    ##################################
    """

    server = SimpleServer(layout_string=layout, rounds=3000)

    def star_to_localhost(str):
        # server might publish to tcp://* in which case we simply try localhost for the clients
        return str.replace("*", "localhost")

    client = SimpleClient(SimpleTeam("the good ones", NQRandomPlayer(), BFSPlayer()), address=star_to_localhost(server.bind_addresses[0]))
    client.autoplay_process()

    client2 = SimpleClient(SimpleTeam("the bad ones", BFSPlayer(), BasicDefensePlayer()), address=star_to_localhost(server.bind_addresses[1]))
    client2.autoplay_process()

    server.run()
    print(server.game_master.universe.pretty)
    print(server.game_master.game_state)

# -*- coding: utf-8 -*-

"""
We start a client using the SimpleClient class and
a couple of previously defined Players.
"""

from pelita.simplesetup import SimpleClient
from pelita.player import SimpleTeam, BFSPlayer, BasicDefensePlayer

# Set up our team named ‘the good ones’ using a
# BFSPlayer and a NQRandomPlayer.
client = SimpleClient("the good ones", SimpleTeam(BFSPlayer(), BasicDefensePlayer()))

# We could also have added more specific information about the server,
# we’d like to connect to:
# client = SimpleClient("the good ones", SimpleTeam(BFSPlayer(), NQRandomPlayer()),
#                       host="pelita.server.example.com",
#                       port=63920)

# Now, we just start the client.
# This method will only return, if the client is interrupted or closes.
client.autoplay()

"""
We start a client using the SimpleClient class and
a couple of previously defined Players.
"""

from pelita.simplesetup import SimpleClient
from pelita.player import SimpleTeam
from players import BFSPlayer, BasicDefensePlayer

# Set up our team named ‘the good ones’ using a
# BFSPlayer and a NQRandomPlayer.
client = SimpleClient(SimpleTeam("the good ones", BFSPlayer(),
                                 BasicDefensePlayer()),
                      address="tcp://localhost:50210")

# Now, we just start the client.
# This method will only return, if the client is interrupted or closes.
client.run()
There is a smaller problem with this: If we start the client before the server,
our client might try to connect before the server is ready. This is ‘fixed’
repeated tries and should not be a problem in general.

On certain operating systems (at least on Windows), starting processes naïvely
may spark a fork bomb. Therefore, all scripts using multiprocessing must use
a __name__=="__main__" part to shield from multiple execution of the script
when a new process is started.

"""

from pelita.simplesetup import SimpleClient, SimpleServer
from pelita.player import RandomPlayer, BFSPlayer, SimpleTeam, BasicDefensePlayer, NQRandomPlayer

if __name__=="__main__":
    client = SimpleClient("the good ones", SimpleTeam(NQRandomPlayer(), BFSPlayer()))
    client.autoplay_background()

    client2 = SimpleClient("the bad ones", SimpleTeam(BFSPlayer(), BasicDefensePlayer()))
    client2.autoplay_background()


    layout = """
    ##################################
    #...   #      .#     #  #       3#
    # ## #   # ###    #  #     #####1#
    #.   # #    # .   # ##           #
    #.#    #  .    #    .  # #########
    # ## # ## ####    # ##.   . .   .#
    #.. .  .  #. . #. #  # ## #####  #
    # ## #### #.## #     #  .  . . ..#
Beispiel #15
0
    #.. .  .  #. . #. #  # ## #####  #
    # ## #### #.## #     #  .  . . ..#
    #..  ..   # #  #  #    ##### #####
    ##### #####    #  #  # #   ..  ..#
    #.. . .  .  #     # ##.# #### ## #
    #  ##### ## #  # .# . .#  .  . ..#
    #.   . .   .## #    #### ## # ## #
    ######### #  .    #    .  #    #.#
    #           ## #   . #    # #   .#
    #0#####     #  #    ### #   # ## #
    #2       #  #     #.      #   ...#
    ##################################
    """

    server = SimpleServer(layout_string=layout, rounds=3000)

    def star_to_localhost(str):
        # server might publish to tcp://* in which case we simply try localhost for the clients
        return str.replace("*", "localhost")

    client = SimpleClient(SimpleTeam("the good ones", NQRandomPlayer(), BFSPlayer()), address=star_to_localhost(server.bind_addresses[0]))
    client.autoplay_process()

    client2 = SimpleClient(SimpleTeam("the bad ones", BFSPlayer(), BasicDefensePlayer()), address=star_to_localhost(server.bind_addresses[1]))
    client2.autoplay_process()

    server.run()
    print(server.game_master.universe.pretty)
    print(server.game_master.game_state)

Beispiel #16
0
from pelita.player import SimpleTeam
from pelita.player import RandomPlayer, BFSPlayer

from pelita.simplesetup import SimpleClient

import logging
try:
    import colorama
    MAGENTA = colorama.Fore.MAGENTA
    RESET = colorama.Fore.RESET
except ImportError:
    MAGENTA = ""
    RESET = ""

FORMAT = '[%(asctime)s,%(msecs)03d][%(name)s][%(levelname)s][%(funcName)s]' + MAGENTA + ' %(message)s' + RESET
#logging.basicConfig(format=FORMAT, datefmt="%H:%M:%S", level=logging.WARNING)

team1 = SimpleTeam("the good ones", BFSPlayer(), BFSPlayer())
client1 = SimpleClient(team1, address="tcp://localhost:50007")

team2 = SimpleTeam("the bad ones", BFSPlayer(), BFSPlayer())
client2 = SimpleClient(team2, address="tcp://localhost:50008")

client1.autoplay_process()
client2.autoplay_process()
Beispiel #17
0
"""
We start a client using the SimpleClient class and
a couple of previously defined Players.
"""

from pelita.simplesetup import SimpleClient
from pelita.player import SimpleTeam
from players import BFSPlayer, BasicDefensePlayer

# Set up our team named ‘the good ones’ using a
# BFSPlayer and a NQRandomPlayer.
client = SimpleClient(SimpleTeam("the good ones", BFSPlayer(), BasicDefensePlayer()), address="tcp://localhost:50210")

# Now, we just start the client.
# This method will only return, if the client is interrupted or closes.
client.run()