4 <title>Hangman (again)
</title>
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8"/>
6 <style type=
"text/css">
15 h1 { font-size:
3em; }
16 h2 { font-size:
2em; }
17 h3 { font-size:
1.6em; }
19 text-decoration: none;
22 -moz-border-radius:
5px;
23 -web-border-radius:
5px;
31 text-shadow:
0 0 20px #
333;
37 text-shadow:
0 0 20px #
333;
51 <textarea id=
"source">
53 # Hangman ![Open University logo](oulogo_hor.png)
55 1. **Set a puzzle** ← here
60 ## Filtering the dictionary
62 What's the best way to filter the dictionary of invalid words?
68 .indexlink[![Open University logo](oulogo_hor.png) [Index](index.html)]
74 * 'List' of words to choose from
82 <th> </th>
90 * In order in the word
99 * Update discovered letters
101 * Show discovered word
102 * Detect game end, report
103 * Detect game win or loss, report
113 We've got a list of valid words.
116 * look in the `random` module
118 * Once we have a word, how to represent what's been guessed?
122 # Representing the game state
126 * `target`, the target word: a `string`
127 * `discovered`, what's been found so far: a `list` of letters (each a length-one `string`)
128 * unguessed letters are `_`
129 * `guess`, the letter that's just been guessed: a length-one `string`
130 * `lives`, the lives remaining: an `int`
131 * `wrong_letters`, the incorrect guesses: a `list` (optional)
132 * (but it makes the automated players easier if they don't have to track wrong guesses)
138 1. Print a `discovered` word
140 3. Check if the `guess`ed letter is in the `target`
141 4. Update `discovered` word with the newly-`guess`ed letter
142 5. Update `lives` and `wrong_letters` if the guess is wrong
146 1. Remember `
<string>.join(
<list>)` ?
147 2. `input('prompt')` (or `guess = input('prompt').strip().lower()[
0]` )
148 3. Walk over `target`, looking for `guess`.
149 * Explicit iteration (`for i in range(len(target)):`)
150 * With a comprehension (`[... for i, l in enumerate(target) if... ]`)
151 4. `target[n] = guess`.
152 * Note that you need the numerical position, and you need *all* the positions for repeated letters.
153 5. How do you know the guess is wrong?
157 <script src=
"http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type=
"text/javascript">
160 <script type=
"text/javascript"
161 src=
"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured"></script>
163 <script type=
"text/javascript">
164 var slideshow = remark.create({ ratio:
"16:9" });
169 skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
172 MathJax.Hub.Queue(function() {
173 $(MathJax.Hub.getAllJax()).map(function(index, elem) {
174 return(elem.SourceElement());
175 }).parent().addClass('has-jax');
177 MathJax.Hub.Configured();