Cute Chess  0.1
board.h
1 /*
2  This file is part of Cute Chess.
3  Copyright (C) 2008-2018 Cute Chess authors
4 
5  Cute Chess 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 3 of the License, or
8  (at your option) any later version.
9 
10  Cute Chess is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Cute Chess. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef BOARD_H
20 #define BOARD_H
21 
22 #include <QString>
23 #include <QVector>
24 #include <QVarLengthArray>
25 #include <QSharedPointer>
26 #include <QDebug>
27 #include <QCoreApplication>
28 #include "square.h"
29 #include "piece.h"
30 #include "move.h"
31 #include "genericmove.h"
32 #include "zobrist.h"
33 #include "result.h"
34 
35 
36 namespace Chess {
37 
38 class BoardTransition;
39 
56 class LIB_EXPORT Board
57 {
58  Q_DECLARE_TR_FUNCTIONS(Board)
59 
60  public:
63  {
83  InvertedCoordinates
84  };
87  {
89  LongAlgebraic
90  };
93  {
100  ShredderFen
101  };
102 
110  Board(Zobrist* zobrist);
112  virtual ~Board();
114  virtual Board* copy() const = 0;
115 
117  virtual QString variant() const = 0;
122  virtual bool isRandomVariant() const;
129  virtual bool variantHasDrops() const;
134  virtual bool variantHasWallSquares() const;
141  virtual QList<Piece> reservePieceTypes() const;
143  virtual CoordinateSystem coordinateSystem() const;
145  virtual int width() const = 0;
147  virtual int height() const = 0;
149  virtual QString defaultFenString() const = 0;
151  quint64 key() const;
157  void initialize();
158 
160  bool isValidSquare(const Square& square) const;
161 
165  QStringList pieceList(Side side) const;
170  QString fenString(FenNotation notation = XFen) const;
175  QString startingFenString() const;
184  bool setFenString(const QString& fen);
189  void reset();
190 
195  virtual Side upperCaseSide() const;
197  Side sideToMove() const;
199  Side startingSide() const;
201  Piece pieceAt(const Square& square) const;
203  int plyCount() const;
208  int repeatCount() const;
214  virtual int reversibleMoveCount() const;
221  int reserveCount(Piece piece) const;
223  QString pieceSymbol(Piece piece) const;
225  Piece pieceFromSymbol(const QString& pieceSymbol) const;
227  QString pieceString(int pieceType) const;
229  QString representation(Piece piece) const;
230 
239  void makeMove(const Move& move, BoardTransition* transition = nullptr);
241  void undoMove();
242 
249  QString moveString(const Move& move, MoveNotation notation);
258  Move moveFromString(const QString& str);
265  Move moveFromGenericMove(const GenericMove& move) const;
272  GenericMove genericMove(const Move& move) const;
273 
275  bool isLegalMove(const Move& move);
280  bool isRepetition(const Move& move);
282  QVector<Move> legalMoves();
287  virtual Result result() = 0;
297  virtual Result tablebaseResult(unsigned int* dtm = nullptr) const;
298 
299  protected:
306  virtual void vInitialize() = 0;
307 
322  void setPieceType(int type,
323  const QString& name,
324  const QString& symbol,
325  unsigned movement = 0,
326  const QString & gsymbol = QString());
328  virtual bool pieceHasCaptureMovement(Piece piece, int square, unsigned movement) const;
330  virtual bool pieceHasMovement(Piece piece, int square, unsigned movement) const;
332  virtual bool pieceTypeHasMovement(int pieceType, unsigned movement) const;
333 
345  virtual void vMakeMove(const Move& move,
346  BoardTransition* transition) = 0;
356  virtual void vUndoMove(const Move& move) = 0;
357 
359  Square chessSquare(int index) const;
361  Square chessSquare(const QString& str) const;
363  int squareIndex(const Square& square) const;
365  int squareIndex(const QString& str) const;
367  QString squareString(int index) const;
369  QString squareString(const Square& square) const;
370 
375  virtual QString lanMoveString(const Move& move);
382  virtual QString sanMoveString(const Move& move) = 0;
384  virtual Move moveFromLanString(const QString& str);
386  virtual Move moveFromSanString(const QString& str) = 0;
388  virtual int maxPieceSymbolLength() const;
389 
397  virtual QString vFenString(FenNotation notation) const = 0;
405  virtual bool vSetFenString(const QStringList& fen) = 0;
406 
414  void generateMoves(QVarLengthArray<Move>& moves,
415  int pieceType = Piece::NoPiece) const;
423  void generateDropMoves(QVarLengthArray<Move>& moves, int pieceType) const;
431  virtual void generateMovesForPiece(QVarLengthArray<Move>& moves,
432  int pieceType,
433  int square) const = 0;
441  void generateHoppingMoves(int sourceSquare,
442  const QVarLengthArray<int>& offsets,
443  QVarLengthArray<Move>& moves) const;
451  void generateSlidingMoves(int sourceSquare,
452  const QVarLengthArray<int>& offsets,
453  QVarLengthArray<Move>& moves) const;
459  virtual bool isLegalPosition() = 0;
471  virtual bool vIsLegalMove(const Move& move);
476  virtual int captureType(const Move& move) const;
478  void xorKey(quint64 key);
483  bool moveExists(const Move& move) const;
485  bool canMove();
490  int arraySize() const;
492  Piece pieceAt(int square) const;
499  void setSquare(int square, Piece piece);
501  const Move& lastMove() const;
511  virtual int reserveType(int pieceType) const;
513  void addToReserve(const Piece& piece, int count = 1);
515  void removeFromReserve(const Piece& piece);
516 
517  private:
518  struct PieceData
519  {
520  QString name;
521  QString symbol;
522  unsigned movement;
523  QString representation;
524  };
525  struct MoveData
526  {
527  Move move;
528  quint64 key;
529  };
530  friend LIB_EXPORT QDebug operator<<(QDebug dbg, const Board* board);
531 
532  bool m_initialized;
533  int m_width;
534  int m_height;
535  Side m_side;
536  Side m_startingSide;
537  QString m_startingFen;
538  int m_maxPieceSymbolLength;
539  quint64 m_key;
540  Zobrist* m_zobrist;
541  QSharedPointer<Zobrist> m_sharedZobrist;
542  QVarLengthArray<PieceData> m_pieceData;
543  QVarLengthArray<Piece> m_squares;
544  QVector<MoveData> m_moveHistory;
545  QVector<int> m_reserve[2];
546 };
547 
548 
549 extern LIB_EXPORT QDebug operator<<(QDebug dbg, const Board* board);
550 
551 inline int Board::arraySize() const
552 {
553  return m_squares.size();
554 }
555 
556 inline Side Board::sideToMove() const
557 {
558  return m_side;
559 }
560 
561 inline Side Board::startingSide() const
562 {
563  return m_startingSide;
564 }
565 
567 {
568  return m_startingFen;
569 }
570 
571 inline quint64 Board::key() const
572 {
573  return m_key;
574 }
575 
576 inline void Board::xorKey(quint64 key)
577 {
578  m_key ^= key;
579 }
580 
581 inline Piece Board::pieceAt(int square) const
582 {
583  return m_squares[square];
584 }
585 
586 inline void Board::setSquare(int square, Piece piece)
587 {
588  Piece& old = m_squares[square];
589  if (old.isValid())
590  xorKey(m_zobrist->piece(old, square));
591  if (piece.isValid())
592  xorKey(m_zobrist->piece(piece, square));
593 
594  old = piece;
595 }
596 
597 inline int Board::plyCount() const
598 {
599  return m_moveHistory.size();
600 }
601 
602 inline const Move& Board::lastMove() const
603 {
604  return m_moveHistory.last().move;
605 }
606 
607 inline bool Board::pieceHasCaptureMovement(Piece piece, int square, unsigned movement) const
608 {
609  return Board::pieceHasMovement(piece.type(), square, movement);
610 }
611 
612 inline bool Board::pieceHasMovement(Piece piece, int square, unsigned movement) const
613 {
614  Q_UNUSED(square);
615 
616  return Board::pieceTypeHasMovement(piece.type(), movement);
617 }
618 
619 inline bool Board::pieceTypeHasMovement(int pieceType, unsigned movement) const
620 {
621  Q_ASSERT(pieceType != Piece::NoPiece);
622  Q_ASSERT(pieceType < m_pieceData.size());
623 
624  return (m_pieceData[pieceType].movement & movement);
625 }
626 
627 } // namespace Chess
628 #endif // BOARD_H
static const int NoPiece
Definition: piece.h:44
int plyCount() const
Definition: board.h:597
Side startingSide() const
Definition: board.h:561
const Move & lastMove() const
Definition: board.h:602
void setSquare(int square, Piece piece)
Definition: board.h:586
CoordinateSystem
Definition: board.h:62
MoveNotation
Definition: board.h:86
FenNotation
Definition: board.h:92
An internal chessboard class.
Definition: board.h:56
Standard Algebraic notation (SAN).
Definition: board.h:88
Piece pieceAt(const Square &square) const
Definition: board.cpp:109
Definition: board.h:98
Unsigned 64-bit values for generating zobrist position keys.
Definition: zobrist.h:36
QString startingFenString() const
Definition: board.h:566
virtual bool pieceHasMovement(Piece piece, int square, unsigned movement) const
Definition: board.h:612
A chess move independent of chess variant or opening book format.
Definition: genericmove.h:34
void xorKey(quint64 key)
Definition: board.h:576
quint64 key() const
Definition: board.h:571
int arraySize() const
Definition: board.h:551
Definition: boardscene.h:30
virtual bool pieceHasCaptureMovement(Piece piece, int square, unsigned movement) const
Definition: board.h:607
The side or color of a chess player.
Definition: side.h:35
bool isValid() const
Definition: piece.h:122
A chess piece.
Definition: piece.h:40
The result of a chess game.
Definition: result.h:34
virtual quint64 piece(const Piece &piece, int square) const
Definition: zobrist.cpp:106
Details of a board transition caused by a move.
Definition: boardtransition.h:40
A small and efficient chessmove class.
Definition: move.h:42
int type() const
Definition: piece.h:137
Side sideToMove() const
Definition: board.h:556
Definition: board.h:73
virtual bool pieceTypeHasMovement(int pieceType, unsigned movement) const
Definition: board.h:619
A generic chess square type consisting of a file and a rank.
Definition: square.h:33