Class: ChessData::PositionDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/chess_data/position-definition.rb

Overview

A PositionDefinition is a collection of criteria (matchers) which define a valid type of position.

e.g. some kind of Rook endings.

database.select do 
  at_least 5, "P"
  exactly 1, "R"
  at_most 4, "p"
  exactly 1, "r"
end

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ PositionDefinition

The constructor evaluates the provided block, to set up the user’s board criteria. It then sets up some default criteria for the king’s and pieces not covered by the block.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/chess_data/position-definition.rb', line 20

def initialize(&block)
  @matchers = []
  instance_eval(&block)

  # fill in a default matcher for any not present
  ["K", "k"].each do |piece|
    unless @matchers.any? {|matcher| matcher.piece == piece}
      exactly 1, piece
    end
  end
  ["P", "p", "N", "n", "B", "b", "R", "r", "Q", "q"].each do |piece|
    unless @matchers.any? {|matcher| matcher.piece == piece}
      exactly 0, piece
    end
  end
end

Instance Method Details

#check(board) ⇒ Object

Checks that all the criteria are matched by the given board.



38
39
40
41
42
# File 'lib/chess_data/position-definition.rb', line 38

def check board
  @matchers.all? do |matcher|
    matcher.check board
  end
end