Class: Gobstones::Runner::Head

Inherits:
Object
  • Object
show all
Defined in:
lib/gobstones/runner/head.rb

Constant Summary collapse

MAX_ROWS =
9
MAX_COLS =
9

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHead

Returns a new instance of Head.



20
21
22
# File 'lib/gobstones/runner/head.rb', line 20

def initialize
  with_position_and_board(0, 0, Board.new(MAX_ROWS, MAX_COLS))
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



10
11
12
# File 'lib/gobstones/runner/head.rb', line 10

def board
  @board
end

#x_posObject (readonly)

Returns the value of attribute x_pos.



10
11
12
# File 'lib/gobstones/runner/head.rb', line 10

def x_pos
  @x_pos
end

#y_posObject (readonly)

Returns the value of attribute y_pos.



10
11
12
# File 'lib/gobstones/runner/head.rb', line 10

def y_pos
  @y_pos
end

Class Method Details

.at_randomObject



12
13
14
# File 'lib/gobstones/runner/head.rb', line 12

def self.at_random
  new.at_random
end

.with_position_and_board(x_pos, y_pos, board) ⇒ Object



16
17
18
# File 'lib/gobstones/runner/head.rb', line 16

def self.with_position_and_board(x_pos, y_pos, board)
  new.with_position_and_board(x_pos, y_pos, board)
end

Instance Method Details

#are_there_balls?(color) ⇒ Boolean

Returns:



97
98
99
# File 'lib/gobstones/runner/head.rb', line 97

def are_there_balls?(color)
  @board.are_there_balls?(x_pos, y_pos, color)
end

#at_randomObject



24
25
26
27
28
# File 'lib/gobstones/runner/head.rb', line 24

def at_random
  @x_pos = rand(MAX_ROWS)
  @y_pos = rand(MAX_COLS)
  self
end

#can_move?(dir) ⇒ Boolean

Returns:



37
38
39
40
# File 'lib/gobstones/runner/head.rb', line 37

def can_move?(dir)
  check dir
  dir.can_move?(self)
end

#can_move_east?Boolean

Returns:



50
51
52
# File 'lib/gobstones/runner/head.rb', line 50

def can_move_east?
  @x_pos < MAX_ROWS - 1
end

#can_move_north?Boolean

Returns:



42
43
44
# File 'lib/gobstones/runner/head.rb', line 42

def can_move_north?
  @y_pos < MAX_COLS - 1
end

#can_move_south?Boolean

Returns:



46
47
48
# File 'lib/gobstones/runner/head.rb', line 46

def can_move_south?
  @y_pos.positive?
end

#can_move_west?Boolean

Returns:



54
55
56
# File 'lib/gobstones/runner/head.rb', line 54

def can_move_west?
  @x_pos.positive?
end

#cloneObject



101
102
103
# File 'lib/gobstones/runner/head.rb', line 101

def clone
  self.class.with_position_and_board(x_pos, y_pos, board.clone)
end

#go_to_originObject



80
81
82
83
# File 'lib/gobstones/runner/head.rb', line 80

def go_to_origin
  @x_pos = 0
  @y_pos = 0
end

#move(dir) ⇒ Object

Raises:



58
59
60
61
62
# File 'lib/gobstones/runner/head.rb', line 58

def move(dir)
  raise OutOfBoardError unless can_move?(dir)

  dir.move self
end

#move_eastObject



72
73
74
# File 'lib/gobstones/runner/head.rb', line 72

def move_east
  @x_pos += 1
end

#move_northObject



64
65
66
# File 'lib/gobstones/runner/head.rb', line 64

def move_north
  @y_pos += 1
end

#move_southObject



68
69
70
# File 'lib/gobstones/runner/head.rb', line 68

def move_south
  @y_pos -= 1
end

#move_westObject



76
77
78
# File 'lib/gobstones/runner/head.rb', line 76

def move_west
  @x_pos -= 1
end

#number_of_balls(color) ⇒ Object



93
94
95
# File 'lib/gobstones/runner/head.rb', line 93

def number_of_balls(color)
  @board.number_of_balls(x_pos, y_pos, color)
end

#put(color) ⇒ Object



85
86
87
# File 'lib/gobstones/runner/head.rb', line 85

def put(color)
  @board.put x_pos, y_pos, color
end

#take_out(color) ⇒ Object



89
90
91
# File 'lib/gobstones/runner/head.rb', line 89

def take_out(color)
  @board.take_out x_pos, y_pos, color
end

#with_position_and_board(x_pos, y_pos, board) ⇒ Object



30
31
32
33
34
35
# File 'lib/gobstones/runner/head.rb', line 30

def with_position_and_board(x_pos, y_pos, board)
  @x_pos = x_pos
  @y_pos = y_pos
  @board = board
  self
end