Class: RubyTictactoe::PlayerFactory

Inherits:
Object
  • Object
show all
Includes:
TictactoeConstants
Defined in:
lib/player_factory.rb

Constant Summary

Constants included from TictactoeConstants

TictactoeConstants::AI_PLAYER, TictactoeConstants::COMPUTER_PLAYER, TictactoeConstants::EASY_LEVEL, TictactoeConstants::HARD_LEVEL, TictactoeConstants::HUMAN_PLAYER, TictactoeConstants::MARKER_O, TictactoeConstants::MARKER_X

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_one, type_two) ⇒ PlayerFactory

Returns a new instance of PlayerFactory.



8
9
10
11
12
# File 'lib/player_factory.rb', line 8

def initialize(type_one, type_two)
  @player_one = create_player(type_one, MARKER_X)
  @player_two = create_player(type_two, MARKER_O)
  set_opponents
end

Instance Attribute Details

#player_oneObject

Returns the value of attribute player_one.



7
8
9
# File 'lib/player_factory.rb', line 7

def player_one
  @player_one
end

#player_twoObject

Returns the value of attribute player_two.



7
8
9
# File 'lib/player_factory.rb', line 7

def player_two
  @player_two
end

Instance Method Details

#create_player(type, marker) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/player_factory.rb', line 14

def create_player(type, marker)
  case type
  when COMPUTER_PLAYER
    ComputerPlayer.new(marker)
  when HUMAN_PLAYER
    HumanPlayer.new(marker)
  when AI_PLAYER
    AIPlayer.new(marker)
  else
    raise "#{type} is an invalid player type"
  end
end

#player_goes_firstObject



27
28
29
# File 'lib/player_factory.rb', line 27

def player_goes_first
  rand(0..1) == 1 ? player_one : player_two
end

#set_opponentsObject



31
32
33
34
# File 'lib/player_factory.rb', line 31

def set_opponents
  player_one.opponent = player_two
  player_two.opponent = player_one
end