Class: ConnectNGame::JustRandom

Inherits:
Player
  • Object
show all
Defined in:
lib/connect_n_game/players/just_random.rb

Overview

The JustRandom player simply make random 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 = "Random") ⇒ JustRandom

Build the player



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

def initialize(name = "Random")
  super(name, "Moves randomly.", :silicon)
end

Instance Method Details

#losers_commentsObject

The agony of defeat



33
34
35
# File 'lib/connect_n_game/players/just_random.rb', line 33

def losers_comments
  "#{name} says 'No comment.'"
end

#make_move(game, _piece) ⇒ Object

Make a move. This bot moves randomly.
Parameters

  • game - the game being played.

  • _piece - the piece to be played, 1 or 2. (Not used here)


Returns

  • A move, 1 .. rack.width



19
20
21
22
23
24
25
# File 'lib/connect_n_game/players/just_random.rb', line 19

def make_move(game, _piece)
  begin
    channel = rand(1..(game.rack.width))
  end while game.rack.channel_full?(channel)

  channel
end

#winners_commentsObject

The thrill of victory.



28
29
30
# File 'lib/connect_n_game/players/just_random.rb', line 28

def winners_comments
  "#{name} says 'It was all up to pure skill!'"
end