-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmastermind.rb
More file actions
165 lines (139 loc) · 3.82 KB
/
mastermind.rb
File metadata and controls
165 lines (139 loc) · 3.82 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require_relative 'response'
class Mastermind
attr_reader :colors, :guess, :correct_colors, :colors_positions, :guess_counter, :mode
def initialize
@guess_counter = 0
end
def guess_count
@guess_counter += 1
end
def time
Time.now
end
#sets the difficulty setting
def difficulty(input)
difficulty_level = input.upcase
case difficulty_level
when "B"
@mode = [4, 4]
puts "Beginner Difficulty"
when "I"
@mode = [6, 5]
puts "Intermediate Difficulty"
when "H"
@mode = [8, 6]
puts "Hard Difficulty"
else
puts "Please input (B)eginner, (I)ntermediate, or (H)ard."
end
end
#creates an array of random colors based on difficulty setting
def color_gen
@colors = []
number_of_chars = @mode[0]
number_of_colors = @mode[1]
case number_of_colors
when 4
color_options = ["G", "R", "Y", "B"]
when 5
color_options = ["G", "R", "Y", "B", "W"]
when 6
color_options = ["G", "R", "Y", "B", "W", "P"]
end
number_of_chars.times do
@colors << color_options.sample
end
@colors
#@colors = ["G", "Y", "B", "B"] #Test Array
end
#creates an array out of GUESS input
def guess_gen(input)
@guess = []
input.each_char do |char|
@guess << char.upcase
end
@guess
end
#counts the number of color & position matches
def compare_positions
@correct_positions = 0
answer = @colors
match = nil
@guess.each_with_index do |n, index|
if answer[index] == @guess[index]
@correct_positions += 1
end
end
@correct_positions
end
#counts the number of just color matches
def compare_colors
@correct_colors = 0
answer = @colors.dup
@guess.each do |g|
if answer.include? g
match = answer.find_index(g)
answer[match] = nil
@correct_colors += 1
end
end
@correct_colors
end
#in-game response message
def compare
number_of_chars = @mode[0]
player_input = @guess
if player_input[0] == ("Q")
Response.new(:message => "Exiting...", :status => :quit)
elsif player_input[0] == ("C")
Response.new(:message => "#{@colors}. If ya ain't cheatin\', ya ain't tryin\'.", :status => :continue)
elsif player_input.size > number_of_chars
Response.new(:message => "That guess is too long. Please guess #{number_of_chars} colors.")
elsif player_input.size != number_of_chars
Response.new(:message => "That guess is too short. Please guess #{number_of_chars} colors.")
elsif @correct_positions == number_of_chars
Response.new(:message => "You Win! It only took you #{@guess_counter} guesses!", :status => :won)
else
Response.new(:message => "\'#{player_input}\' had #{@correct_positions} correct positions with #{@correct_colors} correct colors! Guess again!", :status => :continue)
end
end
#menu messages
def print_play
printer = Response.new(:message => nil, :status => :continue)
number_of_elements = @mode[0]
case number_of_elements
when 4
printer.beginner
when 6
printer.intermediate
when 8
printer.hard
end
end
def play_again
printer = Response.new(:message => nil, :status => :continue)
printer.again
end
def print_menu
printer = Response.new(:message => nil, :status => :continue)
printer.menu
end
def print_quit
printer = Response.new(:message => nil, :status => :continue)
printer.quit
end
def print_difficulty
printer = Response.new(:message => nil, :status => :continue)
printer.difficult
end
def print_instruct
string = nil
printer = Response.new(:message => nil, :status => :continue)
printer.instructions
end
def print_exception
string = nil
printer = Response.new(:message => nil, :status => :continue)
printer.exception
end
end