Class: BoardGameGrid::Square
- Inherits:
-
Object
- Object
- BoardGameGrid::Square
- Defined in:
- lib/board_game_grid/square.rb
Overview
Square
A Square on a board
Instance Attribute Summary collapse
-
#id ⇒ Fixnum
readonly
The unique identifier of the square.
-
#piece ⇒ Hash, NilClass
The piece on the square if any.
-
#x ⇒ Fixnum
readonly
The x co-ordinate of the square.
-
#y ⇒ Fixnum
readonly
The y co-ordinate of the square.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Is the square the same as another one?.
-
#as_json ⇒ Hash
A serialized version of the square as a hash.
-
#attribute_match?(attribute, value) ⇒ Boolean
checks if the square matches the attributes passed.
-
#initialize(id:, x:, y:, piece: nil) ⇒ Square
constructor
New objects can be instantiated by passing in a hash with.
-
#occupied? ⇒ Boolean
Is the square occupied by a piece?.
-
#occupied_by_opponent?(player_number) ⇒ Boolean
Is the square occupied by the specified player?.
-
#occupied_by_player?(player_number) ⇒ Boolean
Is the square occupied by the specified player?.
-
#point ⇒ Point
A point object with the square’s co-ordinates.
-
#unoccupied? ⇒ Boolean
Is the square unoccupied by a piece?.
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 }
})
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
#id ⇒ Fixnum (readonly)
Returns the unique identifier of the square.
40 41 42 |
# File 'lib/board_game_grid/square.rb', line 40 def id @id end |
#piece ⇒ Hash, NilClass
Returns The piece on the square if any.
49 50 51 |
# File 'lib/board_game_grid/square.rb', line 49 def piece @piece end |
#x ⇒ Fixnum (readonly)
Returns the x co-ordinate of the square.
43 44 45 |
# File 'lib/board_game_grid/square.rb', line 43 def x @x end |
#y ⇒ Fixnum (readonly)
Returns 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?
118 119 120 |
# File 'lib/board_game_grid/square.rb', line 118 def ==(other) self.id == other.id end |
#as_json ⇒ Hash
A serialized version of the square as a 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)
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?
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?
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?
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 |
#point ⇒ Point
A point object with the square’s co-ordinates.
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?
97 98 99 |
# File 'lib/board_game_grid/square.rb', line 97 def unoccupied? !piece end |