Class: TicTacToe::PlayerList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tic_tac_toe/player.rb

Constant Summary collapse

AVAILABLE_PLAYERS =
[
  Player.new("x", :red),
  Player.new("", :blue),
  Player.new("", :yellow),
  Player.new("", :green),
  Player.new("", :magenta),
]

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ PlayerList

Returns a new instance of PlayerList.



15
16
17
18
# File 'lib/tic_tac_toe/player.rb', line 15

def initialize(number)
  @players     = AVAILABLE_PLAYERS.first(number)
  @current_idx = 0
end

Instance Method Details

#colorsObject



32
33
34
35
36
# File 'lib/tic_tac_toe/player.rb', line 32

def colors
  @players.inject({}) do |colors, player|
    colors.update(player.symbol => player.color)
  end
end

#currentObject



24
25
26
# File 'lib/tic_tac_toe/player.rb', line 24

def current
  @players[@current_idx]
end

#each(&block) ⇒ Object



20
21
22
# File 'lib/tic_tac_toe/player.rb', line 20

def each(&block)
  @players.each(&block)
end

#next!Object



28
29
30
# File 'lib/tic_tac_toe/player.rb', line 28

def next!
  @current_idx = (@current_idx + 1) % @players.count
end