Class: Gobstones::Runner::Head
- Inherits:
-
Object
- Object
- Gobstones::Runner::Head
- Defined in:
- lib/gobstones/runner/head.rb
Constant Summary collapse
- MAX_ROWS =
9- MAX_COLS =
9
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#x_pos ⇒ Object
readonly
Returns the value of attribute x_pos.
-
#y_pos ⇒ Object
readonly
Returns the value of attribute y_pos.
Class Method Summary collapse
Instance Method Summary collapse
- #are_there_balls?(color) ⇒ Boolean
- #at_random ⇒ Object
- #can_move?(dir) ⇒ Boolean
- #can_move_east? ⇒ Boolean
- #can_move_north? ⇒ Boolean
- #can_move_south? ⇒ Boolean
- #can_move_west? ⇒ Boolean
- #clone ⇒ Object
- #go_to_origin ⇒ Object
-
#initialize ⇒ Head
constructor
A new instance of Head.
- #move(dir) ⇒ Object
- #move_east ⇒ Object
- #move_north ⇒ Object
- #move_south ⇒ Object
- #move_west ⇒ Object
- #number_of_balls(color) ⇒ Object
- #put(color) ⇒ Object
- #take_out(color) ⇒ Object
- #with_position_and_board(x_pos, y_pos, board) ⇒ Object
Constructor Details
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
10 11 12 |
# File 'lib/gobstones/runner/head.rb', line 10 def board @board end |
#x_pos ⇒ Object (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_pos ⇒ Object (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_random ⇒ Object
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
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_random ⇒ Object
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
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
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
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
46 47 48 |
# File 'lib/gobstones/runner/head.rb', line 46 def can_move_south? @y_pos.positive? end |
#can_move_west? ⇒ Boolean
54 55 56 |
# File 'lib/gobstones/runner/head.rb', line 54 def can_move_west? @x_pos.positive? end |
#clone ⇒ Object
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_origin ⇒ Object
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
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_east ⇒ Object
72 73 74 |
# File 'lib/gobstones/runner/head.rb', line 72 def move_east @x_pos += 1 end |
#move_north ⇒ Object
64 65 66 |
# File 'lib/gobstones/runner/head.rb', line 64 def move_north @y_pos += 1 end |
#move_south ⇒ Object
68 69 70 |
# File 'lib/gobstones/runner/head.rb', line 68 def move_south @y_pos -= 1 end |
#move_west ⇒ Object
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 |