First bit of the A-level miscellany
[cas-master-teacher-training.git] / hangman.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Hangman</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
56 2. Guess a word
57 3. Automatic player
58 4. Better strategies
59
60 ---
61
62 layout: true
63
64 .indexlink[![Open University logo](oulogo_hor.png) [Index](index.html)]
65
66 ---
67 # Hangman setter
68
69 A fairly traditional hangman game. The computer chooses a word, the (human) player has to guess it without making too many wrong guesses. You'll need to find a list of words to chose from and develop a simple UI for the game (e.g. text only: display the target word with underscores and letters, lives left, and maybe incorrect guesses).
70
71 ## Data structures
72
73 * What do we need to track?
74 * What operations do we need to perform on it?
75 * How to store it?
76
77 ---
78 # Data for Hangman
79
80 ## Creating a game
81 * 'List' of words to choose from
82 * Pick one at random
83
84 ## Game state
85
86 <table>
87 <tr valign="top">
88 <th>Data</th>
89 <th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
90 <th>Operations</th>
91 </tr>
92 <tr valign="top">
93 <td>
94
95 * Target word
96 * Discovered letters
97 * In order in the word
98 * Lives left
99 * Wrong letters?
100
101 </td>
102 <td></td>
103 <td>
104
105 * Get a guess
106 * Update discovered letters
107 * Update lives
108 * Show discovered word
109 * Detect game end, report
110 * Detect game win or loss, report
111
112 </td>
113 </tr>
114 </table>
115
116 ---
117 # Hangman guesser
118
119 The other side of the game. The human choses a word, the computer has to guess it. Again, go for a very simple UI. Perhaps the human just responds with a list of letter positions for a correct guess, or zero for an incorrect guess.
120
121 ## Data structures
122
123 * What do we need to track?
124 * What operations do we need to perform on it?
125 * How to store it?
126
127 ## Need to think about the algorithm first
128
129 How will we solve the game?
130
131 ---
132
133 # Guessing strategies
134
135 Guess words in order of frequency in English
136
137 What is the frequency of letters in English?
138
139 Pick a novel, count the letters, list them in frequency order
140
141 Hint: four lines of code, two of them are
142 ```python
143 import collections
144 import string```
145
146 ---
147
148 # Building a guesser
149
150 Given:
151
152 * the discovered word
153 * the wrong guesses so far
154 * the order of letters to guess
155
156 Find:
157
158 * The most common unguessed letter
159
160 Build one
161
162 ---
163 # Autoplayer
164
165 Put step 1 and step 2 together so the computer can play both sides of hangman on its own. You'll need to fix the interface between the two sides: perhaps define a fixed format for the "state" of a hangman game. Get them to play several thousand games together and log the results (how many games can they play overnight?). Do a bit of data analysis: how many games were won, how does the word length affect the win rate and the number of guesses needed to get the word, is the number of guesses per target letter constant, and so on.
166
167 ---
168 # Autoplayer
169
170 Tidy things up: encapsulate in objects
171
172 Define a class for a game, a class for a player
173
174 Run a game by instantiating a game object, passing in a player object
175
176 Interface: the `player` must respond to a `guess` method
177
178 Make the `game` generate output only if it's not passed a `player`
179
180 ---
181 # Different player strategies
182
183 Develop different strategies for playing hangman and see which is best. Can you order guesses by letter occurrence frequency (in either normal English or the dictionary)? Does a stochastic or deterministic approach work better?
184 What counts as a "better" strategy?
185
186 * Think of some better players
187 * Implement them
188 * Test their performance
189 * Write records of game to CSV files (look at the standard `csv` library)
190
191 </textarea>
192 <script src="http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type="text/javascript">
193 </script>
194
195 <script type="text/javascript"
196 src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured"></script>
197
198 <script type="text/javascript">
199 var slideshow = remark.create({ ratio: "16:9" });
200
201 // Setup MathJax
202 MathJax.Hub.Config({
203 tex2jax: {
204 skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
205 }
206 });
207 MathJax.Hub.Queue(function() {
208 $(MathJax.Hub.getAllJax()).map(function(index, elem) {
209 return(elem.SourceElement());
210 }).parent().addClass('has-jax');
211 });
212 MathJax.Hub.Configured();
213 </script>
214 </body>
215 </html>