-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.rb
executable file
·50 lines (39 loc) · 1.02 KB
/
player.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
class Player
attr_reader :name, :symbol, :victories
@@players = []
@@players_cycle = nil
@@default_symbols = nil
def initialize name
raise ArgumentError, 'A player with that name already exists' unless self.class.find_by(:name, name).nil?
@@default_symbols ||= [:X, :O].cycle
@@players.push self
@@players_cycle = @@players.cycle
raise 'A game can have only two players' if @@players.count > 2
@name = name
@symbol = @@default_symbols.next
@victories = 0
end
def victory!
@victories += 1
end
def to_s
"#{@name} (#{@symbol})"
end
def self.clear_instances
@@players = []
@@players_cycle = nil
@@default_symbols = nil
end
def self.find_all
@@players
end
def self.find_next
raise 'No players to cycle' if @@players_cycle.nil?
@@players_cycle.next
end
def self.find_by symbol, param
found = nil
@@players.each { |player| found = player if player.send(symbol) == param }
found
end
end