Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Man in Black: All right. Where is the poison? The battle of wits has begun. It ends when you decide and we both drink, and

Man in Black: All right. Where is the poison? The battle of wits has begun. It ends when you decide and we both drink, and find out who is right... and who is dead.

The line above is from the perennial favorite 1980s movie adaptation of William Goldman's The Princess Bride, wherein a mysterious hero sits down to a battle of wits with a villainous Sicilian kidnapper. The setup: two cups positioned between the two, one of which (purportedly) contains a colorless, odorless, lethal poison (viz., iocane powder). After a guess is made as to which cup contains the poison, both drink, and the winner is the one left standing.

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

Implementation

To keep track of the pattern of previous guesses, you will use a dictionary that maps a pattern (of fixed length) to a list of counts for the subsequent guess.

For instance, imagine that the computer observes the player continuing to alternate guesses across ten separate attempts, like so: '1', '2', '1', '2', '1', '2', '1', '2', '1', '2'. If we are using a pattern detection length of three, then after the fourth guess we can create an entry in our dictionary that maps the key '121' to the list [0, 1], where the second value (1) in the list indicates that the player guessed '2' following the sequence '1', '2', '1'. After the fifth guess, we create the entry '212' [1, 0], and after the sixth guess we update the value for '121' to [0, 2] (since the user guesses '2' again, after the sequence '1', '2', '1').

Once the player enters a series of guesses that matches a previously seen pattern, the computer should place the poison in the cup that the player is least likely to guess next. When the player enters the next guess, the dictionary should be updated to reflect the actual guess.

This means that if the computer has yet to see a given pattern of guesses, or when the counts are tied, it will have to place the poison "blindly" --- your implementation should simply place the poison furthest away from itself (cup 2).

image text in transcribed

for play batch i tried this code, but it gives me an error.

def play_batch(guesses, pattern_length=4):

return play_interactive(pattern_length=pattern_length,lst=list(guesses))

print(play_batch(['1', '2', '1', '2', '1', '2'], 4))

### play interactive Now for the fun bit. The function play_interactive' will take just one argument the length of patterns to use as keys in the dictionary and will start an interactive game session, reading either1" or 2, from the player as guesses, using the functions you wrote above and producing output as shown in the sample game session at the beginning of this writeup. If the player types in any other input (besides '1' or 2), the game should terminate. Hint: the L'input!https://docs.python.org/3/1ibrary/functions.html#input) function can be used to read input from the user as a string.* def play_interactive(pattern_length-4) ### play batch. Finally, so that we can check your implementation against a lengthier sequence of guesses without having to play an interactive session, implement the play_batch function, which will take the pattern_length argument as your play_interactive function did, but will also take a sequence of guesses. The function will return the total numbers of wins and losses, as determined by the same algorithm as before. def play_batch (guesses, pattern_length-4): guesses ### play interactive Now for the fun bit. The function play_interactive' will take just one argument the length of patterns to use as keys in the dictionary and will start an interactive game session, reading either1" or 2, from the player as guesses, using the functions you wrote above and producing output as shown in the sample game session at the beginning of this writeup. If the player types in any other input (besides '1' or 2), the game should terminate. Hint: the L'input!https://docs.python.org/3/1ibrary/functions.html#input) function can be used to read input from the user as a string.* def play_interactive(pattern_length-4) ### play batch. Finally, so that we can check your implementation against a lengthier sequence of guesses without having to play an interactive session, implement the play_batch function, which will take the pattern_length argument as your play_interactive function did, but will also take a sequence of guesses. The function will return the total numbers of wins and losses, as determined by the same algorithm as before. def play_batch (guesses, pattern_length-4): guesses

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Accounting And Auditing Research And Databases Practitioner's Desk Reference

Authors: Thomas R. Weirich, Natalie Tatiana Churyk, Thomas C. Pearson

1st Edition

1118334426, 978-1118334423

More Books

Students also viewed these Databases questions

Question

What did they do? What did they say?

Answered: 1 week ago