Advertisement
here2share

# hangman.py

Sep 11th, 2016
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.04 KB | None | 0 0
  1. # hangman.py -- written to be all strategically solved as also difficult
  2.  
  3. import random
  4. import os
  5.  
  6. wordlist='''
  7. abruptly
  8. abyss
  9. acre
  10. adapt
  11. aerosol
  12. ahem
  13. album
  14. among
  15. arch
  16. avenue
  17. awkward
  18. bagpipes
  19. bamboo
  20. banjo
  21. bank
  22. beekeeper
  23. bench
  24. bikini
  25. bingo
  26. black
  27. blitz
  28. blizzard
  29. blue
  30. bluff
  31. blur
  32. boggle
  33. book
  34. bookworm
  35. brag
  36. buoyant
  37. boxcar
  38. brick
  39. brief
  40. brutal
  41. buffalo
  42. built
  43. bulk
  44. busy
  45. buzz
  46. buzzword
  47. calm
  48. camp
  49. candy
  50. cargo
  51. catch
  52. cash
  53. champ
  54. chalk
  55. charm
  56. cheap
  57. check
  58. cheeky
  59. chef
  60. chew
  61. chief
  62. chip
  63. chirp
  64. chop
  65. civil
  66. claw
  67. clap
  68. clip
  69. cloud
  70. clock
  71. club
  72. coax
  73. coin
  74. combo
  75. comic
  76. cook
  77. cough
  78. copy
  79. cube
  80. cuffs
  81. cyan
  82. climax
  83. climb
  84. cobweb
  85. comb
  86. comfy
  87. cozy
  88. crazy
  89. crawl
  90. crowd
  91. croquet
  92. crypt
  93. cubic
  94. curfew
  95. cycle
  96. cynical
  97. daily
  98. dazzle
  99. debt
  100. diary
  101. diva
  102. dizzy
  103. donor
  104. dojo
  105. dove
  106. dozen
  107. drill
  108. drop
  109. dump
  110. dunk
  111. duplex
  112. echo
  113. edgy
  114. eject
  115. embezzle
  116. employ
  117. empty
  118. encrypt
  119. enjoy
  120. envy
  121. epic
  122. equal
  123. etch
  124. ethnic
  125. evoke
  126. evolve
  127. exam
  128. excel
  129. exit
  130. feud
  131. fifty
  132. film
  133. fish
  134. fishhook
  135. fishy
  136. fizz
  137. flake
  138. flex
  139. flow
  140. fluid
  141. foam
  142. foil
  143. fork
  144. foxy
  145. fuel
  146. funny
  147. fridge
  148. from
  149. fuchsia
  150. funny
  151. galaxy
  152. galvanize
  153. gazebo
  154. gecko
  155. giant
  156. gigs
  157. girl
  158. gizmo
  159. glyph
  160. gossip
  161. groggy
  162. gulp
  163. guru
  164. gypsy
  165. gyro
  166. haiku
  167. haphazard
  168. havoc
  169. hawk
  170. hazy
  171. hertz
  172. hike
  173. hips
  174. hoax
  175. hook
  176. hoop
  177. howl
  178. hugs
  179. hunch
  180. hurry
  181. hype
  182. hyphen
  183. icebox
  184. icon
  185. idol
  186. igloo
  187. inch
  188. index
  189. info
  190. injury
  191. input
  192. irony
  193. itch
  194. ivory
  195. jackpot
  196. jazzy
  197. jelly
  198. jerk
  199. jigsaw
  200. jinx
  201. jiujitsu
  202. jobs
  203. jogs
  204. jockey
  205. jogging
  206. join
  207. joke
  208. jokingly
  209. joyful
  210. judge
  211. juicy
  212. jukebox
  213. jumbo
  214. jump
  215. junk
  216. jury
  217. kayak
  218. keep
  219. kegs
  220. ketchup
  221. keyhole
  222. khaki
  223. kick
  224. kilobyte
  225. kiosk
  226. kiwi
  227. klutz
  228. knapsack
  229. knock
  230. know
  231. koala
  232. lamp
  233. laugh
  234. lava
  235. left
  236. lilac
  237. limo
  238. loaf
  239. logic
  240. look
  241. loud
  242. love
  243. lucid
  244. lucky
  245. lust
  246. luxury
  247. lymph
  248. lyrics
  249. marquis
  250. matrix
  251. maze
  252. megahertz
  253. memo
  254. menu
  255. meow
  256. microwave
  257. milk
  258. mimic
  259. mini
  260. minx
  261. mnemonic
  262. mystify
  263. myth
  264. navy
  265. next
  266. nosy
  267. nuke
  268. numb
  269. nightclub
  270. obey
  271. object
  272. okay
  273. once
  274. only
  275. onyx
  276. ostrich
  277. ouch
  278. oval
  279. oven
  280. oxygen
  281. palm
  282. park
  283. pawn
  284. pajama
  285. peekaboo
  286. picnic
  287. pink
  288. pixel
  289. pizza
  290. ploy
  291. plug
  292. plump
  293. pneumonia
  294. poem
  295. poetic
  296. polkadot
  297. prez
  298. proud
  299. psyche
  300. psycho
  301. punch
  302. puppy
  303. puzzle
  304. python
  305. quack
  306. quartet
  307. quartz
  308. quench
  309. queue
  310. quick
  311. quilt
  312. quiz
  313. ramp
  314. rhubarb
  315. rhyme
  316. rhythm
  317. rickshaw
  318. risk
  319. scratch
  320. script
  321. shrink
  322. size
  323. skip
  324. slip
  325. slow
  326. snazzy
  327. soak
  328. sofa
  329. sphinx
  330. spritz
  331. spun
  332. squawk
  333. stop
  334. strength
  335. stretch
  336. subway
  337. subject
  338. swim
  339. swivel
  340. syndrome
  341. system
  342. symptom
  343. taco
  344. taxi
  345. thumbscrew
  346. topaz
  347. tour
  348. transplant
  349. trip
  350. twelfth
  351. twin
  352. type
  353. typical
  354. typhoon
  355. typo
  356. unhitch
  357. unknown
  358. unworthy
  359. uncle
  360. unzip
  361. uptown
  362. vacancy
  363. vacuum
  364. vaporize
  365. venue
  366. verb
  367. vibe
  368. vinyl
  369. violet
  370. vivid
  371. vixen
  372. vodka
  373. voice
  374. volt
  375. volume
  376. voodoo
  377. vortex
  378. wagon
  379. walk
  380. walkway
  381. waltz
  382. wave
  383. wavy
  384. waxy
  385. wheezy
  386. whiskey
  387. whip
  388. whomever
  389. wimpy
  390. wink
  391. witchcraft
  392. wizard
  393. woah
  394. woke
  395. wolf
  396. woozy
  397. work
  398. world
  399. worldwide
  400. wrap
  401. wristwatch
  402. xylophone
  403. yacht
  404. yawn
  405. yellow
  406. yelp
  407. yippee
  408. yoga
  409. young
  410. youthful
  411. yummy
  412. zephyr
  413. zero
  414. zigzag
  415. zilch
  416. zinc
  417. zipper
  418. zodiac
  419. zombie
  420. zoom
  421. '''.split('\n')[1:-1]
  422. random.shuffle(wordlist)
  423. tempwordlist=wordlist[:]
  424.  
  425. HANGMANPICS = [r'''
  426.    ._____.
  427.    |    \|
  428.          |
  429.          |
  430.          |
  431.          |
  432.         /|\
  433.     =============''', r'''
  434.    ._____.
  435.    |    \|
  436.    O     |
  437.          |
  438.          |
  439.          |
  440.         /|\
  441.     =============''', r'''
  442.    ._____.
  443.    |    \|
  444.    O     |
  445.    |     |
  446.          |
  447.          |
  448.         /|\
  449.     =============''', r'''
  450.    ._____.
  451.    |    \|
  452.    O     |
  453.    |\   |
  454.          |
  455.          |
  456.         /|\
  457.     =============''', r'''
  458.    ._____.
  459.    |    \|
  460.    O     |
  461.   /|\   |
  462.          |
  463.          |
  464.         /|\
  465.     =============''', r'''
  466.    ._____.
  467.    |    \|
  468.    O     |
  469.   /|\   |
  470.     \   |
  471.          |
  472.         /|\
  473.     =============''', r'''
  474.    ._____.
  475.    |    \|  GAME
  476.    O     |  OVER
  477.   /|\   |  ----
  478.   / \   |
  479.          |
  480.         /|\
  481.     =============''']
  482.    
  483. def cls():
  484.     os.system('cls' if os.name == 'nt' else 'clear')
  485.     print
  486.  
  487. def getRandomWord(x):
  488.     if not x:
  489.         x=wordlist[:]
  490.     return tempwordlist.pop()
  491.    
  492.    
  493. def displayBoard(missedLetters, correctLetters, secretWord):
  494.     cls()
  495.     print ' H A N G M A N\n'
  496.     print HANGMANPICS[len(missedLetters)]
  497.     print ' Missed letters [' + missedLetters + ']\n',
  498.    
  499.     blanks = ''
  500.    
  501.     for i in range(len(secretWord)):
  502.         if secretWord[i] in correctLetters:
  503.             blanks += secretWord[i]
  504.         else:
  505.             blanks += '_'
  506.    
  507.     print " Mystery Word:",
  508.     for letter in blanks:
  509.         print letter,
  510.        
  511. def getGuess(alreadyGuessed):
  512.     while True:
  513.         guess = raw_input('\n Guess a letter >>> ').strip()
  514.         guess = guess.lower()
  515.        
  516.         if len(guess) != 1:
  517.             print ' Single letter only please... %s' % secretWord
  518.         elif guess in alreadyGuessed:
  519.             print ' Already guessed that one, choose again...'
  520.         elif guess not in 'abcdefghijklmnopqrstuvwxyz':
  521.             print ' Please enter a LETTER'
  522.         else:
  523.             return guess
  524.  
  525. def playAgain():
  526.     print ' Again? Y/N'
  527.     return raw_input('> ').strip().lower().startswith('y')
  528.  
  529. missedLetters = ''
  530. correctLetters = ''
  531. secretWord = getRandomWord(tempwordlist)
  532. gameIsDone = False
  533.  
  534. while True:
  535.     displayBoard(missedLetters, correctLetters, secretWord)
  536.    
  537.     guess = getGuess(missedLetters + correctLetters)
  538.    
  539.     if guess in secretWord:
  540.         correctLetters = correctLetters + guess
  541.         foundAllLetters = True
  542.         for i in range(len(secretWord)):
  543.             if secretWord[i] not in correctLetters:
  544.                 foundAllLetters = False
  545.                 break
  546.         if foundAllLetters:
  547.             displayBoard(missedLetters, correctLetters, secretWord)
  548.             print '\n\n Congratulations... you found the secret word!'
  549.             gameIsDone = True
  550.     else:
  551.         missedLetters = missedLetters + guess
  552.         if len(missedLetters) == len(HANGMANPICS) - 1:
  553.             displayBoard(missedLetters, correctLetters, secretWord)
  554.             print '\n\n You have run out of guesses\n The word was %s' % (secretWord)
  555.             gameIsDone = True
  556.     if gameIsDone:
  557.         if playAgain():
  558.             missedLetters = ''
  559.             correctLetters = ''
  560.             gameIsDone = False
  561.             secretWord = getRandomWord(tempwordlist)
  562.         else:
  563.             break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement