+ }
+ ],
+ "prompt_number": 30
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class Game:\n",
+ " def __init__(self, target, lives=STARTING_LIVES):\n",
+ " self.lives = lives\n",
+ " self.target = target\n",
+ " self.discovered = list('_' * len(target))\n",
+ " self.wrong_letters = []\n",
+ " self.game_finished = False\n",
+ " self.game_won = False\n",
+ " self.game_lost = False\n",
+ " \n",
+ " def find_all(self, letter):\n",
+ " locations = []\n",
+ " starting=0\n",
+ " location = self.target.find(letter)\n",
+ " while location > -1:\n",
+ " locations += [location]\n",
+ " starting = location + 1\n",
+ " location = self.target.find(letter, starting)\n",
+ " return locations\n",
+ " \n",
+ " def updated_discovered_word(self, guessed_letter):\n",
+ " locations = find_all(self, guessed_letter)\n",
+ " for location in locations:\n",
+ " self.discovered[location] = guessed_letter\n",
+ " return self.discovered\n",
+ " \n",
+ " def do_turn(self):\n",
+ " print('Word:', ' '.join(self.discovered), \n",
+ " ' : Lives =', self.lives, \n",
+ " ', wrong guesses:', ' '.join(sorted(self.wrong_letters)))\n",
+ " guess = input('Enter letter: ').strip().lower()[0]\n",
+ " if guess in self.target:\n",
+ " updated_discovered_word(self, guess)\n",
+ " else:\n",
+ " self.lives -= 1\n",
+ " if guess not in self.wrong_letters:\n",
+ " self.wrong_letters += [guess]\n",
+ " if self.lives == 0:\n",
+ " self.game_finished = True\n",
+ " self.game_lost = True\n",
+ " if '_' not in self.discovered:\n",
+ " self.game_finished = True\n",
+ " self.game_won = True\n",
+ " \n",
+ " def play_game(self):\n",
+ " do_turn()\n",
+ " while not self.game_finished:\n",
+ " if self.game_won:\n",
+ " print('You won! The word was', self.target)\n",
+ " elif self.game_lost:\n",
+ " print('You lost. The word was', self.target)\n",
+ " else:\n",
+ " do_turn()\n",
+ " return self.game_won"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 47
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "g = Game(random.choice(WORDS))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 48
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "g.target"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 49,
+ "text": [
+ "'paintbrush'"
+ ]
+ }
+ ],
+ "prompt_number": 49
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "g.discovered"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 50,
+ "text": [
+ "['_', '_', '_', '_', '_', '_', '_', '_', '_', '_']"
+ ]
+ }
+ ],
+ "prompt_number": 50
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "g.do_turn()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [