CS 8 Yahtzee游戏开发

在Yahtzee游戏中,玩家试图获得五个六面骰子的特定组合。对在给定的回合中,他们最多可以进行三次掷骰子:在第一次掷骰子时,他们掷完所有五个骰子;在…上在第二次掷骰子时,他们可以选择重新掷一次、任何一次或所有骰子;和上第三次掷骰子时,他们可以再次选择不重新掷骰子、任何骰子或所有骰子。最好的给定回合的结果是获得所谓的Yahtzee,其中所有五个骰子都有它们的价值相同。这个值是什么并不重要,只是它们都是一样的。下面是一个例子。对于第一个滚动,假设玩家获得:
CS 8: Introduction to Computer Science
Fall 2023 Final Project
Due Tuesday, December 12, 11:59PM (California time)
In the game Yahtzee, players try to get particular combinations of five six-sided dice. For
a given turn, they can make up to three rolls: on the first roll, they roll all five dice; on
the second roll, they can choose to re-roll none, any, or all of the dice again; and on the
third roll, they again can choose to re-roll none, any, or all of the dice again. The best
outcome for a given turn is to get what’s called a Yahtzee, in which all five dice have the
same value on them. It doesn’t matter what that value is, just that they are all the same.
Here is an example. For the first roll, say a player gets:
2 4 1 6 4
Let’s say they choose not to re-roll the two dice that were 4’s, but they do re-roll the dice
that were 2, 1, and 6, giving new values for those three dice:
4 3 4
So at this point, after their second roll, the five dice are:
4 4 3 4 4
(Here I’m keeping the dice “in order”: the dice which was previously a 2 is now a 4, the
dice that was previously a 1 is now a 3, and the dice that was previously a 6 is now a 4.
The dice that were previously 4’s stay as 4’s.) For the player’s third roll, they choose to
only re-roll the dice that is a 3, and let’s say it comes up 4. So after the third roll, the
five dice are:
4 4 4 4 4
They all are the same! Yahtzee!
In this final project, you will write Python code to calculate the probability that a player
will get a Yahtzee on a given turn using the following strategy: You always try to get a
Yahtzee with the highest dice value for which you have the largest number of matching
dice. So if your dice are
4 2 4 5 2
you would keep the 4’s, and re-roll the 2’s and the 5.
There is also a related game called Tenzi, in which each player has ten 6-sided dice. On
the first roll, they roll all ten dice. On the second and all subsequent rolls they can choose
1
to re-roll none, any, or all of the dice again. They keep rolling until all ten dice have the
same value on them - a Tenzi! In this final project, you’ll also write Python code to keep
track of how many rolls it takes in order to get a Tenzi.
Please use the following code in order to generate a random integer 1,2,3,4,5, or 6:
import random
def rollDice():
return random.randrange(1,7)
Your main program should prompt the user to either play Yahtzee (by inputting Y), play
Tenzi (by inputting T), or quit (by inputting anything else). This prompt should continue
to be appear until the user quits. If the choice is Y, the user should be prompted to input
the number of trials to simulate for the Monte Carlo simulation and the number of dice
to use. The following should be printed to the shell:
Probability of Yahtzee with X dice: y.yyyy
where X is the number of dice which the user input, and y.yyyy is the probability of
getting a Yahtzee. Use four decimals for printing the probability. If the choice is T, the
user should be prompted to input the number of trials to simulate and the number of dice
to use. The following should be printed to the shell:
Rolls to get a Tenzi with X dice:
{1: Y, 2: Y, 3: Y, 4: Y, 5: Y, 6: Y, 7: Y, 8: Y, 9: Y, 10: Y, ’more than 10’ : Y}
where X is the number of dice which the user input, and the Y’s are the number of times
in the set of trials that it took that many rolls to get a Tenzi.
The following gives an example of running your code:
Note that the numbers you get may differ from these, but this should at least give a good
idea of what you should obtain from your code.
2
You’ll turn in a single Python program called yahtzee.py which includes the code given
above for simulating a dice roll, the main program as described above, and the following functions. Please be sure to include appropriate docstrings and comments for each
function.
• firstRoll : This should take an integer numDice as an input parameter which
tells how many dice you’re using in your game (this will be 5 for Yahtzee and 10
for Tenzi, but your function should be written for a general value for numDice),
and returns a list of length numDice of random integers each obtained by calling
rollDice. For example, the call
firstRoll(5)
will return a list such as
[2,4,1,6,4]
• newRoll : This should take the list diceList and an integer choice as input
parameters. Here diceList is a list of dice rolls, such as [2,4,1,6,4], and choice
is an integer corresponding to the dice value that you’re hoping to get a Yahtzee
with, such as 4. This should return a new list for which all dice which are not equal
to choice are re-rolled. For example, the call
newRoll([2,4,1,6,4],4)
will return a list such as
[4,4,3,4,4]
As in the example above, here the dice which was previously a 2 is now a 4, the
dice that was previously a 1 is now a 3, and the dice that was previously a 6 is now
a 4. The dice that were previously 4’s stay as 4’s.
• createDiceDict : This should take the list diceList as an input parameter, and
return a dictionary for which the keys are the possible roll values 1, 2, 3, 4, 5,
and 6, and the values are the number of times that roll value appears in the list.
For example, the call
createDiceDict([2,4,1,6,4])
will return the dictionary
{1:1, 2:1, 3:0, 4:2, 5:0, 6:1}
3
This captures that the value 1 appears once in the list, the value 2 appears once in
the list, the value 4 appears twice in the list, the value 6 appears once in the list,
and the values 3 and 5 don’t appear in the list.
• mostFrequent : This should take diceDict as an input parameter, which is a
dictionary as generated by the function createDiceDict. This should return the
highest dice value for which you have the largest number of matching dice. For
example, the call
mostFrequent({1:1, 2:1, 3:0, 4:2, 5:0, 6:1})
should return the value 4, because the dictionary encodes that the value 4 appeared
most frequently. Note that the call
mostFrequent({1:0, 2:2, 3:0, 4:2, 5:1, 6:0})
should also return the value 4, because we’re using the strategy that you always
try to get a Yahtzee with the highest dice value for which you have the largest
number of matching dice. This would be the dictionary corresponding to the list
[4,2,4,5,2].
• probabilityYahtzee : This should take integers numTrials and numDice as input
parameters. It will perform a Monte Carlo simulation of numTrials turns in which a
player tries to get all of the numDice dice to match. In each turn, the player will do a
first roll, a second roll if needed, and a third roll if needed. It should return the probability of getting all dice to match in one turn, calcuated from the Monte Carlo simulation. Note that in the game Yahtzee there are five dice, but your function should
be general enough to work for any positive integer value for numDice. Your function
should make calls to the functions firstRoll, newRoll, createDiceDict, and
mostFrequent as needed.
• rollsToGetTenzi : This should take integers numTrials and numDice as input
parameters. It will perform a simulation of numTrials trials to keep track of how
many rolls it takes in order for all of the numDice dice to match. It should return a dictionary whose keys are the numbers of rolls that this takes, specifically,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and ’more than 10’, and whose values are the
number of times in the numTrials simulations that it took that many rolls. Note
here that if, in a given trial, after 10 rolls the dice do not all match, that trial
should be recorded as ’more than 10’. Note that in Tenzi there are ten dice, but
your function should be general enough to work for any positive integer value for
numDice. Your function should make calls to the functions firstRoll, newRoll,
createDiceDict, and mostFrequent as needed.
The functions firstRoll, newRoll, createDiceDict, and mostFrequent are worth 10
points each. The functions probabilityYahtzee and rollsToGetTenzi, and the main
program are worth 20 points each.
4
Academic Honesty Agreement
This project is open book, open notes. However, all work submitted must be your
own. By submitting these programs, you are asserting that all work on this project is
yours alone; that you did not use ChatGPT, Copilot, or similar AI bots; and that you
will not provide any information to anyone else working on the project. In addition, you
are agreeing that you will not discuss any part of this project with anyone. You are
not allowed to post any information about this project on the internet at any time, either
before or after the due date. Discussing any aspect of this project with anyone constitutes
a violation of the academic integrity agreement for CS8 and would result in an F in thewe
chat codinghelp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值