Class: ConnectNGame::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/connect_n_game/player.rb

Overview

The abstract Player class of the connect_n_game.
Responsibility

  • This mostly abstract class is responsible for specifying the shared

behavior of all the sorts of players, both human and automaton. This is
where moves are generated, victory celebrated, and defeat mourned.

Direct Known Subclasses

BasicMoves, EchoMoves, Human, JustRandom, MiddleMoves, Prudent

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, type) ⇒ Player

Do the common player setup
Parameters

  • name - The name of the player.

  • description - A witty description of the player.

  • type - :carbon or :silicon


Returns

  • An instance of Player



35
36
37
38
# File 'lib/connect_n_game/player.rb', line 35

def initialize(name, description, type)
   fail "Invalid type #{type}" unless [:carbon, :silicon].include?(type)
   @name, @description, @type = name, description, type
end

Class Attribute Details

.playersObject (readonly)

The list of available players.



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

def players
  @players
end

Instance Attribute Details

#descriptionObject (readonly)

The description of this player.



23
24
25
# File 'lib/connect_n_game/player.rb', line 23

def description
  @description
end

#nameObject (readonly)

The name of this player.



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

def name
  @name
end

#typeObject (readonly)

The type of this player: :carbon or :silicon



26
27
28
# File 'lib/connect_n_game/player.rb', line 26

def type
  @type
end

Instance Method Details

#carbon?Boolean

Is this an organic life-form?

Returns:

  • (Boolean)


46
47
48
# File 'lib/connect_n_game/player.rb', line 46

def carbon?
  type == :carbon
end

#make_move(_game, piece) ⇒ Object

Make a move. This is dummy code for testing only.
Parameters

  • _game - the game being played - not used here.

  • piece - the piece to be played, 1 or 2.


Returns

  • A move, 1 .. rack.width



56
57
58
# File 'lib/connect_n_game/player.rb', line 56

def make_move(_game, piece)
  piece
end

#silicon?Boolean

Is this an automaton?

Returns:

  • (Boolean)


41
42
43
# File 'lib/connect_n_game/player.rb', line 41

def silicon?
  type == :silicon
end