{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Battlepunts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is an exercise designed to tie together the things you will have learnt over the course of the introductory Python course. \n", "\n", "The aim is to write a game, *Battlepunts*." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Background" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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...\n", "\n", "What is that you hear? Facts, no, incorrect facts! In Cambridge? You must stop this.\n", "\n", "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!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Game" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "You'll have to write four functions:\n", "\n", "1. *mobilise_the_punters* which takes the size of the river as an integer. It should create a list of this size, and randomly populate it with punters as 0s and 1s.\n", "2. *move_down_river* which takes the river as a list. As there is a current, this function should move every punter to the *right*, adding a zero to the front of the list, and removing the punter at the back.\n", "3. *throw* which takes the coordinate that the player throws a tomato at, the river as a list, and the number of points that the player has. This function should do a lot of things: it should check that the coordinate give is in the river, it should check if there is a 0 or a 1 present at the coordinate and award points (and remove punters) accordingly.\n", "4. *end_game* which takes the river as a list. This should sum all of the list elements together, if the sum is zero, then there are no punters left and it should return True, otherwise it should return False." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import the relevant library so you can choose a random integer (0 or 1)." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#NAME: BattlePunts\n", "#DESCRIPTION: Creating a game where you throw tomatoes at punters.\n", "\n", "import random" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write the function *mobilise_the_punters* in the cell immediately below." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def mobilise_the_punters(size):\n", " ### BEGIN SOLUTION\n", " result = []\n", " for i in range(0, size):\n", " result.append(random.randint(0,1))\n", " return result\n", " ### END SOLUTION" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write the function *move_down_river* in the cell immediately below." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def move_down_river(river_list):\n", " ### BEGIN SOLUTION\n", " n = len(river_list)\n", " result = river_list[:n-1]\n", " result.insert(0,0)\n", " return result\n", " ### END SOLUTION" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write the function *throw* in the cell immediately below." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def throw(coord, river_list, point_count):\n", " ### BEGIN SOLUTION\n", " if (coord > 0) and (coord < len(river_list)):\n", " if river_list[coord] > 0:\n", " print(\"You knocked a punter into the river!\\n\")\n", " point_count += 1\n", " river_list[coord] -= 1\n", " else:\n", " print(\"You missed!\\n\")\n", " return point_count, river_list\n", " else:\n", " print(\"You missed!\\n\")\n", " return point_count, river_list\n", " ### END SOLUTION" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write the function *end_game* in the cell immediately below." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": true, "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "def end_game(river_list):\n", " ### BEGIN SOLUTION\n", " if sum(river_list) is 0:\n", " return True\n", " return False\n", " ### END SOLUTION" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that you've written the functions, you'll need to call some of them to initialise the variables *river_size*, *river*, and *points*.\n", "\n", "Use the print function to print the sum of the elements of the river, so you know how many punters there are." ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There are 16 punters! Try to hit them all before they punt away!\n" ] } ], "source": [ "river_size = 32\n", "river = mobilise_the_punters(river_size)\n", "points = 0\n", "\n", "print(\"There are\", sum(river), \"punters! Try to hit them all before they punt away!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You'll have to now write a while loop which checks whether or not the game has finished, using the *end_game* function.\n", "\n", "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.\n", "\n", "Print a statement when the game is over to let the player know how many punters they've hit." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "while end_game(river) is False:\n", " pos = int(input(\"Select a coordinate to throw a tomato at: \"))\n", " points, river = throw(pos, river, points)\n", " river = move_down_river(river)\n", " \n", "print(\"Game Over! You have\", points, \"points!\")" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" }, "widgets": { "state": {}, "version": "1.1.2" } }, "nbformat": 4, "nbformat_minor": 0 }