class Mathangman::Game
Attributes
disp_word[RW]
display[R]
files[RW]
folder[RW]
guess[RW]
len[RW]
name[RW]
restart[RW]
secret_word[RW]
word[RW]
wrongs_num[RW]
Public Class Methods
new(disp = nil)
click to toggle source
# File lib/mathangman/game.rb, line 16 def initialize(disp = nil) @guess_bonus = 2 @wrongs_num = 0 @display = disp unless disp.nil? end
Public Instance Methods
act_on_guess()
click to toggle source
# File lib/mathangman/game.rb, line 62 def act_on_guess if is_correct? print @word = show_letter completed else wrong_guess print @disp_word end end
call_info()
click to toggle source
# File lib/mathangman/game.rb, line 126 def call_info puts @display.info response = gets.chomp.downcase return show_disp_menu if response == "y" || response == "yes" exit end
call_name()
click to toggle source
# File lib/mathangman/game.rb, line 119 def call_name begin puts @display.get_name @name = gets.chomp.downcase end until check_validity end
check_validity()
click to toggle source
# File lib/mathangman/game.rb, line 141 def check_validity true if is_alpha? @name end
completed()
click to toggle source
# File lib/mathangman/game.rb, line 72 def completed if !@word.include? "-" puts @display.complete_disp if @restart File.delete "saved_games/#{@folder}/#{@restart}" del_dir end show_disp_menu end end
first_guess(diff = nil)
click to toggle source
# File lib/mathangman/game.rb, line 33 def first_guess(diff = nil) wrd = rand_word diff puts @disp_word = "-" * @len guesses end
guesses(obj = nil)
click to toggle source
# File lib/mathangman/game.rb, line 43 def guesses(obj = nil) puts @display.msg "Enter an alphabet guess for the #{@len} letter word." input_from_user if @guess == "*" quitter(self) end process end
input_from_user()
click to toggle source
# File lib/mathangman/game.rb, line 39 def input_from_user @guess = gets.chomp.downcase end
is_correct?()
click to toggle source
# File lib/mathangman/game.rb, line 83 def is_correct? @word_arr = @secret_word.split('') @word_arr.include? @guess end
player_choice(choice)
click to toggle source
# File lib/mathangman/game.rb, line 108 def player_choice(choice) if supported_actions.include? choice send(supported_actions[choice]) elsif choice == "*" quitter("force") else puts @display.invalid_entry show_disp_menu end end
process()
click to toggle source
# File lib/mathangman/game.rb, line 52 def process if is_alpha? @guess act_on_guess else puts @display.msg "Invalid input. Only alphabets are allowed." end puts @display.msg "You have #{@wrongs_num} wrong guess(es) left." guesses end
rand_word(diff = nil)
click to toggle source
# File lib/mathangman/game.rb, line 22 def rand_word(diff = nil) dict = check_source "/5desk.txt" unusable = true while unusable @secret_word = dict.sample.chomp.downcase @len = @secret_word.length unusable = diff_level(diff) end @secret_word end
show_letter()
click to toggle source
# File lib/mathangman/game.rb, line 88 def show_letter matches = [] i = 0 @word_arr.each do | char | matches << i if char == @guess i += 1 end matches.each { | item | @disp_word[item] = @guess } @disp_word end
supported_actions()
click to toggle source
# File lib/mathangman/game.rb, line 133 def supported_actions { "1" => 'call_name', "2" => 'load_game', "3" => 'call_info' } end
wrong_guess()
click to toggle source
# File lib/mathangman/game.rb, line 99 def wrong_guess @wrongs_num -= 1 if @wrongs_num == 0 secret_word = @secret_word puts @display.lost secret_word show_disp_menu end end