Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #require 'debugger'
- class Hangman
- def self.two_player_game
- #this method is not finished yet
- end
- def self.human_guesser
- Hangman.new(AI.new, HumanPlayer.new)
- end
- def self.computer_guesser
- Hangman.new(HumanPlayer.new, AI.new)
- end
- attr_accessor :secret_word, :dictionary_words, :player_string, :word_size_floor, :word_size_ceiling,
- :incorrect_guesses, :correct_guesses
- def initialize(secret_word_generating_player, guessing_player,
- quantity_wrong_guesses = 15, word_size_floor = 5, word_size_ceiling = 25)
- @secret_word = []
- @dictionary_words = {}
- @correct_guesses = []
- @incorrect_guesses = []
- @current_guess = ""
- @quantity_wrong_guesses = quantity_wrong_guesses
- @dict_filename = "5desk.txt"
- @word_size_floor = word_size_floor
- @word_size_ceiling = word_size_ceiling
- @secret_word_generating_player = secret_word_generating_player
- @guessing_player = guessing_player
- # set the players' Hangman objects to this particular hangman
- @secret_word_generating_player.hangman_game = self
- @guessing_player.hangman_game = self
- end
- def play
- # Create Players
- # Display instructions
- print_instructions
- # Load in the dictionary
- load_dictionary
- # Generate the secret_word
- # debugger
- @secret_word = @secret_word_generating_player.generate_secret_word.downcase.split('')
- @correct_guesses = ["_"] * @secret_word.length
- puts @correct_guesses.join(' ')
- # Begin looping!
- until game_over?
- # Ask for and validate user input
- @current_guess = @guessing_player.get_letter
- puts "Current guess: #{@current_guess.inspect}"
- # do stuff with user input
- write_current_guess
- do_game_logic
- # display our current words
- print_game
- end
- end
- def print_instructions
- puts
- puts "Hey welcome to Hangman"
- puts "You know how to play. You will have #{@quantity_wrong_guesses} wrong guesses before death."
- puts
- end
- def load_dictionary
- File.foreach(@dict_filename) do |line|
- @dictionary_words[line.strip] = nil
- end
- true
- end
- # returns the secret word
- def game_over?
- if victory?
- puts "CONGRATULATIONS! Play again? (y/n)"
- play_again = gets.chomp
- if play_again == "y"
- self.play
- else
- exit
- end
- true
- elsif failure?
- puts "YOU SUCK"
- puts "You should have guessed: #{@secret_word}!"
- true
- else
- false
- end
- end
- def victory?
- @correct_guesses.join == @secret_word.join
- end
- def failure?
- @incorrect_guesses.size >= @quantity_wrong_guesses
- end
- def write_current_guess
- if !@secret_word.include?(@current_guess)
- @incorrect_guesses << @current_guess
- else
- @secret_word.each_with_index do |char, index|
- @correct_guesses[index] = char if char == @current_guess
- end
- end
- end
- def do_game_logic
- end
- def print_game
- puts
- puts "Your correct guesses are: #{@correct_guesses.join(' ')}"
- puts "Your INcorrect guesses are: #{@incorrect_guesses}"
- puts "You have #{@quantity_wrong_guesses - @incorrect_guesses.size} guesses left."
- puts
- end
- end
- # ------------------------------------------------------
- class Player
- attr_accessor :hangman_game
- def initialize
- @hangman_game = []
- end
- # determines if the attempted secret word is valid
- def valid_secret_word?(attempted_word)
- if attempted_word.length < @hangman_game.word_size_floor || attempted_word.length > @hangman_game.word_size_ceiling
- false
- elsif in_dictionary?(attempted_word)
- # checks that it's in the dictionary
- true
- else
- false
- end
- end
- def in_dictionary?(attempted_word)
- @hangman_game.dictionary_words.has_key?(attempted_word)
- end
- end
- # ------------------------------------------------------
- class HumanPlayer < Player
- def generate_secret_word
- attempted_word = ""
- until valid_secret_word?(attempted_word)
- puts "Please enter a secret word of between #{@hangman_game.word_size_floor} and #{@hangman_game.word_size_ceiling} characters:"
- attempted_word = gets.chomp.downcase
- end
- attempted_word
- end
- def valid_letter?(let)
- if let == ""
- false
- elsif ("a".."z").include?(let)
- true
- else
- puts "Seriously, give us a VALID letter."
- false
- end
- end
- def get_letter
- input_letter = ""
- until valid_letter?(input_letter)
- puts "Please input a valid guess"
- input_letter = gets.chomp.downcase
- end
- input_letter
- end
- end
- # ------------------------------------------------------
- class AI < Player
- attr_accessor :possible_words, :alphabet_hash_table
- def initialize
- @possible_words = []
- @alphabet_hash_table = {}
- ("a".."z").each { |char| @alphabet_hash_table[char] = 0 }
- end
- def generate_secret_word
- attempted_word = ""
- until valid_secret_word?(attempted_word)
- attempted_word = @hangman_game.dictionary_words.keys.sample
- end
- attempted_word
- end
- def word_size_match
- @possible_words = @hangman_game.dictionary_words.keys.select do |word|
- word.size == @hangman_game.correct_guesses.size
- end
- optimal_words
- end
- def optimal_words
- words_to_delete = []
- @possible_words.each_with_index do |word, wordindex|
- word.split('').each_with_index do |letter, letterindex|
- if @hangman_game.correct_guesses[letterindex] == "_"
- next
- else @hangman_game.correct_guesses[letterindex] != letter
- words_to_delete << word
- end
- end
- end
- @possible_words -= words_to_delete
- letter_frequency
- end
- def letter_frequency
- @possible_words.join.each_char do |char|
- @alphabet_hash_table[char.downcase] += 1
- end
- end
- # returns that character which has the highest number of occurrences in the hash table
- def pick_highest_available_char
- max = 0
- @alphabet_hash_table.sort_by {|k,v| -v}.each do |pair|
- if !(@hangman_game.correct_guesses.include?(pair[0])) && !(@hangman_game.incorrect_guesses.include?(pair[0]))
- return pair[0]
- end
- end
- end
- def get_letter
- puts "The AI is guessing..."
- #letter_possibilities = ("a".."z").to_a - @hangman_game.correct_guesses - @hangman_game.incorrect_guesses
- #input_letter = letter_possibilities.sample
- word_size_match
- puts "This is possible letters: #{@alphabet_hash_table}"
- pick_highest_available_char
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement