def test_create_board_without_holes(self): board = Board(2, 2) num_fish = 4 board.create_board_without_holes(num_fish) for c in range(0, board.col): for r in range(0, board.row): self.assertEqual(board.tiles[c][r].fish, num_fish)
def test_set_all(self): board = Board(3, 3) for col in board.tiles: for t in col: self.assertEqual(t.fish, None) board.set_all(3) for col in board.tiles: for t in col: self.assertEqual(t.fish, 3)
def test_get_offset(self): size = GUI_UNIT * MAX_FISH board = Board(4, 3) self.assertEqual(board.get_offset(0, 0), (0, 0)) tile2_off_x = 16 * size tile2_off_y = size * 4 self.assertEqual(board.get_offset(4, 4), (tile2_off_x, tile2_off_y)) tile3_off_x = 12 * size + (2 * size) tile3_off_y = size * 3 self.assertEqual(board.get_offset(3, 3), (tile3_off_x, tile3_off_y))
class PlayerTestCase(unittest.TestCase): def setUp(self): self.player1 = Player() self.player2 = Player() mini_board_array = [[1, 5], [2, 1], [1, 5]] self.mini_board = Board(3, 2) self.mini_board.create_board_from_json(mini_board_array) self.strategy = Strategy() self.in_player1 = PlayerData(Color.RED) self.in_player2 = PlayerData(Color.WHITE) self.players = [self.in_player1, self.in_player2] self.mini_state = State(self.players, self.mini_board) self.mini_tree = GameTree(self.mini_state) def test_choose_placement(self): self.assertEqual(self.player1.choose_placement(self.mini_state), (0, 0)) next_state = self.mini_state.place_penguin_for_player( Color.RED, (0, 0)) self.assertEqual(self.player1.choose_placement(next_state), (0, 1)) def test_choose_placement_wrap(self): next_state = self.mini_state.place_penguin_for_player( Color.RED, (0, 0)) next_state = next_state.place_penguin_for_player(Color.RED, (0, 1)) self.assertEqual(self.player1.choose_placement(next_state), (1, 0)) def test_choose_next_move(self): next_state = self.mini_state.place_penguin_for_player( Color.RED, (0, 0)) next_state = next_state.place_penguin_for_player(Color.WHITE, (0, 1)) self.player1.set_state(next_state) self.assertEqual(self.player1.choose_next_move().print_json(), [(0, 0), (2, 1)]) def test_choose_next_move_no_moves(self): next_state = self.mini_state.place_penguin_for_player( Color.RED, (0, 0)) next_state = next_state.place_penguin_for_player(Color.WHITE, (1, 0)) next_state = next_state.place_penguin_for_player(Color.WHITE, (2, 0)) self.player1.set_state(next_state) self.assertEqual(self.player1.choose_next_move().print_json(), False) def test_assign_color(self): self.assertEqual(self.player1.get_color(), None) self.player1.assign_color(Color.RED) self.assertEqual(self.player1.get_color(), Color.RED) def test_assign_color_twice(self): self.assertEqual(self.player1.get_color(), None) self.player1.assign_color(Color.RED) self.assertEqual(self.player1.get_color(), Color.RED) self.player1.assign_color(Color.WHITE) self.assertEqual(self.player1.get_color(), Color.WHITE)
def test_create_board_json(self): tiles = [ [1, 2, 3, 0], [4, 0, 0, 5], [1, 1, 0, 1] ] board = Board(3, 4) board.create_board_from_json(tiles) for r in range(0, len(tiles)): for c in range(0, len(tiles[r])): self.assertEqual(board.tiles[c][r].fish, tiles[r][c]) self.assertEqual(board, Board.from_json(tiles))
def setUp(self): self.player1 = Player() self.player2 = Player() mini_board_array = [[1, 5], [2, 1], [1, 5]] self.mini_board = Board(3, 2) self.mini_board.create_board_from_json(mini_board_array) self.strategy = Strategy() self.in_player1 = PlayerData(Color.RED) self.in_player2 = PlayerData(Color.WHITE) self.players = [self.in_player1, self.in_player2] self.mini_state = State(self.players, self.mini_board) self.mini_tree = GameTree(self.mini_state)
def test_create_board_with_holes(self): board = Board(4, 2) holes = [(0, 0), (1, 1)] min_tiles = 1 board.create_board_with_holes(holes, min_tiles) num_1_tiles = 0 for c in range(0, board.col): for r in range(0, board.row): if board.tiles[c][r].fish == 1: num_1_tiles += 1 self.assertGreaterEqual(num_1_tiles, min_tiles) self.assertFalse(board.tiles[0][0].is_active) self.assertFalse(board.tiles[1][1].is_active)
def test_create_board_json_short_row(self): tiles = [ [1, 2], [4, 0, 0, 5], [1, 1, 0, 1] ] board = Board(3, 4) board.create_board_from_json(tiles) self.assertEqual(board.tiles[2][0].fish, 0) self.assertEqual(board.tiles[3][0].fish, 0) self.assertFalse(board.tiles[2][0].is_active) self.assertFalse(board.tiles[3][0].is_active) self.assertEqual(board, Board.from_json(tiles))
def setUp(self): self.board_full = Board(4, 3) self.board_full.create_board_without_holes(4) self.board_holes = Board(4, 3) self.board_holes.create_board_with_holes([(0, 0), (1, 1)], 3) self.player1 = PlayerData(Color.RED, 5) self.player2 = PlayerData(Color.WHITE, 10) self.player3 = PlayerData(Color.BROWN) self.player4 = PlayerData(Color.BLACK) self.player5 = PlayerData(Color.BLACK) self.players = [self.player1, self.player2] self.state_full = State(self.players, self.board_full) self.state_holes = State(self.players, self.board_holes)
def test_tt(self): self.legacy_player.set_state = MagicMock( wraps=self.legacy_player.set_state) self.legacy_player.choose_next_move = MagicMock( wraps=self.legacy_player.choose_next_move) board = Board(3, 3) board.create_board_without_holes(1) state = State([PlayerData(Color.RED)], board) state = state.place_penguin_for_player(Color.RED, (0, 0)) self.adapter.play_as(Color.RED) actions = [Action(), Action(), Action()] self.assertIsInstance(self.adapter.tt(state, actions), Action) self.legacy_player.set_state.assert_called_once_with(state) self.legacy_player.choose_next_move.assert_called_once_with()
def setUp(self): self.logical_player = LogicalPlayerInterface() self.adapted_player = LegacyToLogicalPlayer(self.logical_player) self.board = Board(4, 4) self.board.create_board_without_holes(1) self.players = [ PlayerData(Color.RED), PlayerData(Color.WHITE), PlayerData(Color.BROWN) ] state = State(self.players, self.board) state = state.place_penguin_for_player(Color.RED, (0, 0)) state = state.place_penguin_for_player(Color.WHITE, (1, 1)) state = state.place_penguin_for_player(Color.BROWN, (1, 3)) self.state = state
def test_from_json(self): players_json = [ { 'color': "red", 'score': 0, 'places': [] }, { 'color': "black", 'score': 123, 'places': [[2, 3]] }, { 'color': "white", 'score': 321, 'places': [[0, 0], [4, 5]] }, { 'color': "brown", 'score': 7, 'places': [[0, 1], [321, 123]] }, ] board_json = [ [1, 2], [4, 0, 0, 5], [1, 1, 0, 1] ] state_json = {'players': players_json, 'board': board_json} players = [PlayerData.from_json(player) for player in players_json] board = Board.from_json(board_json) self.assertEqual(State(players, board), State.from_json(state_json))
def from_json(value): """ Class method to create a State from a JSON value. JSON value -> State """ players = [PlayerData.from_json(player) for player in value['players']] board = Board.from_json(value['board']) return State(players, board)
def setUp(self): self.state = State([ PlayerData(Color.RED), PlayerData(Color.BROWN), PlayerData(Color.WHITE) ], Board(4, 4)) self.sock = socket.socket() self.json_sock = JSONSocket(self.sock) self.json_sock.send_json = MagicMock() self.json_sock.recv_json = MagicMock() self.proxy_player = RemotePlayerProxy(self.json_sock)
def test_tile_exists(self): board = Board(2, 2) holes = [(0, 0)] board.create_board_with_holes(holes, 1) self.assertTrue(board.tile_exists((0, 1))) self.assertFalse(board.tile_exists((0, 0))) self.assertFalse(board.tile_exists((0, 5)))
def test_set_random_tile(self, random, random_set): random_set.return_value = [5, 7] random.return_value = .4 board = Board(3, 3) board.create_board_without_holes(3) for col in board.tiles: for t in col: self.assertEqual(t.fish, 3) board.set_random_tiles(2) self.assertEqual(board.tiles[1][2].fish, 2) self.assertEqual(board.tiles[2][1].fish, 2)
def test_remove_tile(self): board = Board(2, 2) num_fish = 3 row, col = [1, 1] board.create_board_without_holes(num_fish) tile = board.tiles[col][row] self.assertTrue(tile.is_active) self.assertEqual(tile.fish, num_fish) board.remove_tile(row, col) self.assertEqual(tile.fish, 0) self.assertFalse(tile.is_active)
def setUp(self): self.state = State( [PlayerData(Color.RED), PlayerData(Color.BROWN), PlayerData(Color.WHITE)], Board(4, 4)) self.sock = socket.socket() self.json_sock = JSONSocket(self.sock) self.player = LogicalPlayerInterface() self.player.start = MagicMock() self.player.end = MagicMock() self.player.play_as = MagicMock() self.player.play_with = MagicMock() self.posn = (0, 0) self.player.setup = MagicMock(return_value=self.posn) self.move = Move((0, 0), (1, 0)) self.player.tt = MagicMock(return_value=self.move) self.server_proxy = ServerProxy(self.player, self.json_sock)
def test_get_coordinates_from_num(self): board = Board(3, 3) self.assertEqual(board.get_coordinates_from_num(5), (2, 1)) self.assertEqual(board.get_coordinates_from_num(6), (0, 2)) self.assertEqual(board.get_coordinates_from_num(7), (1, 2))
def setUp(self): self.board = Board(4, 3) self.board.create_board_without_holes(4)
class BoardTestCase(unittest.TestCase): def setUp(self): self.board = Board(4, 3) self.board.create_board_without_holes(4) def test_make_board_with_neg_row(self): self.assertRaisesRegex(ValueError, "row and col must be positive", Board, -1, 5) def test_make_board_with_neg_col(self): self.assertRaisesRegex(ValueError, "row and col must be positive", Board, 5, -1) def test_make_board_with_zeros(self): self.assertRaisesRegex(ValueError, "row and col must be positive", Board, 0, 0) def test_create_board_with_holes(self): board = Board(4, 2) holes = [(0, 0), (1, 1)] min_tiles = 1 board.create_board_with_holes(holes, min_tiles) num_1_tiles = 0 for c in range(0, board.col): for r in range(0, board.row): if board.tiles[c][r].fish == 1: num_1_tiles += 1 self.assertGreaterEqual(num_1_tiles, min_tiles) self.assertFalse(board.tiles[0][0].is_active) self.assertFalse(board.tiles[1][1].is_active) def test_create_board_without_holes(self): board = Board(2, 2) num_fish = 4 board.create_board_without_holes(num_fish) for c in range(0, board.col): for r in range(0, board.row): self.assertEqual(board.tiles[c][r].fish, num_fish) def test_create_board_without_holes_error(self): board = Board(2, 2) self.assertRaisesRegex(ValueError, "fish must be positive and less than the max allowed fish", board.create_board_without_holes, 0) def test_create_board_json(self): tiles = [ [1, 2, 3, 0], [4, 0, 0, 5], [1, 1, 0, 1] ] board = Board(3, 4) board.create_board_from_json(tiles) for r in range(0, len(tiles)): for c in range(0, len(tiles[r])): self.assertEqual(board.tiles[c][r].fish, tiles[r][c]) self.assertEqual(board, Board.from_json(tiles)) def test_create_board_json_short_row(self): tiles = [ [1, 2], [4, 0, 0, 5], [1, 1, 0, 1] ] board = Board(3, 4) board.create_board_from_json(tiles) self.assertEqual(board.tiles[2][0].fish, 0) self.assertEqual(board.tiles[3][0].fish, 0) self.assertFalse(board.tiles[2][0].is_active) self.assertFalse(board.tiles[3][0].is_active) self.assertEqual(board, Board.from_json(tiles)) def test_remove_tile(self): board = Board(2, 2) num_fish = 3 row, col = [1, 1] board.create_board_without_holes(num_fish) tile = board.tiles[col][row] self.assertTrue(tile.is_active) self.assertEqual(tile.fish, num_fish) board.remove_tile(row, col) self.assertEqual(tile.fish, 0) self.assertFalse(tile.is_active) def test_tile_exists(self): board = Board(2, 2) holes = [(0, 0)] board.create_board_with_holes(holes, 1) self.assertTrue(board.tile_exists((0, 1))) self.assertFalse(board.tile_exists((0, 0))) self.assertFalse(board.tile_exists((0, 5))) def test_create_board_negative_one_fish(self): self.assertRaisesRegex(ValueError, "the number of tiles with one fish cannot be negative", self.board.create_board_with_holes, [(0, 0), (0, 1)], -2) def test_create_board_too_many_holes(self): self.assertRaisesRegex(ValueError, "holes and number of one fish tiles cannot be greater than the total number of tiles", self.board.create_board_with_holes, [(0, 0), (0, 1)], 12) def test_get_reachable_posn_north(self): board = Board(8, 1) board.create_board_without_holes(3) self.assertEqual(board.get_reachable_posn_dir( 7, 0, Tile.get_north_coord), [(5, 0), (3, 0), (1, 0)]) self.assertEqual(board.get_reachable_posn_dir(0, 0, Tile.get_north_coord), []) def test_get_reachable_posn_south(self): board = Board(8, 1) board.create_board_without_holes(3) self.assertEqual(board.get_reachable_posn_dir(7, 0, Tile.get_south_coord), []) self.assertEqual(board.get_reachable_posn_dir( 1, 0, Tile.get_south_coord), [(3, 0), (5, 0), (7, 0)]) def test_get_reachable_posn_ne(self): board = Board(3, 3) board.create_board_without_holes(3) self.assertEqual(board.get_reachable_posn_dir(0, 0, Tile.get_ne_coord), []) self.assertEqual(board.get_reachable_posn_dir(1, 0, Tile.get_ne_coord), [(0, 1)]) def test_get_reachable_posn_nw(self): board = Board(3, 3) board.create_board_without_holes(3) self.assertEqual(board.get_reachable_posn_dir(0, 0, Tile.get_nw_coord), []) self.assertEqual(board.get_reachable_posn_dir(1, 0, Tile.get_nw_coord), [(0, 0)]) def test_get_reachable_posn_se(self): board = Board(3, 3) board.create_board_without_holes(3) self.assertEqual(board.get_reachable_posn_dir(0, 0, Tile.get_se_coord), [(1, 0), (2, 1)]) self.assertEqual(board.get_reachable_posn_dir(1, 0, Tile.get_se_coord), [(2, 1)]) def test_get_reachable_posn_sw(self): board = Board(3, 3) board.create_board_without_holes(3) self.assertEqual(board.get_reachable_posn_dir(0, 0, Tile.get_sw_coord), []) self.assertEqual(board.get_reachable_posn_dir(1, 0, Tile.get_sw_coord), [(2, 0)]) def test_get_all_reachable_posn(self): board = Board(4, 3) board.create_board_without_holes(1) self.assertEqual(board.get_all_reachable_posn(0, 0), [(2, 0), (1, 0), (2, 1), (3, 1)]) def test_get_all_reachable_posn_holes(self): board = Board(4, 3) board.create_board_with_holes([(0, 0)], 1) self.assertEqual(board.get_all_reachable_posn(1, 0), [ (3, 0), (0, 1), (2, 0), (2, 1), (3, 1)]) def test_tile_exists(self): board = Board(3, 3) self.assertTrue(board.tile_exists((2, 2))) self.assertFalse(board.tile_exists((-1, 1))) self.assertFalse(board.tile_exists((3, 1))) def test_get_coordinates_from_num(self): board = Board(3, 3) self.assertEqual(board.get_coordinates_from_num(5), (2, 1)) self.assertEqual(board.get_coordinates_from_num(6), (0, 2)) self.assertEqual(board.get_coordinates_from_num(7), (1, 2)) def test_set_all(self): board = Board(3, 3) for col in board.tiles: for t in col: self.assertEqual(t.fish, None) board.set_all(3) for col in board.tiles: for t in col: self.assertEqual(t.fish, 3) def test_remove_tile(self): board = Board(3, 3) self.assertTrue(board.tiles[0][0].is_active) board.remove_tile(0, 0) self.assertFalse(board.tiles[0][0].is_active) @mock.patch('random.sample') @mock.patch('random.random') def test_set_random_tile(self, random, random_set): random_set.return_value = [5, 7] random.return_value = .4 board = Board(3, 3) board.create_board_without_holes(3) for col in board.tiles: for t in col: self.assertEqual(t.fish, 3) board.set_random_tiles(2) self.assertEqual(board.tiles[1][2].fish, 2) self.assertEqual(board.tiles[2][1].fish, 2) def test_get_offset(self): size = GUI_UNIT * MAX_FISH board = Board(4, 3) self.assertEqual(board.get_offset(0, 0), (0, 0)) tile2_off_x = 16 * size tile2_off_y = size * 4 self.assertEqual(board.get_offset(4, 4), (tile2_off_x, tile2_off_y)) tile3_off_x = 12 * size + (2 * size) tile3_off_y = size * 3 self.assertEqual(board.get_offset(3, 3), (tile3_off_x, tile3_off_y)) def test_board_get_reachable_penguins(self): positions = self.board.get_all_reachable_posn(0, 0, [(2, 1)]) self.assertEqual(positions, [(2, 0), (1, 0)])
def test_any_remaining_moves_no_moves(self): board = Board(4, 3) board.create_board_with_holes([(1, 0), (2, 0)], 0) state = State(self.players, board) self.player1.add_penguin((0, 0)) self.assertFalse(state.any_remaining_moves())
def test_create_board_without_holes_error(self): board = Board(2, 2) self.assertRaisesRegex(ValueError, "fish must be positive and less than the max allowed fish", board.create_board_without_holes, 0)
class StateTestCase(unittest.TestCase): def setUp(self): self.board_full = Board(4, 3) self.board_full.create_board_without_holes(4) self.board_holes = Board(4, 3) self.board_holes.create_board_with_holes([(0, 0), (1, 1)], 3) self.player1 = PlayerData(Color.RED, 5) self.player2 = PlayerData(Color.WHITE, 10) self.player3 = PlayerData(Color.BROWN) self.player4 = PlayerData(Color.BLACK) self.player5 = PlayerData(Color.BLACK) self.players = [self.player1, self.player2] self.state_full = State(self.players, self.board_full) self.state_holes = State(self.players, self.board_holes) def test_duplicate_player_colors(self): duplicate_colors = [self.player1, self.player3, self.player4, self.player5] self.assertRaisesRegex(ValueError, "Players cannot have duplicate colors", State, duplicate_colors, self.board_full) def test_place_penguin_for_player_success(self): self.assertEqual(self.state_full.get_all_penguins(), []) new_state = self.state_full.place_penguin_for_player(Color.RED, (1, 2)) self.assertEqual(new_state.get_all_penguins(), [(1, 2)]) def test_place_penguin_for_player_out_of_bounds(self): self.assertEqual(self.state_full.get_all_penguins(), []) self.assertFalse(self.state_full.place_penguin_for_player(Color.RED, (4, 3))) def test_place_penguin_for_player_hole(self): self.assertEqual(self.state_holes.get_all_penguins(), []) self.assertFalse(self.state_holes.place_penguin_for_player(Color.RED, (0, 0))) def test_place_penguin_for_player_no_player(self): self.assertEqual(self.state_full.get_all_penguins(), []) self.assertFalse(self.state_full.place_penguin_for_player(Color.BROWN, (1, 2))) def test_get_all_penguins(self): self.assertEqual(self.state_full.get_all_penguins(), []) self.player1.get_penguins = MagicMock(return_value=[]) self.player2.get_penguins = MagicMock(return_value=[(0, 0), (3, 2)]) self.assertEqual(self.state_full.get_all_penguins(), [(0, 0), (3, 2)]) def test_get_player_success(self): self.assertEqual(self.state_full.get_player(Color.RED), self.player1) self.assertEqual(self.state_full.get_player(Color.WHITE), self.player2) def test_get_player_no_player(self): self.assertFalse(self.state_full.get_player(Color.BROWN)) def test_is_tile_available_true(self): self.assertTrue(self.state_full.is_tile_available((1, 1))) self.assertTrue(self.state_holes.is_tile_available((2, 2))) def test_is_tile_available_holes(self): self.assertFalse(self.state_holes.is_tile_available((0, 0))) self.assertFalse(self.state_holes.is_tile_available((1, 1))) def test_is_tile_available_out_of_bounds(self): self.assertFalse(self.state_full.is_tile_available((4, 3))) self.assertFalse(self.state_full.is_tile_available((-2, 3))) def test_is_tile_available_occupied(self): new_state = self.state_full.place_penguin_for_player(Color.RED, (1, 2)) self.assertFalse(new_state.is_tile_available((1, 2))) def test_valid_move_success(self): self.state_full.get_all_penguins = MagicMock(return_value=[(0, 0), (1, 0), (3, 2)]) self.assertTrue(self.state_full.valid_move((0, 0), (2, 0))) def test_valid_move_occupied(self): self.state_full.get_all_penguins = MagicMock(return_value=[(0, 0), (1, 0), (3, 2)]) self.assertFalse(self.state_full.valid_move((0, 0), (1, 0))) def test_valid_move_hole(self): self.state_holes.get_all_penguins = MagicMock(return_value=[(0, 1), (1, 0), (3, 2)]) self.assertFalse(self.state_holes.valid_move((0, 1), (1, 1))) def test_valid_move_no_penguin(self): self.state_holes.get_all_penguins = MagicMock(return_value=[(0, 1), (1, 0), (3, 2)]) self.assertFalse(self.state_holes.valid_move((0, 0), (2, 0))) def test_valid_move_not_reachable(self): self.state_holes.get_all_penguins = MagicMock(return_value=[(0, 1), (1, 0), (3, 2)]) self.assertFalse(self.state_holes.valid_move((0, 0), (3, 0))) def test_get_player_from_penguin_exists(self): self.player1.get_penguins = MagicMock(return_value=[(0, 0), (1, 0)]) self.player2.get_penguins = MagicMock(return_value=[(3, 2)]) self.assertEqual(self.state_full.get_player_from_penguin((3, 2)), self.player2) def test_get_player_from_penguin_no_player(self): self.player1.get_penguins = MagicMock(return_value=[(0, 0), (1, 0)]) self.assertFalse(self.state_full.get_player_from_penguin((3, 2))) def test_move_penguin_success(self): self.player1.add_penguin((0, 0)) self.assertTrue((0, 0) in self.state_full.get_all_penguins()) self.assertFalse((2, 0) in self.state_full.get_all_penguins()) self.assertEqual(self.state_full.get_current_player_color(), Color.RED) new_state = self.state_full.move_penguin((0, 0), (2, 0)) self.assertTrue((2, 0) in new_state.get_all_penguins()) self.assertFalse((0, 0) in new_state.get_all_penguins()) self.assertEqual(new_state.get_current_player_color(), Color.WHITE) def test_move_penguin_occupied(self): self.player1.add_penguin((0, 0)) self.player2.add_penguin((2, 0)) self.assertEqual(self.state_full.get_all_penguins(), [(0, 0), (2, 0)]) self.assertEqual(self.state_full.get_current_player_color(), Color.RED) self.assertFalse(self.state_full.move_penguin((0, 0), (2, 0))) self.assertEqual(self.state_full.get_all_penguins(), [(0, 0), (2, 0)]) self.assertEqual(self.state_full.get_current_player_color(), Color.RED) def test_any_remaining_moves_true(self): self.player1.add_penguin((0, 0)) self.player2.add_penguin((2, 0)) self.assertTrue(self.state_full.any_remaining_moves()) def test_any_remaining_moves_no_penguins(self): self.assertFalse(self.state_full.any_remaining_moves()) def test_any_remaining_moves_no_moves(self): board = Board(4, 3) board.create_board_with_holes([(1, 0), (2, 0)], 0) state = State(self.players, board) self.player1.add_penguin((0, 0)) self.assertFalse(state.any_remaining_moves()) def test_get_possible_moves(self): self.player1.add_penguin((0, 0)) actions = [Move((0, 0), (1, 0)), Move((0, 0), (2, 0)), Move((0, 0), (2, 1)), Move((0, 0), (3, 1))] for action in self.state_full.get_possible_moves(): self.assertTrue(action in actions) self.assertEqual(len(self.state_full.get_possible_moves()), len(actions)) def test_get_possible_moves_another_penguin(self): self.player1.add_penguin((0, 0)) self.player2.add_penguin((2, 1)) actions = [Move((0, 0), (1, 0)), Move((0, 0), (2, 0))] for action in self.state_full.get_possible_moves(): self.assertTrue(action in actions) self.assertEqual(len(self.state_full.get_possible_moves()), len(actions)) def test_get_players_score_after_move(self): self.assertEqual(self.state_full.get_players_score(Color.RED), 0) self.player1.add_penguin((0, 0)) new_state = self.state_full.move_penguin((0, 0), (1, 0)) self.assertEqual(new_state.get_players_score(Color.RED), 4) self.assertEqual(self.state_full.get_players_score(Color.WHITE), 0) def test_get_players_score_mutate_player(self): self.assertEqual(self.state_full.get_players_score(Color.RED), 0) self.player1.add_to_score(10) self.assertEqual(self.state_full.get_players_score(Color.RED), 10) def test_from_json(self): players_json = [ { 'color': "red", 'score': 0, 'places': [] }, { 'color': "black", 'score': 123, 'places': [[2, 3]] }, { 'color': "white", 'score': 321, 'places': [[0, 0], [4, 5]] }, { 'color': "brown", 'score': 7, 'places': [[0, 1], [321, 123]] }, ] board_json = [ [1, 2], [4, 0, 0, 5], [1, 1, 0, 1] ] state_json = {'players': players_json, 'board': board_json} players = [PlayerData.from_json(player) for player in players_json] board = Board.from_json(board_json) self.assertEqual(State(players, board), State.from_json(state_json))
class TestLegacyToLogicalPlayer(unittest.TestCase): def setUp(self): self.logical_player = LogicalPlayerInterface() self.adapted_player = LegacyToLogicalPlayer(self.logical_player) self.board = Board(4, 4) self.board.create_board_without_holes(1) self.players = [ PlayerData(Color.RED), PlayerData(Color.WHITE), PlayerData(Color.BROWN) ] state = State(self.players, self.board) state = state.place_penguin_for_player(Color.RED, (0, 0)) state = state.place_penguin_for_player(Color.WHITE, (1, 1)) state = state.place_penguin_for_player(Color.BROWN, (1, 3)) self.state = state # R X X X # X W X B # X X X X # X X X X # Testing __init__ ################################################### def test_bad_player(self): self.assertRaisesRegex( ValueError, "logical_player must a subclass of LogicalPlayerInterface", LegacyToLogicalPlayer, Player()) def test_good_player(self): adapted_player = LegacyToLogicalPlayer(self.logical_player) self.assertEqual(adapted_player._LegacyToLogicalPlayer__logical_player, self.logical_player) # Testing set_state #################################################### def test_set_state(self): self.adapted_player.set_state(self.state) actual = self.adapted_player._LegacyToLogicalPlayer__state expected = self.state.copy() self.assertEqual(actual, expected) # Testing choose_next_move ############################################# # def test_choose_next_move(self): # Testing choose_placement ############################################# # def test_choose_placement(self): # Testing assign_color ############################################# def test_assign_color(self): self.adapted_player.assign_color(Color.RED) self.assertEqual(self.adapted_player.get_color(), Color.RED) # Testing play_with ############################################# # def test_play_with(self): # Testing game_over ############################################# # def test_game_over(self): # Testing update_with_action ############################################# def test_update_with_action1(self): action = Move((0, 0), (1, 0)) state = self.state.move_penguin(action.get_from_posn(), action.get_to_posn()) # 0 X X X # R W X B # X X X X # X X X X self.adapted_player.set_state(self.state) self.adapted_player.update_with_action(action) self.assertEqual(state, self.adapted_player._LegacyToLogicalPlayer__state) def test_update_with_action2(self): action = Move((0, 0), (3, 1)) state = self.state.move_penguin(action.get_from_posn(), action.get_to_posn()) # 0 X X X # X W X B # X X X X # X R X X self.adapted_player.set_state(self.state) self.adapted_player.update_with_action(action) self.assertEqual(state, self.adapted_player._LegacyToLogicalPlayer__state) def test_update_with_multiple_actions(self): action1 = Move((0, 0), (3, 1)) action2 = Move((1, 1), (0, 2)) state = self.state.move_penguin(action1.get_from_posn(), action1.get_to_posn()) state = state.move_penguin(action2.get_from_posn(), action2.get_to_posn()) # 0 X W X # X 0 X B # X X X X # X R X X self.adapted_player.set_state(self.state) self.adapted_player.update_with_action(action1) self.adapted_player.update_with_action(action2) self.assertEqual(state, self.adapted_player._LegacyToLogicalPlayer__state)
def test_get_reachable_posn_south(self): board = Board(8, 1) board.create_board_without_holes(3) self.assertEqual(board.get_reachable_posn_dir(7, 0, Tile.get_south_coord), []) self.assertEqual(board.get_reachable_posn_dir( 1, 0, Tile.get_south_coord), [(3, 0), (5, 0), (7, 0)])
def test_remove_tile(self): board = Board(3, 3) self.assertTrue(board.tiles[0][0].is_active) board.remove_tile(0, 0) self.assertFalse(board.tiles[0][0].is_active)
def test_get_reachable_posn_sw(self): board = Board(3, 3) board.create_board_without_holes(3) self.assertEqual(board.get_reachable_posn_dir(0, 0, Tile.get_sw_coord), []) self.assertEqual(board.get_reachable_posn_dir(1, 0, Tile.get_sw_coord), [(2, 0)])
def test_get_all_reachable_posn_holes(self): board = Board(4, 3) board.create_board_with_holes([(0, 0)], 1) self.assertEqual(board.get_all_reachable_posn(1, 0), [ (3, 0), (0, 1), (2, 0), (2, 1), (3, 1)])
def test_tile_exists(self): board = Board(3, 3) self.assertTrue(board.tile_exists((2, 2))) self.assertFalse(board.tile_exists((-1, 1))) self.assertFalse(board.tile_exists((3, 1)))