-
Notifications
You must be signed in to change notification settings - Fork 0
/
mastermind.rb
74 lines (68 loc) · 1.41 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
class MasterMind
def initialize
puts "A new game created!"
puts "Guess the code which"
puts "includes numbers from 1 to 6"
puts "in 12 turns, OR LOSE!"
puts "Use play to start the game."
end
private
def set
@code = []
4.times {@code << (1+Random.rand(6)).to_s}
@code = @code.join
@input = 0
@turn = 0
end
public
def play
set
while ((@input != @code) && (@turn < 12))
codeCheck
displayStatus
end
if @input == @code
"You won in #{@turn} turn(s)!"
elsif @turn == 12
"You LOSE, the code is #{@code}."
end
end
def codeCheck
puts "Enter the code: "
@input = gets.chomp
@input.split("").each do |num|
unless ((num.to_i < 7) && (num.to_i > 0))
puts "Make sure each number you entered is between 1 and 6."
codeCheck
return
end
end
@turn += 1
end
def displayStatus
@reply = []
input = @input.split("")
code = @code.split("")
for i in 0..3
if (input[i] == code[i]) && (@reply.length < 4)
@reply << "W"
input[i] = "0"
code[i] = "7"
end
end
input.each_with_index do |num1,i1|
code.each_with_index do |num2,i2|
if num1 == num2
if i1 != i2
if @reply.length < 4
@reply << "B"
input[i1] = "0"
code[i2] = "7"
end
end
end
end
end
puts "#{@input} #{@reply.join} turns left:#{12 - @turn}"
end
end