Class: King

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

Class Method Summary collapse

Methods inherited from Piece

capturable?, destination_occupied?, obstructed?

Class Method Details

.move_is_valid?(orig, dest, board, castling) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
# File 'lib/pieces/king.rb', line 7

def self.move_is_valid?(orig, dest, board, castling)
  if (orig[1] - dest[1]).abs == 2
    valid_castling_move = valid_castling_move?(orig, dest, board, castling)
  end

  valid_castling_move || (
  (orig[0] - dest[0]).abs <= 1 && (orig[1] - dest[1]).abs <= 1 &&
    (!destination_occupied?(dest, board) || capturable?(orig, dest, board))
)
end

.valid_castling_move?(orig, dest, board, castling) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pieces/king.rb', line 18

def self.valid_castling_move?(orig, dest, board, castling)
  orig_y, orig_x = orig
  dest_y, dest_x = dest

  return false if orig_y != dest_y || (orig_x - dest_x).abs != 2

  king = board[orig_y][orig_x]
  king_color = king[0] == 'W' ? :white : :black
  castle_direction = orig_x < dest_x ? :kingside : :queenside

  return false if castling[king_color][castle_direction] == false

  test_board = board.map(&:dup)
  return false if ChessMate.new(board: test_board).in_check?(test_board)[king_color]

  test_range = castle_direction == :kingside ? [5, 6] : [1, 2, 3]
  test_range.each do |x|
    return false if board[orig_y][x]

    test_board = board.map(&:dup)
    test_board[orig_y][x] = 'WK'
    test_board[orig_y][orig_x] = nil
    test = ChessMate.new(board: test_board)
    return false if test.in_check?(test_board)[king_color] && x != 1
  end
  true
end