class FemisHangman::Game

Attributes

feedback[RW]
history[RW]
status[RW]
turns[RW]
word[RW]

Public Class Methods

new(difficulty, feedback) click to toggle source
# File lib/femis_hangman/game.rb, line 6
def initialize(difficulty, feedback)
  word = Word.new
  @word = word.generate(difficulty)
  @history = []
  @turns = 7 + difficulty
  @feedback = feedback
  @status = 'play'
end

Public Instance Methods

check_game() click to toggle source
# File lib/femis_hangman/game.rb, line 46
def check_game
  if won? then game_won
  elsif lost? then game_lost
  else
    puts turns_prompt(@turns)
    puts show_word
  end
end
commands(input) click to toggle source
# File lib/femis_hangman/game.rb, line 22
def commands(input)
  case input
    when ':h', 'history' then puts ("You have used: #{game_history}")
    when ':q', 'quit' then quit_game
    else puts invalid_prompt
  end
end
control(input) click to toggle source
# File lib/femis_hangman/game.rb, line 15
def control(input)
  if input.size > 1 then commands(input)
  elsif input.size == 1 then play(input)
  else puts invalid_prompt
  end
end
game_history() click to toggle source
# File lib/femis_hangman/game.rb, line 99
def game_history
  if @history.empty?
    'NO LETTER YET'
  else
    output = ''
    @history.each {|letter| output << "#{letter} "}
  end
  output
end
game_lost() click to toggle source
# File lib/femis_hangman/game.rb, line 89
def game_lost
  if @feedback == 2
    puts lost_gui(@word)
  else
    puts lost_prompt(@word)
  end
  puts replay_prompt
  @status = 'restart'
end
game_won() click to toggle source
# File lib/femis_hangman/game.rb, line 79
def game_won
  if @feedback == 2
    puts won_gui(@word)
  else
    puts won_prompt(@word)
  end
  puts replay_prompt
  @status = 'restart'
end
include_letter(letter, history) click to toggle source
# File lib/femis_hangman/game.rb, line 36
def include_letter(letter, history)
  if history.include?(letter)
    puts duplicate_prompt(letter)
    @history
  else
    history << letter
    @history = history
  end
end
lost?() click to toggle source
# File lib/femis_hangman/game.rb, line 75
def lost?
  @turns == 0
end
play(input) click to toggle source
# File lib/femis_hangman/game.rb, line 30
def play(input)
  include_letter(input, @history)
  @turns -= 1 unless @word.include?(input)
  check_game
end
quit_game() click to toggle source
# File lib/femis_hangman/game.rb, line 109
def quit_game
  @status = 'quit'
  puts save_prompt
end
show_word() click to toggle source
# File lib/femis_hangman/game.rb, line 55
def show_word
  output = ''
  @word.split('').each do |letter|
    if @history.include?(letter)
      output << "#{letter} "
    else
      output << '_ '
    end
  end
  output
end
won?() click to toggle source
# File lib/femis_hangman/game.rb, line 67
def won?
  length = 0
  @word.split('').each {|val| length += 1 if @history.include?(val)}
  if length == word.size then true
  else false
  end
end