Cute Chess  0.1
westernboard.h
1 /*
2  This file is part of Cute Chess.
3 
4  Cute Chess 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 3 of the License, or
7  (at your option) any later version.
8 
9  Cute Chess is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with Cute Chess. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef WESTERNBOARD_H
19 #define WESTERNBOARD_H
20 
21 #include "board.h"
22 
23 namespace Chess {
24 
25 class WesternZobrist;
26 
27 
40 class LIB_EXPORT WesternBoard : public Board
41 {
42  public:
45  {
46  Pawn = 1,
49  Rook,
51  King
52  };
53 
55  WesternBoard(WesternZobrist* zobrist);
56 
57  // Inherited from Board
58  virtual int width() const;
59  virtual int height() const;
60  virtual Result result();
61  virtual int reversibleMoveCount() const;
62 
63  protected:
66  {
69  NoCastlingSide
70  };
71 
73  static const unsigned KnightMovement = 2;
75  static const unsigned BishopMovement = 4;
77  static const unsigned RookMovement = 8;
78 
80  enum StepType
81  {
82  NoStep = 0,
83  FreeStep = 1,
84  CaptureStep = 2
85  /* FreeOrCaptureStep = FreeStep|CaptureStep //!< like King or Sergeant*/
86  };
88  struct PawnStep { StepType type; int file; };
103  int pawnAmbiguity(StepType type = FreeStep) const;
111  virtual bool kingsCountAssertion(int whiteKings,
112  int blackKings) const;
118  virtual bool kingCanCapture() const;
124  virtual bool hasCastling() const;
130  virtual bool pawnHasDoubleStep() const;
136  virtual bool hasEnPassantCaptures() const;
144  virtual bool variantHasChanneling(Side side, int square) const;
153  virtual void addPromotions(int sourceSquare,
154  int targetSquare,
155  QVarLengthArray<Move>& moves) const;
157  int kingSquare(Side side) const;
159  int enpassantSquare() const;
164  virtual bool parseCastlingRights(QChar c);
172  bool hasCastlingRight(Side side, CastlingSide castlingSide) const;
180  void removeCastlingRights(int square);
184  void removeCastlingRights(Side side);
189  virtual int castlingFile(CastlingSide castlingSide) const;
193  virtual bool defendedByKnight(Side side, int square) const;
198  virtual bool inCheck(Side side, int square = 0) const;
199 
207  virtual QString vFenIncludeString(FenNotation notation) const;
208 
209  // Inherited from Board
210  virtual void vInitialize();
211  virtual QString vFenString(FenNotation notation) const;
212  virtual bool vSetFenString(const QStringList& fen);
213  virtual QString lanMoveString(const Move& move);
214  virtual QString sanMoveString(const Move& move);
215  virtual Move moveFromLanString(const QString& str);
216  virtual Move moveFromSanString(const QString& str);
217  virtual void vMakeMove(const Move& move,
218  BoardTransition* transition);
219  virtual void vUndoMove(const Move& move);
220  virtual void generateMovesForPiece(QVarLengthArray<Move>& moves,
221  int pieceType,
222  int square) const;
223  virtual bool vIsLegalMove(const Move& move);
224  virtual bool isLegalPosition();
225  virtual int captureType(const Move& move) const;
226 
227  private:
228  struct CastlingRights
229  {
230  // Usage: 'rookSquare[Side][CastlingSide]'
231  // A value of zero (square 0) means no castling rights
232  int rookSquare[2][2];
233  };
234 
235  // Data for reversing/unmaking a move
236  struct MoveData
237  {
238  Piece capture;
239  int enpassantSquare;
240  int enpassantTarget;
241  CastlingRights castlingRights;
242  CastlingSide castlingSide;
243  int reversibleMoveCount;
244  };
245 
246  void generateCastlingMoves(QVarLengthArray<Move>& moves) const;
247  void generatePawnMoves(int sourceSquare,
248  QVarLengthArray<Move>& moves) const;
249 
250  bool canCastle(CastlingSide castlingSide) const;
251  QString castlingRightsString(FenNotation notation) const;
252  CastlingSide castlingSide(const Move& move) const;
253  void setEnpassantSquare(int square,
254  int target=0);
255  void setCastlingSquare(Side side,
256  CastlingSide cside,
257  int square);
260  inline int pawnPushOffset(const PawnStep& ps,
261  int sign) const;
262 
263  int m_arwidth;
264  int m_sign;
265  int m_kingSquare[2];
266  int m_enpassantSquare;
267  int m_enpassantTarget;
268  int m_plyOffset;
269  int m_reversibleMoveCount;
270  bool m_kingCanCapture;
271  bool m_hasCastling;
272  bool m_pawnHasDoubleStep;
273  bool m_hasEnPassantCaptures;
274  bool m_pawnAmbiguous;
275  bool m_multiDigitNotation;
276  QVector<MoveData> m_history;
277  CastlingRights m_castlingRights;
278  int m_castleTarget[2][2];
279  const WesternZobrist* m_zobrist;
280 
281  QVarLengthArray<int> m_knightOffsets;
282  QVarLengthArray<int> m_bishopOffsets;
283  QVarLengthArray<int> m_rookOffsets;
284 };
285 
286 
287 } // namespace Chess
288 #endif // WESTERNBOARD_H
Knight.
Definition: westernboard.h:47
WesternPieceType
Definition: westernboard.h:44
FenNotation
Definition: board.h:92
Bishop.
Definition: westernboard.h:48
An internal chessboard class.
Definition: board.h:56
StepType
Definition: westernboard.h:80
A board for western chess variants.
Definition: westernboard.h:40
Definition: westernboard.h:88
Queen side (O-O-O)
Definition: westernboard.h:67
Definition: boardscene.h:30
QVarLengthArray< PawnStep, 8 > m_pawnSteps
Definition: westernboard.h:98
The side or color of a chess player.
Definition: side.h:35
King side (O-O)
Definition: westernboard.h:68
A chess piece.
Definition: piece.h:40
The result of a chess game.
Definition: result.h:34
CastlingSide
Definition: westernboard.h:65
Details of a board transition caused by a move.
Definition: boardtransition.h:40
A small and efficient chessmove class.
Definition: move.h:42
Queen.
Definition: westernboard.h:50
Rook.
Definition: westernboard.h:49
Zobrist keys for Western chess variants.
Definition: westernzobrist.h:28