-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.rb
62 lines (49 loc) · 1.01 KB
/
interface.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# This class gets valid input from the user
class Interface
MODES = {
"b" => :fairplay,
"c" => :cheat,
"g" => :rules
}
CHOICES = {
"r" => :rock,
"p" => :paper,
"s" => :scissors,
"q" => :quit,
"x" => :result,
}
VALID_CHOICES = CHOICES.keys
VALID_MODES = MODES.keys
def get_game_mode
MODES[get_valid_mode]
end
def get_user_choice
CHOICES[get_valid_choice]
end
def get_computer_choice
[:rock, :paper, :scissors].sample
end
private
def get_valid_mode
Message.welcome_msg
validate_mode(gets.chomp) || wrong_mode_input
end
def get_valid_choice
Message.get_choice_msg
validate(gets&.chomp) || wrong_input
end
def validate_mode(mode)
VALID_MODES.detect { |valid_modes| valid_modes == mode }
end
def validate(choice)
VALID_CHOICES.detect { |valid_choices| valid_choices == choice }
end
def wrong_mode_input
Message.invalid_msg
get_valid_mode
end
def wrong_input
Message.invalid_msg
get_valid_choice
end
end