Example #1
0
    def setUp(self):
        """Mock-up context for testing the rulechecker.

        4 workers for 2 players, each placed along the diagonal
        (i.e. (0, 0), (1, 1), etc)

        To the east of each worker, a building is built with
        height equal to the worker's number in the total
        workers (i.e. Worker 2 builds a 2-floor building)

        Example:
        0W | 1  | 0  | 0  | 0 | 0
        0  | 0W | 2  | 0  | 0 | 0
        0  | 0  | 0W | 3  | 0 | 0
        0  | 0  | 0  | 0W | 4 | 0
        0  | 0  | 0  | 0  | 0 | 0
        0  | 0  | 0  | 0  | 0 | 0

        """
        self.workers = [
            Worker("player1", 1),
            Worker("player1", 2),
            Worker("player2", 1),
            Worker("player2", 2)
        ]
        self.board = Board()
        for i, worker in enumerate(self.workers):
            self.board.place_worker(worker, (i, i))
            for _ in range(i):
                self.board.build_floor(worker, Direction.EAST)
Example #2
0
 def test_build_floor_off_board(self):
     """Case for building a floor off of the board."""
     board = Board([[0, 2]], {self.workers[0]: (0, 0)})
     with self.assertRaises(IndexError) as context:
         board.build_floor(self.workers[0], Direction.NORTH)
     self.assertTrue("Cannot place a worker out of board bounds" in str(
         context.exception))
Example #3
0
 def test_empty(self):
     """Tests placement of four workers on an empty board."""
     board = Board()
     for i in range(len(self.workers)):
         place = diag.plan_placement(self.workers[i].player, board)
         self.assertEqual(place, (i, i))
         board.place_worker(self.workers[i], place)
Example #4
0
 def test_get_height_off_board(self):
     """Case for getting the height of a building off of the board."""
     board = Board([[0, 3]], {self.workers[0]: (0, 0)})
     with self.assertRaises(IndexError) as context:
         board.get_height(board.worker_position(self.workers[0]),
                          Direction.NORTH)
     self.assertTrue("Cannot place a worker out of board bounds" in str(
         context.exception))
Example #5
0
 def test_is_not_occupied(self):
     """Case for seeing if a cell is not occupied by a worker."""
     board = Board([[0, 4]], {
         self.workers[0]: (0, 0),
         self.workers[1]: (1, 1)
     })
     self.assertEqual(board.worker_position(self.workers[1]), (1, 1))
     self.assertFalse(board.is_occupied((1, 2)))
