Guest User

Choicescript Hangman Coding Challenge /w Comments

a guest
Jul 9th, 2013
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. *title Choicescript Hangman Coding Challenge
  2.  
  3. *comment -- LETTER DEFINITIONS --
  4.  
  5. *temp l1 "a"
  6. *temp l2 "b"
  7. *temp l3 "c"
  8. *temp l4 "d"
  9. *temp l5 "e"
  10. *temp l6 "f"
  11. *temp l7 "g"
  12. *temp l8 "h"
  13. *temp l9 "i"
  14. *temp l10 "j"
  15. *temp l11 "k"
  16. *temp l12 "l"
  17. *temp l13 "m"
  18. *temp l14 "n"
  19. *temp l15 "o"
  20. *temp l16 "p"
  21. *temp l17 "q"
  22. *temp l18 "r"
  23. *temp l19 "s"
  24. *temp l20 "t"
  25. *temp l21 "u"
  26. *temp l22 "v"
  27. *temp l23 "w"
  28. *temp l24 "x"
  29. *temp l25 "y"
  30. *temp l26 "z"
  31.  
  32. *comment -- GAME [RE]START --
  33. *label hm_reset
  34.  
  35. *comment Number of guesses allowed
  36. *temp hm_lives 10
  37. *temp n
  38. *temp hmvictory false
  39. *comment Randomly select this game's word
  40. *rand n 1 10
  41. *gotoref "hmword_"&n
  42.  
  43. *comment -- THE WORDS --
  44.  
  45. *label hmword_1
  46. *temp hmword "dog"
  47. *temp hmletter_count 3
  48. *temp hmletter_1 "d"
  49. *temp hmletter_1_found false
  50. *temp hmletter_2 "o"
  51. *temp hmletter_2_found false
  52. *temp hmletter_3 "g"
  53. *temp hmletter_3_found false
  54. *goto hang_man_top
  55.  
  56. *label hmword_2
  57. *temp hmword "cat"
  58. *temp hmletter_count 3
  59. *temp hmletter_1 "c"
  60. *temp hmletter_1_found false
  61. *temp hmletter_2 "a"
  62. *temp hmletter_2_found false
  63. *temp hmletter_3 "t"
  64. *temp hmletter_3_found false
  65. *goto hang_man_top
  66.  
  67. *label hmword_3
  68. *temp hmword "fish"
  69. *temp hmletter_count 4
  70. *temp hmletter_1 "f"
  71. *temp hmletter_1_found false
  72. *temp hmletter_2 "i"
  73. *temp hmletter_2_found false
  74. *temp hmletter_3 "s"
  75. *temp hmletter_3_found false
  76. *temp hmletter_4 "h"
  77. *temp hmletter_4_found false
  78. *goto hang_man_top
  79.  
  80. *label hmword_4
  81. *temp hmword "shark"
  82. *temp hmletter_count 5
  83. *temp hmletter_1 "s"
  84. *temp hmletter_1_found false
  85. *temp hmletter_2 "h"
  86. *temp hmletter_2_found false
  87. *temp hmletter_3 "a"
  88. *temp hmletter_3_found false
  89. *temp hmletter_4 "r"
  90. *temp hmletter_4_found false
  91. *temp hmletter_5 "k"
  92. *temp hmletter_5_found false
  93. *goto hang_man_top
  94.  
  95. *label hmword_5
  96. *temp hmword "horse"
  97. *temp hmletter_count 5
  98. *temp hmletter_1 "h"
  99. *temp hmletter_1_found false
  100. *temp hmletter_2 "o"
  101. *temp hmletter_2_found false
  102. *temp hmletter_3 "r"
  103. *temp hmletter_3_found false
  104. *temp hmletter_4 "s"
  105. *temp hmletter_4_found false
  106. *temp hmletter_5 "e"
  107. *temp hmletter_5_found false
  108. *goto hang_man_top
  109.  
  110. *label hmword_6
  111. *temp hmword "rabbit"
  112. *temp hmletter_count 6
  113. *temp hmletter_1 "r"
  114. *temp hmletter_1_found false
  115. *temp hmletter_2 "a"
  116. *temp hmletter_2_found false
  117. *temp hmletter_3 "b"
  118. *temp hmletter_3_found false
  119. *temp hmletter_4 "b"
  120. *temp hmletter_4_found false
  121. *temp hmletter_5 "i"
  122. *temp hmletter_5_found false
  123. *temp hmletter_6 "t"
  124. *temp hmletter_6_found false
  125. *goto hang_man_top
  126.  
  127. *label hmword_7
  128. *temp hmword "mouse"
  129. *temp hmletter_count 5
  130. *temp hmletter_1 "m"
  131. *temp hmletter_1_found false
  132. *temp hmletter_2 "o"
  133. *temp hmletter_2_found false
  134. *temp hmletter_3 "u"
  135. *temp hmletter_3_found false
  136. *temp hmletter_4 "s"
  137. *temp hmletter_4_found false
  138. *temp hmletter_5 "e"
  139. *temp hmletter_5_found false
  140. *goto hang_man_top
  141.  
  142. *label hmword_8
  143. *temp hmword "badger"
  144. *temp hmletter_count 6
  145. *temp hmletter_1 "b"
  146. *temp hmletter_1_found false
  147. *temp hmletter_2 "a"
  148. *temp hmletter_2_found false
  149. *temp hmletter_3 "d"
  150. *temp hmletter_3_found false
  151. *temp hmletter_4 "g"
  152. *temp hmletter_4_found false
  153. *temp hmletter_5 "e"
  154. *temp hmletter_5_found false
  155. *temp hmletter_6 "r"
  156. *temp hmletter_6_found false
  157. *goto hang_man_top
  158.  
  159. *label hmword_9
  160. *temp hmword "elephant"
  161. *temp hmletter_count 8
  162. *temp hmletter_1 "e"
  163. *temp hmletter_1_found false
  164. *temp hmletter_2 "l"
  165. *temp hmletter_2_found false
  166. *temp hmletter_3 "e"
  167. *temp hmletter_3_found false
  168. *temp hmletter_4 "p"
  169. *temp hmletter_4_found false
  170. *temp hmletter_5 "h"
  171. *temp hmletter_5_found false
  172. *temp hmletter_6 "a"
  173. *temp hmletter_6_found false
  174. *temp hmletter_7 "n"
  175. *temp hmletter_7_found false
  176. *temp hmletter_8 "t"
  177. *temp hmletter_8_found false
  178. *goto hang_man_top
  179.  
  180. *label hmword_10
  181. *temp hmword "meerkat"
  182. *temp hmletter_count 7
  183. *temp hmletter_1 "m"
  184. *temp hmletter_1_found false
  185. *temp hmletter_2 "e"
  186. *temp hmletter_2_found false
  187. *temp hmletter_3 "e"
  188. *temp hmletter_3_found false
  189. *temp hmletter_4 "r"
  190. *temp hmletter_4_found false
  191. *temp hmletter_5 "k"
  192. *temp hmletter_5_found false
  193. *temp hmletter_6 "a"
  194. *temp hmletter_6_found false
  195. *temp hmletter_7 "t"
  196. *temp hmletter_7_found false
  197. *goto hang_man_top
  198.  
  199. *comment -- / END THE WORDS --
  200.  
  201.  
  202.  
  203. *label hang_man_top
  204. *comment Going to "hang_man_top" label basically refreshes the page information, via the routine below.
  205. *gosub hangman_parse
  206.  
  207. *comment GAME MENU/INTERFACE
  208. *choice
  209. *if (hmvictory) #YOU WIN!!
  210. *goto hang_man_top
  211. *if (hm_lives = 0) #YOU LOSE!!
  212. *goto hang_man_top
  213. *if ((hm_lives != 0) and (hmvictory != true)) #Guess a Letter
  214. *label guess_letter
  215. *gosub hangman_parse
  216. What letter?
  217. *temp str
  218. *input_text str
  219. *comment convert input to lowercase for easier matching:
  220. *script temps.str = temps.str.toLowerCase();
  221. *temp n 1
  222. *temp count 0
  223. *temp this_letter
  224. *comment Seeing as we can't limit length in input fields, the following loop
  225. *comment makes sure that input string matches the value of a single letter.
  226. *comment If for some reason it doesn't, it prints an error and prompts re-entry.
  227. *label check_letter_loop
  228. *set this_letter ({"l"&n})
  229. *if (this_letter != str)
  230. *if (n < 26)
  231. *comment if we've not checked the input against all 26 letters, check the next one.
  232. *set n + 1
  233. *goto check_letter_loop
  234. *line_break
  235. Error: '${str}' is not a letter.
  236. *line_break
  237. *goto guess_letter
  238. *comment reset n (count variable) for next loop
  239. *set n 1
  240. *comment Once we know the input is a valid letter we can check that letter against the word itself.
  241. *comment The following loop checks the input string (letter) against all the letters of the word in play.
  242. *comment Note that if we get a match we still carry on searching the rest as a letter may appear more than once.
  243. *label guess_letter_loop
  244. *set this_letter {("hmletter_"&n)}
  245. *if (this_letter = str)
  246. *comment Mark the letter as found/which in turn will tell the parse routine to display it.
  247. *setref (("hmletter_"&n)&"_found") true
  248. *set count + 1
  249. *if (n < hmletter_count)
  250. *set n + 1
  251. *goto guess_letter_loop
  252. *comment Tell the player how many matches there were for that letter.
  253. ${count} letters matched.
  254. *comment If there were no matches, i.e. the player guessed wrong, we deduct a life.
  255. *if (count = 0)
  256. *line_break
  257. Life lost.
  258. *line_break
  259. *set hm_lives - 1
  260. *line_break
  261. *goto hang_man_top
  262. *if ((hm_lives != 0) and (hmvictory != true)) #Guess the Word
  263. *gosub hangman_parse
  264. What do you think the word is?
  265. *temp str
  266. *input_text str
  267. *comment convert input to lowercase for easier matching:
  268. *script temps.str = temps.str.toLowerCase();
  269. *comment Check if the input string matches the word...
  270. *if (str = hmword)
  271. *set hmvictory true
  272. *comment The following loop just sets all the word's letters to 'found'/true so they're displayed on screen.
  273. *temp n 1
  274. *label guessed_word_loop
  275. *setref (("hmletter_"&n)&"_found") true
  276. *if (n != hmletter_count)
  277. *set n + 1
  278. *goto guessed_word_loop
  279. *goto hang_man_top
  280. *else
  281. *comment If it doesn't set the lives to 0 and reset the screen, essentially causing an instant game over.
  282. *set hm_lives 0
  283. *goto hang_man_top
  284. #Start Over
  285. *comment Reset the game
  286. *goto hm_reset
  287.  
  288. *comment This whole routine is responsible for printing the text display
  289. *label hangman_parse
  290. The Word:
  291. *temp n 1
  292. *temp count 0
  293. *comment This loop checks every letter's (in the active word) found status - if it's not been found (false) then we print an underscore.
  294. *comment If it has been found, we print that letter instead.
  295. *label hmword_print_loop
  296. *temp this_letter {(("hmletter_"&n)&"_found")}
  297. *if (this_letter != true)
  298. _
  299. *if (this_letter)
  300. *set this_letter {("hmletter_"&n)}
  301. *set count + 1
  302. ${this_letter}
  303. *if (n < hmletter_count)
  304. *set n + 1
  305. *goto hmword_print_loop
  306. *comment Check for victory
  307. *if (count = hmletter_count)
  308. *set hmvictory true
  309. *comment Print hangman diagram parts based on current lives.
  310. *line_break
  311. *line_break
  312. *if (hm_lives < 5)
  313. _____
  314. *line_break
  315. *if (hm_lives < 6)
  316. |/
  317. *if (hm_lives < 4)
  318. |
  319. *line_break
  320. *if (hm_lives < 7)
  321. |
  322. *if (hm_lives < 3)
  323. (x)
  324. *line_break
  325. *if (hm_lives < 8)
  326. |
  327. *if (hm_lives < 2)
  328. /|\
  329. *line_break
  330. *if (hm_lives < 9)
  331. |
  332. *if (hm_lives < 1)
  333. / \
  334. *line_break
  335. *if (hm_lives < 10)
  336. |\_
  337. *line_break
  338. *line_break
  339. *comment Display remaining lives to the player.
  340. Lives: ${hm_lives}
  341. *line_break
  342. *line_break
  343.  
  344. *return
Advertisement
Add Comment
Please, Sign In to add comment