{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#########################################################################\n", "# Imports\n", "#########################################################################\n", "import random\n", "import importlib\n", "\n", "#########################################################################\n", "# Functions provided, you don't have to touch these.\n", "#########################################################################\n", "\n", "def updateScore(score):\n", " \"\"\"Increments the score by one (you don't need to change this)\"\"\"\n", " return score + 1\n", "\n", "def prettyPrint(cubes, player, score, gridSize=5):\n", " \"\"\"Prints the board state (you don't need to change this)\"\"\"\n", " assert(0 <= player[0] < gridSize)\n", " assert(player[1] == gridSize - 1)\n", " square = u\" \\u25A0 \"\n", " triangle = u\" \\u25B2 \"\n", " space = \" \"\n", " toRet = \"\\n+\" + \"---+\"*gridSize + \"\\n\"\n", " for y in range(gridSize):\n", " toRet += \"|\" + \"|\".join([triangle if [x,y] == player else (space if [x,y] not in cubes else square) for x in\n", " range(gridSize)]) + \"|\\n\"\n", " toRet += \"+\" + \"---+\"*gridSize + \"\\n\"\n", " print(toRet)\n", "\n", "def reload():\n", " \"\"\"Don't change this! This lets you reload your file by typing `reload()`\"\"\"\n", " import cubegame\n", " importlib.reload(cubegame)\n", " exec(\"from cubegame import *\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Cube Runner \n", "---\n", "\n", "

\n", " The goal of the cube runner is to stay away from the cubes as long as possible; for each turn we survive, we will earn a point. \n", "

\n", "\n", "

\n", " We can input: \"left\", \"right\", or \"stay\" to move left or right on a 5x5 grid, represented in [x,y] with [0,0] being the top left. \n", "

\n", " \n", "

\n", " Make sure the player can't go out of bounds! \n", "

\n", "\n", "

\n", " Each turn, the existing cubes will move one space down, and three new cubes will spawn at the top of the grid.\n", "

\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Student Code\n", "\n", "

\n", " updateCubes() will take a list of cubes and then output a list of updated cubes on the gameboard. \n", " \n", "

\n", " Each cubes location is represented by a list [x, y]. \n", "

\n", "Here is an example of cubes: [[1, 2], [4, 4], [2, 3], [1, 0]].\n", "\n", "

\n", " Steps:\n", "

    \n", "
  1. increment each cube's y-location by one (dropping down one square) with for loop.
  2. \n", "
  3. generate a list of 3 random, non-repeated numbers (make sure they do not repeat) using a while loop and the random.randint() function
  4. \n", "
  5. using the randomly generated list of numbers, add 3 new cubes to the cubes list (note: y-location should be 0 since it's at the top)
  6. \n", "
  7. return the new list of cubes
  8. \n", "
\n", "\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Student Code\n", "def updateCubes(cubes):\n", " \"\"\" \n", " input: list of cubes \n", " output: the updated list of cubes \n", " \"\"\"\n", " pass # Delete the 'pass' once you finish your code.\n", "\n", " # update each cube's location (for loop)\n", " \n", " # generate list of 3 random, non-repeated numbers (while loop & random.randint())\n", " \n", " # add 3 random new cubes to list (for loop)\n", " \n", " # return list of cubes " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " updatePlayerLocation() will take the player's current location and a direction to move (or not move) and then output the player's new location. Directions can be 'left', 'right', or 'stay'. If their direction would move them off the grid, they stay still instead (and lose their turn).\n", "

\n", "\n", " \n", "\n", "

\n", " Steps:\n", "

    \n", "
  1. if direction is 'left', move the player left if it will not move the player off the board. \n", "
  2. if direction is 'right', move the player right if it will not move the player off the board.\n", "
  3. return updated player location
  4. \n", "
\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def updatePlayerLocation(player, direction):\n", " \"\"\" \n", " input: player location & direction to move \n", " output: updated player location \n", " \"\"\"\n", " pass # delete 'pass' once you have completed your code.\n", "\n", " # if direction is 'left', move the player left if it will not move the player off the board.\n", "\n", "\n", " # if direction is 'right', move the player right if it will not move the player off the board.\n", "\n", " \n", " # return (new) player location " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " collision() will take the list of cubes and the player location and then output if player is going to collide with a cube or not (a boolean). \n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def collision(cubes, player):\n", " \"\"\" Take as input the list of cube locations and the player location.\n", " Return True if there is a collision between the player and a cube, False otherwise.\n", " \"\"\"\n", " pass # delete 'pass' once you have completed your code.\n", " # check if player will collide w/ a cube \n", " \n", " \n", " # return true/false\n", " \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Main Loop\n", "\n", "

\n", " Run this once you have completed the previous functions. You don't need to edit any of the code below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def runGame():\n", " \"\"\"Contains the main loop for running the game\"\"\"\n", " # Game state\n", " player = [2,4] # initial location of the player\n", " score = 0 # initial score\n", " cubes = [[0,0], [3,0], [4,0]] # initial cube locations\n", "\n", " print(\"Welcome to cubes! Quit by typing 'quit'\")\n", " prettyPrint(cubes, player, score)\n", "\n", " # Main loop\n", " while True:\n", " direction = input(\"Input 'left', 'right', 'stay', or 'quit': \")\n", " if direction=='quit':\n", " print(\"You quit! Score was\", score)\n", " break\n", " if direction !='left' and direction != 'right' and direction != 'stay':\n", " continue\n", " player = updatePlayerLocation(player, direction)\n", "\n", " cubes = updateCubes(cubes)\n", " score = updateScore(score)\n", " prettyPrint(cubes, player, score)\n", "\n", " if collision(cubes, player):\n", " print(\"You lose! Score was\", score)\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Run Game" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "runGame()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Extensions\n", "\n", "

\n", " Here are some extra ideas for making our game more interesting!\n", "

    \n", "
  1. Add a \"Play again?\" option when the player loses that re-starts the game with re-initialized player, score, and cubes variables.
  2. \n", "
  3. Expand the grid! Create a variable called gridSize and replace the (5 x 5) grid with a (gridSize x gridSize) grid. The helper functions should already be able to do this, but you must now input your additional variable to them.
  4. \n", "
  5. Implement \"harder\" levels that create more cubes as the game goes on.
  6. \n", "
  7. Check to newly generate rows to guarantee the level is solvable.
  8. \n", "
  9. Come up with your own idea and discuss it with an instructor!
  10. \n", "
\n", "

" ] } ], "metadata": { "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.8.1" } }, "nbformat": 4, "nbformat_minor": 4 }