Class: JustChess::Piece

Inherits:
BoardGameGrid::Piece
  • Object
show all
Defined in:
lib/just_chess/pieces/piece.rb

Overview

Piece

A piece that can move on a chess board

Direct Known Subclasses

Bishop, King, Knight, Pawn, Queen, Rook

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, player_number:, type: nil, has_moved: false) ⇒ Piece

Returns a new instance of Piece.



9
10
11
12
13
# File 'lib/just_chess/pieces/piece.rb', line 9

def initialize(id: , player_number: , type: nil, has_moved: false)
  @id = id
  @player_number = player_number
  @has_moved = has_moved
end

Instance Attribute Details

#has_movedBoolean (readonly) Also known as: has_moved?

Returns determines if the piece has moved.

Returns:

  • (Boolean)

    determines if the piece has moved.



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

def has_moved
  @has_moved
end

Instance Method Details

#as_jsonHash

returns a serialized piece as a hash

Returns:

  • (Hash)


36
37
38
39
40
41
42
43
# File 'lib/just_chess/pieces/piece.rb', line 36

def as_json
  {
    id: id,
    player_number: player_number,
    type: type,
    has_moved: has_moved?
  }
end

#has_not_moved?TrueClass, FalseClass

Has the piece not moved yet?

Returns:

  • (TrueClass, FalseClass)


29
30
31
# File 'lib/just_chess/pieces/piece.rb', line 29

def has_not_moved?
  !has_moved?
end

#movedTrueClass

mark the piece as moved

Returns:

  • (TrueClass)


22
23
24
# File 'lib/just_chess/pieces/piece.rb', line 22

def moved
  @has_moved = true
end