Class: BoardGameGrid::Piece

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#idFixnum (readonly)

Returns the identifier of the piece.

Returns:

  • (Fixnum)

    the identifier of the piece.



16
17
18
# File 'lib/board_game_grid/piece.rb', line 16

def id
  @id
end

#player_numberFixnum (readonly)

Returns the owner of the piece.

Returns:

  • (Fixnum)

    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_jsonHash

returns a serialized piece as a hash

Returns:

  • (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?

Parameters:

  • from (Square)

    the origin square.

  • to (Square)

    the destination square.

  • game_state (GameState)

    the current game state.

Returns:

  • (Boolean)


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.

Parameters:

  • square (Square)

    the origin square.

  • game_state (GameState)

    the current game state.

Returns:



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.

Parameters:

  • square (Square)

    the origin square.

  • game_state (GameState)

    the current game state.

Returns:



60
61
62
# File 'lib/board_game_grid/piece.rb', line 60

def destinations(square, game_state)
  []
end

#forwards_directionFixnum

returns the direction the piece moves

player 1 moves up (-1) player 2 moves down (1)

Returns:

  • (Fixnum)


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.

Parameters:

  • square (Square)

    the origin square.

  • game_state (GameState)

    the current game state.

Returns:



73
74
75
# File 'lib/board_game_grid/piece.rb', line 73

def move_squares(square, game_state)
  destinations(square, game_state)
end

#opponentFixnum

The opposing player number

Returns:

  • (Fixnum)


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)

Parameters:

  • square (Square)

    the origin square.

  • game_state (GameState)

    the current game state.

Returns:



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

#typeFixnum

The stringified identifier of the piece type.

Returns:

  • (Fixnum)


31
32
33
# File 'lib/board_game_grid/piece.rb', line 31

def type
  self.class.to_s.split('::').last.downcase
end