-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmastermind.rb
286 lines (213 loc) · 5.74 KB
/
mastermind.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
module FormatOutput
def format_output(player)
player.guess.split('').map do |e|
case e
when '1'
e = '1'.bg_blue
when '2'
e = '2'.bg_cyan
when '3'
e = '3'.bg_green
when '4'
e = '4'.bg_magenta
when '5'
e = '5'.bg_red
when '6'
e = '6'.bg_yellow
end
end
end
end
class String
# colorization
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
def red
colorize(31)
end
def green
colorize(32)
end
def yellow
colorize(33)
end
def blue
colorize(34)
end
def pink
colorize(35)
end
def light_blue
colorize(36)
end
def bg_red
colorize(41)
end
def bg_green
colorize(42)
end
def bg_blue
colorize(44)
end
def bg_white
colorize(47)
end
def bg_cyan
colorize(46)
end
def bg_magenta
colorize(45)
end
def bg_yellow
colorize(43)
end
end
class Computer
include FormatOutput
attr_reader :numbers, :correct_position, :wrong_position, :code_remainder, :guess_remainder
attr_accessor :guess, :combinations
def initialize
@numbers = []
@correct_position = []
@wrong_position = []
@guess = []
a = [1, 2, 3, 4, 5, 6]
b = [1, 2, 3, 4, 5, 6]
c = [1, 2, 3, 4, 5, 6]
d = [1, 2, 3, 4, 5, 6]
@combinations = a.product(b, c, d)
@combinations.map! {|nums| nums.join}
end
def generate_code
number = Random.new
(1..4).each do |_i|
@numbers.push(number.rand(1..6))
end
@numbers = @numbers.join
end
def compare(human)
code_split = @numbers.split('')
guess_split = human.guess.split('')
code_split.each_with_index do |code_val, code_index|
guess_split.each_with_index do |guess_val, guess_index|
@correct_position.push(guess_val) if code_index == guess_index && code_val == guess_val
end
end
@code_remainder = code_split.dup
@guess_remainder = guess_split.dup
@correct_position.all? do |n|
idx = @code_remainder.index(n)
return false if idx.nil?
@code_remainder.delete_at(idx)
end
@correct_position.all? do |n|
idx = @guess_remainder.index(n)
return false if idx.nil?
@guess_remainder.delete_at(idx)
end
@code_remainder.each do |code_val|
@guess_remainder.each do |guess_val|
next unless code_val == guess_val
@wrong_position.push(guess_val)
@guess_remainder.delete_at(@guess_remainder.index(guess_val))
break
end
end
end
def guess_color
@guess = @combinations.sample
end
end
class Human
include FormatOutput
attr_reader :guess, :secret_code, :correct_position, :wrong_position, :code_remainder, :guess_remainder
def initialize
@correct_position = []
@wrong_position = []
end
def create_code
@secret_code = gets.chomp
end
def guess_color
@guess = gets.chomp
end
def compare(computer)
code_split = @secret_code.split('')
guess_split = computer.guess.split('')
code_split.each_with_index do |code_val, code_index|
guess_split.each_with_index do |guess_val, guess_index|
@correct_position.push(guess_val) if code_index == guess_index && code_val == guess_val
end
end
@code_remainder = code_split.dup
@guess_remainder = guess_split.dup
@correct_position.all? do |n|
idx = @code_remainder.index(n)
return false if idx.nil?
@code_remainder.delete_at(idx)
end
@correct_position.all? do |n|
idx = @guess_remainder.index(n)
return false if idx.nil?
@guess_remainder.delete_at(idx)
end
@code_remainder.each do |code_val|
@guess_remainder.each do |guess_val|
next unless code_val == guess_val
@wrong_position.push(guess_val)
@guess_remainder.delete_at(@guess_remainder.index(guess_val))
break
end
end
end
end
computer = Computer.new
human = Human.new
puts 'Would like to be the code maker or code breaker?'
puts "Enter '1' to be the code maker \nEnter '2' to be the code breaker"
choice = gets.chomp
if choice == '2'
computer.generate_code
puts "colors: #{'1'.bg_blue} #{'2'.bg_cyan} #{'3'.bg_green} #{'4'.bg_magenta} #{'5'.bg_red} #{'6'.bg_yellow}"
i = 1
until human.guess == computer.numbers
puts 'enter a 4 digit number between 1-6: '
human.guess_color
computer.compare(human)
size_correct = computer.correct_position.size
size_wrong = computer.wrong_position.size
correct_cpy = computer.correct_position.dup
wrong_cpy = computer.wrong_position.dup
correct_cpy.replace(Array.new(size_correct, 'O'.green))
wrong_cpy.replace(Array.new(size_wrong, 'O'.red))
puts "Turn #{i}"
puts "#{human.format_output(human).join(' ')} Clues: #{(correct_cpy + wrong_cpy).join}"
i += 1
computer.correct_position.clear
computer.wrong_position.clear
end
elsif choice == '1'
puts "colors: #{'1'.bg_blue} #{'2'.bg_cyan} #{'3'.bg_green} #{'4'.bg_magenta} #{'5'.bg_red} #{'6'.bg_yellow}"
print 'Enter your 4 digit secret code between these colors(1-6)> '
human.create_code
i = 1
until computer.guess == human.secret_code
computer.guess_color
human.compare(computer)
computer.combinations.delete(computer.guess)
size_correct = human.correct_position.size
size_wrong = human.wrong_position.size
correct_cpy = human.correct_position.dup
wrong_cpy = human.wrong_position.dup
correct_cpy.replace(Array.new(size_correct, 'O'.green))
wrong_cpy.replace(Array.new(size_wrong, 'O'.red))
puts "Turn #{i}"
i += 1
puts "#{computer.format_output(computer).join(' ')} Clues: #{(correct_cpy + wrong_cpy).join}"
human.correct_position.clear
human.wrong_position.clear
end
else
puts 'Invalid input'
end