-
Notifications
You must be signed in to change notification settings - Fork 0
/
mastermind.rb
294 lines (259 loc) · 6.89 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
287
288
289
290
291
292
293
# console version of mastermind game
# for more information check the wikipedia
class String
def black; "\e[40m#{self}\e[0m" end
def red; "\e[41m#{self}\e[0m" end
def green; "\e[42m#{self}\e[0m" end
def brown; "\e[43m#{self}\e[0m" end
def blue; "\e[44m#{self}\e[0m" end
def magenta; "\e[45m#{self}\e[0m" end
def cyan; "\e[46m#{self}\e[0m" end
def gray; "\e[47m#{self}\e[0m" end
def ozblack; "\e[30m#{self}\e[0m" end
def ozred; "\e[31m#{self}\e[0m" end
end
module Colors
@@color_palette = {
'1' =>'black',
'2' => 'red',
'3' => 'green',
'4' => 'brown',
'5' => 'blue',
'6' => 'magenta',
'7' => 'cyan',
'8' => 'gray'
}
@@hash_to_store_loop = {
'1' => 0,
'2' => 0,
'3' => 0,
'4' => 0,
'5' => 0,
'6' => 0
}
end
class Maker
include Colors
@@num = 0
@@chosen_colors = [] # bu sonra yoxlama ucun lazim olasan
@@random_colors = []
def self.random_color_chose
while @@random_colors.length < 4
random_color = rand(1..6).to_s
@@random_colors.push(random_color)
end
end
def self.ask_for_num
puts 'Please enter 4 numbers from 1 to 6 inclusive.'
@@num = gets.chomp
ask_for_num if @@num.length < 4 || @@num.to_s.length > 4
@@chosen_colors = @@num.split("")
end
def self.random_color_getter
@@random_colors
end
def self.print_color(arr)
arr.each do |n|
case n
when '1'
print n.black
when '2'
print n.red
when '3'
print n.green
when '4'
print n.brown
when '5'
print n.blue
when '6'
print n.magenta
when '7'
print n.cyan
when '8'
print n.gray
end
end
puts " "
end
end
class CompBreaker < Maker
include Colors
@@random_comp_attempts = []
@@colors_that_have_been_checked = []
def self.random_comp_color_chose
random_color = (rand(1..6).to_s * (4 - @@random_comp_attempts.length)).split("")
unless @@random_comp_attempts.include?(random_color[0]) || @@colors_that_have_been_checked.include?(random_color[0])
@@random_comp_attempts += random_color
@@colors_that_have_been_checked += random_color
else
self.random_comp_color_chose
end
end
def self.equality
@@random_comp_attempts == @@chosen_colors
end
@@turn_counter = 1
@@x = 0
@@temporary_holder = []
def self.check
puts "Turn #{@@turn_counter}"
@@turn_counter += 1
self.print_color(@@random_comp_attempts)
if self.equality
@@random_comp_attempts = []
@@temporary_holder = []
@@for_test_counter = 0
@@colors_that_have_been_checked = []
@@x = 0
@@turn_counter = 1
"Computer wins!"
else
if @@chosen_colors.include?(@@random_comp_attempts[@@x])
@@temporary_holder += @@chosen_colors.select{|num| num == @@random_comp_attempts[@@x]} #.count() - la evez et bunu
@@x += @@chosen_colors.select{|num| num == @@random_comp_attempts[@@x]}.length
end
@@random_comp_attempts = @@temporary_holder
if @@random_comp_attempts.length < 4
self.random_comp_color_chose
self.check
else
for x in 0..3
for y in x + 1..3
if (@@random_comp_attempts[x] != @@chosen_colors[x] && @@random_comp_attempts[y] != @@chosen_colors[y]) && x != 3
@@random_comp_attempts[x], @@random_comp_attempts[y] = @@random_comp_attempts[y], @@random_comp_attempts[x]
break if equality
elsif x == 3 && @@random_comp_attempts[x] != @@chosen_colors[x] && @@random_comp_attempts[0] != @@chosen_colors[0]
@@random_comp_attempts[x], @@random_comp_attempts[0] = @@random_comp_attempts[0], @@random_comp_attempts[x]
break if self.equality == true
end
end
end
self.check
end
end
end
end
class HumanBreaker < Maker
include Colors
@@num = 0
@@find_attempts = []
def initialize(name)
@name = name
end
def self.ask_for_num
puts 'Please enter 4 numbers from 1 to 6 inclusive.'
@@num = gets.chomp
if (@@num.to_s.length < 4 || @@num.to_s.length > 4)
ask_for_num
end
@@find_attempts = @@num.to_s.split("")
@@qurbanliq_quzu = @@num.to_s.split("")
end
def self.choose
self.ask_for_num
@@find_attempts.each do |n|
case n
when '1'
print n.black
when '2'
print n.red
when '3'
print n.green
when '4'
print n.brown
when '5'
print n.blue
when '6'
print n.magenta
when '7'
print n.cyan
when '8'
print n.gray
end
end
end
def self.random_colors_getter
@@random_colors
end
def self.find_attempts_getter
@@find_attempts
end
def self.check
if @@find_attempts == @@random_colors
@@random_colors = []
"You Win!"
else
@@find_attempts.each_with_index do |n, indx|
if @@random_colors.include?(n) && @@random_colors[indx] == n
print '◯'.ozred
@@hash_to_store_loop[n] += 1
@@qurbanliq_quzu.delete_at(indx)
end
end
@@qurbanliq_quzu.each_with_index do |n ,indx|
if @@random_colors.include?(n) && @@hash_to_store_loop[n] < @@random_colors.count(n)
print '◯'.ozblack
@@hash_to_store_loop[n] += 1
end
end
@@hash_to_store_loop.each {|k, v| @@hash_to_store_loop[k] = 0}
puts " "
"Try Again"
end
end
end
def question_to_user
puts "Would you like to play again? Enter 'y' for yes or 'n' for no:"
an_answer = gets.chomp.downcase
if an_answer == 'y'
game
elsif an_answer == 'n'
puts "Maybe another time. Bye!"
else
puts "Please enter either 'y' or 'n'."
question_to_user
end
end
def game
puts "Press and enter 'm' for Maker and 'b' for Breaker:"
user_choice = gets.chomp.downcase
if user_choice == 'b'
turn = 1
while turn <= 12 do
puts "#Turn #{turn}"
Maker.random_color_chose
HumanBreaker.choose
puts " "
turn += 1
if HumanBreaker.check == "You Win!"
puts "You Win!"
break
elsif turn == 12
puts 'Well, your twelve tries are over, sadly...'
puts "The correct answer was..."
Maker.print_color (Maker.random_color_getter)
break
end
end
elsif user_choice == 'm'
Maker.ask_for_num
CompBreaker.random_comp_color_chose
p CompBreaker.check
else
puts "Please stop being stupid and enter either 'm' or 'b' for gods sake!"
game
end
question_to_user
end
puts "Okay, the rules of the game are simple, but I ain't gonna explain them."
puts "Just google it and you'll find the information necessary."
puts "Anyways, in my version of the game you can choose to be breaker of maker."
puts "If you choose to be a breaker then you'll have to guess the randomly generated code by the computer."
puts "You'll have 12 rounds and each round you'll be given hints such as #{'◯'.red} or #{'◯'.black}."
puts "Red one means one of your guesses is included in code and is in correct position, black one means it is in code,"
puts "But not in correct position."
puts "Oh yes, the colors being used are:"
puts "#{'1'.black}#{'2'.red}#{'3'.green}#{'4'.brown}#{'5'.blue}#{'6'.magenta}"
puts "You'll figure out the rest just start playin already!!"
puts "Would you like to play as a Maker or Breaker?"
game