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 2 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 (1) or yours (2)?1
Wrong! Ha! Never bet against a Sicilian!
You died 1 times, and I drank the poison 0 times
Where is the iocane powder: my cup (1) or yours (2)?2
Good guess! Ack! I drank the poison!
You died 1 times, and I drank the poison 1 times
Where is the iocane powder: my cup (1) or yours (2)?1
Wrong! Ha! Never bet against a Sicilian!
You died 2 times, and I drank the poison 1 times
Where is the iocane powder: my cup (1) or yours (2)?2
Good guess! Ack! I drank the poison!
You died 2 times, and I drank the poison 2 times
Where is the iocane powder: my cup (1) or yours (2)?1
Wrong! Ha! Never bet against a Sicilian!
You died 3 times, and I drank the poison 2 times
Where is the iocane powder: my cup (1) or yours (2)?2
Wrong! Ha! Never bet against a Sicilian!
You died 4 times, and I drank the poison 2 times
Where is the iocane powder: my cup (1) or yours (2)?1
Wrong! Ha! Never bet against a Sicilian!
You died 5 times, and I drank the poison 2 times
Where is the iocane powder: my cup (1) or yours (2)?2
Wrong! Ha! Never bet against a Sicilian!
You died 6 times, and I drank the poison 2 times
Where is the iocane powder: my cup (1) or yours (2)?1
Wrong! Ha! Never bet against a Sicilian!
You died 7 times, and I drank the poison 2 times
///// code:
import random
def record_guess(pattern_dict, pattern, guess):
"""Updates the `pattern_dict` dictionary by either creating a new entry
or updating an existing entry for key `pattern`, increasing the count
correspondingto `guess` in the list."""
count = pattern_dict.setdefault(pattern,[0,0])
count[int(guess)-1]+=1
pass
def next_placement(pattern_dict, pattern):
"""Given a `pattern_dict` 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 '2'."""
counts = pattern_dict.get(pattern,[0,0])
if(counts [0]== counts[1]):
return '2'
elif counts[0]> counts[1]:
return '2'
else:
return '1'
pass
def play_interactive(pattern_length=4):
"""Plays an interactive game, where the user is asked to repeatedly guess
where the iocane powder is, and the computer uses the `next_placement`
function to determine where to place the poison. See the sample session in
the assignment description for an example."""
while True:
guess = input('Where is the iocane powder: my cup (1) or yours (2)?')
if not (guess =='1' or guess =='2'):
break
def play_batch(guesses, pattern_length=4):
"""Plays a batch game, where the `guesses` iterable contains a sequence of
guesses, and the `pattern_length` is the length of the pattern to use."""
#IMPLEMENT CODE
return wins, losses
pass
if __name__=='__main__':
play_interactive()
//////here are some test cases:
def test_play_batch():
assert iocane.play_batch(['1','1','1','1','1','1'],
3)==(0,6)
assert iocane.play_batch(['1','2','1','2','1','2'],
3)==(2,4)
assert iocane.play_batch(['1','2','1','2','1','2'],
4)==(3,3)
assert iocane.play_batch(['1','2']*100,5)==(3,197)
assert iocane.play_batch(['1','1','2','1','2','1']*100,
2)==(398,202)
assert iocane.play_batch(['1','1','2','1','2','1']*100,
3)==(201,399)
assert iocane.play_batch(['1','1','2','1','2','1']*100,
5)==(4,596)
import random
random.seed(0, version=2)
assert iocane.play_batch((random.choice(['1','2'])
for _ in range(10000)),4)==(5047,4953)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!