-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputer.rb
43 lines (35 loc) · 1.02 KB
/
computer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Computer
attr_reader :secret_word, :correct_guesses, :incorrect_guesses
def initialize
@secret_word = nil
@correct_guesses = []
@incorrect_guesses = []
end
def gen_secret_word
dictionary = File.open('./5desk.txt', 'r')
# read contents, newline delimited
@secret_word = dictionary.readlines
.map(&:chomp)
.select { |word| word.length >= 5 && word.length <= 12 }
.sample
.downcase
dictionary.close
init_correct_guesses
end
def letter_exists?(letter)
@secret_word.include?(letter)
end
def update_correct(letter)
# replace '_' with letter for all matches/occurences
@secret_word.split('').each_with_index do |s, i|
@correct_guesses[i] = s if s == letter
end
end
def update_incorrect(letter)
@incorrect_guesses << letter
end
private
def init_correct_guesses
@secret_word.length.to_i.times { @correct_guesses << '_' }
end
end