{ "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",
" 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",
"
\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",
"
\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",
" The function display_board
is passed the following parameters:\n",
"
missed_letters
: a string of missed letters.word_state
: a string of the current word state, with underscores and letters.\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",
" Test your function by calling display_board('aiu', '_r__')
. It should look like the example!\n",
"
\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",
"
\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", "1
.\n",
" The argument letters_guessed
is a string of the letters that have been guessed already.\n",
"
\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", "