Question: For this machine problem you will write a program that simulates multiple rounds of this battle of wits, allowing the player to repeatedly guess which
For this machine problem you will write a program that simulates multiple rounds of this battle of wits, allowing the player to repeatedly guess which cup is poisoned. The computer will "place" the poison before the player guesses, and will reveal who is right... and who is dead, afterwards.
At the outset, the computer will always place the poison in cup before letting the player guess, but after enough guesses have been entered the computer will start to place the poison based on the pattern of previous guesses so as to outsmart the player.
Here's a sample game session note how the silly player keeps alternating guesses, and that the computer catches on to this fact after a while:
Where is the iocane powder: my cup or yours
Wrong! Ha Never bet against a Sicilian!
You died times, and I drank the poison times
Where is the iocane powder: my cup or yours
Good guess! Ack! I drank the poison!
You died times, and I drank the poison times
Where is the iocane powder: my cup or yours
Wrong! Ha Never bet against a Sicilian!
You died times, and I drank the poison times
Where is the iocane powder: my cup or yours
Good guess! Ack! I drank the poison!
You died times, and I drank the poison times
Where is the iocane powder: my cup or yours
Wrong! Ha Never bet against a Sicilian!
You died times, and I drank the poison times
Where is the iocane powder: my cup or yours
Wrong! Ha Never bet against a Sicilian!
You died times, and I drank the poison times
Where is the iocane powder: my cup or yours
Wrong! Ha Never bet against a Sicilian!
You died times, and I drank the poison times
Where is the iocane powder: my cup or yours
Wrong! Ha Never bet against a Sicilian!
You died times, and I drank the poison times
Where is the iocane powder: my cup or yours
Wrong! Ha Never bet against a Sicilian!
You died times, and I drank the poison times
code:
import random
def recordguesspatterndict, pattern, guess:
Updates the patterndict dictionary by either creating a new entry
or updating an existing entry for key pattern increasing the count
correspondingto guess in the list."""
count patterndict.setdefaultpattern
countintguess
pass
def nextplacementpatterndict, pattern:
Given a patterndict dictionary and a pattern string, returns the
next placement to make in the pattern. The next placement is the number
that has been guessed the least number of times for that pattern. If the
pattern has not been seen before or the counts are tied returns
counts patterndict.getpattern
ifcounts counts:
return
elif counts counts:
return
else:
return
pass
def playinteractivepatternlength:
Plays an interactive game, where the user is asked to repeatedly guess
where the iocane powder is and the computer uses the nextplacement
function to determine where to place the poison. See the sample session in
the assignment description for an example."""
while True:
guess inputWhere is the iocane powder: my cup or yours
if not guess or guess :
break
def playbatchguesses patternlength:
Plays a batch game, where the guesses iterable contains a sequence of
guesses, and the patternlength is the length of the pattern to use."""
#IMPLEMENT CODE
return wins, losses
pass
if namemain:
playinteractive
here are some test cases:
def testplaybatch:
assert iocane.playbatch
assert iocane.playbatch
assert iocane.playbatch
assert iocane.playbatch
assert iocane.playbatch
assert iocane.playbatch
assert iocane.playbatch
import random
random.seed version
assert iocane.playbatchrandomchoice
for in range
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
