首页 > > 详细

调试C/C++程序、C/C++编程辅导、讲解留学生C/C++程序、C/C++编程辅导


Write code in the dealer_turn function so that it hits the dealer with a card with the dealer has not bust and while their
hand value is less than 17. Call the output_player function with the dealer as an argument each time the dealer draws
a card. You will need to call the get_hand_value method on the dealer object.

In the play_game function, after calling the player_turn function, if the player has not bust, it is the dealer's turn. Call
the dealer_turn function. After the dealer's turn if the dealer has bust or the player's hand value is greater than the
dealer's, then print " wins!" It should call the player's win method to add 1 to their win streak score;
otherwise, if the dealer's hand value is greater than the player's, print " loses!" and call the player lose
method; otherwise, print "Draw!"

When the game is complete, remember to reset each player's hand and reset the deck using the reset_hand method
and reset method respectively. Make sure the deck is shuffled before the next game by calling the shuffle method.
Running your code should output something like this:

Welcome to Blackjack
Enter your name: Abigail

Dealer's hand: 8 of Diamonds, 10 of Clubs
Total: 18
Abigail's hand: Ace of Hearts, King of Hearts
Total: 21

hit or stand? stand
Abigail wins!

Would you like to play again (yes/no): yes
Dealer's hand: 4 of Clubs, 8 of Diamonds
Total: 12
Abigail's hand: 5 of Diamonds, 8 of Spades
Total: 13

hit or stand? hit
Abigail's hand: 5 of Diamonds, 8 of Spades, Ace of Diamonds
Total: 14

hit or stand? hit
Abigail's hand: 5 of Diamonds, 8 of Spades, Ace of Diamonds, 2 of Spades
Total: 16

hit or stand? hit
Abigail's hand: 5 of Diamonds, 8 of Spades, Ace of Diamonds, 2 of Spades, King of Spades
Total: 26
Abigail busts!

Would you like to play again (yes/no): yes
Dealer's hand: Ace of Diamonds, 5 of Spades
Total: 16
Abigail's hand: 4 of Spades, Ace of Diamonds
Total: 15

hit or stand? hit
Abigail's hand: 4 of Spades, Ace of Diamonds, Queen of Diamonds
Total: 15

hit or stand? hit
Abigail's hand: 4 of Spades, Ace of Diamonds, Queen of Diamonds, 4 of Hearts
Total: 19

hit or stand? stand
Dealer's hand: Ace of Diamonds, 5 of Spades, 10 of Spades
Total: 16
Dealer's hand: Ace of Diamonds, 5 of Spades, 10 of Spades, 9 of Hearts
Total: 25
Abigail wins!

Would you like to play again (yes/no): no

PSP Assignment 2 - 201801 Page 16 of 24

Stage 8
Implement the read_high_scores function so that it reads each line in the file located at the argument filename. You
will need to call the open function to read the file. For each line, get the substring before the space character to find
the player's name and get the substring after the space character to find the player's score. The name should be
added to the argument names list. The score should be typecast to an int and added to the argument scores list. At
the end of the function, close the file. You will need to call the append method to add to the lists. You do not need to
return anything from this function. A list parameter is a reference to its argument. When a list is changed inside a
function, these changes will be saved in global scope.

Add code to the output_high_scores function to loop through each index in the lists and prints, " ".
Where name is each name in the list and score is each score in the list.

In the global main code, after the player has decided to stop playing, create an empty list called names and an empty
list called scores. Invoke the read_high_scores function with "highscores.txt" and your two lists as arguments. Follow
this with a call to the output_high_scores function. At the end of the game, you should see something like this:

Tiffany 3
Jack 2
Bill 0

Stage 9
Implement the get_high_score_index function so that it finds the first score in the scores list that is less than or equal
to the score argument. It should return the index of that value. If it did not find a score that was less than the argument,
and the length of the scores list is less than 5, it should return the length of the scores list. In all other cases, it should
return -1.

Before calling output_high_scores in the main program, it should find the position that the player's high score should
be inserted into. Call the get_high_score_index method with the player's score as an argument. You will need to call
the get_score method to return this value. Test this by printing "Player score is " and "INSERT AT
". Ensure that after playing the game it prints the index of where your score should be added given that
position 0 means it is the top score. Remove these prints statements when you are sure it is working.

Stage 10
Implement the insert_high_score function so that it inserts the argument name at the pos in the names list. It should
also insert the argument score into the scores list at the pos. If the length of the names list is greater than 5, it should
remove the last item in both the names and scores lists. In any case, it should print, "New High Score!"

In the global code, if the index returned from get_high_score_index is greater than -1, you should call the
insert_high_score function with the correct arguments. If the output_high_scores function is called afterwards, the
current player's name and score should be added to the lists.

Stage 11
Implement the write_high_scores function so that it opens the file at the argument filename for writing. It should then
loop through the first 5 positions in the names list and write each object in names followed by a space, then the object
in the scores list at the same index, followed by a new line character. One line of the file could look like, "Tony 3\n".
You may have to convert the score to a string before it can be written to file. After writing all the objects, remember to
call the close method to save the file.

In your main code, after outputting the high scores, call the write_high_scores method. Then print, "Goodbye". You
may like to test this works by saving to a file called "highscorestest.txt". When you believe it is working, change the file
name to "highscores.txt", so that it overwrites the highscores file each time you play the game.

Stage 12
Modify your output_high_scores function so that the high scores are formatted in a pretty table. The names should be
left aligned within a field of 16 spaces. The scores should be right aligned within a field of 4 spaces. There should be
vertical bars on the left and right sides. The top and bottom should be filled with dash characters, but the words, "High
Scores", should appear in the center of the top row of dashes. Check the section titled, 'Useful Built-in Python
Functions' for help with formatting text!

PSP Assignment 2 - 201801 Page 17 of 24

Stage 13
Finally, check the section titled ‘Sample Output’ and, if necessary, modify your code so that:
 The output produced by your program is formatted EXACTLY the same way as the sample output provided.
 Your program behaves as described in these specifications and in the same way as the sample output.
 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!