Sha256: 496a0802c9cab75fe7bb32b3ad8b9e16db371d7ca1a26a2ab2fb4e483caf4268

Contents?: true

Size: 892 Bytes

Versions: 1

Compression:

Stored size: 892 Bytes

Contents

require 'board_game_grid'

module JustChess

  # = Piece
  #
  # A piece that can move on a chess board
  class Piece < BoardGameGrid::Piece
    def initialize(id: , player_number: , type: nil, has_moved: false)
      @id = id
      @player_number = player_number
      @has_moved = has_moved
    end

    # @return [Boolean] determines if the piece has moved.
    attr_reader :has_moved
    alias_method :has_moved?, :has_moved

    # mark the piece as moved
    #
    # @return [TrueClass]
    def moved
      @has_moved = true
    end

    # Has the piece not moved yet?
    #
    # @return [TrueClass, FalseClass]
    def has_not_moved?
      !has_moved?
    end

    # returns a serialized piece as a hash
    #
    # @return [Hash]
    def as_json
      {
        id: id,
        player_number: player_number,
        type: type,
        has_moved: has_moved?
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
just_chess-1.0.9 lib/just_chess/pieces/piece.rb