3 #include <core/behaviour.h>
4 #include <core/board.h>
6 #include <core/state.h>
13 Validator::Validator() : m_delegator(this) { }
15 bool Validator::pseudolegal(const IState
* state
, Move
& move
) const {
16 if (!state
->board()->valid(move
.src())) return false;
17 if (!state
->board()->valid(move
.dst())) return false;
19 Piece piece
= state
->board()->get(move
.src());
20 if (piece
== Piece()) return false;
22 const IBehaviour
* behaviour
= state
->behaviour();
23 if (!behaviour
) return false;
25 const IColor
* thisTurn
= piece
.color();
26 const IColor
* otherTurn
= behaviour
->opponent(thisTurn
);
28 if (piece
!= Piece() && state
->turn() == thisTurn
) {
29 Piece target
= state
->board()->get(move
.dst());
30 if (target
.color() == piece
.color())
32 if (!piece
.type()->canMove(piece
, target
, move
, state
))
35 // TODO: check promotion type
37 if (move
.type() == "king_side_castling") {
38 if (m_delegator
->attacks(state
, otherTurn
, move
.src()) ||
39 m_delegator
->attacks(state
, otherTurn
, move
.src() + Point(1, 0), piece
))
42 if (move
.type() == "queen_side_castling") {
43 if (m_delegator
->attacks(state
, otherTurn
, move
.src()) ||
44 m_delegator
->attacks(state
, otherTurn
, move
.src() + Point(-1, 0), piece
))
55 bool Validator::legal(const IState
* state
, Move
& move
) const {
56 // if (!move.type().isEmpty()) return true;
58 if (!m_delegator
->pseudolegal(state
, move
))
61 const IBehaviour
* behaviour
= state
->behaviour();
62 if (!behaviour
) return false;
64 const IColor
* turn
= m_delegator
->mover(state
, move
);
66 std::auto_ptr
<IState
> tmp(state
->clone());
69 Point kingPos
= tmp
->board()->find(Piece(turn
, King::self()));
71 if (kingPos
== Point::invalid())
74 if (m_delegator
->attacks(tmp
.get(), behaviour
->opponent(turn
), kingPos
))
80 bool Validator::attacks(const IState
* state
, const IColor
* player
,
81 const Point
& square
, const Piece
& target_
) const {
83 if (target_
!= Piece())
86 target
= state
->board()->get(square
);
88 for (int i
= 0; i
< state
->board()->size().x
; i
++) {
89 for (int j
= 0; j
< state
->board()->size().y
; j
++) {
91 Piece piece
= state
->board()->get(p
);
93 if (piece
!= Piece() &&
94 piece
.color() == player
&&
95 piece
.type()->canMove(piece
, target
, move
, state
))
102 const IColor
* Validator::mover(const IState
* state
, const Move
& move
) const {
103 return state
->board()->get(move
.src()).color();
106 void Validator::setDelegator(IValidator
* delegator
) {
107 m_delegator
= delegator
;