Cute Chess  0.1
board.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 BOARD_H
19 #define BOARD_H
20 
21 #include <QString>
22 #include <QVector>
23 #include <QVarLengthArray>
24 #include <QSharedPointer>
25 #include <QDebug>
26 #include <QCoreApplication>
27 #include "square.h"
28 #include "piece.h"
29 #include "move.h"
30 #include "genericmove.h"
31 #include "zobrist.h"
32 #include "result.h"
33 class QStringList;
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;
136  virtual QList<Piece> reservePieceTypes() const;
138  virtual CoordinateSystem coordinateSystem() const;
140  virtual int width() const = 0;
142  virtual int height() const = 0;
144  virtual QString defaultFenString() const = 0;
146  quint64 key() const;
152  void initialize();
153 
155  bool isValidSquare(const Square& square) const;
156 
160  QStringList pieceList(Side side) const;
165  QString fenString(FenNotation notation = XFen) const;
170  QString startingFenString() const;
179  bool setFenString(const QString& fen);
184  void reset();
185 
190  virtual Side upperCaseSide() const;
192  Side sideToMove() const;
194  Side startingSide() const;
196  Piece pieceAt(const Square& square) const;
198  int plyCount() const;
203  int repeatCount() const;
209  virtual int reversibleMoveCount() const;
216  int reserveCount(Piece piece) const;
218  QString pieceSymbol(Piece piece) const;
220  Piece pieceFromSymbol(const QString& pieceSymbol) const;
222  QString pieceString(int pieceType) const;
224  QString representation(Piece piece) const;
225 
234  void makeMove(const Move& move, BoardTransition* transition = nullptr);
236  void undoMove();
237 
244  QString moveString(const Move& move, MoveNotation notation);
253  Move moveFromString(const QString& str);
260  Move moveFromGenericMove(const GenericMove& move) const;
267  GenericMove genericMove(const Move& move) const;
268 
270  bool isLegalMove(const Move& move);
275  bool isRepetition(const Move& move);
277  QVector<Move> legalMoves();
282  virtual Result result() = 0;
292  virtual Result tablebaseResult(unsigned int* dtm = nullptr) const;
293 
294  protected:
301  virtual void vInitialize() = 0;
302 
317  void setPieceType(int type,
318  const QString& name,
319  const QString& symbol,
320  unsigned movement = 0,
321  const QString & gsymbol = QString());
323  bool pieceHasMovement(int pieceType, unsigned movement) const;
324 
336  virtual void vMakeMove(const Move& move,
337  BoardTransition* transition) = 0;
347  virtual void vUndoMove(const Move& move) = 0;
348 
350  Square chessSquare(int index) const;
352  Square chessSquare(const QString& str) const;
354  int squareIndex(const Square& square) const;
356  int squareIndex(const QString& str) const;
358  QString squareString(int index) const;
360  QString squareString(const Square& square) const;
361 
366  virtual QString lanMoveString(const Move& move);
373  virtual QString sanMoveString(const Move& move) = 0;
375  virtual Move moveFromLanString(const QString& str);
377  virtual Move moveFromSanString(const QString& str) = 0;
379  virtual int maxPieceSymbolLength() const;
380 
388  virtual QString vFenString(FenNotation notation) const = 0;
396  virtual bool vSetFenString(const QStringList& fen) = 0;
397 
405  void generateMoves(QVarLengthArray<Move>& moves,
406  int pieceType = Piece::NoPiece) const;
414  void generateDropMoves(QVarLengthArray<Move>& moves, int pieceType) const;
422  virtual void generateMovesForPiece(QVarLengthArray<Move>& moves,
423  int pieceType,
424  int square) const = 0;
432  void generateHoppingMoves(int sourceSquare,
433  const QVarLengthArray<int>& offsets,
434  QVarLengthArray<Move>& moves) const;
442  void generateSlidingMoves(int sourceSquare,
443  const QVarLengthArray<int>& offsets,
444  QVarLengthArray<Move>& moves) const;
450  virtual bool isLegalPosition() = 0;
462  virtual bool vIsLegalMove(const Move& move);
467  virtual int captureType(const Move& move) const;
469  void xorKey(quint64 key);
474  bool moveExists(const Move& move) const;
476  bool canMove();
481  int arraySize() const;
483  Piece pieceAt(int square) const;
490  void setSquare(int square, Piece piece);
492  const Move& lastMove() const;
502  virtual int reserveType(int pieceType) const;
504  void addToReserve(const Piece& piece, int count = 1);
506  void removeFromReserve(const Piece& piece);
507 
508  private:
509  struct PieceData
510  {
511  QString name;
512  QString symbol;
513  unsigned movement;
514  QString representation;
515  };
516  struct MoveData
517  {
518  Move move;
519  quint64 key;
520  };
521  friend LIB_EXPORT QDebug operator<<(QDebug dbg, const Board* board);
522 
523  bool m_initialized;
524  int m_width;
525  int m_height;
526  Side m_side;
527  Side m_startingSide;
528  QString m_startingFen;
529  int m_maxPieceSymbolLength;
530  quint64 m_key;
531  Zobrist* m_zobrist;
532  QSharedPointer<Zobrist> m_sharedZobrist;
533  QVarLengthArray<PieceData> m_pieceData;
534  QVarLengthArray<Piece> m_squares;
535  QVector<MoveData> m_moveHistory;
536  QVector<int> m_reserve[2];
537 };
538 
539 
540 extern LIB_EXPORT QDebug operator<<(QDebug dbg, const Board* board);
541 
542 inline int Board::arraySize() const
543 {
544  return m_squares.size();
545 }
546 
547 inline Side Board::sideToMove() const
548 {
549  return m_side;
550 }
551 
552 inline Side Board::startingSide() const
553 {
554  return m_startingSide;
555 }
556 
558 {
559  return m_startingFen;
560 }
561 
562 inline quint64 Board::key() const
563 {
564  return m_key;
565 }
566 
567 inline void Board::xorKey(quint64 key)
568 {
569  m_key ^= key;
570 }
571 
572 inline Piece Board::pieceAt(int square) const
573 {
574  return m_squares[square];
575 }
576 
577 inline void Board::setSquare(int square, Piece piece)
578 {
579  Piece& old = m_squares[square];
580  if (old.isValid())
581  xorKey(m_zobrist->piece(old, square));
582  if (piece.isValid())
583  xorKey(m_zobrist->piece(piece, square));
584 
585  old = piece;
586 }
587 
588 inline int Board::plyCount() const
589 {
590  return m_moveHistory.size();
591 }
592 
593 inline const Move& Board::lastMove() const
594 {
595  return m_moveHistory.last().move;
596 }
597 
598 inline bool Board::pieceHasMovement(int pieceType, unsigned movement) const
599 {
600  Q_ASSERT(pieceType != Piece::NoPiece);
601  Q_ASSERT(pieceType < m_pieceData.size());
602 
603  return (m_pieceData[pieceType].movement & movement);
604 }
605 
606 } // namespace Chess
607 #endif // BOARD_H
static const int NoPiece
Definition: piece.h:43
int plyCount() const
Definition: board.h:588
Side startingSide() const
Definition: board.h:552
const Move & lastMove() const
Definition: board.h:593
void setSquare(int square, Piece piece)
Definition: board.h:577
bool pieceHasMovement(int pieceType, unsigned movement) const
Definition: board.h:598
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:99
Definition: board.h:98
Unsigned 64-bit values for generating zobrist position keys.
Definition: zobrist.h:35
QString startingFenString() const
Definition: board.h:557
A chess move independent of chess variant or opening book format.
Definition: genericmove.h:33
void xorKey(quint64 key)
Definition: board.h:567
quint64 key() const
Definition: board.h:562
int arraySize() const
Definition: board.h:542
Definition: boardscene.h:28
The side or color of a chess player.
Definition: side.h:34
bool isValid() const
Definition: piece.h:121
A chess piece.
Definition: piece.h:39
The result of a chess game.
Definition: result.h:33
virtual quint64 piece(const Piece &piece, int square) const
Definition: zobrist.cpp:105
Details of a board transition caused by a move.
Definition: boardtransition.h:39
A small and efficient chessmove class.
Definition: move.h:41
Side sideToMove() const
Definition: board.h:547
Definition: board.h:73
A generic chess square type consisting of a file and a rank.
Definition: square.h:32