class GameDriver(): ''' This class creates a game_state object and runs the game logic above this level ''' def __init__(self): '''Declares cli class and initialises coloured fonts.''' self.cli = Cli() self.game = None init() def create(self): ''' Creates game state from user input and runs the game until the rounds are concluded. ''' # Create Game State based on user input for game parameters self.game = GameState(self.cli.create_game()) # Run the game until a player has won while not self.game.check_game_end(): print("\nGame Start") self.run_game() def run_game(self): ''' Runs the rounds and starts new rounds if a player is identified as a winner. ''' # Start a new round self.game.start_new_round() # Call run_round until there is a winner while not self.game.check_round_winner(): self.run_round() def run_round(self): ''' Handles the running of a turn, print to cli and requesting user input. ''' # Display current game state in cli output self.game.print_round_state_to_cli() # Process command from current_player self.game.process_command() # Check if this was a winning move if self.game.check_round_winner(): print("Player '{}' is the Winner!".format( self.game.current_player)) else: self.game.end_turn()
def test_round_end(self): '''This test check that a winner is found''' test_input = { 'decks': 1, 'rounds': 2, 'players': 1, 'human players': 1, 'comp_levels': {} } state = GameState(test_input) state.start_new_round() player_obj = state.players.get_player_by_id(state.current_player) player_obj.hand = Deck(1).cards state.print_round_state_to_cli() test_cmd_strs_1 = [ str(i) + s + "L" + str(suit_layout_dict[s]) for s in suit_layout_dict.keys() for i in range(7, 15) ] test_cmd_strs_2 = [ str(i) + s + "L" + str(suit_layout_dict[s]) for s in suit_layout_dict.keys() for i in range(2, 7) ] test_cmd_strs_2.reverse() test_cmd_strs = test_cmd_strs_1 + test_cmd_strs_2 test_commands = [Command(c, state.layouts) for c in test_cmd_strs] player_obj = state.players.get_player_by_id(state.current_player) while not state.check_round_winner(): for test_command in test_commands: state.current_command = test_command state.update() state.print_round_state_to_cli() state.start_new_round() self.assertEqual(state.round_number, 2) self.assertEqual(state.dealer_id, 0) while not state.check_round_winner(): for test_command in test_commands: state.current_command = test_command state.update() state.print_round_state_to_cli() print("Total rounds: ", state.total_rounds) print("round_number: ", state.round_number) self.assertTrue(state.check_game_end())
def test_card_ten(self): '''This test check that a card can be played with a double digit rank''' test_input = { 'decks': 1, 'rounds': 1, 'players': 1, 'human players': 1, 'comp_levels': {} } state = GameState(test_input) state.start_new_round() player_obj = state.players.get_player_by_id(state.current_player) player_obj.hand = [c for c in Deck(1)] state.print_round_state_to_cli() test_commands = [] test_commands.append(Command("7CL0", state.layouts)) test_commands.append(Command("8CL0", state.layouts)) test_commands.append(Command("9CL0", state.layouts)) test_commands.append(Command("10CL0", state.layouts)) test_commands.append(Command("JCL0", state.layouts)) test_commands.append(Command("QCL0", state.layouts)) test_commands.append(Command("KCL0", state.layouts)) test_commands.append(Command("ACL0", state.layouts)) test_commands.append(Command("6CL0", state.layouts)) test_commands.append(Command("5CL0", state.layouts)) test_commands.append(Command("4CL0", state.layouts)) test_commands.append(Command("3CL0", state.layouts)) test_commands.append(Command("2CL0", state.layouts)) for test_command in test_commands: state.current_command = test_command state.update() state.print_round_state_to_cli() extra_command = Command("7HL2", state.layouts) state.current_command = extra_command state.update() state.print_round_state_to_cli()