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 class QStringList;
35 
36 
37 namespace Chess {
38 
39 class BoardTransition;
40 
57 class LIB_EXPORT Board
58 {
59  Q_DECLARE_TR_FUNCTIONS(Board)
60 
61  public:
64  {
84  InvertedCoordinates
85  };
88  {
90  LongAlgebraic
91  };
94  {
101  ShredderFen
102  };
103 
111  Board(Zobrist* zobrist);
113  virtual ~Board();
115  virtual Board* copy() const = 0;
116 
118  virtual QString variant() const = 0;
123  virtual bool isRandomVariant() const;
130  virtual bool variantHasDrops() const;
135  virtual bool variantHasWallSquares() const;
142  virtual QList<Piece> reservePieceTypes() const;
144  virtual CoordinateSystem coordinateSystem() const;
146  virtual int width() const = 0;
148  virtual int height() const = 0;
150  virtual QString defaultFenString() const = 0;
152  quint64 key() const;
158  void initialize();
159 
161  bool isValidSquare(const Square& square) const;
162 
166  QStringList pieceList(Side side) const;
171  QString fenString(FenNotation notation = XFen) const;
176  QString startingFenString() const;
185  bool setFenString(const QString& fen);
190  void reset();
191 
196  virtual Side upperCaseSide() const;
198  Side sideToMove() const;
200  Side startingSide() const;
202  Piece pieceAt(const Square& square) const;
204  int plyCount() const;
209  int repeatCount() const;
215  virtual int reversibleMoveCount() const;
222  int reserveCount(Piece piece) const;
224  QString pieceSymbol(Piece piece) const;
226  Piece pieceFromSymbol(const QString& pieceSymbol) const;
228  QString pieceString(int pieceType) const;
230  QString representation(Piece piece) const;
231 
240  void makeMove(const Move& move, BoardTransition* transition = nullptr);
242  void undoMove();
243 
250  QString moveString(const Move& move, MoveNotation notation);
259  Move moveFromString(const QString& str);
266  Move moveFromGenericMove(const GenericMove& move) const;
273  GenericMove genericMove(const Move& move) const;
274 
276  bool isLegalMove(const Move& move);
281  bool isRepetition(const Move& move);
283  QVector<Move> legalMoves();
288  virtual Result result() = 0;
298  virtual Result tablebaseResult(unsigned int* dtm = nullptr) const;
299 
300  protected:
307  virtual void vInitialize() = 0;
308 
323  void setPieceType(int type,
324  const QString& name,
325  const QString& symbol,
326  unsigned movement = 0,
327  const QString & gsymbol = QString());
329  bool pieceHasMovement(int pieceType, unsigned movement) const;
330 
342  virtual void vMakeMove(const Move& move,
343  BoardTransition* transition) = 0;
353  virtual void vUndoMove(const Move& move) = 0;
354 
356  Square chessSquare(int index) const;
358  Square chessSquare(const QString& str) const;
360  int squareIndex(const Square& square) const;
362  int squareIndex(const QString& str) const;
364  QString squareString(int index) const;
366  QString squareString(const Square& square) const;
367 
372  virtual QString lanMoveString(const Move& move);
379  virtual QString sanMoveString(const Move& move) = 0;
381  virtual Move moveFromLanString(const QString& str);
383  virtual Move moveFromSanString(const QString& str) = 0;
385  virtual int maxPieceSymbolLength() const;
386 
394  virtual QString vFenString(FenNotation notation) const = 0;
402  virtual bool vSetFenString(const QStringList& fen) = 0;
403 
411  void generateMoves(QVarLengthArray<Move>& moves,
412  int pieceType = Piece::NoPiece) const;
420  void generateDropMoves(QVarLengthArray<Move>& moves, int pieceType) const;
428  virtual void generateMovesForPiece(QVarLengthArray<Move>& moves,
429  int pieceType,
430  int square) const = 0;
438  void generateHoppingMoves(int sourceSquare,
439  const QVarLengthArray<int>& offsets,
440  QVarLengthArray<Move>& moves) const;
448  void generateSlidingMoves(int sourceSquare,
449  const QVarLengthArray<int>& offsets,
450  QVarLengthArray<Move>& moves) const;
456  virtual bool isLegalPosition() = 0;
468  virtual bool vIsLegalMove(const Move& move);
473  virtual int captureType(const Move& move) const;
475  void xorKey(quint64 key);
480  bool moveExists(const Move& move) const;
482  bool canMove();
487  int arraySize() const;
489  Piece pieceAt(int square) const;
496  void setSquare(int square, Piece piece);
498  const Move& lastMove() const;
508  virtual int reserveType(int pieceType) const;
510  void addToReserve(const Piece& piece, int count = 1);
512  void removeFromReserve(const Piece& piece);
513 
514  private:
515  struct PieceData
516  {
517  QString name;
518  QString symbol;
519  unsigned movement;
520  QString representation;
521  };
522  struct MoveData
523  {
524  Move move;
525  quint64 key;
526  };
527  friend LIB_EXPORT QDebug operator<<(QDebug dbg, const Board* board);
528 
529  bool m_initialized;
530  int m_width;
531  int m_height;
532  Side m_side;
533  Side m_startingSide;
534  QString m_startingFen;
535  int m_maxPieceSymbolLength;
536  quint64 m_key;
537  Zobrist* m_zobrist;
538  QSharedPointer<Zobrist> m_sharedZobrist;
539  QVarLengthArray<PieceData> m_pieceData;
540  QVarLengthArray<Piece> m_squares;
541  QVector<MoveData> m_moveHistory;
542  QVector<int> m_reserve[2];
543 };
544 
545 
546 extern LIB_EXPORT QDebug operator<<(QDebug dbg, const Board* board);
547 
548 inline int Board::arraySize() const
549 {
550  return m_squares.size();
551 }
552 
553 inline Side Board::sideToMove() const
554 {
555  return m_side;
556 }
557 
558 inline Side Board::startingSide() const
559 {
560  return m_startingSide;
561 }
562 
564 {
565  return m_startingFen;
566 }
567 
568 inline quint64 Board::key() const
569 {
570  return m_key;
571 }
572 
573 inline void Board::xorKey(quint64 key)
574 {
575  m_key ^= key;
576 }
577 
578 inline Piece Board::pieceAt(int square) const
579 {
580  return m_squares[square];
581 }
582 
583 inline void Board::setSquare(int square, Piece piece)
584 {
585  Piece& old = m_squares[square];
586  if (old.isValid())
587  xorKey(m_zobrist->piece(old, square));
588  if (piece.isValid())
589  xorKey(m_zobrist->piece(piece, square));
590 
591  old = piece;
592 }
593 
594 inline int Board::plyCount() const
595 {
596  return m_moveHistory.size();
597 }
598 
599 inline const Move& Board::lastMove() const
600 {
601  return m_moveHistory.last().move;
602 }
603 
604 inline bool Board::pieceHasMovement(int pieceType, unsigned movement) const
605 {
606  Q_ASSERT(pieceType != Piece::NoPiece);
607  Q_ASSERT(pieceType < m_pieceData.size());
608 
609  return (m_pieceData[pieceType].movement & movement);
610 }
611 
612 } // namespace Chess
613 #endif // BOARD_H
static const int NoPiece
Definition: piece.h:44
int plyCount() const
Definition: board.h:594
Side startingSide() const
Definition: board.h:558
const Move & lastMove() const
Definition: board.h:599
void setSquare(int square, Piece piece)
Definition: board.h:583
bool pieceHasMovement(int pieceType, unsigned movement) const
Definition: board.h:604
CoordinateSystem
Definition: board.h:63
MoveNotation
Definition: board.h:87
FenNotation
Definition: board.h:93
An internal chessboard class.
Definition: board.h:57
Standard Algebraic notation (SAN).
Definition: board.h:89
Piece pieceAt(const Square &square) const
Definition: board.cpp:105
Definition: board.h:99
Unsigned 64-bit values for generating zobrist position keys.
Definition: zobrist.h:36
QString startingFenString() const
Definition: board.h:563
A chess move independent of chess variant or opening book format.
Definition: genericmove.h:34
void xorKey(quint64 key)
Definition: board.h:573
quint64 key() const
Definition: board.h:568
int arraySize() const
Definition: board.h:548
Definition: boardscene.h:29
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
Side sideToMove() const
Definition: board.h:553
Definition: board.h:74
A generic chess square type consisting of a file and a rank.
Definition: square.h:33