Class: ConnectNGame::Human

Inherits:
Player
  • Object
show all
Defined in:
lib/connect_n_game/human.rb,
lib/cli/cli_human.rb

Overview

The Human player simply makes carbon based moves.

Instance Attribute Summary

Attributes inherited from Player

#description, #name, #type

Instance Method Summary collapse

Methods inherited from Player

#carbon?, #silicon?

Constructor Details

#initialize(name = "Human") ⇒ Human

Build the player



9
10
11
# File 'lib/connect_n_game/human.rb', line 9

def initialize(name = "Human")
  super(name, "An actual player.", :carbon)
end

Instance Method Details

Get a string of legal moves.
Parameters

  • rack - the playing area.


Returns

  • A string with all possible legal moves.



30
31
32
33
34
35
36
37
# File 'lib/cli/cli_human.rb', line 30

def legal_moves(rack)
  legal = rack.channel_names.reject do |name|
    channel = Utl.name_to_channel(name)
    rack.channel_full?(channel)
  end

  legal.join("")
end

#losers_commentsObject

The agony of defeat



21
22
23
# File 'lib/connect_n_game/human.rb', line 21

def losers_comments
  "Too bad #{name}, you lose. Hang your head in shame."
end

#make_move(game, piece) ⇒ Object

Make a move. This player moves with DNA and other stuff too.
Parameters

  • game - the game being played.

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


Returns

  • A move, 1 .. rack.width



14
15
16
17
18
19
20
21
22
23
# File 'lib/cli/cli_human.rb', line 14

def make_move(game, piece)
  begin
    legal = legal_moves(game.rack)
    puts "Player #{piece} select [#{legal}] "
    reply = gets[0].upcase
    channel = Utl.name_to_channel(reply)
  end until channel && legal.include?(reply)

  channel
end

#winners_commentsObject

The thrill of victory.



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

def winners_comments
  "Congratulations #{name}! You're our winner today!"
end