In this lab, we will develop computational strategies for a simple game about dividing a cake.
Once upon a time two children, Ryan and Isla, found a cake. Ryan said: “Splendid! I will eat the cake.” Isla said: “No, that is not fair! We found the cake together, and we should share and share alike; half for you and half for me.” Ryan said, “No, I should have the whole cake!” Isla said, “No, we should share and share alike; half for you and half for me.” Ryan said, “No, I want the whole cake.” Isla said, “No, let us share it half and half.” Along came an adult who said: “You shouldn’t fight about this; you should compromise. So Ryan gets three quarters of the cake.”
adapted from Raymond Smullyan’s This Book Needs No Title: A Budget of Living Paradoxes.
The above situation is related to the Prisoner’s Dilemma a classic example in Economic Game Theory. If the children are given two options, ask for ALL the cake, or ask for HALF the cake, what should they choose?
It is temping to try and ask for ALL the cake when the other player does not, but they will have the same idea, and the rational end result is that both will get less.
However, if we put the children in this situation repeatedly, without knowing when the process will end, a different outcome is possible. This is known as the Iterated Prisoner’s Dilemma. Now, the children can start to formulate strategies of how to play based on the history of interactions with the other player.
In this lab, you will be coding up different strategies for players trying to optimize the amount of cake you receive.
cake151
File
Open
src
engine
specs
strategies
First, let’s make sure everything is set up correctly and you can run the skeleton code. Open the Simulator class in the engine package and click the green Play button at the top of the class. If everything works, you should see the following output.
Simulator
Name Total Greedy Mimic Timid Greedy 48.42 40.00 40.25 65.00 Mimic 46.58 39.75 50.00 50.00 Timid 38.33 15.00 50.00 50.00
Note that these three strategies, Timid, Greedy, and Mimic, are found in the strategy package, and they all implement the Strategy interface.
strategy
When we run the code, all of the class that implement Strategy will be run through a tournament against each other. The Total column shows the percentage of the cake gathered by each competitor on average across their rounds versus the other competitors.
Strategy
Total
During each round of the tourament, a Strategy is called upon to play the cake game through two methods. First, askForAll is called, and the strategy must answer with a boolean, where true means they want ALL the cake, and false means they ask for HALF the cake.
askForAll
boolean
true
false
Once both competitors have been called, the game determines the state and doles out the cake according to the description above. Now, the second method, rememberOtherLast is called, and the boolean parameter askedForAll is what the other player did during the round.
rememberOtherLast
askedForAll
The code in these two methods can be simple or complicated depending on how you want to play the game. Review the three provided classes to see how each strategy makes its decision. Notice how in the above examples, only Mimic has any data fields, namely a boolean. This is the persistent state of the class. All of the strategies you write in this lab must include persistent state; this could be boolean, int, float, String, or arrays, to name a few possibilities. This means they must have a private data member / field, outside of the methods but inside the class, and make use of it in the code somehow. The more state you record, the more you will be able to record the history of game play and have better reactions to opponents’ strategies.
Mimic
int
float
String
To add a new strategy, first use IntelliJ to make a new class in the strategies package. The easiest way to do this is by right-clicking on the strategies folder, choosing New > Java Class, then providing a name for your class.
New
Java Class
Now add implements Strategy right after the public class MyFirstClass and before the {. IntelliJ will be grumpy, so add the line import specs.Strategy; to the top of your file. Now IntelliJ is grumpy for another reason. When the red squiggles appear under the class description, right click on them, and select Show Context Actions > Implement Methods. This will automatically add the two needed methods to this class.
implements Strategy
public class MyFirstClass
{
import specs.Strategy;
Show Context Actions
Implement Methods
Your grade will depend upon the total number of strategies you create, subject to the following constraints:
GoadrichYorgey1.java
GoadrichYorgey2.java
Be sure to regularly run simulations with your strategies, by executing the Simulator class, to check on your syntax and semantics as well as performance.
In an evaluation document, reflect on your choices for each strategy. Write a few sentences about your goals in writing these strategy. If you were playing this game in real life, would you use any of the strategies you wrote? Why or why not?
Submit all of your strategy files (the .java files you wrote) and your reflection document via Teams by the next lab period.
We will run a simulation competition during class the following Wednesday.