Class: JustChess::King

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

Overview

King

The piece that moves 1 space away. Can castle and must not be put into check.

Instance Attribute Summary

Attributes inherited from Piece

#has_moved

Instance Method Summary collapse

Methods inherited from Piece

#as_json, #has_not_moved?, #initialize, #moved

Constructor Details

This class inherits a constructor from JustChess::Piece

Instance Method Details

#base_destinations(square, game_state) ⇒ SquareSet

All the squares that the king could move to normally.

Parameters:

  • square (Square)

    the origin square.

  • game_state (GameState)

    the current game state.

Returns:



32
33
34
# File 'lib/just_chess/pieces/king.rb', line 32

def base_destinations(square, game_state)
  game_state.squares.at_range(square, 1).unoccupied_or_occupied_by_opponent(player_number)
end

#castle(square, game_state) ⇒ SquareSet

All the squares that the king could castle to.

Parameters:

  • square (Square)

    the origin square.

  • game_state (GameState)

    the current game state.

Returns:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/just_chess/pieces/king.rb', line 45

def castle(square, game_state)
  rooks = game_state.squares.occupied_by_piece(Rook).occupied_by_player(player_number).unmoved()

  if has_not_moved? && rooks.any?
    _squares = rooks.map do |s|
      vector = BoardGameGrid::Vector.new(square, s)
      x = square.x + (2 * vector.direction.x)
      y = square.y
      game_state.squares.find_by_x_and_y(x, y)
    end

    potential = SquareSet.new(squares: _squares)

    potential.unoccupied().unblocked(square, game_state.squares)
  else
    SquareSet.new(squares: [])
  end
end

#checked_squares(square, game_state) ⇒ SquareSet

All the squares that the king could not move to because of check.

Parameters:

  • square (Square)

    the origin square.

  • game_state (GameState)

    the current game state.

Returns:



73
74
75
76
77
78
# File 'lib/just_chess/pieces/king.rb', line 73

def checked_squares(square, game_state)
  dup = game_state.clone
  # set piece to nil to handle case where a piece threatens squares behind the king
  dup.squares.find_king_for_player(player_number).piece = nil
  dup.squares.threatened_by(opponent, dup)
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:



19
20
21
# File 'lib/just_chess/pieces/king.rb', line 19

def destinations(square, game_state)
  base_destinations(square, game_state) + castle(square, game_state) - checked_squares(square, game_state) - shared_king_squares(game_state)
end

#shared_king_squares(game_state) ⇒ SquareSet

All the squares that the king could not move to because another king is nearby.

Parameters:

  • game_state (GameState)

    the current game state.

Returns:



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/just_chess/pieces/king.rb', line 86

def shared_king_squares(game_state)
  all = game_state.squares.occupied_by_piece(King).map { |s| s.piece.base_destinations(s, game_state) }

  all.reduce(nil) do |memo, set|
    if memo
      memo & set
    else
      set
    end
  end
end