Module: ChessEngine::MoveValidator

Included in:
Game
Defined in:
lib/chess_engine/move_validator.rb

Overview

This module contains all the methods needed to check if some move is valid or not. It is included in the Game class and so uses some of its attributes: board, current_color and last_piece (for en passant only)

Instance Method Summary collapse

Instance Method Details

#safe_moves(from) ⇒ Object

Excludes from valid_moves all fatal moves



11
12
13
# File 'lib/chess_engine/move_validator.rb', line 11

def safe_moves(from)
  valid_moves(from).reject { |move| fatal_move?(from, move) }
end

#valid_moves(from) ⇒ Object

Returns an array of valid moves for a piece at the given position. Note: this method doesn’t exclude moves that lead current king to be attacked (See #safe_moves method)



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/chess_engine/move_validator.rb', line 20

def valid_moves(from)
  piece = @board.at(from)
  if piece.king? || piece.knight?
    piece.moves.map do |move|
      to = relative_coords(from, move)
      to if possible_move?(to)
    end.compact
  elsif piece.pawn?
    pawn_valid_moves(from)
  else
    valid_moves_recursive(from)
  end
end