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;
139  virtual bool variantHasOptionalPromotions() const;
144  virtual bool variantHasWallSquares() const;
151  virtual QList<Piece> reservePieceTypes() const;
153  virtual CoordinateSystem coordinateSystem() const;
155  virtual int width() const = 0;
157  virtual int height() const = 0;
159  virtual QString defaultFenString() const = 0;
161  quint64 key() const;
167  void initialize();
168 
170  bool isValidSquare(const Square& square) const;
171 
175  QStringList pieceList(Side side) const;
180  QString fenString(FenNotation notation = XFen) const;
185  QString startingFenString() const;
194  bool setFenString(const QString& fen);
199  void reset();
200 
205  virtual Side upperCaseSide() const;
207  Side sideToMove() const;
209  Side startingSide() const;
211  Piece pieceAt(const Square& square) const;
213  int plyCount() const;
218  int repeatCount() const;
224  virtual int reversibleMoveCount() const;
231  int reserveCount(Piece piece) const;
233  QString pieceSymbol(Piece piece) const;
235  Piece pieceFromSymbol(const QString& pieceSymbol) const;
237  QString pieceString(int pieceType) const;
239  QString representation(Piece piece) const;
240 
249  void makeMove(const Move& move, BoardTransition* transition = nullptr);
251  void undoMove();
252 
259  QString moveString(const Move& move, MoveNotation notation);
268  Move moveFromString(const QString& str);
275  Move moveFromGenericMove(const GenericMove& move) const;
282  GenericMove genericMove(const Move& move) const;
283 
285  bool isLegalMove(const Move& move);
290  bool isRepetition(const Move& move);
292  QVector<Move> legalMoves();
297  virtual Result result() = 0;
307  virtual Result tablebaseResult(unsigned int* dtm = nullptr) const;
308 
309  protected:
316  virtual void vInitialize() = 0;
317 
332  void setPieceType(int type,
333  const QString& name,
334  const QString& symbol,
335  unsigned movement = 0,
336  const QString & gsymbol = QString());
338  bool pieceHasMovement(int pieceType, unsigned movement) const;
339 
351  virtual void vMakeMove(const Move& move,
352  BoardTransition* transition) = 0;
362  virtual void vUndoMove(const Move& move) = 0;
363 
365  Square chessSquare(int index) const;
367  Square chessSquare(const QString& str) const;
369  int squareIndex(const Square& square) const;
371  int squareIndex(const QString& str) const;
373  QString squareString(int index) const;
375  QString squareString(const Square& square) const;
376 
381  virtual QString lanMoveString(const Move& move);
388  virtual QString sanMoveString(const Move& move) = 0;
390  virtual Move moveFromLanString(const QString& str);
392  virtual Move moveFromSanString(const QString& str) = 0;
394  virtual int maxPieceSymbolLength() const;
395 
403  virtual QString vFenString(FenNotation notation) const = 0;
411  virtual bool vSetFenString(const QStringList& fen) = 0;
412 
420  void generateMoves(QVarLengthArray<Move>& moves,
421  int pieceType = Piece::NoPiece) const;
429  void generateDropMoves(QVarLengthArray<Move>& moves, int pieceType) const;
437  virtual void generateMovesForPiece(QVarLengthArray<Move>& moves,
438  int pieceType,
439  int square) const = 0;
447  void generateHoppingMoves(int sourceSquare,
448  const QVarLengthArray<int>& offsets,
449  QVarLengthArray<Move>& moves) const;
457  void generateSlidingMoves(int sourceSquare,
458  const QVarLengthArray<int>& offsets,
459  QVarLengthArray<Move>& moves) const;
465  virtual bool isLegalPosition() = 0;
477  virtual bool vIsLegalMove(const Move& move);
482  virtual int captureType(const Move& move) const;
484  void xorKey(quint64 key);
489  bool moveExists(const Move& move) const;
491  bool canMove();
496  int arraySize() const;
498  Piece pieceAt(int square) const;
505  void setSquare(int square, Piece piece);
507  const Move& lastMove() const;
517  virtual int reserveType(int pieceType) const;
519  void addToReserve(const Piece& piece, int count = 1);
521  void removeFromReserve(const Piece& piece);
522 
523  private:
524  struct PieceData
525  {
526  QString name;
527  QString symbol;
528  unsigned movement;
529  QString representation;
530  };
531  struct MoveData
532  {
533  Move move;
534  quint64 key;
535  };
536  friend LIB_EXPORT QDebug operator<<(QDebug dbg, const Board* board);
537 
538  bool m_initialized;
539  int m_width;
540  int m_height;
541  Side m_side;
542  Side m_startingSide;
543  QString m_startingFen;
544  int m_maxPieceSymbolLength;
545  quint64 m_key;
546  Zobrist* m_zobrist;
547  QSharedPointer<Zobrist> m_sharedZobrist;
548  QVarLengthArray<PieceData> m_pieceData;
549  QVarLengthArray<Piece> m_squares;
550  QVector<MoveData> m_moveHistory;
551  QVector<int> m_reserve[2];
552 };
553 
554 
555 extern LIB_EXPORT QDebug operator<<(QDebug dbg, const Board* board);
556 
557 inline int Board::arraySize() const
558 {
559  return m_squares.size();
560 }
561 
562 inline Side Board::sideToMove() const
563 {
564  return m_side;
565 }
566 
567 inline Side Board::startingSide() const
568 {
569  return m_startingSide;
570 }
571 
573 {
574  return m_startingFen;
575 }
576 
577 inline quint64 Board::key() const
578 {
579  return m_key;
580 }
581 
582 inline void Board::xorKey(quint64 key)
583 {
584  m_key ^= key;
585 }
586 
587 inline Piece Board::pieceAt(int square) const
588 {
589  return m_squares[square];
590 }
591 
592 inline void Board::setSquare(int square, Piece piece)
593 {
594  Piece& old = m_squares[square];
595  if (old.isValid())
596  xorKey(m_zobrist->piece(old, square));
597  if (piece.isValid())
598  xorKey(m_zobrist->piece(piece, square));
599 
600  old = piece;
601 }
602 
603 inline int Board::plyCount() const
604 {
605  return m_moveHistory.size();
606 }
607 
608 inline const Move& Board::lastMove() const
609 {
610  return m_moveHistory.last().move;
611 }
612 
613 inline bool Board::pieceHasMovement(int pieceType, unsigned movement) const
614 {
615  Q_ASSERT(pieceType != Piece::NoPiece);
616  Q_ASSERT(pieceType < m_pieceData.size());
617 
618  return (m_pieceData[pieceType].movement & movement);
619 }
620 
621 } // namespace Chess
622 #endif // BOARD_H
static const int NoPiece
Definition: piece.h:44
bool pieceHasMovement(int pieceType, unsigned movement) const
Definition: board.h:613
bool isValid() const
Definition: piece.h:122
const Move & lastMove() const
Definition: board.h:608
QString startingFenString() const
Definition: board.h:572
void setSquare(int square, Piece piece)
Definition: board.h:592
CoordinateSystem
Definition: board.h:63
MoveNotation
Definition: board.h:87
FenNotation
Definition: board.h:93
quint64 key() const
Definition: board.h:577
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:110
Definition: board.h:99
Unsigned 64-bit values for generating zobrist position keys.
Definition: zobrist.h:36
int arraySize() const
Definition: board.h:557
A chess move independent of chess variant or opening book format.
Definition: genericmove.h:34
void xorKey(quint64 key)
Definition: board.h:582
Definition: boardscene.h:29
Side sideToMove() const
Definition: board.h:562
The side or color of a chess player.
Definition: side.h:35
A chess piece.
Definition: piece.h:40
The result of a chess game.
Definition: result.h:34
Details of a board transition caused by a move.
Definition: boardtransition.h:40
A small and efficient chessmove class.
Definition: move.h:42
Definition: board.h:74
int plyCount() const
Definition: board.h:603
A generic chess square type consisting of a file and a rank.
Definition: square.h:33
Side startingSide() const
Definition: board.h:567