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.
11 #ifndef HLVARIANT__SHOGI__LEGALITYCHECK_H
12 #define HLVARIANT__SHOGI__LEGALITYCHECK_H
14 #include "interactiontype.h"
15 #include "turnpolicy.h"
20 template <typename _GameState
>
23 typedef _GameState GameState
;
24 typedef typename
GameState::Board Board
;
25 typedef typename
Board::Piece Piece
;
26 typedef typename
GameState::Move Move
;
28 const GameState
& m_state
;
30 LegalityCheck(const GameState
& state
);
31 virtual ~LegalityCheck();
33 virtual bool getMoveType(const Piece
& piece
, const Move
& move
) const;
34 bool legal(Move
& move
) const;
35 bool pseudolegal(Move
& move
) const;
37 virtual InteractionType
movable(const TurnTest
&, const Point
& x
) const;
38 virtual InteractionType
droppable(const TurnTest
&, int index
) const;
43 template <typename GameState
>
44 LegalityCheck
<GameState
>::LegalityCheck(const GameState
& state
)
47 template <typename GameState
>
48 LegalityCheck
<GameState
>::~LegalityCheck() { }
50 template <typename GameState
>
51 bool LegalityCheck
<GameState
>::getMoveType(const Piece
& piece
, const Move
& move
) const {
55 if (move
.from() == move
.to())
58 if (m_state
.board().get(move
.to()).color() == piece
.color())
60 Point delta
= move
.to() - move
.from();
62 if (!piece
.promoted()) {
63 switch (piece
.type()) {
65 return abs(delta
.x
) <= 1 && abs(delta
.y
) <= 1;
67 return (delta
.x
== 0 && abs(delta
.y
) == 1)
68 || (delta
.y
== 0 && abs(delta
.x
) == 1)
69 || (delta
.y
== m_state
.direction(piece
.color()).y
&& abs(delta
.x
) <= 1);
71 return (abs(delta
.x
) == abs(delta
.y
) && abs(delta
.x
) == 1)
72 || (delta
.y
== m_state
.direction(piece
.color()).y
&& abs(delta
.x
) <= 1);
75 PathInfo path
= m_state
.board().path(move
.from(), move
.to());
76 return path
.parallel() && path
.clear();
80 PathInfo path
= m_state
.board().path(move
.from(), move
.to());
81 return path
.diagonal() && path
.clear();
85 return abs(delta
.x
) == 1 && delta
.y
== m_state
.direction(piece
.color()).y
* 2;
89 PathInfo path
= m_state
.board().path(move
.from(), move
.to());
90 return delta
.x
== 0 &&
92 (delta
.y
* m_state
.direction(piece
.color()).y
> 0);
95 return delta
== m_state
.direction(piece
.color());
101 switch (piece
.type()) {
106 return (delta
.x
== 0 && abs(delta
.y
) == 1)
107 || (delta
.y
== 0 && abs(delta
.x
) == 1)
108 || (delta
.y
== m_state
.direction(piece
.color()).y
&& abs(delta
.x
) <= 1);
111 if (abs(delta
.x
) <= 1 && abs(delta
.y
) <= 1) return true;
112 PathInfo path
= m_state
.board().path(move
.from(), move
.to());
113 return path
.parallel() && path
.clear();
117 if (abs(delta
.x
) <= 1 && abs(delta
.y
) <= 1) return true;
118 PathInfo path
= m_state
.board().path(move
.from(), move
.to());
119 return path
.diagonal() && path
.clear();
127 template <typename GameState
>
128 bool LegalityCheck
<GameState
>::pseudolegal(Move
& move
) const {
129 if (move
.drop() == Piece() &&
131 move
.index() != -1) {
132 move
.setDrop(m_state
.pools().pool(move
.pool()).get(move
.index()));
135 Piece dropped
= move
.drop();
136 if (dropped
!= Piece()) {
137 if (m_state
.board().get(move
.to()) != Piece())
140 // if (stuckPiece(dropped, m.to)) return false; // BROKEN
142 if (dropped
.type() == Piece::PAWN
) {
143 for (int i
= 0; i
< m_state
.board().size().y
; i
++) {
144 Piece other
= m_state
.board().get(Point(move
.to().x
, i
));
145 if (other
.type() == Piece::PAWN
&&
146 other
.color() == m_state
.turn() &&
155 Piece piece
= m_state
.board().get(move
.from());
156 if (piece
!= Piece() && getMoveType(piece
, move
)) {
157 if (!piece
.promoted() &&
158 (m_state
.promotionZone(piece
.color(), move
.to()) ||
159 m_state
.promotionZone(piece
.color(), move
.from())))
160 move
.setType(Move::PROMOTION
);
164 //std::cerr << "CANNOT MOVE: piece type cannot go there" << std::endl;
170 template <typename GameState
>
171 bool LegalityCheck
<GameState
>::legal(Move
& move
) const {
172 if (!pseudolegal(move
))
175 GameState
tmp(m_state
);
178 // find king position
179 Point king_pos
= tmp
.board().find(Piece(m_state
.turn(), Piece::KING
));
180 if (!king_pos
.valid())
183 // check if the king can be captured
184 for (int i
= 0; i
< m_state
.board().size().x
; i
++) {
185 for (int j
= 0; j
< m_state
.board().size().y
; j
++) {
187 Piece piece
= tmp
.board().get(p
);
188 LegalityCheck
<GameState
> check(tmp
);
189 if (piece
.color() == tmp
.turn() && check
.getMoveType(piece
, Move(p
, king_pos
))) {
198 template <typename GameState
>
199 InteractionType LegalityCheck
<GameState
>::movable(const TurnTest
& test
, const Point
& p
) const {
200 Piece piece
= m_state
.board().get(p
);
201 if (piece
== Piece() || !test(piece
.color()))
204 return piece
.color() == m_state
.turn() ? Moving
: Premoving
;
207 template <typename GameState
>
208 InteractionType LegalityCheck
<GameState
>::droppable(const TurnTest
& test
, int index
) const {
212 typename
Piece::Color c
= static_cast<typename
Piece::Color
>(index
);
213 return c
== m_state
.turn() ? Moving
: Premoving
;
217 } // namespace HLVariant
219 #endif // HLVARIANT__SHOGI__LEGALITYCHECK_H