edX MIT 6.00.1x 2020 Introduction to Computer Science and Programming Using Python
Week 4: Good Programming Practices: Problem Set 4 Problem 7 Solution
Problem 7 - You and your Computer
Now that your computer can choose a word, you need to give the computer the option to play. Write the code that re-implements the playGame function. You will modify the function to behave as described below in the function's comments. As before, you should use the HAND_SIZE constant to determine the number of cards in a hand. Be sure to try out different values for HAND_SIZE with your program.
Sample Output and Hints
Here is how the game output should look...
Enter n to deal a new hand, r to replay the last hand, or e to end game: n
Enter u to have yourself play, c to have the computer play: u
Current Hand: a s r e t t t
Enter word, or a "." to indicate that you are finished: tatters
"tatters" earned 99 points. Total: 99 points
Run out of letters. Total score: 99 points.
Enter n to deal a new hand, r to replay the last hand, or e to end game: r
Enter u to have yourself play, c to have the computer play: c
Current Hand: a s r e t t t
"stretta" earned 99 points. Total: 99 points
Total score: 99 points.
Enter n to deal a new hand, r to replay the last hand, or e to end game: x
Invalid command.
Enter n to deal a new hand, r to replay the last hand, or e to end game: n
Enter u to have yourself play, c to have the computer play: me
Invalid command.
Enter u to have yourself play, c to have the computer play: you
Invalid command.
Enter u to have yourself play, c to have the computer play: c
Current Hand: a c e d x l n
"axled" earned 65 points. Total: 65 points
Current Hand: c n
Total score: 65 points.
Enter n to deal a new hand, r to replay the last hand, or e to end game: n
Enter u to have yourself play, c to have the computer play: u
Current Hand: a p y h h z o
Enter word, or a "." to indicate that you are finished: zap
"zap" earned 42 points. Total: 42 points
Current Hand: y h h o
Enter word, or a "." to indicate that you are finished: oy
"oy" earned 10 points. Total: 52 points
Current Hand: h h
Enter word, or a "." to indicate that you are finished: .
Goodbye! Total score: 52 points.
Enter n to deal a new hand, r to replay the last hand, or e to end game: r
Enter u to have yourself play, c to have the computer play: c
Current Hand: a p y h h z o
"hypha" earned 80 points. Total: 80 points
Current Hand: z o
Total score: 80 points.
Enter n to deal a new hand, r to replay the last hand, or e to end game: e
Hints about the output
Be sure to inspect the above sample output carefully - very little is actually printed out in this function specifically. Most of the printed output actually comes from the code you wrote in playHand and compPlayHand - be sure that your code is modular and uses function calls to these helper functions!
You should also make calls to the dealHand helper function. You shouldn't make calls to any other helper function that we've written so far - in fact, this function can be written in about 15-20 lines of code.
Here is the above output, with the output from playHand and compPlayHand obscured:
Enter n to deal a new hand, r to replay the last hand, or e to end game: r
You have not played a hand yet. Please play a new hand first!
Enter n to deal a new hand, r to replay the last hand, or e to end game: n
Enter u to have yourself play, c to have the computer play: u
call to playHand
Enter n to deal a new hand, r to replay the last hand, or e to end game: r
Enter u to have yourself play, c to have the computer play: c
call to compPlayHand
Enter n to deal a new hand, r to replay the last hand, or e to end game: x
Invalid command.
Enter n to deal a new hand, r to replay the last hand, or e to end game: n
Enter u to have yourself play, c to have the computer play: me
Invalid command.
Enter u to have yourself play, c to have the computer play: you
Invalid command.
Enter u to have yourself play, c to have the computer play: c
call to compPlayHand
Enter n to deal a new hand, r to replay the last hand, or e to end game: n
Enter u to have yourself play, c to have the computer play: u
call to playHand
Enter n to deal a new hand, r to replay the last hand, or e to end game: r
Enter u to have yourself play, c to have the computer play: c
call to compPlayHand
Enter n to deal a new hand, r to replay the last hand, or e to end game: e