Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is an exercise in implementing functions to solve a large problem. Some of the function specifications are already dictated by the assignment below, but

This is an exercise in implementing functions to solve a large problem. Some of the function specifications are already dictated by the assignment below, but a student is welcome to define any other functions that might be helpful in solving the problem.

There is a starter program file provided with this assignment (in the Functions Module) that predefines the names and interfaces of several functions in a suitable order in the file. A section later in this document will give a road map to that file to explain what will be filled in.

Assignment Description

This program will simulate part of the game of Yahtzee!

This is a dice game that involves rolling five dice and scoring points based on what show up on those five dice. The players would record their scores on a score card, and then total them up, and the player with the larger total wins the game.

A Yahtzee score card has two portions:

The upper portion has spaces for six scores, obtained by adding up all of the 1's, 2's, 3's, etc.

The lower portion has special scores for various combinations:

Three of a kind -- at least 3 dice are the same number; the score is the sum of all five dice

Four of a kind -- at least 4 dice are the same number; the score is the sum of all five dice

Small straight -- four consecutive numbers are represented, e.g. 2345; the score is 25 points

Large straight -- five consecutive numbers are represented, e.g. 23456; the score is 30 points

Full House -- three of one kind, two of another; the score is 30 points

Yahtzee! -- five of a kind; the score is 50 points

Chance -- nothing special; the score is the sum of all five dice

In a typical turn of this game, a players take rolls five dice, and then has two opportunities to reroll any dice that are desired, and then those dice are evaluated according to the options above.

To keep the assignment size manageable, the basic assignment will not need to represent the entire game. It will simply roll five dice and evaluate them, and then allow the player to roll another new set of five dice, and evaluate those. It will not need to keep score, either.

Here are some sample results from the instructor's solution. For readability, I put the upper and lower portions side by side.

Rolled: 6 6 4 4 4 Three of a Kind 24 Sets of 1's: 0 Four of a Kind 0 Sets of 2's: 0 Full House 30 Sets of 3's: 0 Small Straight 0 Sets of 4's: 12 Large Straight 0 Sets of 5's: 0 Yahtzee 0 Sets of 6's: 12 Chance 24 Another (y/n)? y Rolled: 2 2 6 6 1 Three of a Kind 0 Sets of 1's 1 Four of a Kind 0 Sets of 2's 4 Full House 0 Sets of 3's 0 Small Straight 0 Sets of 4's 0 Large Straight 0 Sets of 5's 0 Yahtzee 0 Sets of 6's 12 Chance 17 Another (y/n)? y Rolled: 3 5 2 5 4 Three of a Kind 0 Sets of 1's 0 Four of a Kind 0 Sets of 2's 2 Full House 0 Sets of 3's 3 Small Straight 25 Sets of 4's 4 Large Straight 0 Sets of 5's 10 Yahtzee 0 Sets of 6's 0 Chance 19 Another (y/n)? n 

Program Design Hint

The order in which the dice were rolled is never relevant in this game. What does matter is how many 1's, 2's, 3's, were rolled. A list proves to be an excellent way to remember this information: where list element 1 records the number of 1's, list element 2 records the number of 2's, and so on.

This list will play a major role in this assignment. It will be first populated each time a player rolls five dice; it would be modified when the player rerolls dice (for extra credit); and it will be examined to come up with the scores above. And Python provides a lot of help:

The 'in' and 'not in' test conditions can help to recognize some of the scoring patterns.

The list slice operation (such as counts[2:6]) can narrow down which part of the list would be examined with the 'in' and 'not in' test conditions.

And of course, the use of the list instead of separate variables makes it possible to come up with all the left-side scores above within a for loop.

A Very Special Programming Tip

There are several different ways of evaluating the dice rolls above. It does help to have functions for those evaluation methods -- but it may seem a little awkward calling them for the output display. At first glance, it seems you would need to have seven different program statements, one for each row of output above, which especially looks repetitive when considering how the left side behaves.

It turns out that a list can very useful here too!

Here are a few lines of code from the instructor's solution:

name_lower = ['Three of a Kind','Four of a Kind','Full House', 'Small Straight', 'Large Straight', 'Yahtzee', 'Chance'] score_lower = [three_of_a_kind, four_of_a_kind, full_house, small_straight, large_straight, yahtzee, sum_counts] print(f"{name_lower[i]:17} {score_lower[i](counts):2}") 

The first list definition is just a list of strings much like one seen before.

The second defines an list of 7 elements, each of which refers to a function that will take as a parameter the list described in the Program Design Hint above.

The last line above shows both arrays being indexed with the same index variable, and that the function is being passed a list argument (here named counts). All it needs is a loop to pick the values for i, and it can produce all the right-side information (and you can also plug in a print statement to handle the left side).

A Tour of the Starter Program File

The very top of the file deals with the dice rolling. It first obtains access to the 'random' library to learn how to roll dice (random.randint(1,6) picks a number from one to six). Then there are two function definitions to fill in.

The first will roll five dice and record how many 1's, 2's, 3's, 4's, 5's, and 6's were rolled in the single list provided. The list has been initialized to zero's to start with so you can count from there.

The second function will handle rerolls, and a user prompt has been provided to give a hint about how to handle that interface. This code will remove the dice the user wants to reroll (by reducing their frequency), and replace those with newly rolled dice.

NOTE: This function will actually be an extra credit option, so you can postpone it until the others are complete. But it would still be good to do if you get a chance, because the full house and straights are extremely unlikely on a single roll of five dice, so the game really would rerolls to be playable.

