1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
13 # whether this move satisfies basic conditions
17 return false unless @state.board.valid? move.src
18 return false unless @state.board.valid? move.dst
20 piece = @state.board[move.src]
21 return false unless piece and piece.color == @state.turn
25 def check_legality(piece, target, move)
26 king_pos = @state.board.find(@state.piece_factory.new(piece.color, :king))
28 not attacked?(king_pos)
32 def check_pseudolegality(piece, target, move)
33 return false if move.dst == move.src
35 target ||= @state.board[move.dst]
36 return false if piece.same_color_of?(target)
38 m = validator_method(piece.type)
39 valid = if respond_to? m
40 send(m, piece, target, move)
44 def validator_method(type)
45 "validate_#{type.to_s}"
48 def attacked?(dst, target = nil)
49 @state.board.to_enum(:each_square).any? do |src|
50 to_enum(:each_move, src, dst, target).any? { true }