class King

King piece for a gameof chess

Attributes

moved[W]

Public Class Methods

new(color, position) click to toggle source

Initializes a new king piece with color and position.

@param [String] color A string denoting the color of the piece. @param [Array<Integer>] position An integer array of length 2 denoting the location of the piece on the board.

Calls superclass method ChessPiece::new
# File lib/chess_pieces/king.rb, line 15
def initialize(color, position)
  @move_tree_template = build_king_move_tree
  @moved = false
  super(color == 'white' ? '♚'.white : '♔', color, position, 10_000)
end

Public Instance Methods

move(to) click to toggle source

Flags a king as having moved after the first move

Calls superclass method ChessPiece#move
# File lib/chess_pieces/king.rb, line 23
def move(to)
  @moved = true
  super(to)
end
moved?() click to toggle source

Returns whether or not the king has moved before for the purpose of whether or not the king can castle or not.

@return [Boolean] @moved Whether or not the king has moved.

# File lib/chess_pieces/king.rb, line 33
def moved?
  @moved
end

Protected Instance Methods

build_king_move_tree() click to toggle source

Builds a king move tree where the king can move in any direction up to one space.

@return [MoveTree] move_tree_template A move tree template for the king.

# File lib/chess_pieces/king.rb, line 44
def build_king_move_tree
  move_tree_template = MoveTree.new([0, 0])

  # Get the 8 surrounding spaces
  locations = [-1, 0, 1].repeated_permutation(2).to_a
  locations.delete([0, 0])

  locations.each do |loc|
    move_tree_template.root.add_child(loc)
  end

  move_tree_template
end