Run update21, if you haven't already, to create cs21/labs/03. Then cd into your cs21/labs/03 directory and create the python programs for lab 3 in this directory (handin21 looks for your lab 3 assignments in your cs21/labs/03 directory):
$ update21 $ cd $ cd cs21/labs/03 $ pwd /home/your_user_name/cs21/labs/03
Your programs are graded on both correctness and style. Please review
the comments regarding programming style on the main page.
In this problem you will create a simple text-based pirate adventure game! Write a program in the file named adventure.py that allows the player to select from some set of actions, and given their selection, allows them to explore some world. For instance, one possible solution might be:
$ python adventure.py You find yourself in a dark hold of a ship. You look around and see a torch on the wall and a parrot. What do you want to do? (1) take the torch off of the wall (2) leave the room (3) say hello to the parrot Type the number of your action: 3 You said hello to the parrot. The parrot squawks "Polly wants a cracker!" and jumps on your shoulder. What do you want to do? (1) take the torch off the wall (2) feed Polly the parrot the cracker you have in your pocket (3) leave the room Type the number of your action: 1 You take the torch off of the wall, accidentally setting the parrot on fire. When the captain finds out, you are forced to walk the plank. You lose. $ python adventure.py You find yourself in a dark hold of a ship. You look around and see a torch on the wall and a parrot. What do you want to do? (1) take the torch off of the wall (2) leave the room (3) say hello to the parrot Type the number of your action: 19 19 is not a valid choice. You lose.
Your game should include at least three choices for the player's first action, and at least one decision should allow the player to select from another set of actions before the game ends. If a player enters an invalid number that does not correspond to an action, print an appropriate error message and end the game. It might be helpful to diagram the choices in your program on a sheet of paper first, before you begin your coding.
For this part of the assignment, we want you to translate a string into pirate speak. Write a program, in the file translate.py, that takes a string as input, converts that string to pirate speak, and prints it.
To convert a string to pirate speak, replace any character 'r' in the string with 'rrr'. For example:
$ python translate.py Enter a string to convert: hello there, friends. hello therrre, frrriends. $ python translate.py Enter a string to convert: swarthmore college swarrrthmorrre collegeIf the string doesn't have the letter 'r' in it, add an 'arrrr' to the end of the string to make it more pirate like.
$ python translate.py Enter a string to convert: what fun! what fun! arrrr
While waiting around for ships to attack, pirates like to play a game called Pirate Dice. To play, a pirate selects an integer from 1 to 6 and then rolls three dice. If none of the dice show the number the pirate picked, that pirate loses a gold coin. However, if exactly one die shows the number, the pirate wins a gold coin. If exactly two dice show the number, the pirate wins two gold coins and if all three dice show the number, the pirate wins three gold coins. Write a program in a file called dice.py that plays one round of Pirate Dice. The computer can choose a random number to simulate a roll of the dice as follows:
from random import choice computer_pick = choice([1, 2, 3, 4, 5, 6])A few runs of your program might look like this:
$ python dice.py Ahoy Matey! Give me a number from 1 to 6: 4 You rolled: 2, 5, 1 You lost a gold coin. $ python dice.py Ahoy Matey! Give me a number from 1 to 6: 2 You rolled: 5, 3, 2 You won a gold coin. $ python dice.py Ahoy Matey! Give me a number from 1 to 6: 4 You rolled: 4, 3, 4 You won two gold coins.
This problem is an optional bonus problem. It is not required, and you should not attempt it until you are completely finished with the other three lab problems. It will also not get you any extra points -- you will only be rewarded with extra knowledge and satisfaction. :)
In the file dice_rounds.py, write a program that allows the pirate to play multiple rounds of the dice game and keep track of how many gold coins the pirate has won or lost. Before the game begins, prompt the pirate for how many rounds to play and how many coins the pirate has. The game should keep playing till the pirate runs out of coins or has played all the rounds requested. At the end of each round of the game you should print the pirate's total number of coins. For example:
$ python dice_rounds.py
How many rounds would you like to play? 3
How many gold coins do you have? 10
Ahoy Matey! Give me a number from 1 to 6: 1
You rolled: 1, 1, 4
You won two gold coins.
You have a total of 12 gold coins.
Ahoy Matey! Give me a number from 1 to 6: 6
You rolled: 4, 2, 2
You lost a gold coin.
You have a total of 11 gold coins.
Ahoy Matey! Give me a number from 1 to 6: 5
You rolled: 3, 4, 2
You lost a gold coin.
You have a total of 10 gold coins.