File indexing completed on 2024-05-12 04:04:16

0001 /*
0002     This file is part of Knights, a chess board for KDE SC 4.
0003     SPDX-FileCopyrightText: 2009, 2010, 2011 Miha Čančula <miha@noughmad.eu>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #ifndef KCHESS_CHESSRULES_H
0009 #define KCHESS_CHESSRULES_H
0010 
0011 #include <rules/rules.h>
0012 #include <core/move.h>
0013 
0014 #include <QMap>
0015 #include <QStack>
0016 
0017 namespace Knights {
0018 class Move;
0019 class Pos;
0020 
0021 class ChessRules : public Rules {
0022 
0023 public:
0024     ChessRules();
0025     ~ChessRules() override;
0026 
0027     Color winner() override;
0028     bool hasLegalMoves ( Color color ) override;
0029     PieceDataMap startingPieces () override;
0030     QList<Move> legalMoves ( const Pos& pos ) override;
0031     void moveMade ( const Move& m ) override;
0032     Directions legalDirections ( PieceType type ) override;
0033 
0034     bool isAttacked ( const Pos& pos, Color color, Grid * grid = nullptr );
0035     bool isAttacking ( const Pos& attackingPos ) override;
0036 
0037     void checkSpecialFlags ( Move* move, Color color ) override;
0038     virtual void changeNotation ( Knights::Move* move, Move::Notation notation, Color color );
0039 
0040 private:
0041     struct MoveData {
0042         Move m;
0043         PieceType pieceType;
0044         Color color;
0045     };
0046 
0047     QMap<Direction, Pos> directions;
0048     QMap<Direction, Pos> lineDirs;
0049     QMap<Direction, Pos> diagDirs;
0050     QList<Pos> knightDirs;
0051     QStack<MoveData> moveHistory;
0052 
0053     QList<Move> movesInDirection ( const Pos& dir, const Pos& pos, int length = 8, bool attackYours = false, Grid* grid = nullptr );
0054     QList<Move> pawnMoves ( const Pos& pos );
0055     QList<Move> castlingMoves ( const Pos& pos );
0056     int length ( const Move& move );
0057     bool isPathClearForCastling ( const Pos& kingPos, const Pos& rookPos );
0058     QList<Move> legalAttackMoves ( const Pos& pos, Grid* grid = nullptr );
0059     bool isKingAttacked ( Color color, Grid* grid = nullptr );
0060 
0061     bool hasKingMoved ( Color color );
0062     bool hasRookMoved ( Color color, Move::CastlingSide side );
0063 
0064     QMap<Color, bool> kingMoved;
0065     QMap<Color, bool> kingRookMoved;
0066     QMap<Color, bool> queenRookMoved;
0067     QMap<Color, Pos> queenRookStartPos;
0068     QMap<Color, Pos> kingRookStartPos;
0069     QList<Move> m_enPassantMoves;
0070     QMap<Color, Pos> kingPos;
0071 };
0072 }
0073 
0074 #endif // KCHESS_CHESSRULES_H