Guest User

Untitled

a guest
Dec 27th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. import string
  2. import random
  3.  
  4.  
  5. class Main:
  6.  
  7. def __init__(self):
  8. self.place_holder = []
  9. self.wrong_guesses = 10
  10. self.guesses = []
  11. with open('./words.txt', mode='r') as f:
  12. self.word = random.choice(f.readlines())
  13. self.word = self.word.strip()
  14. print(self.word, end='')
  15. return self.place_holders()
  16.  
  17. def place_holders(self):
  18. # creates the placeholders for the word
  19. for char in self.word:
  20. if char != " ":
  21. self.place_holder.append(' _ ')
  22. elif char == " ":
  23. self.place_holder.append(' ')
  24. print("".join(self.place_holder)) # prints initial placeholder
  25. return self.is_letter()
  26.  
  27. @staticmethod
  28. def guess():
  29. # Prompts the user for a guess
  30. while True:
  31. guessed_letter = input(str('Give a guess: ')).lower()
  32. if guessed_letter not in string.ascii_lowercase or len(guessed_letter) > 1:
  33. print(
  34. 'You have inputted more than one character or the character entered was not recognized.nPlease try again.')
  35. else:
  36. break
  37. return guessed_letter
  38.  
  39. def is_letter(self):
  40. # finds out if the letter belongs to the word, and if so, places it on the right placeholder
  41. guessed_letter = self.guess()
  42. if guessed_letter in self.word and guessed_letter not in self.guesses:
  43. for i in range(len(self.word)):
  44. if guessed_letter == self.word[i]:
  45. self.place_holder[i] = f' {self.word[i]} '
  46. elif guessed_letter in self.guesses:
  47. print('You already said that..n')
  48. else:
  49. self.wrong_guesses -= 1
  50. print(f'Sorry, missed.nYou have {self.wrong_guesses} guesses left.n')
  51.  
  52. self.guesses.append(guessed_letter)
  53. print("".join(self.place_holder)) # prints the updated placeholder
  54. return self.is_over()
  55.  
  56. def is_over(self):
  57. # Checks if the players has guessed the full word or if the player ran out of guesses
  58. if ' _ ' in self.place_holder and self.wrong_guesses > 0:
  59. self.is_letter()
  60. elif ' _ ' in self.place_holder and self.wrong_guesses == 0:
  61. print(f'Sorry, Game Over!nThe word to guess was: {self.word}')
  62. self.play_again()
  63. else:
  64. self.play_again()
  65.  
  66. @staticmethod
  67. def play_again():
  68. # Prompts the player if he wants to play again or not
  69. if input('Do you want to play again? (y/n): ').lower().startswith('y'):
  70. Main()
  71. else:
  72. print('Fair enough.. Thanks for playing!')
  73. quit()
  74.  
  75.  
  76. Main()
Add Comment
Please, Sign In to add comment