Add Sho Shogi variant.
[tagua/yd.git] / src / hlvariant / sho-shogi / legalitycheck.h
bloba11fd077d1a89e667674e8b3322c3b9832f59919
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__SHOSHOGI__LEGALITYCHECK_H
12 #define HLVARIANT__SHOSHOGI__LEGALITYCHECK_H
14 #include "interactiontype.h"
15 #include "../shogi/legalitycheck.h"
17 namespace HLVariant {
18 namespace ShoShogi {
20 template <typename _GameState>
21 class LegalityCheck: public Shogi::LegalityCheck<_GameState> {
22 typedef Shogi::LegalityCheck<_GameState> Base;
23 public:
24 typedef _GameState GameState;
25 typedef typename GameState::Board Board;
26 typedef typename Board::Piece Piece;
27 typedef typename GameState::Move Move;
29 LegalityCheck(const GameState& state);
30 bool legal(Move& move) const;
31 virtual InteractionType droppable(const TurnTest&, int index) const;
34 // IMPLEMENTATION
36 template <typename GameState>
37 LegalityCheck<GameState>::LegalityCheck(const GameState& state)
38 : Base(state) { }
40 template <typename GameState>
41 InteractionType LegalityCheck<GameState>::droppable(const TurnTest&, int) const {
42 return NoAction;
45 template <typename GameState>
46 bool LegalityCheck<GameState>::legal(Move& move) const {
47 if (!pseudolegal(move))
48 return false;
50 GameState tmp(Base::m_state);
51 tmp.move(move);
53 // find king and prince positions
54 Point king_pos = tmp.board().find(Piece(Base::m_state.turn(), Piece::KING));
55 Point prince_pos = tmp.board().find(Piece(Base::m_state.turn(), Piece::DRUNKEN_ELEPHANT, true));
57 // check if the king and prince can be captured
58 if ((canBeCaptured(tmp, king_pos) && canBeCaptured(tmp, prince_pos)) ||
59 (canBeCaptured(tmp, king_pos) && !prince_pos.valid()) ||
60 (canBeCaptured(tmp, prince_pos) && !king_pos.valid()) ||
61 (!prince_pos.valid() && !king_pos.valid()))
62 return false;
64 return true;
67 } // namespace ShoShogi
68 } // namespace HLVariant
70 #endif // HLVARIANT__SHOSHOGI__LEGALITYCHECK_H