Class: Rchess::Paths::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rchess/path.rb

Direct Known Subclasses

Bishop, King, Knight, Pawn, Queen, Rook

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Base

Returns a new instance of Base.



7
8
9
10
# File 'lib/rchess/path.rb', line 7

def initialize(params)
  @coord = params.fetch(:coord)
  @board = params.fetch(:board)
end

Instance Attribute Details

#boardObject

Returns the value of attribute board.



5
6
7
# File 'lib/rchess/path.rb', line 5

def board
  @board
end

#coordObject (readonly)

Returns the value of attribute coord.



4
5
6
# File 'lib/rchess/path.rb', line 4

def coord
  @coord
end

Class Method Details

.threaten_destinations_from_coord(coord, board) ⇒ Object



28
29
30
31
32
33
# File 'lib/rchess/path.rb', line 28

def self.threaten_destinations_from_coord(coord, board)
  params = { coord: coord, board: board }
  [
    Paths::Pawn.new(params).destinations, Paths::Bishop.new(params).destinations, Paths::Knight.new(params).destinations, Paths::Rook.new(params).destinations
  ].flatten(1)
end

Instance Method Details

#destinationsObject



20
21
22
# File 'lib/rchess/path.rb', line 20

def destinations
  @destinations ||= self.paths.map{ |path| apply_delta_to_path(path).delete_if{ |coord| coord.x < 0 || coord.y < 0 } }
end

#pathsObject

Raises:

  • (NotImplementedError)


24
25
26
# File 'lib/rchess/path.rb', line 24

def paths
  raise NotImplementedError.new("You must implement paths")
end

#srcBoxObject



12
13
14
# File 'lib/rchess/path.rb', line 12

def srcBox
  @srcBox ||= @bord.box_at_coord(@coord)
end

#srcDirectionObject



16
17
18
# File 'lib/rchess/path.rb', line 16

def srcDirection
  @srcDirection ||= srcBox.downcase == srcBox ? -1 : 1 #:up or :down
end