def __init__(self): self.game = PuzzleGame() self.game_settings = GameSettingsManager() self.keys_settings = KeySettingsManager() self.input_manager = None self.display = None self.menus = None
class TestGameBoard(unittest.TestCase): def setUp(self): self.tester = PuzzleGame() self.test_settings = GameSettingsManager() self.test_settings.load_settings() def test_game_board_size(self): self.test_settings.set(consts.BOARD_SIZE_STR, SettingsType.TYPE_NUMERIC, 4) self.tester.generate_new_puzzle(self.test_settings) board = self.tester.game_board.board assert len(board) == 4 self.test_settings.set(consts.BOARD_SIZE_STR, SettingsType.TYPE_NUMERIC, 3) self.tester.generate_new_puzzle(self.test_settings) board = self.tester.game_board.board assert len(board) == 3 self.test_settings.set(consts.BOARD_SIZE_STR, SettingsType.TYPE_NUMERIC, 4) self.tester.generate_new_puzzle(self.test_settings) board = self.tester.game_board.board assert len(board) == 4
def setUp(self): self.tester = PuzzleGame() self.test_settings = GameSettingsManager() self.test_settings.load_settings() self.test_settings.set(consts.BOARD_SIZE_STR, SettingsType.TYPE_NUMERIC, 3)
class TestGameBoard(unittest.TestCase): def setUp(self): self.tester = PuzzleGame() self.test_settings = GameSettingsManager() self.test_settings.load_settings() self.test_settings.set(consts.BOARD_SIZE_STR, SettingsType.TYPE_NUMERIC, 3) def test_initial_boards_game_won(self): self.tester.generate_new_puzzle(self.test_settings, fixed_board=get_completed_board(), fixed_empty_spot=BoardPosition(2, 2)) assert self.tester.game_won() self.tester.generate_new_puzzle( self.test_settings, fixed_board=get_almost_complete_board(), fixed_empty_spot=BoardPosition(2, 1)) assert not self.tester.game_won() def test_game_won_with_move(self): self.tester.generate_new_puzzle( self.test_settings, fixed_board=get_almost_complete_board(), fixed_empty_spot=BoardPosition(2, 1)) assert not self.tester.game_won() self.tester.move(consts.LEFT) assert self.tester.game_won() self.tester.move(consts.RIGHT) assert not self.tester.game_won() self.tester.move(consts.RIGHT) assert not self.tester.game_won()
def setUp(self): self.tester = PuzzleGame() self.test_settings = GameSettingsManager() self.test_settings.load_settings()
class PuzzleGameUI(object): """ Puzzle game user interface - Handles user interaction, run main game loop, calls display and logic modules. """ def __init__(self): self.game = PuzzleGame() self.game_settings = GameSettingsManager() self.keys_settings = KeySettingsManager() self.input_manager = None self.display = None self.menus = None def main_game_loop(self): while True: self.display.display_table(self.game.get_display_matrix()) if self.game.game_won(): self.handle_game_won() key = self.input_manager.get_user_input() user_move = self.input_manager.get_user_move(key) if user_move is None: if self.input_manager.basic_user_input_loop( consts.SURE_YOU_WANT_TO_QUIT, [consts.YES, consts.NO]) == consts.YES: self.menus.open_main_menu() if user_move: self.game.move(user_move) def handle_game_won(self): if self.input_manager.basic_user_input_loop( consts.WIN_MESSAGE, [consts.YES, consts.NO]) == consts.YES: self.start_new_game() else: self.quit() def start_new_game(self): print(consts.INIT_NEW_BOARD) self.game.generate_new_puzzle(self.game_settings) print(consts.LETS_START_THE_GAME) self.main_game_loop() def run(self): self.display.display_message(consts.GAME_OPENING) self.display.display_message(consts.RULES) self.menus.open_main_menu() @staticmethod def quit(): print(consts.GOODBYE) exit(0) def bootstrap(self): self.game_settings.load_settings() self.keys_settings.load_settings() self.display = BasicDisplayUtils() self.input_manager = InputManager(self.keys_settings, self.display) self.menus = Menus(self.input_manager, self.display, self.game_settings, self.keys_settings, self.start_new_game, self.quit)
class TestGameBoard(unittest.TestCase): def setUp(self): self.tester = PuzzleGame() self.test_settings = GameSettingsManager() self.test_settings.load_settings() def test_game_move_in_all_directions(self): self.test_settings.set(consts.BOARD_SIZE_STR, SettingsType.TYPE_NUMERIC, 3) self.tester.generate_new_puzzle(self.test_settings) first_empty_spot = self.tester.game_board.empty_spot empty_spot_left_neighbour = first_empty_spot.get_left_neighbour() self.tester.move(consts.RIGHT) current_empty_spot = self.tester.game_board.empty_spot assert current_empty_spot == empty_spot_left_neighbour empty_spot_above_neighbour = current_empty_spot.get_above_neighbour() self.tester.move(consts.DOWN) current_empty_spot = self.tester.game_board.empty_spot assert current_empty_spot == empty_spot_above_neighbour empty_spot_right_neighbour = current_empty_spot.get_right_neighbour() self.tester.move(consts.LEFT) current_empty_spot = self.tester.game_board.empty_spot assert current_empty_spot == empty_spot_right_neighbour def test_game_ignore_impossible_moves(self): self.tester.generate_new_puzzle(self.test_settings) first_empty_spot = self.tester.game_board.empty_spot empty_spot_right_neighbour = first_empty_spot.get_right_neighbour() #since the default empty spot is also the right most bottom corener, a "left move" won't affect it self.tester.move(consts.LEFT) current_empty_spot = self.tester.game_board.empty_spot assert current_empty_spot == first_empty_spot assert current_empty_spot != empty_spot_right_neighbour