Question: ( Python ) The line above is from the perennial favorite 1 9 8 0 s movie adaptation of William Goldman's The Princess Bride, wherein
Python
The line above is from the perennial favorite s 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 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!
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: 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 to the list where the second value in the list indicates that the player guessed following the sequence After the fifth guess, we create the entry and after the sixth guess we update the value for to since the user guesses again, after the sequence
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
Exercise : recordguess points
The first function you are to complete is recordguess. It will take the following arguments:
a dictionary to update possibly containing previously recorded pattern list mappings
a pattern string
a guess which is either or
If necessary, the function will create a new entry for the pattern if one doesn't already exist then record the updated count for the guess. Since the dictionary is updated in place ie mutated the function will not return anything.
Exercise : nextplacement points
The next function you'll write will take a dictionary of pattern counts mappings and a string representing the pattern of most recent guesses, and return the next best location either or for the poison ie to try and outwit the player If the pattern hasn't been seen previously or the counts are tied, the function should return
Exercise : playinteractive optional but fun!
Now for some fun. The function playinteractive will take just one argument the length of patterns to use as keys in the dictionary and will start an interactive game session, reading either or 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 or the game should terminate.
Exercise : playbatch points
Finally, so that we can check your implementation against a lengthier sequence of guesses without having to play an interactive session, implement the playbatch function, which will take the patternlength argument as your playinteractive 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.
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
