In the lectures, we considered two different solutions to the World Series puzzle. The divide & conquer solution utilized recursion to break the problem into simpler instances and combined solutions to those instances to solve the overall problem. The dynamic programming solution utilized a table of subproblem solutions that it filled in a bottom-up manner. Both solutions were based on a simple recurrence relation that defined how a problem solution could be built out of solutions to smaller problems.
For this assignment, you will consider a new game named Evensies that is being introduced at the CSDJ Casino for the Probabilistically Challenged. In Evensies, a player puts down a stack of tokens and specifies the number of rounds they want to play. In each round, the player predicts the result of rolling two dice, either Evensies (predicting an even sum) or Oddsies (predicting an odd sum). If they guess correctly, they win a token from the house, but if they were incorrect, they lose a token. There is a wrinkle, however. Any roll that consists only of 1's and 2's is referred to as Bottomsies. A roll of Bottomsies causes the player to lose a token on top of the token they won or lost based on even/odd. The game ends when the specified number of rounds have been played or the player runs out of tokens.
For example, suppose a player begins a game with 10 tokens and wants to play 5 rounds. The following is a possible game outcome:
| Round | Guess | Roll | Result |
|---|---|---|---|
| starts with 10 tokens | |||
| 1 | Evensies | 5-3 | CORRECT: 10 + 1 = 11 tokens |
| 2 | Oddsies | 1-3 | INCORRECT: 11 - 1 = 10 tokens |
| 3 | Evensies | 2-2 | CORRECT BUT BOTTOMSIES: 10 + 1 - 1 = 10 tokens |
| 4 | Oddsies | 1-1 | INCORRECT AND BOTTOMSIES: 10 - 1 - 1 = 8 tokens |
| 2 | Evensies | 6-3 | INCORRECT: 8 - 1 = 7 tokens |
Create a class named Evensies that contains the following static method:
Once you have tested your method thoroughly, answer the following questions and provide experimental results to justify your answers:
expectedTopDown method can handle? If so, what is that limit?
Add a second static method to your Evensies class named expectedBottomUp. This method should have the same parameters as expectedTopDown and should calculate the same value, only now using a dynamic programming approach. In particular, it should avoid duplicate calculations by building up a table of answers in a bottom-up fashion.
Answer the following questions and provide experimental results to justify your answers:
expectedBottomUp is significant? Add a third static method named expectedCaching. This method should have the same parameters as the other two methods and should calculate the same value, only now using top-down approach with caching. That is, it should use the same recursive approach as expectedTopDown but should maintain a data structure that stores subproblem solutions. When it solves a subproblem, it should store the answer in the data structure. The next time it sees that same subproblem, it should simply use the stored answer and avoid the duplicate recursive call.
Answer the following questions and provide experimental results to justify your answers:
expectedTopDown? How does it compare with expectedBottomUp?