-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPS.rb
68 lines (60 loc) · 1.6 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
class RPS
attr_reader :player1, :player2, :player1win, :player2win, :total
def initialize (player1, player2)
@player1 = player1
@player2 = player2
@total = 0
end
def play (move1, move2)
move1 = move1.downcase
move2 = move2.downcase
raise :error if move1 != "rock" || move1 != "paper" || move1 != "scissors"
raise :error if move2 != "rock" || move2 != "paper" || move2 != "scissors"
if move1 == 'rock' && move2 == 'paper'
return :player2
elsif move1 == 'rock' && move2 == 'scissors'
return :player1
elsif move1 == 'paper' && move2 == 'rock'
return :player1
elsif move1 == 'paper' && move2 == 'scissors'
return :player2
elsif move1 == 'scissors' && move2 == 'rock'
return :player2
elsif move1 == 'scissors' && move2 == 'paper'
return :player1
else
return :tie
end
# @total += 1
end
end
require 'io/console'
class RPSPlayer
def self.start
p1_wins = 0
p2_wins = 0
puts "Player 1 Name: "
player1 = gets.chomp
puts "Player 2 Name: "
player2 = gets.chomp
rps_game = RPS.new(player1, player2)
while (true)
puts "Player 1 Move: "
move1 = STDIN.noecho(&:gets).chomp
puts "Player 2 Move: "
move2 = STDIN.noecho(&:gets).chomp
rps_game.play(move1, move2)
winner = rps_game.play(move1, move2)
if winner == :player1
puts "#{player1} wins!"
p1_wins += 1
elsif winner == :player2
puts "#{player2} wins!"
p2_wins += 1
else
puts "tie"
end
break if p1_wins == 2 || p2_wins == 2
end
end
end