{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# FIRST RUN this code block of imports\n", "import random # import random library\n", "from hangman import hangman_pics # import hangman pictures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Hangman" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Let's try recreating the game hangman! \n", "
If you haven't played before, the goal is to guess a word within 6 tries!\n", "

\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Choosing Words" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " First we need to give the program a list of possible words to choose from. \n", "
Feel free to add or take away words from this list. Then run the code block.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A list of possible words\n", "# Feel free to add more or change these!\n", "words = ['mochi', 'boba', 'dinosaur', 'clam', 'dog', 'balcony',\n", " 'tree', 'buildings', 'forest', 'signature', 'peak', 'projector',\n", " 'lightning', 'backpack', 'spaghetti', 'ravioli']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " In order to choose a word, we can use the function random.choice() from the random library. If we pass a list to this function, it will return one of the items at random. For example, try running this code block several times:\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "random.choice(words)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display the Board" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " We need to print the current state of the game. \n", "

\n", " \n", "

\n", " hangman_pics is a list of strings representing different states of the hangman, from 0 (no incorrect guesses) to 6 (6 incorrect guesses).\n", "

\n", "

\n", " The function display_board is passed the following parameters:\n", "

\n", "\n", "

\n", " We want to print the current hangman state, the missed letters, and the current word state. For example, display_board('aiu', '_r__') should print something like:\n", "

\n", "\n", "```\n", " +---+\n", " | |\n", " O |\n", " /| |\n", " |\n", " |\n", " =========\n", "Missed letters: aiu\n", "_r__\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def display_board(missed_letters, word_state):\n", " # Print the current hangman state. \n", " # HINT: Index into the list hangman_pics to get the string to print.\n", " \n", "\n", " # Print the letters they've missed\n", " \n", " \n", " # Print the current word state\n", " \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Test your function by calling display_board('aiu', '_r__'). It should look like the example!\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Test your function here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting the Word State" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " We need to get the word state based on the word and the player's previous guesses.\n", "

\n", " \n", "

\n", " Remember, the word state shows how many letters have currently been guessed correctly in the word.\n", "

\n", " \n", "

\n", " For example, if the word is cat, and the player has correctly guessed a, then the current word state should be c_t.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_word_state(word, correct_letters):\n", " word_state = '' # initialize word_state\n", " \n", " # For every letter in the word...\n", " \n", " \n", " # If the letter is in correct_letters (it has been correctly guessed)...\n", " \n", " \n", " # Add the letter to word_state\n", " \n", " \n", " # Otherwise...\n", " \n", " \n", " # Add an underscore to word_state\n", " \n", " \n", " # Return the word state\n", " \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting the Guess" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " We need to get and validate the player's guess. This function will loop until the player's guess is valid. A guess has the following requirements:\n", "

\n", "\n", "

\n", " The argument letters_guessed is a string of the letters that have been guessed already.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_guess(letters_guessed):\n", " valid_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' # valid characters\n", " valid_guess = False # assume guess is not valid\n", " \n", " # While the guess is not valid...\n", " \n", " \n", " # Get input from the player (this is written for you)\n", " guess = raw_input('Guess a letter: ')\n", " \n", "\n", " # If guess does not have length 1, tell the player\n", " \n", " \n", "\n", " # Otherwise, guess is not a valid letter, tell the player. (Hint: elif)\n", " \n", "\n", "\n", " # Otherwise, guess has already been guessed, tell the player. (Hint: elif) \n", " # Remember, letters_guessed is a string of the letters that have been guessed already.\n", " \n", "\n", "\n", " # Otherwise,(Hint: else) set valid_guess to True\n", " \n", " \n", " \n", " # Return the guess\n", " return guess\n", " \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Try running the code block below a few times. The letters a, b, c, d, e, f, and g have already been guessed. Make sure your validation and feedback works.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "get_guess('abcdefg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Game" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Now let's put everything together! We want to continue playing until we have won or lost, so we can use a loop! Think about which type of loop would work best here.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def play_hangman(word):\n", " game_over = False # keeps track of if the game is over\n", " correct_letters = '' # string of correctly guessed letters\n", " missed_letters = '' # string of incorrectly guessed letters\n", " word_state = '_' * len(word) # the word state (start with all underscores)\n", " \n", " # While the game is not over...\n", " \n", " \n", " # Display the current board (call the display_board function!)\n", " \n", " \n", " # Find all letters_guessed (Hint: Use concatenation between correct and missed letters)\n", " \n", " \n", " # Get and save a guess from the user (call the get_guess function!)\n", " \n", " \n", " # If the guess is correct (if the guess is in the word)...\n", " \n", " \n", " # Print correct guess message\n", " \n", " \n", " # Update correct_letters (add the guess)\n", " \n", " \n", " # Update word_state (call the get_word_state function)\n", " \n", " \n", " # If they have won the game (there will be no more underscores in word_state)...\n", " \n", " \n", " # Print the board (call display_board!)\n", " \n", " \n", " # Print winning message\n", " \n", " \n", " # Set game_over to True\n", " \n", " \n", " # Otherwise, if the guess is incorrect...\n", " \n", " \n", " # Print incorrect guess message\n", " \n", " \n", " # Update missed_letters\n", " \n", " \n", " # If they have lost the game (6 or more missed letters)\n", " \n", " \n", " # Print the board (call display_board!)\n", " \n", " \n", " # Let the player know what the correct word was!\n", " \n", " \n", " # Print losing message\n", " \n", " \n", " # Set game_over to True\n", " \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Playing the Game" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " If everything works, you should now be able to play the game by running the code block below. Play the game a few times and test to see if there are any bugs.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "word = random.choice(words)\n", "play_hangman(word)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More Hangman Ideas!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Only attempt these once you've finished the lab and have extra time.\n", "

\n", "" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.16" } }, "nbformat": 4, "nbformat_minor": 2 }