This is an exercise designed to tie together the things you will have learnt over the course of the introductory Python course.
The aim is to write a game, Battlepunts.
You're walking along the Cam. There's no one around, and your phone is dead. Out of the corner of your eye you spot them, punters! They're coming towards you, about 30 feet back. You listen to what they tell the tourists...
What is that you hear? Facts, no, incorrect facts! In Cambridge? You must stop this.
Fortunately, you bought some tomatoes from the market. Quick! Throw some tomatoes at the punters to knock them into the river and save the tourists!
In Battlepunts, the river will be represented by a list river, of size river_length. In this list, represent punters with a 1, and no punters present with a 0.
You'll have to write four functions:
Import the relevant library so you can choose a random integer (0 or 1).
#NAME: BattlePunts
#DESCRIPTION: Creating a game where you throw tomatoes at punters.
import random
Write the function mobilise_the_punters in the cell immediately below.
def mobilise_the_punters(size):
### BEGIN SOLUTION
result = []
for i in range(0, size):
result.append(random.randint(0,1))
return result
### END SOLUTION
Write the function move_down_river in the cell immediately below.
def move_down_river(river_list):
### BEGIN SOLUTION
n = len(river_list)
result = river_list[:n-1]
result.insert(0,0)
return result
### END SOLUTION
Write the function throw in the cell immediately below.
def throw(coord, river_list, point_count):
### BEGIN SOLUTION
if (coord > 0) and (coord < len(river_list)):
if river_list[coord] > 0:
print("You knocked a punter into the river!\n")
point_count += 1
river_list[coord] -= 1
else:
print("You missed!\n")
return point_count, river_list
else:
print("You missed!\n")
return point_count, river_list
### END SOLUTION
Write the function end_game in the cell immediately below.
def end_game(river_list):
### BEGIN SOLUTION
if sum(river_list) is 0:
return True
return False
### END SOLUTION
Now that you've written the functions, you'll need to call some of them to initialise the variables river_size, river, and points.
Use the print function to print the sum of the elements of the river, so you know how many punters there are.
river_size = 32
river = mobilise_the_punters(river_size)
points = 0
print("There are", sum(river), "punters! Try to hit them all before they punt away!")
There are 16 punters! Try to hit them all before they punt away!
You'll have to now write a while loop which checks whether or not the game has finished, using the end_game function.
You'll need to use an input() in order to take a coordinate. Use the other functions you have written to update the points variable and the river list.
Print a statement when the game is over to let the player know how many punters they've hit.
while end_game(river) is False:
pos = int(input("Select a coordinate to throw a tomato at: "))
points, river = throw(pos, river, points)
river = move_down_river(river)
print("Game Over! You have", points, "points!")