Change Board::get() to return a pointer.
[tagua/yd.git] / src / variants / crazyhouse / validator.cpp
blobe25193ed75f6d3e7cf380ae3dddb83e51bddccdc
1 /*
2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 */
10 #include "validator.h"
12 #include <KDebug>
14 #include <core/board.h>
15 #include <core/move.h>
16 #include <core/poolcollection.h>
17 #include <core/pool.h>
18 #include <core/state.h>
20 #include "global.h"
22 namespace Crazyhouse {
24 Validator::Validator(IValidator* validator)
25 : Delegators::Validator(validator) { }
27 Validator::~Validator() { }
29 bool Validator::pseudolegal(const IState* state, Move& move) const {
30 // add drop information to move, if missing
31 if (move.drop() == Piece() &&
32 move.pool() && move.index() != -1) {
33 move.setDrop(state->pools()->pool(move.pool())->get(move.index()));
36 Piece dropped = move.drop();
37 if (dropped == Piece()) {
38 return m_validator->pseudolegal(state, move);
40 else {
41 // dropping on a valid square
42 if (!state->board()->valid(move.dst()))
43 return false;
45 // cannot drop on occupied squares
46 if (state->board()->get(move.dst()) != NULL &&
47 *state->board()->get(move.dst()) != Piece())
48 return false;
50 // cannot drop pawns in first or eighth rank
51 if (dropped.type() == pawn &&
52 (move.dst().y == state->rank(0, players[0]) ||
53 move.dst().y == state->rank(state->board()->size().y - 1, players[0])))
54 return false;
56 return true;
60 const IColor* Validator::mover(const IState* state, const Move& move) const {
61 if (move.drop() != Piece())
62 return move.drop().color();
63 else
64 return m_validator->mover(state, move);
67 } // namespace Crazyhouse