First bit of the A-level miscellany
[cas-master-teacher-training.git] / hangman2.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Hangman (again)</title>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
6 <style type="text/css">
7 /* Slideshow styles */
8 body {
9 font-size: 20px;
10 }
11 h1, h2, h3 {
12 font-weight: 400;
13 margin-bottom: 0;
14 }
15 h1 { font-size: 3em; }
16 h2 { font-size: 2em; }
17 h3 { font-size: 1.6em; }
18 a, a > code {
19 text-decoration: none;
20 }
21 code {
22 -moz-border-radius: 5px;
23 -web-border-radius: 5px;
24 background: #e7e8e2;
25 border-radius: 5px;
26 font-size: 16px;
27 }
28 .plaintext {
29 background: #272822;
30 color: #80ff80;
31 text-shadow: 0 0 20px #333;
32 padding: 2px 5px;
33 }
34 .ciphertext {
35 background: #272822;
36 color: #ff6666;
37 text-shadow: 0 0 20px #333;
38 padding: 2px 5px;
39 }
40 .indexlink {
41 position: absolute;
42 bottom: 1em;
43 left: 1em;
44 }
45 .float-right {
46 float: right;
47 }
48 </style>
49 </head>
50 <body>
51 <textarea id="source">
52
53 # Hangman ![Open University logo](oulogo_hor.png)
54
55 1. **Set a puzzle** ← here
56 2. Guess a word
57 3. Automatic player
58 4. Better strategies
59
60 ## Filtering the dictionary
61
62 What's the best way to filter the dictionary of invalid words?
63
64 ---
65
66 layout: true
67
68 .indexlink[![Open University logo](oulogo_hor.png) [Index](index.html)]
69
70 ---
71 # Data for Hangman
72
73 ## Creating a game
74 * 'List' of words to choose from
75 * Pick one at random
76
77 ## Game state
78
79 <table>
80 <tr valign="top">
81 <th>Data</th>
82 <th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
83 <th>Operations</th>
84 </tr>
85 <tr valign="top">
86 <td>
87
88 * Target word
89 * Discovered letters
90 * In order in the word
91 * Lives left
92 * Wrong letters?
93
94 </td>
95 <td></td>
96 <td>
97
98 * Get a guess
99 * Update discovered letters
100 * Update lives
101 * Show discovered word
102 * Detect game end, report
103 * Detect game win or loss, report
104
105 </td>
106 </tr>
107 </table>
108
109 ---
110
111 # Next steps
112
113 We've got a list of valid words.
114
115 * How to pick one?
116 * look in the `random` module
117
118 * Once we have a word, how to represent what's been guessed?
119
120 ---
121
122 # Representing the game state
123
124 Five data items:
125
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)
133
134 ---
135
136 # Challenges
137
138 1. Print a `discovered` word
139 2. Read a letter
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
143
144 ## Hints
145
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?
154
155
156 </textarea>
157 <script src="http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type="text/javascript">
158 </script>
159
160 <script type="text/javascript"
161 src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured"></script>
162
163 <script type="text/javascript">
164 var slideshow = remark.create({ ratio: "16:9" });
165
166 // Setup MathJax
167 MathJax.Hub.Config({
168 tex2jax: {
169 skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
170 }
171 });
172 MathJax.Hub.Queue(function() {
173 $(MathJax.Hub.getAllJax()).map(function(index, elem) {
174 return(elem.SourceElement());
175 }).parent().addClass('has-jax');
176 });
177 MathJax.Hub.Configured();
178 </script>
179 </body>
180 </html>