-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
98 lines (86 loc) · 1.81 KB
/
game.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
require_relative 'door.rb'
class Game
attr_reader :end_choice
attr_reader :end_door
attr_reader :doors
attr_reader :choice
def initialize(doors)
if doors.is_a? Integer
build_doors_int(doors)
elsif doors.is_a? Array
build_doors_array(doors)
else
raise ArgumentError("Please provide a number or array for doors!")
end
end
def choose(number)
@choice = number
end
def reveal_goat
revealed_total = 0
@doors.each_with_index do |door, i|
break unless revealed_total < (@doors.length - 2)
if ((i.eql? @choice) || (door.entity.eql? "car"))
next
else
door.revealed = true
revealed_total += 1
end
end
end
#stay or switch
def decide(decision)
@end_choice = nil
if decision.eql?("stay")
@end_choice = @choice
elsif decision.eql?("switch")
@doors.each_with_index do |door, i|
if ((i == @choice) || (door.revealed))
next
else
@end_choice = i
break
end
puts i
end
else
raise ArgumentError("Stay or switch please")
end
@end_door = @doors[@end_choice]
end
def build_doors_int(number)
@doors = Array.new
@car = rand(number)
number.times { |num|
if num.eql? @car
@doors[num] = Door.new("car")
else
@doors[num] = Door.new("goat")
end
}
end
def build_doors_array(array)
@doors = Array.new
array.each { |door|
@doors.push Door.new(door)
}
end
def car_count
count = 0
@doors.each { |door|
if door.entity.eql? "car"
count += 1
end
}
count
end
def goat_count
count = 0
@doors.each { |door|
if door.entity.eql? "goat"
count += 1
end
}
count
end
end