Class: Mathangman::Game

Inherits:
Object
  • Object
show all
Includes:
Difficulty, Filer, Utility
Defined in:
lib/mathangman/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Filer

#archives, #check_repeated, #create_dir, #del_dir, #folder_not_exist?, #get_folder_items, #get_time, #load_file, #load_game, #restore_state, #save_game, #save_state

Methods included from Difficulty

#check_difficulty, #diff_level

Methods included from Utility

#check_source, #is_alpha?, #quit_reply, #quitter, #save_on_quit

Constructor Details

#initialize(disp = nil) ⇒ Game

Returns a new instance of Game.



16
17
18
19
20
# File 'lib/mathangman/game.rb', line 16

def initialize(disp = nil)
  @guess_bonus = 2
  @wrongs_num = 0
  @display = disp unless disp.nil?
end

Instance Attribute Details

#disp_wordObject

Returns the value of attribute disp_word.



14
15
16
# File 'lib/mathangman/game.rb', line 14

def disp_word
  @disp_word
end

#displayObject (readonly)

Returns the value of attribute display.



13
14
15
# File 'lib/mathangman/game.rb', line 13

def display
  @display
end

#filesObject

Returns the value of attribute files.



14
15
16
# File 'lib/mathangman/game.rb', line 14

def files
  @files
end

#folderObject

Returns the value of attribute folder.



14
15
16
# File 'lib/mathangman/game.rb', line 14

def folder
  @folder
end

#guessObject

Returns the value of attribute guess.



14
15
16
# File 'lib/mathangman/game.rb', line 14

def guess
  @guess
end

#lenObject

Returns the value of attribute len.



14
15
16
# File 'lib/mathangman/game.rb', line 14

def len
  @len
end

#nameObject

Returns the value of attribute name.



14
15
16
# File 'lib/mathangman/game.rb', line 14

def name
  @name
end

#restartObject

Returns the value of attribute restart.



14
15
16
# File 'lib/mathangman/game.rb', line 14

def restart
  @restart
end

#secret_wordObject

Returns the value of attribute secret_word.



14
15
16
# File 'lib/mathangman/game.rb', line 14

def secret_word
  @secret_word
end

#wordObject

Returns the value of attribute word.



14
15
16
# File 'lib/mathangman/game.rb', line 14

def word
  @word
end

#wrongs_numObject

Returns the value of attribute wrongs_num.



14
15
16
# File 'lib/mathangman/game.rb', line 14

def wrongs_num
  @wrongs_num
end

Instance Method Details

#act_on_guessObject



62
63
64
65
66
67
68
69
70
# 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_infoObject



126
127
128
129
130
131
# 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_nameObject



119
120
121
122
123
124
# 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_validityObject



141
142
143
# File 'lib/mathangman/game.rb', line 141

def check_validity
  true if is_alpha? @name
end

#completedObject



72
73
74
75
76
77
78
79
80
81
# 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) ⇒ Object



33
34
35
36
37
# 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) ⇒ Object



43
44
45
46
47
48
49
50
# 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_userObject



39
40
41
# File 'lib/mathangman/game.rb', line 39

def input_from_user
  @guess = gets.chomp.downcase
end

#is_correct?Boolean

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/mathangman/game.rb', line 83

def is_correct?
  @word_arr = @secret_word.split('')
  @word_arr.include? @guess
end

#player_choice(choice) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# 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

#processObject



52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# 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_disp_menuObject



145
146
147
148
149
150
151
# File 'lib/mathangman/game.rb', line 145

def show_disp_menu
  puts @display.greeting
  choice = gets.chomp
  player_choice(choice)
  puts @display.difficulty
  check_difficulty
end

#show_letterObject



88
89
90
91
92
93
94
95
96
97
# 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_actionsObject



133
134
135
136
137
138
139
# File 'lib/mathangman/game.rb', line 133

def supported_actions
  {
    "1" => 'call_name',
    "2" => 'load_game',
    "3" => 'call_info'
  }
end

#wrong_guessObject



99
100
101
102
103
104
105
106
# 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