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_RULES_H
0009 #define KCHESS_RULES_H
0010 
0011 #include "board.h"
0012 #include "core/piece.h"
0013 #include "knightsdebug.h"
0014 
0015 template<class Key, class T > class QMap;
0016 namespace Knights {
0017 class Pos;
0018 class Move;
0019 
0020 class Rules {
0021 public:
0022     enum Direction {
0023         None = 0,
0024         N = 0x001,
0025         S = 0x002,
0026         W = 0x004,
0027         E = 0x008,
0028         NW = 0x010,
0029         NE = 0x020,
0030         SW = 0x040,
0031         SE = 0x080,
0032         LineDirections = N | S | W | E,
0033         DiagDirections = NW | NE | SW | SE,
0034         AllDirections = LineDirections | DiagDirections
0035     };
0036 
0037     typedef QFlags<Direction> Directions;
0038 
0039     virtual ~Rules() = default;
0040 
0041     virtual void setGrid ( Grid* grid ) {
0042         qCDebug(LOG_KNIGHTS) << "Setting Grid";
0043         m_grid = grid;
0044     }
0045 
0046     virtual QList<Move> legalMoves ( const Pos& pos ) = 0;
0047     /**
0048       * @return The positions and types of the staring pieces of the color @a color
0049       */
0050     virtual PieceDataMap startingPieces () = 0;
0051 
0052     /**
0053       * Used to check whether any player has a winning position.
0054       * @return the color of the winner, or NoColor if no one has won yet
0055       * @note if the game is in a stalemate, NoColor is returned
0056       * @sa hasLegalMoves()
0057       */
0058     virtual Color winner() = 0;
0059 
0060     /**
0061       * Check if the player has no legal moves
0062       * Used for determining stalemates
0063       */
0064     virtual bool hasLegalMoves ( Color color ) = 0;
0065 
0066     /**
0067       * This function is more of a guideline for the board to determine whether a piece should be freely dragable.
0068       * It is currently not used anywhere in the game.
0069       */
0070     virtual Directions legalDirections ( PieceType type ) = 0;
0071 
0072     /**
0073      * Checks if a piece on @a attackingPos is attacking the opponent's king
0074      * @param pos the position to check
0075      * @return true if a piece is attacking the king, false otherwise
0076      */
0077     virtual bool isAttacking ( const Pos& attackingPos ) = 0;
0078 
0079     /**
0080       * Adds appropriate flags to the move.
0081       * Useful when processing moves from a computer engine, as they only specify the start and end pos,
0082       * and no other information
0083       * @param move a reference to the move with either @a from and @a to or its @a string already set.
0084       * @param color the color if the player who made this move
0085       */
0086     virtual void checkSpecialFlags ( Move* move, Color color ) = 0;
0087 
0088     /**
0089       * Called when a move has been made, either by the player or a computer opponent
0090       * Useful to update any game state the rules engine has saved
0091       * @param move The move which was made, with all information about it.
0092       */
0093     virtual void moveMade ( const Move& move ) = 0;
0094 
0095 protected:
0096     Grid *m_grid;
0097 };
0098 
0099 }
0100 
0101 #endif // KCHESS_RULES_H