Class: Sashite::Ggn::Ruleset::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/sashite/ggn/ruleset/source.rb,
lib/sashite/ggn/ruleset/source/destination.rb,
lib/sashite/ggn/ruleset/source/destination/engine.rb

Overview

Represents movement possibilities for a piece type

Defined Under Namespace

Classes: Destination

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Source

Create a new Source

Parameters:

  • data (Hash)

    Sources data structure



15
16
17
18
19
# File 'lib/sashite/ggn/ruleset/source.rb', line 15

def initialize(data)
  @data = data

  freeze
end

Instance Method Details

#from(source) ⇒ Destination

Specify the source location for the piece

Examples:

destination = source.from("e1")

Parameters:

  • source (String)

    Source location (CELL coordinate or HAND “*”)

Returns:

Raises:

  • (KeyError)

    If source not found for this piece



29
30
31
32
33
# File 'lib/sashite/ggn/ruleset/source.rb', line 29

def from(source)
  raise ::KeyError, "Source not found: #{source}" unless source?(source)

  Destination.new(@data.fetch(source))
end

#source?(location) ⇒ Boolean

Check if location is a valid source for this piece

Examples:

source.source?("e1") # => true

Parameters:

  • location (String)

    Source location

Returns:

  • (Boolean)


52
53
54
# File 'lib/sashite/ggn/ruleset/source.rb', line 52

def source?(location)
  @data.key?(location)
end

#sourcesArray<String>

Return all valid source locations for this piece

Examples:

source.sources # => ["e1", "d1", "*"]

Returns:

  • (Array<String>)

    Source locations



41
42
43
# File 'lib/sashite/ggn/ruleset/source.rb', line 41

def sources
  @data.keys
end