Next appear several functions used to evaluate the dice, each with a description. Many of these can be solved just an if condition choosing whether a score is needed or not. For hints about the if conditions, review the Program Design Hint section above.

After all of these functions are defined, they are wrapped up in the lists already mentioned above, and then comes the rest of the program that will make use of all the functions. It will roll dice, possibly reroll dice, and score those dice, as is illustrated in the sample user interface above. And since a lot of stuff has been squeezed into lists, all you need to do is count list indexes in a loop.

Incremental Implementation

One nice thing about breaking a large program up into several small functions is that one can implement just one or two functions at a time and make sure they work before moving on. The fun part is figuring out how to test them when the whole program is not in place.

For this assignment most of the functions actually can be tested independently of the others.

For example:

If you implemented the roll_dice function and ran your program, you would be able to go into the Python Shell and just type roll_dice() over and over again, and see if it not only displays the rolled dice while it runs, but also returns the correct frequency counts (how many 1's, etc.)

If you implemented just the sum_counts function, you could test it in the Python Shell with various values like:

sum_counts([0,5,0,0,0,0,0]) -- 5 1's add up to 5 sum_counts([0,0,0,0,0,0,5]) -- 5 6's add up to 30 sum_counts([0,0,0,2,3,0,0]) -- 2 3's and 3 4's add up to 18

If you implemented just the full house function, you could test it in the very same way (I'll let you figure out what the list looks like when a full house should be scored).

All of the functions in the given file have been defined in such a way so that they will all compile and 'run' successfully, even if they don't do anything meaningful yet. That way you can easily pick and choose what to work on next.

A Small Extra Credit Option

Some of the scoring options are extremely unlikely to occur on a single roll of five dice, so a more complete simulation of the game should allow rerolls. A separate function has been defined for this purpose. It will take the given roll from before, and then ask the user which dice to reroll. It will remove a previous die roll simply by reducing the number of occurrences that number had from the given information, and then replace it with a new roll.

It is simplest to have this function do that operation just once, and then have the larger part of the program decide whether to call it a second time. A return value has been defined indicate whether any reroll actually happened. (If the player already chose not to reroll, don't ask a second time.)

Rolled: 3 5 4 2 2 Enter the values of the dice to reroll, separated by spaces or enter a blank line to keep the dice as they are. 2 Rerolling 1 dice: 6 Enter the values of the dice to reroll, separated by spaces or enter a blank line to keep the dice as they are. Three of a Kind 0 Sets of 1's: 0 Four of a Kind 0 Sets of 2's: 2 Full House 0 Sets of 3's: 3 Small Straight 25 Sets of 4's: 4 Large Straight 30 Sets of 5's: 5 Yahtzee 0 Sets of 6's: 6 Chance 20 Another (y/n)? y Rolled: 1 4 1 1 2 Enter the values of the dice to reroll, separated by spaces or enter a blank line to keep the dice as they are. 2 4 Rerolling 2 dice: 6 1 Enter the values of the dice to reroll, separated by spaces or enter a blank line to keep the dice as they are. 6 Rerolling 1 dice: 1 Three of a Kind 5 Sets of 1's: 5 Four of a Kind 5 Sets of 2's: 0 Full House 0 Sets of 3's: 0 Small Straight 0 Sets of 4's: 0 Large Straight 0 Sets of 5's: 0 Yahtzee 50 Sets of 6's: 0 Chance 5 Another (y/n)? n

A Challenging Extra Credit Option

Actually keep score for an entire solitaire came of Yahtzee!

This game would consist of 13 turns (there are 13 categories to score).

After displaying the possible scores for each category, ask the user which score to keep (it might not be the largest). Once the box is filled, it cannot be scored again.

It would be very helpful to have the display somehow indicate which boxes have already been filled, and which scores are available.

In addition, there are these things to note about the score:

There is a bonus score of 35 points if the upper half scores add up to more than 63. You can display this below those upper scores; it would also be nice to have a total score for everything (which can be placed under the lower scores).

If a player rolls a Yahtzee! and has already scored 50 points for Yahtzee, they may instead score 50 points in any of the other lower-score boxes, in place of the regular scoring mechanism. However, this cannot be done if a 0 has already been placed in the Yahtzee box (or if that box is empty).

Of course, on the 13th round, when there is only one unscored box, there should be no need to ask the user where to place that last score.

With this Extra Credit option, a player should be able to play a complete game solitaire.

Here is an illustration of one turn in an instructor solution. It shows what can be scored with the given dice roll, with the previously recorded scores shifted to the left. It would not be important to mimic this picture exactly, just as long as the player knows what scores are available to fill in.

Rolled: 4 4 4 6 3 Enter the values of the dice to reroll, separated by spaces or enter a blank line to keep the dice as they are. 6 3 Rerolling 2 dice: 2 3 Enter the values of the dice to reroll, separated by spaces or enter a blank line to keep the dice as they are. 2 3 Rerolling 2 dice: 3 6 7: Three of a Kind 21 1: Sets of 1's: 3 8: Four of a Kind 0 2: Sets of 2's: 4 9: Full House 30 3: Sets of 3's: 3 10: Small Straight 25 4: Sets of 4's: 12 11: Large Straight 0 5: Sets of 5's: 10 12: Yahtzee 0 6: Sets of 6's: 6 13: Chance 21 Bonus: 0 Total 72 Please enter which score you wish to save: (1-13): 7

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Is rehearsal a useful tool? How or how not?

Answered: 1 week ago

Question

Find the complexity of the Following: for (int i = 0; i

Answered: 1 week ago