Class: H4ngman::Hangman

Inherits:
Object
  • Object
show all
Defined in:
lib/teacher/hangman.rb

Class Method Summary collapse

Class Method Details

.choose_word(list_) ⇒ Object



66
67
68
69
# File 'lib/teacher/hangman.rb', line 66

def self.choose_word(list_)
  @keyword = list_.sample
  puts "Your word has been selected!"
end

.displayObject



88
89
90
91
92
93
94
# File 'lib/teacher/hangman.rb', line 88

def self.display
  sprite_dict = {5 => @hangman_sprite5, 4 => @hangman_sprite4, 3 => @hangman_sprite3, 2 =>@hangman_sprite2, 1 =>@hangman_sprite1, 0 => @hangman_sprite0}
  self.handle_mystery_text
  puts @mystery_text
  puts sprite_dict[@remaining_credits]
  puts "You have #{@remaining_credits} tries left." 
end

.finishObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/teacher/hangman.rb', line 55

def self.finish
  if @remaining_credits == 0
    puts "GAME OVER, YOU LOSE :("
  elsif 
    puts "YOU WON! CONGRATS"
  end
  
  puts "the word was #{Morse.text_to_morse(@keyword)}"

end

.generate_mystery_textObject



71
72
73
# File 'lib/teacher/hangman.rb', line 71

def self.generate_mystery_text
  @mystery_text = ("?"*@keyword.length).split(//)
end

.handle_mystery_textObject



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/teacher/hangman.rb', line 75

def self.handle_mystery_text
  begin
    @mystery_text = @mystery_text.split(//)
  rescue
  end
  matching_indexes = @keyword.split(//).each_index.select { |i| @keyword[i] == @user_answer }
  for index in matching_indexes
    @mystery_text.delete_at index
    @mystery_text.insert(index, @user_answer)
  end
  @mystery_text = @mystery_text.join()
end

.mainObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/teacher/hangman.rb', line 96

def self.main
  self.generate_mystery_text
  @correct_guesses = []
  @remaining_credits = 6
  while @remaining_credits != 0
    print "Try to guess the word or a character that the word consists! (in morse) --> "
    @user_answer = Morse.morse_to_text(gets.chomp.strip)
    system('clear')
    system('cls')
    
    if @keyword.include?(@user_answer)
      puts "#{Morse.text_to_morse(@user_answer)} is an accurate guess!"
      if @user_answer == @keyword
        break
      else
        @correct_guesses.append(@user_answer)
      end

    elsif not @keyword.include?(@user_answer) or @user_answer == ''
      puts "#{Morse.text_to_morse(@user_answer)} isn't an accurate guess."
      @remaining_credits = @remaining_credits - 1
    end
    
    self.display
  
  end
  
  self.finish

end