-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
39 lines (34 loc) · 853 Bytes
/
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
# frozen_string_literal: true
class Game
attr_reader :player1, :player2, :player1_score, :player2_score
def initialize(player1, player2)
@player1 = player1
@player2 = player2
@player1_score = 0
@player2_score = 0
end
def play
1000.times do
play_round
end
end
def play_round
player1_choice = Player.players[@player1].call(history.dup)
player2_choice = Player.players[@player2].call(history.dup)
history << { player1 => player1_choice, player2 => player2_choice }
case [player1_choice, player2_choice]
when [:share, :share]
@player1_score += 2
@player2_score += 2
when [:share, :steal]
@player2_score += 3
when [:steal, :share]
@player1_score += 3
when [:steal, :steal]
# nothing happens
end
end
def history
@history ||= []
end
end