Added CustomBoard.
[tagua/yd.git] / src / hlvariant / crazyhouse / legalitycheck.h
blob52003eba289f7d272eb621a0d3b529f82b0ceb0c
1 /*
2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2007 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #ifndef HLVARIANT__CRAZYHOUSE__LEGALITYCHECK_H
12 #define HLVARIANT__CRAZYHOUSE__LEGALITYCHECK_H
14 namespace HLVariant {
15 namespace Crazyhouse {
17 template <typename _GameState>
18 class LegalityCheck : public Chess::LegalityCheck<_GameState> {
19 public:
20 typedef _GameState GameState;
21 private:
22 typedef Chess::LegalityCheck<GameState> Base;
23 typedef typename Base::Board Board;
24 typedef typename Base::Move Move;
25 typedef typename Base::Piece Piece;
26 protected:
27 using Base::m_state;
28 public:
29 LegalityCheck(const GameState& state);
31 virtual bool pseudolegal(Move& move) const;
32 virtual typename Piece::Color mover(const Move& move) const;
35 // IMPLEMENTATION
37 template <typename GameState>
38 LegalityCheck<GameState>::LegalityCheck(const GameState& state)
39 : Base(state) { }
41 template <typename GameState>
42 bool LegalityCheck<GameState>::pseudolegal(Move& move) const {
43 // add drop information to move, if missing
44 if (move.drop() == Piece() &&
45 move.pool() != Piece::INVALID_COLOR &&
46 move.index() != -1) {
47 move.setDrop(m_state.pools().pool(move.pool()).get(move.index()));
50 Piece dropped = move.drop();
52 if (dropped == Piece()) {
53 return Base::pseudolegal(move);
55 else {
56 // dropping on a valid square
57 if (!m_state.board().valid(move.to()))
58 return false;
60 // cannot drop on occupied squares
61 if (m_state.board().get(move.to()) != Piece())
62 return false;
64 // cannot drop pawns in first or eighth rank
65 if (dropped.type() == Piece::PAWN &&
66 (move.to().y == m_state.startingRank(Piece::WHITE) ||
67 move.to().y == m_state.promotionRank(Piece::WHITE)))
68 return false;
70 return true;
74 template <typename GameState>
75 typename LegalityCheck<GameState>::Piece::Color
76 LegalityCheck<GameState>::mover(const Move& move) const {
77 if (move.drop() != Piece())
78 return move.drop().color();
79 else
80 return Base::mover(move);
83 } // namespace Crazyhouse
84 } // namespace HLVariant
87 #endif // HLVARIANT__CRAZYHOUSE__LEGALITYCHECK_H