def test_function_gives_message_and_keeps_asking_input_until_valid(self):
     p = Player("test_player")
     # mock to input different inputs in sequence - first three should cause print message, last (1) correct so breaks - also mock the print to check is correct
     with mock.patch("builtins.input", side_effect=['one', 3, 3.5, 1]):
         with io.StringIO() as buffer:
             with contextlib.redirect_stdout(buffer):
                 p.make_game_decision()
                 printed_text = buffer.getvalue()
                 self.assertTrue(printed_text.count("Please enter") == 3)
 def test_add_player_adds_players_to_list(self):
     g = Game()
     p_one = Player("testplayer1")
     p_two = Player("testplayer2")
     g.add_player_to_game(p_one)
     num_players = len(g.get_list_of_players())
     player_name = g.get_list_of_players()[0].get_username()
     g.add_player_to_game(p_two)
     if num_players == 1 and player_name =='testplayer1':
         self.assertTrue((len(g.get_list_of_players())==2) and (g.get_list_of_players()[1].get_username() == 'testplayer2'))
     else:
         self.assertTrue(False)
 def test_make_game_decision_gives_error_for_invalid_integer_input(self):
     p = Player("test_player")
     #make the input so it will be three
     with mock.patch("builtins.input", side_effect=[3, 1]):
         #this should return a print statement - redirect to buffer
         # #side effect - two different inputs, second will break loop
         with io.StringIO() as buffer:
             with contextlib.redirect_stdout(buffer):
                 p.make_game_decision()
                 printed_text = buffer.getvalue()
                 self.assertTrue(
                     printed_text.count(
                         "Please enter only 1 for yes or 0 for no.") == 1)
 def test_make_game_decision_gives_expected_message_when_string_as_input(
         self):
     # don't have to test for both error and message - the error is only way to get the message
     # by giving list of inputs can break the loop - also checking can keep inputting till correct
     p = Player("test_player")
     # mock to input a string - need to check that a ValueError results
     with mock.patch("builtins.input", side_effect=['one', 'two', 1]):
         with io.StringIO() as buffer:
             # redirect the stdout to the buffer - whatever goes to stdout will go to buffer
             with contextlib.redirect_stdout(buffer):
                 p.make_game_decision()
                 printed_text = buffer.getvalue()
                 #check that the printed text contains the intended message two times - as final value input is 1 will return true
                 self.assertTrue(
                     printed_text.count(
                         "Please enter decision to take a card as an integer (0 for no, 1 for yes)"
                     ) == 2)
Beispiel #5
0
                need_username = False
                list_of_players_names.append(player_name)
    print(
        "{} players have been added! You will be playing against a dealer called Bot."
        .format(num_players))

    # way to count how many rounds each player won
    games_won_dict = {}
    for name in list_of_players_names:
        games_won_dict[name] = 0
    games_won_dict["bot"] = 0

    play_game_flag = True
    while play_game_flag:
        # have taken the names of players, make into list of players
        list_of_players = [Player(name) for name in list_of_players_names]
        # play the game with the list of players
        g = Game(list_of_players)
        g.play_game_alternative()
        # add the winner to counts of number of games won dictionary
        if g.get_winner() is not None:
            games_won_dict[g.get_winner().get_username()] += 1
        # see if user wants to play again
        new_game_choice_needed = True
        wrong_input_count = 0
        while new_game_choice_needed and wrong_input_count <= 3:
            try:
                new_game_dec = int(
                    input(
                        "Would you like to play another game? Enter 1 for yes or 0 for no."
                    ))
 def test_make_game_decision_gives_false_for_input_0(self):
     p = Player("test_player")
     with mock.patch("builtins.input", return_value=0):
         self.assertFalse(p.make_game_decision())
 def test_make_game_decision_gives_true_for_input_1(self):
     p = Player("test_player")
     with mock.patch("builtins.input", return_value=1):
         self.assertTrue(p.make_game_decision())