class PaitinHangman::GameEngine

Attributes

count[R]
guess[R]
misses[R]
player2[R]
right_guesses[R]

Public Class Methods

new() click to toggle source
# File lib/paitin_hangman/game_engine.rb, line 8
def initialize
  @misses = []
  @right_guesses = []
end

Public Instance Methods

cheat_or_quit_or_history(chances) click to toggle source
# File lib/paitin_hangman/game_engine.rb, line 78
def cheat_or_quit_or_history(chances)
  if @guess == ":c"
    puts @game_word
    verify_guess(chances)
  elsif @guess == "quit" || @guess == ":q"
    quit_or_save(chances)
  elsif @guess == ":h" || @guess == "history"
    history_verify_guess(chances)
  end
end
compare_guess() click to toggle source
# File lib/paitin_hangman/game_engine.rb, line 24
def compare_guess
  if @game_word.include?(@guess) == false
    wrong_guess
  else
    correct_guess
  end
end
correct_guess() click to toggle source
# File lib/paitin_hangman/game_engine.rb, line 49
def correct_guess
  @game_word.each_char.with_index do |letter, index|
    right_guess(letter, index)
  end
  @right_guesses << @guess
  puts @word_control.gsub("_", " __ ").upcase
end
end_game(player = @player2) click to toggle source
# File lib/paitin_hangman/game_engine.rb, line 142
def end_game(player = @player2)
  puts "Game over! You have been HANGED! Sorry #{player}".red
  puts "The word is #{@game_word.upcase}"
  Message.end_games
  choice_integrity
end
history() click to toggle source
# File lib/paitin_hangman/game_engine.rb, line 115
def history
  if @misses.empty? && @right_guesses.empty?
    puts "you have not entered any guesses"
  else
    print_history
  end
end
history_verify_guess(chances) click to toggle source
# File lib/paitin_hangman/game_engine.rb, line 89
def history_verify_guess(chances)
  history
  verify_guess(chances)
end
print_history() click to toggle source
quit_or_save(chances) click to toggle source
# File lib/paitin_hangman/game_engine.rb, line 94
def quit_or_save(chances)
  exit if @player1
  puts "Do you want to save your game? type 'Yes' or 'No'"
  choice = STDIN.gets.chomp.downcase
  until choice == "yes" || choice == "no"
    puts "Please type a 'Yes' or a 'No'"
    choice = STDIN.gets.chomp.downcase
  end
  choice == "no" ? exit : save_game(chances)
end
right_guess(letter, index) click to toggle source
# File lib/paitin_hangman/game_engine.rb, line 57
def right_guess(letter, index)
  if letter == @guess
    if @word_control.include?(@guess) == false
      @count += 1
      puts "Nice one!".green
    end
    @word_control[index] = @guess
  end
end
save_game(chances) click to toggle source
# File lib/paitin_hangman/game_engine.rb, line 105
def save_game(chances)
  game_data = GameData.new(@player2, @misses, @right_guesses,
                           (chances - @counter), @word_control,
                           @game_word, @count)
  file_name = File.join(File.dirname(File.expand_path(__FILE__)), '../../games.yml')
  File.open(file_name, "a") { |data| YAML.dump(game_data, data) }
  puts "Goodbye, your game has been saved successfully".green
  exit
end
setup(game_word, player2, player1) click to toggle source
# File lib/paitin_hangman/game_engine.rb, line 32
def setup(game_word, player2, player1)
  @player2 = player2
  @player1 = player1
  @game_word = game_word
  @word_control = ""
  @game_word.length.times { @word_control << "_" }
  @count = 0
  puts @word_control.gsub("_", "__ ")
end
trials(chances, game_word, player2, player1) click to toggle source
# File lib/paitin_hangman/game_engine.rb, line 13
def trials(chances, game_word, player2, player1)
  setup(game_word, player2, player1)
  chances.times do |counter|
    @counter = counter
    verify_guess(chances)
    compare_guess
    win_game(chances, counter)
  end
  end_game
end
verify_guess(chances) click to toggle source
# File lib/paitin_hangman/game_engine.rb, line 67
def verify_guess(chances)
  puts "Enter a guess"
  @guess = gets.chomp.downcase
  cheat_or_quit_or_history(chances)
  until length_one?(@guess) && unique?(@guess) && number?(@guess)
    @guess = STDIN.gets.chomp.downcase
    puts @game_word if @guess == ":c"
    puts "Your missed guesses: #{@misses.join(', ')}" if @guess == ":h"
  end
end
win_game(chances, counter, player = @player2) click to toggle source
# File lib/paitin_hangman/game_engine.rb, line 134
def win_game(chances, counter, player = @player2)
  if @count == @game_word.chars.uniq.length
    puts "Congratulations #{player}! You have won the game".green
    exit
  end
  puts "You have #{chances - 1 - counter} chance(s) left"
end
wrong_guess() click to toggle source
# File lib/paitin_hangman/game_engine.rb, line 42
def wrong_guess
  @misses << @guess
  puts "Bad guess, the noose tightens".red
  puts "Try again"
  puts @word_control.gsub("_", " __ ").upcase
end