Name: _________________________________________

CSC 107       Fall 2003

Lab 3: Prisoners and Pirates

In this lab, you will consider two different applications. The first is a simulation of of the Prisoner's Dilemma. Using an existing Web page that conducts repeated Prisoner's Dilemma transactions, you will experiment with the basic starategies such as coop, defect, and titForTat, and consider their biological analogies. The second application in this lab is a rather silly Web page for translating English words and phrases into pirate talk. You will expand the page to include a variety of words and phrases, and will consider ways to simplify the page.

The Prisoner's Dilemma

Recall the basic rules of the repeated Prisoner's Dilemma, as described in Douglas Hofstadter's paper:

For each round of play, two players independently choose to cooperate or defect. The goal for each player is to accumulate as many points as they can.

In his paper, Hofstadter describes several basic strategies for playing the repeated Prisoner's Dilemma. The coop strategy always cooperates, the defect strategy always defects, while the randomness strategy cooperates or defects at random. The titForTat strategy cooperates in the first round, and then mimics whatever the opponent did in the previous round. It is interesting to note that the defect strategy will never "lose" to an opponent, meaning that it will never accumulate fewer points than any opponent. Similarly, coop and titForTat will never "win" against an opponent. However, the number of points accumulated in "winning" and "losing" may vary greatly.

EXERCISE 1:    For each of the following pairings, predict the outcome if the two strategies were to compete in a repeated Prisoner's Dilemma. In particular, how many points would each strategy accumulate (assuming 50 rounds)?

coop vs. coop defect vs. defect titForTat vs. titForTat coop vs. defect coop vs. titForTat defect vs. titForTat

Which of the strategies would tend to do best against randomness? Which would do worst? Explain.

EXERCISE 2:    The Web page dilemma.html contains JavaScript code for simulating the repeated Prisoner's Dilemma. You are able to select which strategies you want to compete, and then click on a button to see the simulation. The button labeled "Click for next round" will display one round at a time, while the button labeled "Click for complete simulation" will complete all 50 rounds.

Load this page into the browser and use it to verify your predictions from the previous exercise.

Prisoner's Dilemma Tournaments

What makes the Prisoner's Dilemma interesting is that locally optimal behavior is not necessarily globally optimal. Consider a round-robin tournament between defect and two copies of titForTat (assuming 50 rounds per contest). Even though defect outscores each opponent in head-to-head contests, it only earns 54 points in each, for a total of 108. When one copy of titForTat plays the other, however, they both earn 150 points. Adding this to the 49 points earned against defect, each copy of titForTat earns a total of 199 points. Thus, defect wins all of its battles, but loses the war!

A Prisoner's Dilemma tournament can be described nicely in ecological terms. The defect and coop strategies, because of their aggressive and passive natures, can be characterized as hawks and doves. The titForTat strategy, which is passive by nature but will become aggressive when provoked, may be characterized as a sparrow. According to this analogy, the fact that defect outscores titForTat in a head-to-head contest shows that a lone hawk dominates a lone sparrow. However, the tournament shows that two sparrows support each other and can fight off a lone hawk. Using this type of analogy, the Prisoner's Dilemma has been used by researchers as a way of explaining how cooperative behavior can evolve without consciousness. In fact, studies done on the hunting patterns of lions and the defensive behaviors of fish have supported the Prisoner's Dilemma model of cooperation.

The results of a round-robin tournament can be displayed neatly in a table. The above tournament between a hawk (defect) and two sparrows (titForTat) would appear as follows. The entries across a row contain the points earned by a strategy playing against each of the other strategies. For example, the hawk earns 54 points when it goes against sparrow1, so there is a 54 in the table at the row labeled hawk and column labeled sparrow1. Conversely, sparrow1 only earns 49 points against the hawk, so there is a 49 in the table at the row labeled sparrow1 and column labeled hawk.

 hawk   sparrow1 sparrow2 TOTAL
hawk 54 54 108
sparrow1 49 150 199
sparrow2 49 150 199

EXERCISE 3:    In the table below, show the results of a round-robin tournament between a hawk, a dove, and two sparrows. How does the addition of the dove affect the dynamics between the hawk and the sparrows compared to the example above?

 hawk     dove   sparrow1 sparrow2 TOTAL

EXERCISE 4:    In the table below, show the results of a round-robin tournament between a hawk, a dove, and three sparrows. How does the additional sparrow affect the results when compared to the previous exercise?

 hawk     dove   sparrow1 sparrow2 sparrow3 TOTAL

EXERCISE 5:    Suppose we characterized the titFor2Tats strategy as a robin. Would robins be more or less effective than sparrows (titForTat) in protecting a lone dove (coop) from a lone hawk (defect)? In other words, would a round-robin tournament involving a dove, hawk, and some number of robins result in the dove and each robin earning more points than the hawk? Does it take more or fewer robins than it took sparrows to acheive this? Why would this be the case?

Talk Like a Pirate

In 1995, John Baur and Mark Summers invented a new holiday, International Talk Like a Pirate Day. As they proposed it, on every September 19th, people all around the world would be united in common purpose by talking like a pirate (with lots of "Arrrrs" and "Ahoys" and the like). Since then, International Talk Like a Pirate Day has received widespread publicity, especially since columnist Dave Barry began lobbying for the holiday. At the official Talk Like a Pirate Day Web site,, you can read about the history of the holiday, order official merchandise, and even experiment with a simple Web page that translates simple English phrases into pirate talk.

The following Web page is a framework for an alternative English-to-Pirate translator. As is, it contains two buttons, labeled "hello" and "friend", and a text area. When the user clicks on a button, the corresponding Pirate word or phrase is added to the text area. For example, if the user were to click on the "hello" button, the word "ahoy" will appear in the text area. If he then clicks on the "friend" button, then "me matey" is added to the text area, resulting in the phrase "ahoy me matey."

<html> <!-- pirate1.html Dave Reed --> <!------------------------------------------------------> <head> <title> Pirate Translator </title> <script type="text/javascript"> function AddOutput(pirateTalk) // Assumes: pirateTalk is the translated pirate word or phrase // Results: pirateTalk is added to document.PirateForm.Output { document.PirateForm.Output.value = document.PirateForm.Output.value + pirateTalk + " "; } </script> </head> <body> <div style="text-align:center"> <h3>Land Lubber's Pirate Translator</h3> <p>Simply click on the buttons to translate<br /> words and/or phrases from English to pirate talk. <hr /> <form name="PirateForm"> <input type="button" value="hello" onClick="AddOutput('ahoy');"> <input type="button" value="friend" onClick="AddOutput('me matey');"> <br /><br /> <textarea name="Output" rows=10 cols=40></textarea> </form> </div> </body> </html>

EXERCISE 6:    Cut-and-paste this HTML text into a Web page named pirate1.html and verify that it behaves as described.

As the page is currently written, multiple translations are awkward. If you finish translating one phrase and want to begin a new one, simply refreshing the page does not suffice to clear the text area. Instead, you must click the mouse in the browser's Address box and hit enter to reload the page and reset the text area. To make repeated translations easier, add a button directly below the text area labeled "Clear". When the user clicks on this button, a function should be called to clear the text area (i.e., set its contents to be the empty string).

EXERCISE 7:    Extend pirate1.html by adding new words and phrases. For each new word or phrase, you will need to add a button with the appropriate label and translated pirate phrase. At a minimum, you must add the new words and phrases below. However, feel free to add a larger vocabulary (see for ideas).
English    Pirate talk
hey youavast ye
strangerye scurvy dog
where iswhar be
is thatbe that
how far is it tothar be how many leagues til
the restroomth' stinkin' head
a restauranta briney galley
a pubth' Skull & Scuppers

Automatic Code Generation

As you no doubt noticed, each button in the pirate1.html page is of the same form. They differ only in the button label (the word/phrase to be translated) and the input to the function call (the translated word/phrase). With a little more programming knowledge, it should be possible to automate the generation of the buttons so that the programmer would not need to add new buttons every time the vocabulary expands. Instead, the programmer could enter the words and phrases in a single list (denoting with [ and ] in JavaScript) and then have the buttons generated when the page loads (via additional JavaScript code). This can be accomplished with the following code, which uses a list named TRANSLATE to store the English and pirate words.

<script type="text/javascript"> TRANSLATE = [ "hello", "ahoy", "friend", "me matey" ]; for (i = 0; i < TRANSLATE.length; i = i+2) { document.write("<input type=\"button\" value=\"" + TRANSLATE[i] + "\" onClick=\"AddOutput('" + TRANSLATE[i+1] + "');\">"); } </script>

EXERCISE 8:    Save a copy of your pirate1.html page under the name pirate2.html. Then, modify this new page by replacing all of the buttons in the body with the the above JavaScript code segment (enclosed in SCRIPT tags). Once you have tested the page to be sure that it behaves exactly the same as the original pirate1.html page, extend it by adding your new words and phrases to the TRANSLATE list. When finished, this new page should behave exactly as your pirate1.html from EXERCISE 7. However, adding additional words and phrases should now be as easy as adding the words/phrases to the TRANSLATE list.

Hand in printouts of pirate1.html and pirate2.html, attached to these sheets.