コード例 #1
0
 def test_rotate_first_player(self):
     round1 = Round([Human(1), Human(2)])
     assert round1.first_player == 0
     assert round1.current_player == 0
     round1.rotate_first_player()
     assert round1.first_player == 1
     assert round1.current_player == 1 % 2
コード例 #2
0
 def test_prepare_turn(self, mocker):
     mocker.spy(Deck, 'check_stack')
     mocker.spy(Round, 'check_knocked')
     round1 = Round(1)
     round1.prepare_turn()
     assert Deck.check_stack.call_count == 1
     assert Round.check_knocked.call_count == 1
コード例 #3
0
ファイル: play.py プロジェクト: rjalfa/Python-Rummy
 def __init__(self):
     ConsoleConfig.colorama()
     setup = SetupPlayers()
     self.players = setup.create_players()
     self.ai_only = not any(isinstance(x, Human) for x in self.players)
     self.score = Score(self.players)
     self.round = Round(self.players)
     self.round.deal_cards(self.players)
     self.play_game()
コード例 #4
0
 def __init__(self):
     self.colorama()
     players = Players()
     players.choose_players()
     players.choose_opponents()
     self.players = players.get_players()
     self.ai_only = players.is_ai_only()
     self.score = Score(self.players)
     self.round = Round(self.players)
     self.round.deal_cards(self.players)
     self.play_game()
コード例 #5
0
 def test_check_knocked(self):
     round1 = Round([Human(1)])
     round1.check_knocked()
     assert round1.last_turn == 1
     round1.knocked = True
     round1.check_knocked()
     assert round1.last_turn == 2
コード例 #6
0
 def test_prepare_new_round(self):
     round1 = Round([Human(1)])
     round1.turn = 2
     round1.last_turn = 3
     round1.knocked = True
     round1.prepare_new_round()
     assert round1.turn == 1
     assert round1.last_turn == 1
     assert not round1.knocked
     assert isinstance(round1.deck, Deck)
コード例 #7
0
 def test_deal_cards(self, mocker):
     mocker.spy(Round, 'deal')
     human1 = Human(1)
     human2 = Human(2)
     human3 = Human(3)
     round1 = Round([human1, human2, human3])
     round1.deal_cards([human1, human2])
     assert round1.deal.call_count == 2
     round1.deal_cards([human1, human2, human3])
     assert round1.deal.call_count == 5
コード例 #8
0
 def test_switch_current_player(self):
     round1 = Round([Human(1), Human(2)])
     assert round1.current_player == 0
     round1.switch_current_player()
     assert round1.current_player == 1 % 2
コード例 #9
0
 def test_end_turn(self, mocker):
     mocker.spy(Round, 'switch_current_player')
     round1 = Round([Human(1)])
     turn_before = round1.turn
     round1.end_turn()
     assert round1.turn == (turn_before + 1)
コード例 #10
0
class Play:
    def __init__(self):
        self.colorama()
        players = Players()
        players.choose_players()
        players.choose_opponents()
        self.players = players.get_players()
        self.ai_only = players.is_ai_only()
        self.score = Score(self.players)
        self.round = Round(self.players)
        self.round.deal_cards(self.players)
        self.play_game()

    @staticmethod
    def colorama():
        if 'PYCHARM_HOSTED' in os.environ:
            convert = False  # in PyCharm, we should disable convert
            strip = False
        else:
            convert = None
            strip = None
        colorama.init(convert=convert, strip=strip)

    def play_game(self):
        while self.round.last_turn != len(self.players):
            self.round.prepare_turn()
            player = self.players[self.round.current_player]
            player.turn(self.round)
            # Todo: Views should be agnostic. Each template will have placeholders for data.
            # Todo: Player should return data to be displayed in views placeholders.
            controller = self._select_player_controller(player)
            controller.show_start_turn(player)
            controller.show_knocked(player)
            controller.draw_card(player)
            controller.show_end_turn(player)
            controller.discard_or_knock(player)
            controller.show_discard(player)
            self.round.end_turn()
        View.render(self.end_round())
        sleep(1.2)
        if self.score.is_end_of_game():
            View.render(self.score.show_winners())
        else:
            self.start_new_round()

    def _select_player_controller(self, player):
        if isinstance(player, Human):
            controller = HumanController
        else:
            controller = AiController
        return controller

    def start_new_round(self):
        self.round.rotate_first_player()
        if not self.ai_only:
            self.confirm_start_new_round()
        self.round.prepare_new_round()
        self.round.deal_cards(self.players)
        self.play_game()

    @staticmethod
    def confirm_start_new_round():
        UserInput.create_input(MenuActionDialog.next_round())

    def end_round(self):
        self.score.update_player_scores()
        return RoundView.this_round_score(self.score.get_end_of_round_scores(),
                                          self.score.get_current_game_scores())
コード例 #11
0
ファイル: play.py プロジェクト: rjalfa/Python-Rummy
class Play:
    def __init__(self):
        ConsoleConfig.colorama()
        setup = SetupPlayers()
        self.players = setup.create_players()
        self.ai_only = not any(isinstance(x, Human) for x in self.players)
        self.score = Score(self.players)
        self.round = Round(self.players)
        self.round.deal_cards(self.players)
        self.play_game()

    def play_game(self):
        while self.round.last_turn != len(self.players):
            self.round.prepare_turn()
            player = self.players[self.round.current_player]
            player.turn(self.round, self.ai_only)
            self.round.end_turn()
        self.end_round()
        sleep(1.2)
        self.start_new_round_or_end_game()

    def start_new_round_or_end_game(self):
        if self.score.is_end_of_game():
            self.score.end_game()
        else:
            self.round.rotate_first_player()
            if not self.ai_only:
                self.confirm_start_new_round()
            self.round.prepare_new_round()
            self.round.deal_cards(self.players)
            self.play_game()

    def confirm_start_new_round(self):
        print("\nReady %s?" % self.players[self.round.current_player])
        ready = ''
        while ready.lower() != 'y':
            ready = input("Enter " + Colour.green('y') +
                          " when you are ready for the next round: ")

    def end_round(self):
        self.score.update_player_scores()
        self.score.render_this_round_score()