-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrps.rb
157 lines (123 loc) · 2.85 KB
/
rps.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
class RPSGame
attr_accessor :human, :computer
def initialize
@human = Human.new
@computer = Computer.new
@@round = 1
system("clear")
end
def play
display_welcome_message
loop do
puts "ROUND: #{@@round}"
puts "SCORES: #{human.name} => #{human.score} | #{computer.name} => #{computer.score}"
human.choose
computer.choose
puts "#{human.name} Chose: #{human.move}"
puts "Computer Chose: #{computer.move}"
update_scores
display_winner
break unless play_again?
@@round += 1
system("clear")
end
display_goodbye_message
end
def display_welcome_message
puts "Welcome to RPS!!"
end
def update_scores
return nil if human.move == computer.move
human.move > computer.move ? human.score += 1 : computer.score += 1
end
def display_winner
if human.move == computer.move
puts "No one wins. Tie game."
elsif human.move > computer.move
puts "#{human.name} wins!"
elsif human.move < computer.move
puts "#{computer.name} wins!"
end
end
def display_goodbye_message
puts "That was a good game. Goodbye now."
end
def play_again?
puts "Would you like to play again? (Type 'y' for 'yes' and 'n' for 'no')"
answer = gets.chomp
loop do
break if answer.match(/(n|y)/i)
puts "Please enter a valid answer of: ['y', 'n']"
answer = gets.chomp
end
case answer
when "y" then true
else false
end
end
end
class Player
attr_accessor :score, :name, :move
def initialize
@score = 0
@move = nil
end
end
class Human < Player
def initialize
puts "Enter a name:"
@name = gets.chomp
super
end
def choose
puts "Choose from => ['rock', 'paper', 'scissors']"
choice = gets.chomp.downcase
until ['rock', 'paper', 'scissors'].include?(choice)
puts "Please enter a valid option of ['rock', 'paper', 'scissors']"
choice = gets.chomp.downcase
end
self.move = Move.new(choice)
end
end
class Computer < Player
def initialize
@name = "Computer Player"
super
end
def choose
choice = ['rock', 'paper', 'scissors'].sample
self.move = Move.new(choice)
end
end
class Move
attr_reader :move_type
def initialize(choice)
@move_type = choice
end
def to_s
move_type
end
def scissors?
move_type == 'scissors'
end
def rock?
move_type == 'rock'
end
def paper?
move_type == 'paper'
end
def >(other_move)
return other_move.scissors? if rock?
return other_move.rock? if paper?
return other_move.paper? if scissors?
end
def <(other_move)
return other_move.paper? if rock?
return other_move.scissors? if paper?
return other_move.rock? if scissors?
end
def ==(other_move)
move_type == other_move.move_type
end
end
p RPSGame.new.instance_variables