Beispiel #1
0
 def setUp(self):
     self.turn_handler = turn_handler.TurnHandler()
     self.players = {
         0: player.Player(0, None),
         1: player.Player(1, None),
         2: player.Player(2, None)
     }
Beispiel #2
0
    def __init__(self, filepaths):
        # Verify arguments
        if len(filepaths) < 2:
            raise Exception(
                "Game requires at least two players. "
                "Provide a filepath for the script used for each player")
        self.strategy_filepaths = filepaths

        # DEFAULT PARAMETERS
        self.board_size = [20, 20]
        self.turn_limit = 10000
        self.unit_limit_pct = 0.05  # The maximum number of allowed units per player, as a percentage of board capacity
        self.log_level = 10  # Level of log info. Use 30 to only display win/lose messages, 20 to also display turn
        # numbers and the board, and 10 to also display action messages
        self.write_to_file = True
        self.log_path = "log.txt"

        # OBJECT INITIALIZATION
        self.players = {}  # Dict of players, player_id -> player_object
        self.turn_handler = turn_handler.TurnHandler(
        )  # Turn handler in charge of determining which unit acts when
        self.board = board.Board(self.turn_handler, self.players,
                                 self.board_size,
                                 self.unit_limit_pct)  # Board and units
        self.user_commands = cmd.Commands(self.board, self.turn_handler)
        self.interpreter = interpreter.Interpreter(self.turn_handler,
                                                   self.user_commands)

        # CONFIGURE LOGGER
        self.configure_logger()
Beispiel #3
0
 def test_current_unit(self):
     # Spawn a new unit and make sure it is the current unit. Then spawn another and make sure it is still the
     # current unit.
     # The turn handler class doesn't really have any logic to unit test as it is a simple queue, so it is arguable
     # whether this is actually needed.
     test_turn_handler = turn_handler.TurnHandler()
     test_board = board.Board(test_turn_handler, self.players, [20, 20],
                              0.01)
     test_board.spawn_unit(self.players[0], [0, 0])
     unit1 = self.players[0].units.pop()
     self.assertEqual(unit1, test_turn_handler.current_unit())
     test_board.spawn_unit(self.players[0], [0, 1])
     self.assertEqual(unit1, test_turn_handler.current_unit())
Beispiel #4
0
    def setUp(self):
        # Setup necessary objects
        self.turn_handler = turn_handler.TurnHandler()
        self.players = {
            0: player.Player(0, None),
            1: player.Player(1, None),
            2: player.Player(2, None)
        }
        self.board = board.Board(self.turn_handler, self.players, [20, 20],
                                 0.01)
        self.cmd = cmd.Commands(self.board, self.turn_handler)

        # The interpreter is mostly static so we can use one instance for all the tests
        self.interpreter = interpreter.Interpreter(self.cmd)
Beispiel #5
0
 def test_end_turn(self):
     # Spawn two units. Make sure the first unit spawned is the current unit (top of the queue), then end turn
     # and make sure the second unit is the current unit
     # Similar to above.
     test_turn_handler = turn_handler.TurnHandler()
     test_board = board.Board(test_turn_handler, self.players, [20, 20],
                              0.01)
     test_board.spawn_unit(self.players[0], [0, 0])
     unit1 = self.players[0].units.pop()
     test_board.spawn_unit(self.players[0], [0, 1])
     unit2 = self.players[0].units.pop()
     self.assertEqual(unit1, test_turn_handler.current_unit())
     test_turn_handler.end_turn()
     self.assertEqual(unit2, test_turn_handler.current_unit())
Beispiel #6
0
    def __init__(self,
                 filepaths,
                 board_size=None,
                 turn_limit=10000,
                 unit_limit_pct=0.05,
                 log_level=LoggerLevels.ActionMessage,
                 write_to_file=False,
                 log_path="log.txt"):

        if board_size is None:  # Avoid mutable default argument
            board_size = [20, 20]
        # Verify arguments
        if len(filepaths) < 2:
            raise Exception(
                "Game requires at least two players. "
                "Provide a file path for the script used for each player")
        if len(filepaths) > board_size[0] * board_size[1]:
            raise Exception("Cannot start game for " + str(len(filepaths)) +
                            " players with board size " + str(board_size) +
                            " (" + str(board_size[0] * board_size[1]) +
                            " tiles). There must be more tiles than players")
        self.strategy_filepaths = filepaths

        # DEFAULT PARAMETERS
        self.board_size = board_size
        self.turn_limit = turn_limit
        self.unit_limit_pct = unit_limit_pct  # The maximum number of allowed units per player, as a percentage
        # of board capacity
        self.log_level = log_level  # Level of log info.
        # Use LoggerLevels.PrimaryInformation to only display win/lose messages,
        # LoggerLevels.SecondaryInformation to also display  turn numbers and the board, and
        # LoggerLevels.ActionMessage to also display action messages
        self.write_to_file = write_to_file
        self.log_path = log_path

        # OBJECT INITIALIZATION
        self.players = {}  # Dict of players, player_id -> player_object
        self.turn_handler = turn_handler.TurnHandler(
        )  # Turn handler in charge of determining which unit acts when
        self.board = board.Board(self.turn_handler, self.players,
                                 self.board_size,
                                 self.unit_limit_pct)  # Board and units
        self.user_commands = cmd.Commands(
            self.board, turn_handler.TurnHandlerInterface(self.turn_handler))
        self.interpreter = interpreter.Interpreter(self.user_commands)

        # CONFIGURE LOGGER
        self.configure_logger()