-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathgame.rb
More file actions
76 lines (65 loc) · 1.48 KB
/
Copy pathgame.rb
File metadata and controls
76 lines (65 loc) · 1.48 KB
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
class Game
attr_reader :players, :turn, :round
def initialize(player_1, player_2, round = Round.new)
@players = [player_1, player_2]
@turn = players[0]
@round = round
end
def player_1
players.first
end
def player_2
players.last
end
def new_round
if round.outcome_decided?
@round = Round.new
players.each { |player| player.reset_action }
end
end
def switch_turn
if turn == player_1
@turn = players[1]
elsif turn == player_2
@turn = players[0]
end
end
def act_for_computer
if turn.computer?
turn.random_throw
switch_turn
end
end
def calculate_outcome
if player_1.thrown_action? && player_2.thrown_action?
rps_logic
end
end
private
def rps_logic
win_condition = { scissors: :paper, paper: :rock, rock: :scissors }
if player_1.action == player_2.action
round.set_outcome("draws with")
elsif win_condition[player_1.action] == player_2.action
round.set_winner(player_1)
round.set_looser(player_2)
round.winner.increase_score
calculate_outcome_message
else
round.set_winner(player_2)
round.set_looser(player_1)
round.winner.increase_score
calculate_outcome_message
end
end
def calculate_outcome_message
case round.winner.action
when :rock
round.set_outcome('smashes')
when :paper
round.set_outcome('wraps')
when :scissors
round.set_outcome('cuts')
end
end
end