Advertisement
Guest User

Untitled

a guest
Jan 11th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. #require 'debugger'
  2.  
  3. class Hangman
  4.  
  5. def self.two_player_game
  6. #this method is not finished yet
  7. end
  8.  
  9. def self.human_guesser
  10. Hangman.new(AI.new, HumanPlayer.new)
  11. end
  12.  
  13. def self.computer_guesser
  14. Hangman.new(HumanPlayer.new, AI.new)
  15. end
  16.  
  17. attr_accessor :secret_word, :dictionary_words, :player_string, :word_size_floor, :word_size_ceiling,
  18. :incorrect_guesses, :correct_guesses
  19.  
  20. def initialize(secret_word_generating_player, guessing_player,
  21. quantity_wrong_guesses = 15, word_size_floor = 5, word_size_ceiling = 25)
  22. @secret_word = []
  23. @dictionary_words = {}
  24. @correct_guesses = []
  25. @incorrect_guesses = []
  26. @current_guess = ""
  27. @quantity_wrong_guesses = quantity_wrong_guesses
  28. @dict_filename = "5desk.txt"
  29. @word_size_floor = word_size_floor
  30. @word_size_ceiling = word_size_ceiling
  31. @secret_word_generating_player = secret_word_generating_player
  32. @guessing_player = guessing_player
  33.  
  34. # set the players' Hangman objects to this particular hangman
  35. @secret_word_generating_player.hangman_game = self
  36. @guessing_player.hangman_game = self
  37. end
  38.  
  39. def play
  40. # Create Players
  41. # Display instructions
  42. print_instructions
  43. # Load in the dictionary
  44. load_dictionary
  45. # Generate the secret_word
  46. # debugger
  47. @secret_word = @secret_word_generating_player.generate_secret_word.downcase.split('')
  48. @correct_guesses = ["_"] * @secret_word.length
  49.  
  50. puts @correct_guesses.join(' ')
  51.  
  52. # Begin looping!
  53.  
  54. until game_over?
  55.  
  56. # Ask for and validate user input
  57. @current_guess = @guessing_player.get_letter
  58. puts "Current guess: #{@current_guess.inspect}"
  59. # do stuff with user input
  60. write_current_guess
  61.  
  62. do_game_logic
  63.  
  64. # display our current words
  65. print_game
  66.  
  67. end
  68.  
  69. end
  70.  
  71. def print_instructions
  72. puts
  73. puts "Hey welcome to Hangman"
  74. puts "You know how to play. You will have #{@quantity_wrong_guesses} wrong guesses before death."
  75. puts
  76. end
  77.  
  78. def load_dictionary
  79.  
  80. File.foreach(@dict_filename) do |line|
  81. @dictionary_words[line.strip] = nil
  82. end
  83.  
  84. true
  85. end
  86.  
  87. # returns the secret word
  88.  
  89. def game_over?
  90. if victory?
  91. puts "CONGRATULATIONS! Play again? (y/n)"
  92. play_again = gets.chomp
  93. if play_again == "y"
  94. self.play
  95. else
  96. exit
  97. end
  98. true
  99. elsif failure?
  100. puts "YOU SUCK"
  101. puts "You should have guessed: #{@secret_word}!"
  102. true
  103. else
  104. false
  105. end
  106. end
  107.  
  108. def victory?
  109. @correct_guesses.join == @secret_word.join
  110. end
  111.  
  112. def failure?
  113. @incorrect_guesses.size >= @quantity_wrong_guesses
  114. end
  115.  
  116.  
  117. def write_current_guess
  118. if !@secret_word.include?(@current_guess)
  119. @incorrect_guesses << @current_guess
  120. else
  121. @secret_word.each_with_index do |char, index|
  122. @correct_guesses[index] = char if char == @current_guess
  123. end
  124. end
  125. end
  126.  
  127. def do_game_logic
  128. end
  129.  
  130. def print_game
  131. puts
  132. puts "Your correct guesses are: #{@correct_guesses.join(' ')}"
  133. puts "Your INcorrect guesses are: #{@incorrect_guesses}"
  134. puts "You have #{@quantity_wrong_guesses - @incorrect_guesses.size} guesses left."
  135. puts
  136. end
  137.  
  138. end
  139.  
  140. # ------------------------------------------------------
  141. class Player
  142.  
  143. attr_accessor :hangman_game
  144.  
  145. def initialize
  146. @hangman_game = []
  147. end
  148.  
  149. # determines if the attempted secret word is valid
  150. def valid_secret_word?(attempted_word)
  151. if attempted_word.length < @hangman_game.word_size_floor || attempted_word.length > @hangman_game.word_size_ceiling
  152. false
  153. elsif in_dictionary?(attempted_word)
  154. # checks that it's in the dictionary
  155. true
  156. else
  157. false
  158. end
  159. end
  160.  
  161. def in_dictionary?(attempted_word)
  162. @hangman_game.dictionary_words.has_key?(attempted_word)
  163. end
  164.  
  165. end
  166.  
  167. # ------------------------------------------------------
  168. class HumanPlayer < Player
  169. def generate_secret_word
  170. attempted_word = ""
  171. until valid_secret_word?(attempted_word)
  172. puts "Please enter a secret word of between #{@hangman_game.word_size_floor} and #{@hangman_game.word_size_ceiling} characters:"
  173. attempted_word = gets.chomp.downcase
  174. end
  175. attempted_word
  176. end
  177.  
  178. def valid_letter?(let)
  179. if let == ""
  180. false
  181. elsif ("a".."z").include?(let)
  182. true
  183. else
  184. puts "Seriously, give us a VALID letter."
  185. false
  186. end
  187. end
  188.  
  189. def get_letter
  190. input_letter = ""
  191. until valid_letter?(input_letter)
  192. puts "Please input a valid guess"
  193. input_letter = gets.chomp.downcase
  194. end
  195. input_letter
  196. end
  197. end
  198.  
  199.  
  200.  
  201. # ------------------------------------------------------
  202. class AI < Player
  203.  
  204. attr_accessor :possible_words, :alphabet_hash_table
  205.  
  206. def initialize
  207. @possible_words = []
  208. @alphabet_hash_table = {}
  209. ("a".."z").each { |char| @alphabet_hash_table[char] = 0 }
  210. end
  211.  
  212. def generate_secret_word
  213. attempted_word = ""
  214. until valid_secret_word?(attempted_word)
  215. attempted_word = @hangman_game.dictionary_words.keys.sample
  216. end
  217. attempted_word
  218. end
  219.  
  220. def word_size_match
  221. @possible_words = @hangman_game.dictionary_words.keys.select do |word|
  222. word.size == @hangman_game.correct_guesses.size
  223. end
  224. optimal_words
  225. end
  226.  
  227. def optimal_words
  228. words_to_delete = []
  229.  
  230. @possible_words.each_with_index do |word, wordindex|
  231. word.split('').each_with_index do |letter, letterindex|
  232. if @hangman_game.correct_guesses[letterindex] == "_"
  233. next
  234. else @hangman_game.correct_guesses[letterindex] != letter
  235. words_to_delete << word
  236. end
  237. end
  238. end
  239. @possible_words -= words_to_delete
  240. letter_frequency
  241. end
  242.  
  243. def letter_frequency
  244. @possible_words.join.each_char do |char|
  245. @alphabet_hash_table[char.downcase] += 1
  246. end
  247. end
  248.  
  249.  
  250.  
  251. # returns that character which has the highest number of occurrences in the hash table
  252. def pick_highest_available_char
  253.  
  254. max = 0
  255. @alphabet_hash_table.sort_by {|k,v| -v}.each do |pair|
  256. if !(@hangman_game.correct_guesses.include?(pair[0])) && !(@hangman_game.incorrect_guesses.include?(pair[0]))
  257. return pair[0]
  258. end
  259. end
  260. end
  261.  
  262. def get_letter
  263. puts "The AI is guessing..."
  264. #letter_possibilities = ("a".."z").to_a - @hangman_game.correct_guesses - @hangman_game.incorrect_guesses
  265. #input_letter = letter_possibilities.sample
  266. word_size_match
  267. puts "This is possible letters: #{@alphabet_hash_table}"
  268. pick_highest_available_char
  269. end
  270. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement