{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "450f5892-ec18-4250-9759-91c0a071a2f1", "metadata": {}, "source": [ "# My First Interactive Python Game" ] }, { "attachments": {}, "cell_type": "markdown", "id": "89e3c523-4cfc-4b4c-aab8-098271a6d3c9", "metadata": {}, "source": [ "## Word Raider" ] }, { "attachments": {}, "cell_type": "markdown", "id": "cd1c3728-58fb-47a0-a960-2bf7994061a1", "metadata": {}, "source": [ "We start by importing the `random` library to use later on." ] }, { "cell_type": "code", "execution_count": null, "id": "16df9641-fa55-4c91-a8a5-5e9d52ba9193", "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "attachments": {}, "cell_type": "markdown", "id": "f0df582d-81c7-4c4d-b225-df204c75f637", "metadata": {}, "source": [ "### Define initial variables" ] }, { "cell_type": "code", "execution_count": null, "id": "10c00858-d7d9-4ac1-8c8b-6644dfdfd73a", "metadata": {}, "outputs": [], "source": [ "game_title = \"Word Raider\"" ] }, { "cell_type": "code", "execution_count": null, "id": "2c817451-8b32-4bef-b595-24ef5aaa5fab", "metadata": {}, "outputs": [], "source": [ "# Set up the list of words to choose from\n", "word_bank = []" ] }, { "attachments": {}, "cell_type": "markdown", "id": "4524e1bd-c737-4715-a6ff-f4857c2883d3", "metadata": {}, "source": [ "### Open file for loading in the word bank" ] }, { "cell_type": "code", "execution_count": null, "id": "93b9a55b-5ed4-42d9-80ca-8c484f02e844", "metadata": {}, "outputs": [], "source": [ "with open(\"words.txt\") as word_file:\n", " for line in word_file:\n", " word_bank.append(line.rstrip().lower())\n" ] }, { "attachments": {}, "cell_type": "markdown", "id": "c14b75fd-26c8-48d4-ad27-a833bce5e004", "metadata": {}, "source": [ "### Select the word to guess" ] }, { "cell_type": "code", "execution_count": null, "id": "5667fb8f-4300-4b1f-a1b1-577d25d1de84", "metadata": {}, "outputs": [], "source": [ "# Pick a random word from the list\n", "word_to_guess = random.choice(word_bank)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "84abd85d-f8bf-4b3b-aea9-ca104bcdf65f", "metadata": {}, "source": [ "### Define the remaining game variables" ] }, { "cell_type": "code", "execution_count": null, "id": "b2a2a89e-9739-4244-9558-7f3b3933ac72", "metadata": {}, "outputs": [], "source": [ "# Set up the game variables\n", "misplaced_guesses = []\n", "incorrect_guesses = []\n", "max_turns = 5\n", "turns_taken = 0" ] }, { "attachments": {}, "cell_type": "markdown", "id": "b34341ce-8ef5-449d-95a9-675ea360f161", "metadata": {}, "source": [ "### Print the current game state" ] }, { "cell_type": "code", "execution_count": null, "id": "7918773d-7e43-4fe8-bec6-313d342effde", "metadata": {}, "outputs": [], "source": [ "# Display the initial game state\n", "print(\"Welcome to\", game_title)\n", "print(\"The word has\", len(word_to_guess), \"letters.\")\n", "print(\"You have\", max_turns - turns_taken, \"turns left.\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "2771a668-a06a-4306-8b61-f39a3871f4c1", "metadata": {}, "source": [ "### Build the game loop" ] }, { "cell_type": "code", "execution_count": null, "id": "b21e5fbd-461f-4556-ae64-b4deaf7f16ba", "metadata": {}, "outputs": [], "source": [ "while turns_taken < max_turns:\n", " # Get the player's guess\n", " guess = input(\"Guess a word: \").lower()\n", "\n", " # Check if the guess length equals 5 letters and is all alpha letters\n", " if len(guess) != len(word_to_guess) or not guess.isalpha():\n", " print(\"Please enter 5-letter word.\")\n", " continue\n", "\n", " # Check each letter in the guess against the word's letters\n", " index = 0\n", " for c in guess:\n", " if c == word_to_guess[index]:\n", " print(c, end=\" \")\n", " if c in misplaced_guesses:\n", " misplaced_guesses.remove(c)\n", " elif c in word_to_guess:\n", " if c not in misplaced_guesses:\n", " misplaced_guesses.append(c)\n", " print(\"_\", end=\" \")\n", " else:\n", " if c not in incorrect_guesses:\n", " incorrect_guesses.append(c)\n", " print(\"_\", end=\" \")\n", " index += 1\n", "\n", " print(\"\\n\")\n", " print(\"Misplaced letters: \", misplaced_guesses)\n", " print(\"Incorrect letters: \", incorrect_guesses)\n", " turns_taken += 1\n", "\n", " # Check if the player has won\n", " if guess == word_to_guess:\n", " print(\"Congratulations, you win!\")\n", " break\n", "\n", " # Check if the player has lost\n", " if turns_taken == max_turns:\n", " print(\"Sorry, you lost. The word was\", word_to_guess)\n", " break\n", "\n", " # Display the number of turns left and ask for another guess\n", " print(\"You have\", max_turns - turns_taken, \"turns left.\")" ] }, { "cell_type": "code", "execution_count": null, "id": "8417510d-ffb1-4593-b65b-2ec49d6900b6", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.3" } }, "nbformat": 4, "nbformat_minor": 5 }