Example #6
0
 def test_dump_board_as_json_string(self):
     """Test that a board can be printed as a json string correctly."""
     tiles = [[1, 2, 2], [], [], [2, 0, 2, 0, 0, 1]]
     board = Board(tiles, workers={self.workers[1]: (2, 2)})
     expected_str = [[1, 2, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0],
                     [0, 0, '0player12', 0, 0, 0], [2, 0, 2, 0, 0, 1],
                     [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
     self.assertEqual(board.dump_as_json(self.uuids_to_name), expected_str)
Example #7
0
    def test_place_worker_off_board(self):
        """Case for placing a worker outside of the board boundaries."""
        empty_board = Board()

        with self.assertRaises(IndexError) as context:
            empty_board.place_worker(self.workers[0], (-1, -1))
        self.assertTrue("Cannot place a worker out of board bounds" in str(
            context.exception))
Example #8
0
    def test_move_worker_off_board(self):
        """Case for moving a worker outside of the board boundaries."""
        board = Board([[0, 1]], {self.workers[0]: (0, 0)})

        self.assertEqual(board.worker_position(self.workers[0]), (0, 0))
        with self.assertRaises(IndexError) as context:
            board.move_worker(self.workers[0], Direction.NORTH)
        self.assertTrue("Cannot place a worker out of board bounds" in str(
            context.exception))
Example #9
0
 def test_winner_2(self):
     """Get the winner."""
     board = Board([[1, 2, 3, 4, 3, 2]],
                   workers={
                       self.workers[0]: (0, 0),
                       self.workers[2]: (0, 1)
                   })
     self.assertFalse(rulechecker.get_winner(board))
     board.move_worker(self.workers[2], Direction.EAST)
     self.assertEqual(rulechecker.get_winner(board), "player2")
Example #10
0
 def test_can_place_worker_twice(self):
     """Base case for placing a worker at the start of a game."""
     board = Board()
     self.assertTrue(
         rulechecker.can_place_worker(board, self.workers[0], (0, 0)))
     board.place_worker(self.workers[0], (0, 0))
     self.assertFalse(
         rulechecker.can_place_worker(board, self.workers[0], (0, 0)))
     self.assertFalse(
         rulechecker.can_place_worker(board, self.workers[0], (4, 4)))
Example #11
0
    def test_place_more_than_4_workers(self):
        """Case for placing more than 4 workers."""
        board = Board(
            [[0, 1, 2, 3, 4, 0]], {
                self.workers[0]: (0, 0),
                self.workers[1]: (0, 1),
                self.workers[2]: (0, 2),
                self.workers[3]: (0, 3)
            })

        self.assertEqual(len(board.workers), 4)
        board.place_worker(Worker("player3", 2), (0, 5))
        self.assertEqual(len(board.workers), 5)
Example #12
0
 def test_place_worker_on_worker(self):
     """Case for placing a worker onto another worker."""
     empty_board = Board()
     empty_board.place_worker(self.workers[0], (0, 0))
     self.assertEqual(empty_board.worker_position(self.workers[1]), None)
     empty_board.place_worker(self.workers[1], (0, 0))
     self.assertEqual(empty_board.worker_position(self.workers[0]),
                      empty_board.worker_position(self.workers[1]))
Example #13
0
    def board_to_board(self, board_arr):
        """
        Converts a 2d Board cell array to a Board object
        :param [[Cell, ...], ...] board_arr: where Cell is either a Height (0, 1, 2, 3, 4)
                                             or a BuildingWorker, a Height followed by a Worker
        :rtype Board: a suitable board
        """
        new_board = [[0 for col in range(Board.BOARD_SIZE)]
                     for row in range(Board.BOARD_SIZE)]
        workers = {}
        for (row, rows) in enumerate(board_arr):
            for col in range(len(rows)):
                cur_cell = board_arr[row][col]
                if isinstance(cur_cell, int):
                    new_board[row][col] = cur_cell
                elif cur_cell:
                    new_board[row][col] = int(cur_cell[0])
                    worker_str = cur_cell[1:]
                    worker_num = int(worker_str[-1:])
                    player_name = worker_str[:-1]
                    cur_worker = Worker(self.name_to_uuid(player_name),
                                        worker_num)
                    if cur_worker:
                        workers[cur_worker] = (row, col)

        cur_board = Board(new_board, workers)
        return cur_board
Example #14
0
    def testUpdateMoveBuildTurn(self):
        """Test expexted output of receiving a move+build turn"""

        board = Board(board=[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
                             [0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0],
                             [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]],
                      workers={
                          self.workers[0]: (0, 0),
                          self.workers[1]: (1, 1),
                          self.workers[2]: (2, 2),
                          self.workers[3]: (3, 4)
                      })

        with MockStdOut() as output:
            self.observer.update_turn(
                board, (self.workers[3], Direction.EAST, Direction.WEST),
                self.uuids_to_name)

        board_str = output[0]
        turn_str = output[1]

        expected_board_json = [["0player11", 0, 0, 0, 0, 0],
                               [0, "0player12", 0, 0, 0, 0],
                               [0, 0, "0player21", 0, 0, 0],
                               [0, 0, 0, 1, "1player22", 0],
                               [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]

        expected_turn_json = ["player22", "EAST", "PUT", "WEST", "PUT"]

        self.assertEqual(json.loads(board_str), expected_board_json)
        self.assertEqual(json.loads(turn_str), expected_turn_json)
Example #15
0
    def testUpdateGameOver(self):
        board = Board(
            workers={
                self.workers[0]: (0, 0),
                self.workers[1]: (1, 1),
                self.workers[2]: (2, 2),
                self.workers[3]: (3, 3)
            })

        with MockStdOut() as output:
            self.observer.update_game_over(board, self.player_names[1],
                                           self.uuids_to_name)

        board_str = output[0]
        turn_str = output[1]

        expected_board_json = [["0player11", 0, 0, 0, 0, 0],
                               [0, "0player12", 0, 0, 0, 0],
                               [0, 0, "0player21", 0, 0, 0],
                               [0, 0, 0, "0player22", 0, 0],
                               [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]

        self.assertEqual(json.loads(board_str), expected_board_json)

        self.assertEqual(json.loads(turn_str),
                         Observer.WINNER_STR + self.player_names[1])
Example #16
0
 def enact_placement(self, workers):
     """
     Gets a placement from the player and sends it to the server.
     :param list(Worker) workers: the list of Workers already placed.
     """
     current_board = Board(workers=workers)
     placement = self._player.place_worker(current_board)
     self._client_msger.respond_placement_message(placement)
Example #17
0
 def test_build_floor_on_worker(self):
     """Case for building a floor on top of another worker."""
     board = Board(workers={
         self.workers[0]: (0, 0),
         self.workers[1]: (1, 1)
     })
     self.assertEqual(
         board.get_height(board.worker_position(self.workers[0]),
                          Direction.SOUTHEAST), 0)
     board.build_floor(self.workers[0], Direction.SOUTHEAST)
     self.assertTrue(board.is_occupied((1, 1)))
     self.assertEqual(
         board.get_height(board.worker_position(self.workers[0]),
                          Direction.SOUTHEAST), 1)
Example #18
0
    def test_two_placed(self):
        """Test placement after p1 places.

        Player 1 has placed two of its workers on the board along the diagonal
        """
        board = Board(workers={self.workers[0]: (0, 0),
                               self.workers[1]: (1, 1)})
        self.assertEqual(farthest.plan_placement(self.workers[2].player, board),
                         (5, 5))
Example #19
0
    def test_same_row_placement(self):
        """Test placement after p1 places 2 workers in top row.

        Two workers are placed at (0, 0) and (0, 5) at the top of the board
        and they both belong to the same opposing player
        """
        board = Board(workers={self.workers[0]: (0, 0),
                               self.workers[1]: (0, 5)})
        self.assertEqual(farthest.plan_placement(self.workers[2].player, board),
                         (5, 2))
Example #20
0
    def test_two_diag_placement(self):
        """Test placement after p1 places 2 workers in diag row.

        Two workers are placed at (1, 1) and (4, 4) on the diagonal
        and they both belong to the same opposing player
        """
        board = Board(workers={self.workers[0]: (1, 1),
                               self.workers[1]: (4, 4)})
        self.assertEqual(farthest.plan_placement(self.workers[2].player, board),
                         (0, 5))
Example #21
0
 def test_can_place_worker_on_worker(self):
     """Case for placing a worker onto another worker."""
     board = Board(
         workers={
             self.workers[0]: (0, 0),
             self.workers[1]: (0, 1),
             self.workers[2]: (0, 2)
         })
     self.assertFalse(
         rulechecker.can_place_worker(board, Worker("player2", 2), (0, 1)))
Example #22
0
 def test_is_game_over_height_three(self):
     """A worker is on a building of height 3."""
     board = Board(
         [[3]],
         workers={
             self.workers[0]: (0, 0),
             self.workers[1]: (0, 1),
             self.workers[2]: (0, 2)
         })
     self.assertTrue(rulechecker.is_game_over(board, self.workers[0:2]))
Example #23
0
 def test_winner_lock_in(self):
     board = Board(
         [[0, 1, 3, 4, 3, 0], [1, 2, 4, 4, 4, 3]],
         workers={
             self.workers[0]: (0, 0),
             self.workers[1]: (0, 5),
             self.workers[2]: (1, 0),
             self.workers[3]: (0, 1)
         })
     self.assertEqual(rulechecker.get_winner(board), "player2")
Example #24
0
 def test_winner_move_no_build(self):
     board = Board(
         [[0, 0, 1, 0], [0, 4, 4], [4, 4]],
         workers={
             self.workers[0]: (0, 0),
             self.workers[1]: (1, 0),
             self.workers[2]: (0, 1),
             self.workers[3]: (0, 3)
         })
     self.assertEqual(rulechecker.get_winner(board), "player2")
Example #25
0
 def test_depth4(self):
     """Test tree strategy for a depth of 4."""
     board = Board(
         workers={
             self.workers[0]: (0, 0),
             self.workers[1]: (1, 1),
             self.workers[2]: (2, 2),
             self.workers[3]: (3, 3)
         })
     self.assertTrue(tree.do_survive(board, "p1", 4, self.workers[0]))
Example #26
0
 def test_plan_turn_forced(self):
     """Test that plan_turn returns the correct turn
     for a simple test case."""
     board = Board([[2, 3], [4, 0]],
                   workers={
                       self.workers[i]: (i, i)
                       for i in range(len(self.workers))
                   })
     tree_strat = tree(2)
     self.assertEqual(tree_strat.plan_turn(self.workers[0:2], board),
                      (self.workers[0], Direction.EAST, None))
Example #27
0
 def test_can_place_five_workers(self):
     """Case for placing more than 4 workers."""
     board = Board(
         [[0, 1, 2, 3, 4, 0]], {
             self.workers[0]: (0, 0),
             self.workers[1]: (0, 1),
             self.workers[2]: (0, 2),
             self.workers[3]: (0, 3)
         })
     self.assertFalse(
         rulechecker.can_place_worker(board, Worker("player3", 2), (0, 5)))
Example #28
0
 def test_depth_zero(self):
     """Test tree strategy for a depth of zero."""
     board = Board([[2, 3]],
                   workers={
                       self.workers[i]: (i, i)
                       for i in range(len(self.workers))
                   })
     self.assertTrue(
         tree.do_survive(board, "p1", 0, self.workers[0], Direction.EAST))
     self.assertTrue(
         tree.do_survive(board, "p1", 0, self.workers[0], Direction.SOUTH,
                         Direction.NORTH))
Example #29
0
    def test_move_worker_on_worker(self):
        """Case for moving a worker onto another worker."""
        board = Board([[0, 1]], {
            self.workers[0]: (0, 0),
            self.workers[1]: (1, 1)
        })

        self.assertEqual(board.worker_position(self.workers[0]), (0, 0))
        self.assertEqual(board.worker_position(self.workers[1]), (1, 1))
        self.assertTrue(
            board.is_occupied(board.worker_position(self.workers[1])))
        board.move_worker(self.workers[0], Direction.SOUTHEAST)
        self.assertEqual(board.worker_position(self.workers[0]),
                         board.worker_position(self.workers[1]))
Example #30
0
 def test_is_not_neighbor(self):
     """Case for seeing if a worker doesn't have a neighboring cell."""
     board = Board(workers={self.workers[0]: (0, 0)})
     self.assertFalse(board.is_neighbor(self.workers[0], Direction.NORTH))
     self.assertFalse(
         board.is_neighbor(self.workers[0], Direction.NORTHEAST))
     self.assertFalse(board.is_neighbor(self.workers[0], Direction.WEST))
     self.assertFalse(
         board.is_neighbor(self.workers[0], Direction.NORTHWEST))
     self.assertFalse(
         board.is_neighbor(self.workers[0], Direction.SOUTHWEST))