Class: BoardGameGrid::Piece
- Inherits:
-
Object
- Object
- BoardGameGrid::Piece
- Defined in:
- lib/board_game_grid/piece.rb
Overview
Piece
A piece that can move on a board
Constant Summary collapse
- FORWARDS_DIRECTION =
The forwards direciton of the piece for each player
{ 1 => -1, 2 => 1 }
Instance Attribute Summary collapse
-
#id ⇒ Fixnum
readonly
The identifier of the piece.
-
#player_number ⇒ Fixnum
readonly
The owner of the piece.
Instance Method Summary collapse
-
#as_json ⇒ Hash
returns a serialized piece as a hash.
-
#can_move?(from, to, game_state) ⇒ Boolean
Can the piece move from a square, to a square, given the game state?.
-
#capture_squares(squares, game_state) ⇒ SquareSet
All the squares that the piece can capture.
-
#destinations(square, game_state) ⇒ SquareSet
All the squares that the piece can move to and/or capture.
-
#forwards_direction ⇒ Fixnum
returns the direction the piece moves.
-
#initialize(id:, player_number:, type: nil) ⇒ Piece
constructor
A new instance of Piece.
-
#move_squares(square, game_state) ⇒ SquareSet
All the squares that the piece can move to.
-
#opponent ⇒ Fixnum
The opposing player number.
-
#potential_capture_squares(square, game_state) ⇒ SquareSet
All the squares that the piece could potentially capture.
-
#type ⇒ Fixnum
The stringified identifier of the piece type.
Constructor Details
#initialize(id:, player_number:, type: nil) ⇒ Piece
Returns a new instance of Piece.
10 11 12 13 |
# File 'lib/board_game_grid/piece.rb', line 10 def initialize(id: , player_number: , type: nil) @id = id @player_number = player_number end |
Instance Attribute Details
#id ⇒ Fixnum (readonly)
Returns the identifier of the piece.
16 17 18 |
# File 'lib/board_game_grid/piece.rb', line 16 def id @id end |
#player_number ⇒ Fixnum (readonly)
Returns the owner of the piece.
19 20 21 |
# File 'lib/board_game_grid/piece.rb', line 19 def player_number @player_number end |
Instance Method Details
#as_json ⇒ Hash
returns a serialized piece as a hash
106 107 108 109 110 111 112 |
# File 'lib/board_game_grid/piece.rb', line 106 def as_json { id: id, player_number: player_number, type: type } end |
#can_move?(from, to, game_state) ⇒ Boolean
Can the piece move from a square, to a square, given the game state?
47 48 49 |
# File 'lib/board_game_grid/piece.rb', line 47 def can_move?(from, to, game_state) destinations(from, game_state).include?(to) end |
#capture_squares(squares, game_state) ⇒ SquareSet
All the squares that the piece can capture.
86 87 88 |
# File 'lib/board_game_grid/piece.rb', line 86 def capture_squares(squares, game_state) destinations(squares, game_state) end |
#destinations(square, game_state) ⇒ SquareSet
All the squares that the piece can move to and/or capture.
60 61 62 |
# File 'lib/board_game_grid/piece.rb', line 60 def destinations(square, game_state) [] end |
#forwards_direction ⇒ Fixnum
returns the direction the piece moves
player 1 moves up (-1) player 2 moves down (1)
120 121 122 |
# File 'lib/board_game_grid/piece.rb', line 120 def forwards_direction FORWARDS_DIRECTION[player_number] end |
#move_squares(square, game_state) ⇒ SquareSet
All the squares that the piece can move to.
73 74 75 |
# File 'lib/board_game_grid/piece.rb', line 73 def move_squares(square, game_state) destinations(square, game_state) end |
#opponent ⇒ Fixnum
The opposing player number
24 25 26 |
# File 'lib/board_game_grid/piece.rb', line 24 def opponent player_number == 1 ? 2 : 1 end |
#potential_capture_squares(square, game_state) ⇒ SquareSet
All the squares that the piece could potentially capture. (i.e. if a piece was there)
99 100 101 |
# File 'lib/board_game_grid/piece.rb', line 99 def potential_capture_squares(square, game_state) capture_squares(square, game_state) end |
#type ⇒ Fixnum
The stringified identifier of the piece type.
31 32 33 |
# File 'lib/board_game_grid/piece.rb', line 31 def type self.class.to_s.split('::').last.downcase end |