Implement ActionList::Entry#clear.
[kaya.git] / lib / games / validator_base.rb
blob2aa58a59376962085e6b55fd00188f464d4a982a
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
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.
8 class ValidatorBase
9   def initialize(state)
10     @state = state
11   end
13   # whether this move satisfies basic conditions
14   # for validity
15   # 
16   def proper?(move)
17     return false unless @state.board.valid? move.src
18     return false unless @state.board.valid? move.dst
19     
20     piece = @state.board[move.src]
21     return false unless piece and piece.color == @state.turn
22     true
23   end
24   
25   def check_legality(piece, target, move)
26     king_pos = @state.board.find(@state.piece_factory.new(piece.color, :king))
27     if king_pos
28       not attacked?(king_pos)
29     end
30   end
31   
32   def check_pseudolegality(piece, target, move)
33     return false if move.dst == move.src
34     
35     target ||= @state.board[move.dst]
36     return false if piece.same_color_of?(target)
37   
38     m = validator_method(piece.type)
39     valid = if respond_to? m
40       send(m, piece, target, move)
41     end
42   end
44   def validator_method(type)
45     "validate_#{type.to_s}"
46   end
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 }
51     end
52   end
53 end