class BoardGameGrid::Square

Square

A Square on a board

Attributes

id[R]

@return [Fixnum] the unique identifier of the square.

piece[RW]

@return [Hash,NilClass] The piece on the square if any.

x[R]

@return [Fixnum] the x co-ordinate of the square.

y[R]

@return [Fixnum] the y co-ordinate of the square.

Public Class Methods

new(id: , x: , y: , piece: nil) click to toggle source

New objects can be instantiated by passing in a hash with

@param [Fixnum] id

the unique identifier of the square.

@param [Fixnum] x

the x co-ordinate of the square.

@param [Fixnum] y

the y co-ordinate of the square.

@option [Hash,NilClass] piece

The piece on the square, can be a hash or nil.

Example:

# Instantiates a new Square
JustCheckers::Square.new({
  id: 1,
  x: 1,
  y: 0,
  piece: { player_number: 1, king: false }
})
# File lib/board_game_grid/square.rb, line 32
def initialize(id: , x: , y: , piece: nil)
  @id = id
  @x = x
  @y = y
  @piece = piece
end

Public Instance Methods

==(other) click to toggle source

Is the square the same as another one?

@return [Boolean]

# File lib/board_game_grid/square.rb, line 122
def ==(other)
  self.id == other.id
end
as_json() click to toggle source

A serialized version of the square as a hash

@return [Hash]

# File lib/board_game_grid/square.rb, line 54
def as_json
  { 
    id: id, 
    x: x, 
    y: y, 
    piece: piece && piece.as_json
  }
end
attribute_match?(attribute, value) click to toggle source

checks if the square matches the attributes passed.

@param [Symbol] attribute

the square's attribute.

@param [Object,Hash] value

a value to match on. Can be a hash of attribute/value pairs for deep matching

Example:

# Check if square has a piece owned by player 1
square.attribute_match?(:piece, player_number: 1)
# File lib/board_game_grid/square.rb, line 74
def attribute_match?(attribute, value)
  hash_obj_matcher = lambda do |obj, k, v|
    value = obj.send(k)
    if v.is_a?(Hash) && !value.nil?
      v.all? { |k2,v2| hash_obj_matcher.call(value, k2, v2) }
    elsif v.is_a?(Array) && !value.is_a?(Array)
      v.include?(value)
    elsif v.is_a?(Proc)
      v.call(value)
    else
      value == v
    end
  end

  hash_obj_matcher.call(self, attribute, value)
end
occupied?() click to toggle source

Is the square occupied by a piece?

@return [Boolean]

# File lib/board_game_grid/square.rb, line 94
def occupied?
  !!piece
end
occupied_by_opponent?(player_number) click to toggle source

Is the square occupied by the specified player?

@return [Boolean]

# File lib/board_game_grid/square.rb, line 115
def occupied_by_opponent?(player_number)
  piece && piece.player_number != player_number
end
occupied_by_player?(player_number) click to toggle source

Is the square occupied by the specified player?

@return [Boolean]

# File lib/board_game_grid/square.rb, line 108
def occupied_by_player?(player_number)
  piece && piece.player_number == player_number
end
point() click to toggle source

A point object with the square's co-ordinates.

@return [Point]

# File lib/board_game_grid/square.rb, line 129
def point
  Point.new(x, y)
end
unoccupied?() click to toggle source

Is the square unoccupied by a piece?

@return [Boolean]

# File lib/board_game_grid/square.rb, line 101
def unoccupied?
  !piece
end