index.lsp
1 <?lsp
2
3 --
4 -- Converted from the CSP, which was converted from a CGI
5 --
6
7 local
8 wordDB = page.hangmanworddb
9 or
10 {
11 {"script", "lsp"},
12 {"action", "drool"},
13 {"action", "forecast"},
14 {"action", "forget"},
15 {"action", "jump"},
16 {"action", "panic"},
17 {"action", "quash"},
18 {"action", "snore"},
19 {"action", "talk"},
20 {"action", "walk"},
21 {"activity", "boating"},
22 {"activity", "repair"},
23 {"activity", "ruffle"},
24 {"activity", "sleep"},
25 {"activity", "surfing"},
26 {"animal", "aardvark"},
27 {"animal", "adder"},
28 {"animal", "beaver"},
29 {"animal", "bison"},
30 {"animal", "cardinal"},
31 {"animal", "cariboo"},
32 {"animal", "cat"},
33 {"animal", "donkey"},
34 {"animal", "elephant"},
35 {"animal", "koala"},
36 {"animal", "otter"},
37 {"animal", "rabbit"},
38 {"animal", "raccoon"},
39 {"animal", "rodent"},
40 {"animal", "shark"},
41 {"animal", "sheep"},
42 {"animal", "squirrel"},
43 {"animal", "tiger"},
44 {"antique", "china"},
45 {"band", "nirvana"},
46 {"band", "rush"},
47 {"band", "ventures"},
48 {"creature", "elf"},
49 {"creature", "hobbit"},
50 {"creature", "mummy"},
51 {"creature", "troll"},
52 {"creature", "unicorn"},
53 {"creature", "vampire"},
54 {"creature", "warewolf"},
55 {"drink", "cola"},
56 {"drink", "juice"},
57 {"event", "drama"},
58 {"event", "mirage"},
59 {"event", "siesta"},
60 {"event", "stampede"},
61 {"food", "apple"},
62 {"food", "butter"},
63 {"food", "caesar salad"},
64 {"food", "cashew"},
65 {"food", "celery"},
66 {"food", "cheese"},
67 {"food", "chestnut"},
68 {"food", "cream"},
69 {"food", "donut"},
70 {"food", "grape"},
71 {"food", "honey"},
72 {"food", "icecream"},
73 {"food", "limburger"},
74 {"food", "milk"},
75 {"food", "noodle"},
76 {"food", "orange"},
77 {"food", "pasta"},
78 {"food", "pizza"},
79 {"food", "potato"},
80 {"food", "spice"},
81 {"food", "sugar"},
82 {"food", "tomatoes"},
83 {"food", "turnip"},
84 {"game", "cards"},
85 {"game", "golf"},
86 {"game", "pacman"},
87 {"icon", "barbie"},
88 {"icon", "frosty"},
89 {"icon", "grinch"},
90 {"icon", "gumby"},
91 {"icon", "lucy"},
92 {"icon", "popeye"},
93 {"icon", "snoopy"},
94 {"icon", "waldo"},
95 {"invention", "velcro"},
96 {"person", "enemy"},
97 {"person", "giant"},
98 {"person", "hero"},
99 {"person", "husband"},
100 {"person", "kennedy"},
101 {"person", "nixon"},
102 {"person", "youngster"},
103 {"place", "amsterdam"},
104 {"place", "athens"},
105 {"place", "beach"},
106 {"place", "bob"},
107 {"place", "buffalo"},
108 {"place", "calgary"},
109 {"place", "canada"},
110 {"place", "detroit"},
111 {"place", "downtown"},
112 {"place", "england"},
113 {"place", "gorge"},
114 {"place", "graceland"},
115 {"place", "hotel"},
116 {"place", "hotel"},
117 {"place", "india"},
118 {"place", "japan"},
119 {"place", "land"},
120 {"place", "library"},
121 {"place", "london"},
122 {"place", "norway"},
123 {"place", "oslo"},
124 {"place", "ottawa"},
125 {"place", "quadrant"},
126 {"place", "restaurant"},
127 {"place", "rink"},
128 {"place", "toronto"},
129 {"place", "victoria"},
130 {"place", "village"},
131 {"place", "zoo"},
132 {"plant", "daffodil"},
133 {"plant", "eggplant"},
134 {"plant", "ivy"},
135 {"plant", "spinach"},
136 {"plant", "tulip"},
137 {"profession", "astronaut"},
138 {"profession", "bard"},
139 {"profession", "hermit"},
140 {"profession", "lawyer"},
141 {"profession", "sorcerer"},
142 {"state", "alaska"},
143 {"subject", "geography"},
144 {"taste", "bitter"},
145 {"taste", "salty"},
146 {"taste", "sour"},
147 {"taste", "sweet"},
148 {"tool", "anvil"},
149 {"tool", "buffer"},
150 {"tool", "hook"},
151 {"tool", "knife"},
152 {"tool", "lasso"},
153 {"tool", "pickaxe"},
154 {"tool", "pliers"},
155 {"tool", "plow"},
156 {"tool", "screwdriver"},
157 {"tool", "wrench"},
158 {"toy", "kite"},
159 {"toy", "slinky"},
160 {"trouble", "tribble"}
161 }
162
163 page.hangmanworddb = wordDB
164
165 local data = request:data()
166
167 local guess = data.guess
168 local wordDbIndex=tonumber(data.wordDbIndex)
169 local noOfUsedGues = tonumber(data.noOfUsedGues)
170 local ch = data.ch and string.sub(data.ch,1,1)
171
172 local fmt = string.format
173 local function prt(...) response:write(...) end
174 local function prtf(...) prt(fmt(...)) end
175 local random = rnd or math.random -- use whichever function is available
176
177
178 local answer, clue, word
179
180 if not wordDbIndex then -- /* Should be true unless first time or a new game.
181 -- * This works since the form in the html above is only
182 -- * emitted if we are playing a game
183 -- */
184 -- /* First time or new game */
185 repeat
186 wordDbIndex = random(#wordDB);
187 until wordDbIndex ~= page.wordDbIndex
188
189 page.wordDbIndex = wordDbIndex
190
191 word = wordDB[wordDbIndex];
192 clue=word[1]
193 answer=word[2]
194 newGuess = string.rep('_', #answer);
195 noOfUsedGues=0
196 else
197 local chCorrect = false;
198 assert(guess);
199 newGuess = guess;
200 assert(ch);
201 assert(wordDB)
202 assert(wordDbIndex > 0, wordDbIndex);
203 word = wordDB[wordDbIndex];
204 assert(word)
205 clue=word[1]
206 answer=word[2]
207 ch = ch:lower()
208 newGuess=""
209 string.gsub(answer, "()(.)", function(pos,c)
210 if string.sub(answer, pos,pos) == ch then
211 newGuess = newGuess .. ch
212 chCorrect = true;
213 else
214 newGuess = newGuess .. string.sub(guess, pos,pos)
215 end
216 end)
217 if newGuess == answer then
218 noOfUsedGues = 7; -- 7 signals "You Won!!!"
219 elseif not chCorrect then
220 noOfUsedGues = noOfUsedGues + 1;
221 end
222 end
223
224 title="hangman"
225 response:include"/rtl/.header.lsp"
226 ?>
227 <div align=center>
228
229 <a href="http://barracudaserver.com/doc/Lua/frame.html" target="_blank">
230 <img border="0" src="bl2.gif" style="float:left;margin: 10px 10px"
231 alt="The Hangman game is powered by the Lua Server Pages Barracuda plugin"></img></a>
232
233 <div style="float:right;width:250px;border-style: dotted; border-color:gray;border-width:1px;padding:5px;margin: 10px 10px 20px 20px">
234
235 <p style="font-size: 12pt;text-align:left;color:gray">
236 The hangman game was initially a CGI script. The CGI script was
237 converted to C/C++ server Pages (CSP). This version of the game is
238 in Lua Server Pages (LSP). Maybe you are interested in reading the
239 <a href="http://barracudaserver.com/WP/CSP/" target="blank">Hangman
240 CSP whitepaper</a>. You can compare the <a
241 href="http://barracudaserver.com/WP/CSP/hangmanHtmlCode.html"
242 target="_blank">CSP code</a> to the <a
243 href="showsource/showsource.lsp?path=
<?lsp=page.iopath?>"
244 target=_blank>LSP code</a>.
245 </p>
246 </div>
247
248 <font color='#ff3333' size=+5>H</font>
249 <font color='#cc6633' size=+5>a</font>
250 <font color='#999900' size=+5>n</font>
251 <font color='#33cc33' size=+5>g</font>
252 <font color='#339999' size=+5>m</font>
253 <font color='#3366cc' size=+5>a</font>
254 <font color='#0033ff' size=+5>n</font>
255 </div>
256
257 <div align=center>
258 <font size=+3 color='#336699'>
259
<?lsp
260 for i=1, #newGuess do
261 prtf("%s ",string.sub(newGuess,i,i));
262 end
263 ?>
264 </font>
265 </div>
266 <br/>
267 <div align=center>
268 <img src='
<?lsp=fmt("hang%d", noOfUsedGues)?>.gif' height=150 width=150 alt=''/>
269
<?lsp
270 if noOfUsedGues >= 6 then
271 ?>
272 <p>
273 <font size=+3 color="#ff3333">
274
<?lsp= noOfUsedGues == 6 and "Game over." or "You Won!!!"?>
275 </font>
276 </p>
277 <p>
278 <font size=+2 color="#3333cc">
279 The correct answer is <B>
<?lsp=answer?></B>.
280 </font>
281 </p>
282 Play another game of <a href='
<?lsp=page.path?>'>Hangman</a>
283
<?lsp
284 else
285 if noOfUsedGues == 0 then
286 ?>
287 <p>Guess your first letter of the above unknown word.</p>
288
<?lsp
289 else
290 ?>
291 <p> You have
<?lsp= 6-noOfUsedGues ?> guesses left! </p>
292
<?lsp
293 end
294 ?>
295 <font color='#0033ff'>hint : <b>
<?lsp=clue?></b></font>
296
297 <form method='post'>
298 <input type='hidden' name='wordDbIndex' value='
<?lsp=wordDbIndex?>'/>
299 <input type='hidden' name='guess' value='
<?lsp=newGuess?>'/>
300 <input type='hidden' name='noOfUsedGues' value='
<?lsp=noOfUsedGues?>'/>
301
<?lsp
302 for i = string.byte('A'), string.byte('Z'), 1 do
303 prtf("\t\t<input type='submit' name='ch' value='%c'/>\n",i);
304 end
305 ?>
306 </form>
307
<?lsp
308 end
309 ?>
310 </div>
311
<?lsp response:include"/rtl/.footer.lsp" ?>
312