1 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Draft//EN'> 2 <html> 3 <head><title> hangman </title></head> 4 <body bgcolor='white' text='#000010' link='blue' vlink='#ff00ff' link='#ffff00'> 5 6 <div align=center> 7 <font color='#ff3333' size=+5>H</font> 8 <font color='#cc6633' size=+5>a</font> 9 <font color='#999900' size=+5>n</font> 10 <font color='#33cc33' size=+5>g</font> 11 <font color='#339999' size=+5>m</font> 12 <font color='#3366cc' size=+5>a</font> 13 <font color='#0033ff' size=+5>n</font> 14 </div> 15 16 <div align=center> 17 <font size=+3 color='#336699'> 18 <% 19 for(i = 0; i < strlen(newGuess) ; i++) 20 response->printf("%c ", newGuess[i]); 21 %> 22 </font> 23 </div> 24 <br/> 25 <div align=center> 26 <img src='images/hang<%="%d" noOfUsedGues%>.gif' height=150 width=150 alt=''/> 27 <% 28 if(noOfUsedGues == 6 || noOfUsedGues == 7) 29 { 30 %> 31 <p> 32 <font size=+3 color="#ff3333"> 33 <%= noOfUsedGues == 6 ? "Game over." : "You Won!!!"%> 34 </font> 35 </p> 36 <p> 37 <font size=+2 color="#3333cc"> 38 The correct answer is <B><%=word->answer%></B>. 39 </font> 40 </p> 41 Play another game of <a href='hangman.html'>Hangman</a> 42 <% 43 } 44 else 45 { 46 if(noOfUsedGues == 0) 47 { 48 %> 49 <p>Guess your first letter of the above unknown word.</p> 50 <% 51 } 52 else 53 { 54 %> 55 <p> You have <%="%d " 6-noOfUsedGues%> guesses left! </p> 56 <% 57 } 58 %> 59 <font color='#0033ff'>hint : <B><%=word->clue%></b></font> 60 61 <form method='post' action='hangman.html'> 62 <input type='hidden' name='wordDbIndex' value='<%="%d" wordDbIndex%>'/> 63 <input type='hidden' name='guess' value='<%=newGuess%>'/> 64 <input type='hidden' name='noOfUsedGues' value='<%="%d" noOfUsedGues%>'/> 65 <% for(i = 'A'; i <= 'Z'; i++) 66 response->printf("\t\t<input type='submit' name='ch' value='%c'/>\n",i); 67 %> 68 </form> 69 <% 70 } 71 %> 72 <p> 73 <a href = "hangman_src.html" target="_blank">view html source code</a> 74 </p> 75 </div> 76 </body> 77 </html> 78 79 <%g 80 #include <stdlib.h> 81 #include <time.h> 82 #include <string.h> 83 #include <ctype.h> 84 #include <HttpServerLib.h> 85 86 struct Word 87 { 88 const char* clue; 89 const char* answer; 90 }; 91 92 const Word wordDB[] = { 93 {"action", "drool"}, 94 {"action", "forecast"}, 95 {"action", "forget"}, 96 {"action", "jump"}, 97 {"action", "panic"}, 98 {"action", "quash"}, 99 {"action", "snore"}, 100 {"action", "talk"}, 101 {"action", "walk"}, 102 {"activity", "boating"}, 103 {"activity", "repair"}, 104 {"activity", "ruffle"}, 105 {"activity", "sleep"}, 106 {"activity", "surfing"}, 107 {"animal", "aardvark"}, 108 {"animal", "adder"}, 109 {"animal", "beaver"}, 110 {"animal", "bison"}, 111 {"animal", "cardinal"}, 112 {"animal", "cariboo"}, 113 {"animal", "cat"}, 114 {"animal", "donkey"}, 115 {"animal", "elephant"}, 116 {"animal", "koala"}, 117 {"animal", "otter"}, 118 {"animal", "rabbit"}, 119 {"animal", "raccoon"}, 120 {"animal", "rodent"}, 121 {"animal", "shark"}, 122 {"animal", "sheep"}, 123 {"animal", "squirrel"}, 124 {"animal", "tiger"}, 125 {"antique", "china"}, 126 {"band", "nirvana"}, 127 {"band", "rush"}, 128 {"band", "ventures"}, 129 {"creature", "elf"}, 130 {"creature", "hobbit"}, 131 {"creature", "mummy"}, 132 {"creature", "troll"}, 133 {"creature", "unicorn"}, 134 {"creature", "vampire"}, 135 {"creature", "warewolf"}, 136 {"drink", "cola"}, 137 {"drink", "juice"}, 138 {"event", "drama"}, 139 {"event", "mirage"}, 140 {"event", "siesta"}, 141 {"event", "stampede"}, 142 {"food", "apple"}, 143 {"food", "butter"}, 144 {"food", "caesar salad"}, 145 {"food", "cashew"}, 146 {"food", "celery"}, 147 {"food", "cheese"}, 148 {"food", "chestnut"}, 149 {"food", "cream"}, 150 {"food", "donut"}, 151 {"food", "grape"}, 152 {"food", "honey"}, 153 {"food", "icecream"}, 154 {"food", "limburger"}, 155 {"food", "milk"}, 156 {"food", "noodle"}, 157 {"food", "orange"}, 158 {"food", "pasta"}, 159 {"food", "pizza"}, 160 {"food", "potato"}, 161 {"food", "spice"}, 162 {"food", "sugar"}, 163 {"food", "tomatoes"}, 164 {"food", "turnip"}, 165 {"game", "cards"}, 166 {"game", "golf"}, 167 {"game", "pacman"}, 168 {"icon", "barbie"}, 169 {"icon", "frosty"}, 170 {"icon", "grinch"}, 171 {"icon", "gumby"}, 172 {"icon", "lucy"}, 173 {"icon", "popeye"}, 174 {"icon", "snoopy"}, 175 {"icon", "waldo"}, 176 {"invention", "velcro"}, 177 {"person", "enemy"}, 178 {"person", "giant"}, 179 {"person", "hero"}, 180 {"person", "husband"}, 181 {"person", "kennedy"}, 182 {"person", "nixon"}, 183 {"person", "youngster"}, 184 {"place", "amsterdam"}, 185 {"place", "athens"}, 186 {"place", "beach"}, 187 {"place", "bob"}, 188 {"place", "buffalo"}, 189 {"place", "calgary"}, 190 {"place", "canada"}, 191 {"place", "detroit"}, 192 {"place", "downtown"}, 193 {"place", "england"}, 194 {"place", "gorge"}, 195 {"place", "graceland"}, 196 {"place", "hotel"}, 197 {"place", "hotel"}, 198 {"place", "india"}, 199 {"place", "japan"}, 200 {"place", "land"}, 201 {"place", "library"}, 202 {"place", "london"}, 203 {"place", "norway"}, 204 {"place", "oslo"}, 205 {"place", "ottawa"}, 206 {"place", "quadrant"}, 207 {"place", "restaurant"}, 208 {"place", "rink"}, 209 {"place", "toronto"}, 210 {"place", "victoria"}, 211 {"place", "village"}, 212 {"place", "zoo"}, 213 {"plant", "daffodil"}, 214 {"plant", "eggplant"}, 215 {"plant", "ivy"}, 216 {"plant", "spinach"}, 217 {"plant", "tulip"}, 218 {"profession", "astronaut"}, 219 {"profession", "bard"}, 220 {"profession", "hermit"}, 221 {"profession", "lawyer"}, 222 {"profession", "sorcerer"}, 223 {"state", "alaska"}, 224 {"subject", "geography"}, 225 {"taste", "bitter"}, 226 {"taste", "salty"}, 227 {"taste", "sour"}, 228 {"taste", "sweet"}, 229 {"tool", "anvil"}, 230 {"tool", "buffer"}, 231 {"tool", "hook"}, 232 {"tool", "knife"}, 233 {"tool", "lasso"}, 234 {"tool", "pickaxe"}, 235 {"tool", "pliers"}, 236 {"tool", "plow"}, 237 {"tool", "screwdriver"}, 238 {"tool", "wrench"}, 239 {"toy", "kite"}, 240 {"toy", "slinky"}, 241 {"trouble", "tribble"} 242 }; 243 244 %> 245 <%!! 246 /* Seed the random-number generator with current time so that 247 * the numbers will be different every time we run. 248 */ 249 srand((unsigned)time(0)); 250 %> 251 <%p 252 unsigned int i; 253 const char* ch=0; 254 const Word* word=0; 255 int wordDbIndex; 256 const char* guess=0; 257 char* newGuess=0; 258 int noOfUsedGues = 0; 259 HttpParameterIterator fIter(request); 260 /* Extract data from form in the above html */ 261 for( ; fIter.hasMoreElements() ; fIter.nextElement()) 262 { 263 if( ! strcmp(fIter.getName(), "guess") ) 264 guess = fIter.getValue(); 265 else if( ! strcmp(fIter.getName(), "ch") ) 266 ch = fIter.getValue(); 267 else if( ! strcmp(fIter.getName(), "wordDbIndex") ) 268 { 269 wordDbIndex = atoi(fIter.getValue()); 270 word = &wordDB[wordDbIndex]; 271 } 272 else if( ! strcmp(fIter.getName(), "noOfUsedGues") ) 273 noOfUsedGues = atoi(fIter.getValue()); 274 } 275 if(word) /* Should be true unless first time or a new game. 276 * This works since the form in the html above is only 277 * emitted if we are playing a game 278 */ 279 { 280 bool chCorrect=false; 281 assert(guess); 282 newGuess = httpStrdup(guess); 283 assert(ch); 284 for(i = 0 ; i < strlen(word->answer) ; i++) 285 { 286 if(tolower(word->answer[i]) == tolower(*ch)) 287 { 288 newGuess[i] = *ch; 289 chCorrect = true; 290 } 291 } 292 if( ! httpStrCaseCmp(newGuess, word->answer) ) 293 noOfUsedGues = 7; /* 7 signals "You Won!!!" */ 294 else if(chCorrect == false) 295 noOfUsedGues++; 296 } 297 else /* First time or new game */ 298 { 299 wordDbIndex = rand() % (sizeof(wordDB) / sizeof(wordDB[0])); 300 word = &wordDB[wordDbIndex]; 301 newGuess = (char*)httpMalloc(strlen(word->answer)+1); 302 memset(newGuess, '_', strlen(word->answer)); 303 newGuess[strlen(word->answer)] = 0; 304 } 305 %> 306 <%e 307 httpFree(newGuess); 308 %>