-
Notifications
You must be signed in to change notification settings - Fork 0
/
ooprps.rb
162 lines (140 loc) · 3.38 KB
/
ooprps.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
class Move
VALUES = ['rock', 'paper', 'scissors']
def initialize(value)
@value = value
end
def scissors?
@value == 'scissors'
end
def rock?
@value == 'rock'
end
def paper?
@value == 'paper'
end
def >(other_move)
if rock?
return true if other_move.scissors?
return false
elsif paper?
return true if other_move.rock?
return false
elsif scissors?
return true if other_move.paper?
return false
end
end
def <(other_move)
if rock?
return true if other_move.paper?
return false
elsif paper?
return true if other_move.scissors?
return false
elsif scissors?
return true if other_move.rock?
return false
end
end
def to_s
@value
end
end
class Player
attr_accessor :move, :name
def initialize
@move = nil
set_name
end
end
class Human < Player
def set_name
name = ""
loop do
puts "What's your name?"
name = gets.chomp
break unless name.empty?
puts "Sorry, must enter a name!"
end
self.name = name
end
def choose
choice = nil
loop do
puts "Please choose rock, paper, or scissors."
choice = gets.chomp
break if Move::VALUES.include? choice
puts "Sorry, please make a valid choice."
end
self.move = Move.new(choice)
end
end
class Computer < Player
def set_name
self.name = ['R2D2', 'Hal', 'Chappie', 'Sonny', 'Number 5'].sample
end
def choose
self.move = Move.new(Move::VALUES.sample)
end
end
class RPSGame
attr_accessor :human, :computer
def initialize
@human = Human.new
@computer = Computer.new
end
def display_welcome_message
puts "Welcome to Rock, Paper, Scissors!"
end
def display_goodbye_message
puts "Thanks for playing Rock, Paper, Scissors!"
end
def display_winner
puts "#{human.name} chose #{human.move}."
puts "#{computer.name} chose #{computer.move}."
if human.move > computer.move
puts "#{human.name} won!"
elsif human.move < computer.move
puts "#{computer.name} won!"
else
puts "It's a tie!"
end
end
# case human.move
# when 'rock'
# puts "It's a tie!" if computer.move == 'rock'
# puts "#{human.name} won!" if computer.move == 'scissors'
# puts "#{computer.name} won!" if computer.move == 'paper'
# when 'paper'
# puts "It's a tie!" if computer.move == 'paper'
# puts "#{human.name} won!" if computer.move == 'rock'
# puts "#{computer.name} won!" if computer.move == 'scissors'
# when 'scissors'
# puts "It's a tie!" if computer.move == 'scissors'
# puts "#{human.name} won!" if computer.move == 'paper'
# puts "#{computer.name} won!" if computer.move == 'rock'
# end
# end
def play_again?
answer = nil
loop do
puts "Would you like to play again? (y/n)"
answer = gets.chomp
break if ['y', 'n'].include? answer.downcase
puts "Sorry, must be y or n."
end
return true if answer == 'y'
return false
end
def play
display_welcome_message
loop do
human.choose
computer.choose
display_winner
break unless play_again?
end
display_goodbye_message
end
end
RPSGame.new.play