class PaitinHangman::Levels

Attributes

chances[R]
game_word[R]
max[R]
min[R]
player1[R]
player2[R]

Public Class Methods

new(player, min, max, chances, another_player = nil) click to toggle source
# File lib/paitin_hangman/levels.rb, line 11
def initialize(player, min, max, chances, another_player = nil)
  initialize_specs(player, min, max, chances, another_player)
  puts "\t\tWelcome #{@player2}, In this level, you will have #{@chances}
  chances to guess the word correctly"
  word_generator(min, max) if @player1.nil?
  challenge_word(min, max) if @player1
end

Public Instance Methods

challenge_word(min, max) click to toggle source
# File lib/paitin_hangman/levels.rb, line 36
def challenge_word(min, max)
  puts "Now, enter your challenge_word #{@player1}, it will be hidden"
  @game_word = STDIN.noecho(&:gets).chomp.downcase.delete(" ")
  until @game_word.length >= min && @game_word.length <= max && number?(@game_word)
    puts "Word length not correct! Make it between #{min} & #{max} characters"
    @game_word = STDIN.noecho(&:gets).chomp.downcase.delete(" ")
  end
  puts "The stage is set #{@player2}"
  GameEngine.new.trials(@chances, @game_word, @player2, @player1)
end
initialize_specs(player, min, max, chances, another_player) click to toggle source
# File lib/paitin_hangman/levels.rb, line 19
def initialize_specs(player, min, max, chances, another_player)
  @player2 = player
  @min = min
  @max = max
  @chances = chances
  @player1 = another_player
end
word_generator(min, max) click to toggle source
# File lib/paitin_hangman/levels.rb, line 27
def word_generator(min, max)
  file_name = File.join(File.dirname(File.expand_path(__FILE__)), '../../dictionary.txt')
  words = File.open(file_name, "r").readlines.map!(&:chomp)
  level_words = words.select { |i| i.length >= min && i.length <= max }
  random_index = rand(level_words.length)
  @game_word = level_words[random_index]
  GameEngine.new.trials(@chances, @game_word, @player2, @player1)
end