Ported shogi.
[tagua/yd.git] / src / hlvariant / crazyhouse / legalitycheck.h
blobf79a4e2673e2a5fce8c2b31e05491007f7859ba1
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 #include "../chess/legalitycheck.h"
16 namespace HLVariant {
17 namespace Crazyhouse {
19 template <typename _GameState>
20 class LegalityCheck : public Chess::LegalityCheck<_GameState> {
21 public:
22 typedef _GameState GameState;
23 private:
24 typedef Chess::LegalityCheck<GameState> Base;
25 typedef typename Base::Board Board;
26 typedef typename Base::Move Move;
27 typedef typename Base::Piece Piece;
28 protected:
29 using Base::m_state;
30 public:
31 LegalityCheck(const GameState& state);
33 virtual bool pseudolegal(Move& move) const;
34 virtual typename Piece::Color mover(const Move& move) const;
37 // IMPLEMENTATION
39 template <typename GameState>
40 LegalityCheck<GameState>::LegalityCheck(const GameState& state)
41 : Base(state) { }
43 template <typename GameState>
44 bool LegalityCheck<GameState>::pseudolegal(Move& move) const {
45 // add drop information to move, if missing
46 if (move.drop() == Piece() &&
47 move.pool() != Piece::INVALID_COLOR &&
48 move.index() != -1) {
49 move.setDrop(m_state.pools().pool(move.pool()).get(move.index()));
52 Piece dropped = move.drop();
54 if (dropped == Piece()) {
55 return Base::pseudolegal(move);
57 else {
58 // dropping on a valid square
59 if (!m_state.board().valid(move.to()))
60 return false;
62 // cannot drop on occupied squares
63 if (m_state.board().get(move.to()) != Piece())
64 return false;
66 // cannot drop pawns in first or eighth rank
67 if (dropped.type() == Piece::PAWN &&
68 (move.to().y == m_state.startingRank(Piece::WHITE) ||
69 move.to().y == m_state.promotionRank(Piece::WHITE)))
70 return false;
72 return true;
76 template <typename GameState>
77 typename LegalityCheck<GameState>::Piece::Color
78 LegalityCheck<GameState>::mover(const Move& move) const {
79 if (move.drop() != Piece())
80 return move.drop().color();
81 else
82 return Base::mover(move);
85 } // namespace Crazyhouse
86 } // namespace HLVariant
89 #endif // HLVARIANT__CRAZYHOUSE__LEGALITYCHECK_H