def Demo2(n_fish_per_tile=1) -> FishGameController: """ Randomly Generated Holes with 1 Fish on all tiles... :return: A controller to be execute in main. """ hexgrid = HexGridView() board = FishBoard(num_rows=HEXES_ROWS, num_cols=HEXES_COLUMNS) for row in range(board.num_rows): for col in range(board.num_cols): if board.retrieve_num_fish( row=row, col=col) == 0 and board.is_tile(row=row, col=col): for i in range(n_fish_per_tile): board = board.add_fish(row=row, col=col) player_1 = Player("red", 0, 0, []) state = FishGameState(board=board, num_players=1, players=[player_1], current_player=player_1) behavior = FishGameBehavior(state=state, view=hexgrid) controller = FishGameController(behavior=behavior, view=hexgrid, state=state) return controller
def test_retrieve_num_fish(self): model = FishBoard(num_rows=4, num_cols=3) self.assertEqual(model.board[0][0].num_fish, 0) model = model.add_fish(0, 0) model = model.add_fish(0, 0) model = model.add_fish(0, 0) self.assertEqual(model.board[0][0].num_fish, 3)
def test_get_reachable_tile_coordinates_by_direction_empty(self): model = FishBoard(num_rows=4, num_cols=3) tile = model.board[1][2] direction = 'top_right_coordinate' expected = [] self.assertEqual( model.get_reachable_tile_coordinates_by_direction(tile, direction), expected)
def test_get_reachable_tile_coordinates_by_direction(self): model = FishBoard(num_rows=4, num_cols=3) tile = model.board[1][2] direction = 'bottom_left_coordinate' expected = [Coordinate(row=2, col=2), Coordinate(row=3, col=1)] self.assertEqual( model.get_reachable_tile_coordinates_by_direction(tile, direction), expected)
def test_validate_input_invalid_row_and_col_has_hole(self): board = FishBoard(4, 3) board = board.create_hole(1, 1) game_state = FishGameState(board, 3, TestGameState.player_list, current_player=TestGameState.player_list[0], phase=GameStatePhase.FINAL) is_valid, error_msg = game_state.validate_input("red", 1, 1) self.assertFalse(is_valid) self.assertEqual(error_msg, "Row and column is a hole")
def RandomlyGenerateFish(board: FishBoard, total_num_fish=10): for _ in range(total_num_fish): not_added = True while not_added: random_row = randint(0, HEXES_ROWS - 1) random_col = randint(0, HEXES_COLUMNS - 1) if board.retrieve_num_fish( random_row, random_col) < MAX_NUM_FISH and board.is_tile( random_row, random_col): board = board.add_fish(row=random_row, col=random_col) not_added = False return board
def RandomlyGenerateHole(board: FishBoard, total_num_hole=2): for _ in range(total_num_hole): not_added = True while not_added: random_row = randint(0, HEXES_ROWS - 1) random_col = randint(0, HEXES_COLUMNS - 1) if board.retrieve_num_fish(random_row, random_col) == 0 and board.is_tile( random_row, random_col): board = board.create_hole(random_row, random_col) not_added = False return board
def test_is_equal(self): board1 = FishBoard(4, 3) board1 = board1.create_hole(2, 0) board1 = board1.create_hole(3, 0) board1 = board1.create_hole(2, 1) board1 = board1.create_hole(0, 1) board2 = FishBoard(4, 3) board2 = board2.create_hole(2, 0) board2 = board2.create_hole(3, 0) board2 = board2.create_hole(2, 1) board2 = board2.create_hole(0, 1) factory1 = FishGameState(board=board1, num_players=3, players=TestGameState.player_list, phase=GameStatePhase.INITIAL, current_player=TestGameState.player_list[0]) factory1 = factory1.add_penguin(0, 0, "red") factory1 = factory1.add_penguin(1, 0, "white") factory2 = FishGameState(board=board2, num_players=3, players=TestGameState.player_list, phase=GameStatePhase.INITIAL, current_player=TestGameState.player_list[0]) factory2 = factory2.add_penguin(0, 0, "red") factory2 = factory2.add_penguin(1, 0, "white") state1 = factory1.finalize() state2 = factory2.finalize() self.assertTrue(state1.is_equal(state2))
def test_get_reachable_tiles_with_holes(self): model = FishBoard(num_rows=5, num_cols=4) model = model.create_hole(3, 1) expected_reachable = [ Coordinate(row=0, col=1), Coordinate(row=1, col=1), Coordinate(row=0, col=2), Coordinate(row=4, col=1), Coordinate(row=3, col=0), Coordinate(row=4, col=0), Coordinate(row=1, col=0), Coordinate(row=0, col=0) ] self.assertEqual(model.get_reachable_tiles(2, 1), expected_reachable)
def test_is_equal(self): board1 = FishBoard(num_rows=4, num_cols=3) board2 = FishBoard(num_rows=4, num_cols=3) board1 = board1.add_fish(row=0, col=0) board2 = board2.add_fish(row=0, col=0) board1 = board1.create_hole(row=1, col=0) board2 = board2.create_hole(row=1, col=0) self.assertTrue(board1.is_equal(board2))
def test_any_player_can_move_failure(self): board = FishBoard(4, 3) board = board.create_hole(2, 0) board = board.create_hole(3, 0) board = board.create_hole(2, 1) board = board.create_hole(0, 1) factory = FishGameState(board=board, num_players=3, players=TestGameState.player_list, phase=GameStatePhase.INITIAL, current_player=TestGameState.player_list[0]) factory = factory.add_penguin(0, 0, "red") factory = factory.add_penguin(1, 0, "white") game_state = factory.finalize() self.assertFalse(game_state.check_any_player_can_move())
def test_is_equal_false_board_only(self): board1 = FishBoard(5, 3) board2 = FishBoard(4, 3) state1 = FishGameState(board=board1, num_players=3, players=TestGameState.player_list[0:2], phase=GameStatePhase.INITIAL, current_player=TestGameState.player_list[0]) state2 = FishGameState(board=board2, num_players=3, players=TestGameState.player_list, phase=GameStatePhase.INITIAL, current_player=TestGameState.player_list[0]) self.assertFalse(state1.is_equal(state2))
def Demo1() -> FishGameController: """ "Randomly Generate number of holes, hole locations, number of fish, and fish location. :return: A controller to be execute in main. """ hexgrid = HexGridView() board = FishBoard(num_rows=HEXES_ROWS, num_cols=HEXES_COLUMNS) board = RandomlyGenerateHole(board) board = RandomlyGenerateFish(board) player_1 = Player("red", 0, 0, []) state = FishGameState(board=board, num_players=1, players=[player_1], current_player=player_1) behavior = FishGameBehavior(state=state, view=hexgrid) controller = FishGameController(behavior=behavior, view=hexgrid, state=state) return controller
def test_get_player_id_success(self): board = FishBoard(4, 3) board = board.create_hole(2, 0) board = board.create_hole(3, 0) board = board.create_hole(2, 1) board = board.create_hole(0, 1) factory = FishGameState(board=board, num_players=3, players=TestGameState.player_list, phase=GameStatePhase.INITIAL, current_player=TestGameState.player_list[0]) factory = factory.add_penguin(0, 0, "red") factory = factory.add_penguin(1, 0, "white") game_state = factory.finalize() self.assertEqual(game_state.get_player_color(0, 0), "red") self.assertEqual(game_state.get_player_color(3, 2), "")
def test_is_equal_false_different_holes(self): board1 = FishBoard(num_rows=4, num_cols=3) board2 = FishBoard(num_rows=4, num_cols=3) board1 = board1.create_hole(row=1, col=0) board2 = board2.create_hole(row=1, col=1) self.assertFalse(board1.is_equal(board2))
def test_is_equal_false_different_num_fish(self): board1 = FishBoard(num_rows=4, num_cols=3) board2 = FishBoard(num_rows=4, num_cols=3) board1 = board1.add_fish(row=0, col=0) board2 = board2.add_fish(row=1, col=0) self.assertFalse(board1.is_equal(board2))
def test_add_penguin_success(self): board = FishBoard(4, 3) factory = FishGameState(board=board, num_players=2, players=TestGameState.player_list, phase=GameStatePhase.INITIAL, current_player=TestGameState.player_list[0]) factory = factory.add_penguin(0, 0, "red") expected_penguins = [Coordinate(0, 0)] self.assertEqual(factory.players["red"].penguins, expected_penguins)
def test_has_penguin(self): board = FishBoard(4, 3) factory = FishGameState(board=board, num_players=3, players=TestGameState.player_list, phase=GameStatePhase.INITIAL, current_player=TestGameState.player_list[0]) factory = factory.add_penguin(0, 0, "red") self.assertTrue(factory.has_penguin(0, 0)) self.assertFalse(factory.has_penguin(1, 1))
def test_validate_input_true(self): board = FishBoard(4, 3) game_state = FishGameState(board, 3, TestGameState.player_list, current_player=TestGameState.player_list[0], phase=GameStatePhase.FINAL) is_valid, error_msg = game_state.validate_input("red", 0, 0) self.assertTrue(is_valid) self.assertEqual(error_msg, "")
def test_validate_input_invalid_player_id_upperbound(self): board = FishBoard(4, 3) game_state = FishGameState(board, 3, TestGameState.player_list, current_player=TestGameState.player_list[0], phase=GameStatePhase.FINAL) is_valid, error_msg = game_state.validate_input("", 0, 0) self.assertFalse(is_valid) self.assertEqual(error_msg, "Player color does not exist")
def setup(no_holes=False) -> Tuple[FishBoard, HexGridView]: hexgrid = HexGridView() board = FishBoard(num_rows=HEXES_ROWS, num_cols=HEXES_COLUMNS) if not no_holes: board = RandomlyGenerateHole(board) board = RandomlyGenerateFish(board) return board, hexgrid
def test_any_player_can_move_success(self): board = FishBoard(4, 3) factory = FishGameState(board=board, num_players=3, players=TestGameState.player_list, phase=GameStatePhase.INITIAL, current_player=TestGameState.player_list[0]) factory = factory.add_penguin(0, 0, "red") factory = factory.add_penguin(1, 1, "white") game_state = factory.finalize() self.assertTrue(game_state.check_any_player_can_move())
def test_add_penguin_failure(self): board = FishBoard(4, 3) factory = FishGameState(board=board, num_players=2, players=TestGameState.player_list, phase=GameStatePhase.INITIAL, current_player=TestGameState.player_list[0]) factory = factory.add_penguin(0, 0, "red") factory = factory.add_penguin(0, 1, "white") with self.assertRaises(ValueError): factory = factory.add_penguin(0, 2, "white")
def test_validate_input_invalid_row_and_col_has_penguin(self): board = FishBoard(4, 3) factory = FishGameState(board=board, num_players=2, players=TestGameState.player_list, phase=GameStatePhase.INITIAL, current_player=TestGameState.player_list[0]) factory = factory.add_penguin(2, 2, "red") is_valid, error_msg = factory.validate_input("white", 2, 2) self.assertFalse(is_valid) self.assertEqual(error_msg, "This tile already has a penguin")
def test_validate_input_invalid_row_and_col_out_of_bounds(self): board = FishBoard(4, 3) game_state = FishGameState(board, 3, TestGameState.player_list, current_player=TestGameState.player_list[0], phase=GameStatePhase.FINAL) is_valid, error_msg = game_state.validate_input("red", 6, 6) self.assertFalse(is_valid) self.assertEqual(error_msg, "Row and column do not exist in this board")
def Demo3(holes: List[Coordinate]) -> FishGameController: """ Given the list of holes, render a board with holes at those cooridates and 1 fish at every non-hole tile. :param holes: List of hole Coordinates :return: A controller to be execute in main. """ hexgrid = HexGridView() board = FishBoard(num_rows=HEXES_ROWS, num_cols=HEXES_COLUMNS) for hole in holes: board = board.create_hole(row=hole.row, col=hole.col) for row in range(board.num_rows): for col in range(board.num_cols): if board.retrieve_num_fish( row=row, col=col) == 0 and board.is_tile(row=row, col=col): board = board.add_fish(row=row, col=col) player_1 = Player("red", 0, 0, []) state = FishGameState(board=board, num_players=1, players=[player_1], current_player=player_1) behavior = FishGameBehavior(state=state, view=hexgrid) controller = FishGameController(behavior=behavior, view=hexgrid, state=state) return controller
def setup(): test_board = FishBoard(4, 3) player_1 = Player("red", 0, 0, []) player_2 = Player("white", 0, 0, []) test_players = [player_1, player_2] state = FishGameState(board=test_board, num_players=len(test_players), players=test_players, current_player=player_1, phase=GameStatePhase.INITIAL) return state
def test_move_penguin_failure(self): board = FishBoard(4, 3) factory = FishGameState(board=board, num_players=3, players=TestGameState.player_list, phase=GameStatePhase.INITIAL, current_player=TestGameState.player_list[0]) factory = factory.add_penguin(row=0, col=0, color="red") factory = factory.add_penguin(row=0, col=1, color="white") factory = factory.add_penguin(row=0, col=2, color="brown") game_state = factory.finalize() with self.assertRaises(ValueError): game_state.move_penguin("red", 0, 0, 0, 2)
def test_move_penguin(self): board = FishBoard(4, 3) factory = FishGameState(board=board, num_players=3, players=TestGameState.player_list, phase=GameStatePhase.INITIAL, current_player=TestGameState.player_list[0]) factory = factory.add_penguin(row=0, col=1, color="red") factory = factory.add_penguin(row=0, col=0, color="white") factory = factory.add_penguin(row=3, col=0, color="brown") game_state = factory.finalize() game_state = game_state.move_penguin("red", 0, 1, 2, 1) expected_penguins = [Coordinate(row=2, col=1)] self.assertEqual(game_state.players["red"].penguins, expected_penguins)
def test_create_game_state(self): board = FishBoard(4, 3) game_state = FishGameState(board, 3, TestGameState.player_list, current_player=TestGameState.player_list[0], phase=GameStatePhase.INITIAL) expected_players = { "red": Player(color="red", age=0, score=0, penguins=[]), "white": Player(color="white", age=0, score=0, penguins=[]), "brown": Player(color="brown", age=0, score=0, penguins=[]) } self.assertEqual(game_state.players, expected_players) self.assertEqual(game_state.board, board) self.assertEqual(game_state.num_players, 3) self.assertEqual(game_state.num_penguins, 3)