/hangman.py

https://github.com/nielsbom/Hangman · Python · 138 lines · 92 code · 30 blank · 16 comment · 30 complexity · d1a254a361214dbcafc5412de28a8887 MD5 · raw file

  1. # Hangman
  2. # Guess letters, then guess the word.
  3. # http://en.wikipedia.org/wiki/Hangman_(game)
  4. # standard library
  5. import sys, os, random
  6. # custom modules
  7. import graphics
  8. # clears the screen
  9. def clear():
  10. os.system("clear")
  11. # get a random word
  12. def getRandomWord(source, wordLength=5):
  13. while True:
  14. word = random.choice(words).strip()
  15. if len(word) == wordLength:
  16. return word
  17. def victory():
  18. print "Congratulations, you guessed the word correctly!"
  19. def areAllLettersGuessed(word, guessedLetters):
  20. # remove duplicates from word
  21. wordNoDuplicates = list(set(word))
  22. correctLetters = [letter for letter in guessedLetters if letter in word]
  23. if len(correctLetters) == len(wordNoDuplicates):
  24. return True
  25. else:
  26. return False
  27. # load the words in the dictionary
  28. file = open("dutch.txt", "r+") # open for reading and writing
  29. words = file.readlines()
  30. # introduction
  31. clear()
  32. print """Welcome to Hangman
  33. The goal of this game is to find out which word I'm thinking of.
  34. You can guess for a letter or a complete word. You can guess wrong 6
  35. times, the 7th wrong guess will be your last mistake.
  36. """
  37. gamePlayed = False
  38. while True:
  39. # start a new game?
  40. if not gamePlayed:
  41. newGameQuestion = "Are you ready to start?"
  42. else:
  43. newGameQuestion = "Do you want to play another game?"
  44. input = raw_input(newGameQuestion + " (y/n) \n")
  45. if input == "n":
  46. clear()
  47. break
  48. elif input != "y":
  49. clear()
  50. print "Please press either a 'y' or an 'n'"
  51. continue
  52. # how long should the word be?
  53. wordLength = int(raw_input("How many letters do you want to guess for?"))
  54. # start the game
  55. word = getRandomWord(words, wordLength)
  56. guessedLetters = [] # all the letters that were guessed
  57. mistakes = 0
  58. while mistakes < 8:
  59. clear()
  60. if guessedLetters:
  61. print "Guessed letters: ",
  62. for letter in guessedLetters:
  63. print letter + " ",
  64. print graphics.hangmanSteps[mistakes] + "\n\n"
  65. if mistakes < 7:
  66. print "Guess this word: ",
  67. for letter in word:
  68. if letter in guessedLetters:
  69. print letter,
  70. else:
  71. print "_",
  72. print ("\n")
  73. print "You have " + str(7 - mistakes) + " guesses left"
  74. guess = raw_input("Guess a letter or the complete word: ")
  75. # guess a letter
  76. if len(guess) == 1:
  77. if guess not in guessedLetters:
  78. guessedLetters.append(guess)
  79. if guess not in word:
  80. mistakes += 1
  81. else:
  82. # if all letters are guessed, player wins
  83. if areAllLettersGuessed(word, guessedLetters):
  84. victory()
  85. break
  86. else:
  87. raw_input(
  88. "You already guessed that letter, press Enter to guess another one"
  89. )
  90. continue
  91. # guesses for the whole word
  92. else:
  93. if len(guess) != len(word):
  94. pass
  95. if len(guess) == len(word):
  96. mistakes += 1
  97. if guess == word:
  98. victory()
  99. break
  100. else:
  101. print "Game over, too bad\n"
  102. print 'The correct word was "' + word + '"\n\n'
  103. break
  104. gamePlayed = True
  105. if gamePlayed:
  106. print "Thanks for playing Hangman!\n\n"
  107. print "This game was made by Niels Bom 2011-09-20"