Added Hangman setter
authorNeil Smith <neil.git@njae.me.uk>
Sat, 27 Sep 2014 15:18:31 +0000 (16:18 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Sat, 27 Sep 2014 15:18:31 +0000 (16:18 +0100)
.ipynb_checkpoints/hangman-setter-checkpoint.ipynb [new file with mode: 0644]
hangman-setter.ipynb [new file with mode: 0644]

diff --git a/.ipynb_checkpoints/hangman-setter-checkpoint.ipynb b/.ipynb_checkpoints/hangman-setter-checkpoint.ipynb
new file mode 100644 (file)
index 0000000..6d3684e
--- /dev/null
@@ -0,0 +1,9 @@
+{
+ "metadata": {
+  "name": "",
+  "signature": "sha256:0b98cca3e07675475eac1f4392386d85d54a09969d7a15a459759e6560551a0d"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": []
+}
\ No newline at end of file
diff --git a/hangman-setter.ipynb b/hangman-setter.ipynb
new file mode 100644 (file)
index 0000000..c1d2ef0
--- /dev/null
@@ -0,0 +1,586 @@
+{
+ "metadata": {
+  "name": "",
+  "signature": "sha256:db806d8bbe929161fce8776d2eeac6bad40d1abb93a97d112b974b6a8d50bdd0"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+  {
+   "cells": [
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "import re\n",
+      "import random"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 1
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "WORDS = [w.strip() for w in open('/usr/share/dict/british-english').readlines() \n",
+      "         if re.match(r'^[a-z]*$', w.strip())]"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 2
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "STARTING_LIVES = 10\n",
+      "lives = 0"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 3
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "wrong_letters = []"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 33
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "len(WORDS)"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 4,
+       "text": [
+        "62856"
+       ]
+      }
+     ],
+     "prompt_number": 4
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "WORDS[30000:30010]"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 5,
+       "text": [
+        "['jotted',\n",
+        " 'jotting',\n",
+        " 'jottings',\n",
+        " 'joule',\n",
+        " 'joules',\n",
+        " 'jounce',\n",
+        " 'jounced',\n",
+        " 'jounces',\n",
+        " 'jouncing',\n",
+        " 'journal']"
+       ]
+      }
+     ],
+     "prompt_number": 5
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "target = random.choice(WORDS)\n",
+      "target"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 6,
+       "text": [
+        "'bluster'"
+       ]
+      }
+     ],
+     "prompt_number": 6
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "guessed_word = list('_' * len(target))\n",
+      "guessed_word"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 7,
+       "text": [
+        "['_', '_', '_', '_', '_', '_', '_']"
+       ]
+      }
+     ],
+     "prompt_number": 7
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "letter = input('Enter letter: ').strip().lower()[0]\n",
+      "letter"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: dsf\n"
+       ]
+      },
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 8,
+       "text": [
+        "'d'"
+       ]
+      }
+     ],
+     "prompt_number": 8
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "def find_all(string, letter):\n",
+      "    locations = []\n",
+      "    starting=0\n",
+      "    location = string.find(letter)\n",
+      "    while location > -1:\n",
+      "        locations += [location]\n",
+      "        starting = location + 1\n",
+      "        location = string.find(letter, starting)\n",
+      "    return locations"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 9
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "find_all('happy', 'q')"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 10,
+       "text": [
+        "[]"
+       ]
+      }
+     ],
+     "prompt_number": 10
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "guessed_letter = 'e'\n",
+      "locations = find_all(target, guessed_letter)\n",
+      "for location in locations:\n",
+      "    guessed_word[location] = guessed_letter\n",
+      "guessed_word"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 11,
+       "text": [
+        "['_', '_', '_', '_', '_', 'e', '_']"
+       ]
+      }
+     ],
+     "prompt_number": 11
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "def updated_guessed_word(guessed_word, guessed_letter):\n",
+      "    locations = find_all(target, guessed_letter)\n",
+      "    for location in locations:\n",
+      "        guessed_word[location] = guessed_letter\n",
+      "    return guessed_word"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 13
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "def initialise():\n",
+      "    global lives, target, guessed_word, wrong_letters\n",
+      "    lives = STARTING_LIVES\n",
+      "    target = random.choice(WORDS)\n",
+      "    guessed_word = list('_' * len(target))\n",
+      "    wrong_letters = []"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 30
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "def do_turn():\n",
+      "    global guessed_words, lives, wrong_letters\n",
+      "    print('Word:', ' '.join(guessed_word), ' : Lives =', lives, ', wrong guesses:', ' '.join(sorted(wrong_letters)))\n",
+      "    guess = input('Enter letter: ').strip().lower()[0]\n",
+      "    if guess in target:\n",
+      "        updated_guessed_word(guessed_word, guess)\n",
+      "    else:\n",
+      "        lives -= 1\n",
+      "        wrong_letters += [guess]"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 41
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "def play_game():\n",
+      "    global guessed_word, lives\n",
+      "    initialise()\n",
+      "    game_finished = False\n",
+      "    do_turn()\n",
+      "    while not game_finished:\n",
+      "        if '_' not in guessed_word:\n",
+      "            print('You won!')\n",
+      "            game_finished = True\n",
+      "        elif lives <= 0:\n",
+      "            print('You lost. The word was', target)\n",
+      "            game_finished = True\n",
+      "        else:\n",
+      "            do_turn()"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 36
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "play_game()"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Word: _ _ _ _ _ _ _ _ _  : Lives = 10 , wrong guesses: \n"
+       ]
+      },
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: e\n"
+       ]
+      },
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Word: _ _ _ _ _ e _ _ _  : Lives = 10 , wrong guesses: \n"
+       ]
+      },
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: a\n"
+       ]
+      },
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Word: a _ _ _ _ e _ _ _  : Lives = 10 , wrong guesses: \n"
+       ]
+      },
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: t\n"
+       ]
+      },
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Word: a _ _ _ _ e t _ _  : Lives = 10 , wrong guesses: \n"
+       ]
+      },
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: o\n"
+       ]
+      },
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Word: a _ _ o _ e t _ _  : Lives = 10 , wrong guesses: \n"
+       ]
+      },
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: i\n"
+       ]
+      },
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Word: a _ _ o _ e t _ _  : Lives = 9 , wrong guesses: i\n"
+       ]
+      },
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: n\n"
+       ]
+      },
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Word: a _ _ o _ e t _ _  : Lives = 8 , wrong guesses: i n\n"
+       ]
+      },
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: s\n"
+       ]
+      },
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Word: a _ _ o _ e t _ _  : Lives = 7 , wrong guesses: i n s\n"
+       ]
+      },
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: h\n"
+       ]
+      },
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Word: a _ _ o _ e t _ _  : Lives = 6 , wrong guesses: h i n s\n"
+       ]
+      },
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: r\n"
+       ]
+      },
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Word: a r _ o r e t _ _  : Lives = 6 , wrong guesses: h i n s\n"
+       ]
+      },
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: g\n"
+       ]
+      },
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Word: a r _ o r e t _ _  : Lives = 5 , wrong guesses: g h i n s\n"
+       ]
+      },
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: r\n"
+       ]
+      },
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Word: a r _ o r e t _ _  : Lives = 5 , wrong guesses: g h i n s\n"
+       ]
+      },
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: d\n"
+       ]
+      },
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Word: a r _ o r e t _ _  : Lives = 4 , wrong guesses: d g h i n s\n"
+       ]
+      },
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: l\n"
+       ]
+      },
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Word: a r _ o r e t _ _  : Lives = 3 , wrong guesses: d g h i l n s\n"
+       ]
+      },
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: u\n"
+       ]
+      },
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Word: a r _ o r e t u _  : Lives = 3 , wrong guesses: d g h i l n s\n"
+       ]
+      },
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: b\n"
+       ]
+      },
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Word: a r b o r e t u _  : Lives = 3 , wrong guesses: d g h i l n s\n"
+       ]
+      },
+      {
+       "name": "stdout",
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Enter letter: m\n"
+       ]
+      },
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "You won!\n"
+       ]
+      }
+     ],
+     "prompt_number": 42
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    }
+   ],
+   "metadata": {}
+  }
+ ]
+}
\ No newline at end of file