Class: BoardGameGrid::Square

Inherits:
Object
  • Object
show all
Defined in:
lib/board_game_grid/square.rb

Overview

Square

A Square on a board

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, x:, y:, piece: nil) ⇒ Square

New objects can be instantiated by passing in a hash with

Example:

# Instantiates a new Square
JustCheckers::Square.new({
  id: 1,
  x: 1,
  y: 0,
  piece: { player_number: 1, king: false }
})

Parameters:

  • id (Fixnum)

    the unique identifier of the square.

  • x (Fixnum)

    the x co-ordinate of the square.

  • y (Fixnum)

    the y co-ordinate of the square.

  • [Hash,NilClass] (Hash)

    a customizable set of options



32
33
34
35
36
37
# 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

Instance Attribute Details

#idFixnum (readonly)

Returns the unique identifier of the square.

Returns:

  • (Fixnum)

    the unique identifier of the square.



40
41
42
# File 'lib/board_game_grid/square.rb', line 40

def id
  @id
end

#pieceHash, NilClass

Returns The piece on the square if any.

Returns:

  • (Hash, NilClass)

    The piece on the square if any.



49
50
51
# File 'lib/board_game_grid/square.rb', line 49

def piece
  @piece
end

#xFixnum (readonly)

Returns the x co-ordinate of the square.

Returns:

  • (Fixnum)

    the x co-ordinate of the square.



43
44
45
# File 'lib/board_game_grid/square.rb', line 43

def x
  @x
end

#yFixnum (readonly)

Returns the y co-ordinate of the square.

Returns:

  • (Fixnum)

    the y co-ordinate of the square.



46
47
48
# File 'lib/board_game_grid/square.rb', line 46

def y
  @y
end

Instance Method Details

#==(other) ⇒ Boolean

Is the square the same as another one?

Returns:

  • (Boolean)


118
119
120
# File 'lib/board_game_grid/square.rb', line 118

def ==(other)
  self.id == other.id
end

#as_jsonHash

A serialized version of the square as a hash

Returns:

  • (Hash)


54
55
56
57
58
59
60
61
# 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) ⇒ Boolean

checks if the square matches the attributes passed.

Example:

# Check if square has a piece owned by player 1
square.attribute_match?(:piece, player_number: 1)

Parameters:

  • attribute (Symbol)

    the square’s attribute.

  • value (Object, Hash)

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

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
# 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 !value.nil? && v.is_a?(Hash)
      v.all? { |k2,v2| hash_obj_matcher.call(value, k2, v2) }
    else
      value == v
    end
  end

  hash_obj_matcher.call(self, attribute, value)
end

#occupied?Boolean

Is the square occupied by a piece?

Returns:

  • (Boolean)


90
91
92
# File 'lib/board_game_grid/square.rb', line 90

def occupied?
  !!piece
end

#occupied_by_opponent?(player_number) ⇒ Boolean

Is the square occupied by the specified player?

Returns:

  • (Boolean)


111
112
113
# File 'lib/board_game_grid/square.rb', line 111

def occupied_by_opponent?(player_number)
  piece && piece.player_number != player_number
end

#occupied_by_player?(player_number) ⇒ Boolean

Is the square occupied by the specified player?

Returns:

  • (Boolean)


104
105
106
# File 'lib/board_game_grid/square.rb', line 104

def occupied_by_player?(player_number)
  piece && piece.player_number == player_number
end

#pointPoint

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

Returns:



125
126
127
# File 'lib/board_game_grid/square.rb', line 125

def point
  Point.new(x, y)
end

#unoccupied?Boolean

Is the square unoccupied by a piece?

Returns:

  • (Boolean)


97
98
99
# File 'lib/board_game_grid/square.rb', line 97

def unoccupied?
  !piece
end