-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrps_game.rb
41 lines (39 loc) · 1.34 KB
/
rps_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
def rps_game(players, turn = 0)
rockPlayers = []; scissorsPlayers = []; paperPlayers = [];
players.size.times do |i|
hand = players[i].shift
if hand == :rock
rockPlayers.push i
elsif hand == :scissors
scissorsPlayers.push i
elsif hand == :paper
paperPlayers.push i
end
end
if (rockPlayers.size == 0 && scissorsPlayers.size == 0 && paperPlayers.size == 0) #draw end
return [-1, -1]
elsif (rockPlayers.size == 1 && paperPlayers.size == 0) #rock wins scissors
return [turn,rockPlayers[0]]
elsif (scissorsPlayers.size == 1 && rockPlayers.size == 0) #scissors wins paper
return [turn,scissorsPlayers[0]]
elsif (paperPlayers.size == 1 && scissorsPlayers.size == 0) #paper win rock
return [turn,paperPlayers[0]]
elsif (paperPlayers.size == 0) #rock wins scissors, next game
scissorsPlayers.each do |i|
players[i] = []
end
return rps_game(players, turn = turn + 1)
elsif (rockPlayers.size == 0) #scissors wins paper, next game
paperPlayers.each do |i|
players[i] = []
end
return rps_game(players, turn = turn + 1)
elsif (scissorsPlayers.size == 0) #paper win rock, next game
rockPlayers.each do |i|
players[i] = []
end
return rps_game(players, turn = turn + 1)
else
return rps_game(players, turn = turn + 1) #draw, next game
end
end