Subversion Repositories Games.Chess Giants

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.     Texel - A UCI chess engine.
  3.     Copyright (C) 2012-2013  Peter Ă–sterlund, peterosterlund2@gmail.com
  4.  
  5.     This program 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.     This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18.  
  19. /*
  20.  * game.hpp
  21.  *
  22.  *  Created on: Feb 25, 2012
  23.  *      Author: petero
  24.  */
  25.  
  26. #ifndef GAME_HPP_
  27. #define GAME_HPP_
  28.  
  29. #include "move.hpp"
  30. #include "undoInfo.hpp"
  31. #include "position.hpp"
  32. #include "player.hpp"
  33.  
  34. #include <vector>
  35. #include <string>
  36. #include <memory>
  37.  
  38. class GameTest;
  39.  
  40. /**
  41.  * Handles a game between two players.
  42.  */
  43. class Game {
  44.     friend class GameTest;
  45. public:
  46.     Game(const std::shared_ptr<Player>& whitePlayer,
  47.          const std::shared_ptr<Player>& blackPlayer);
  48.     virtual ~Game();
  49.     Game(const Game& other) = delete;
  50.     Game& operator=(const Game& other) = delete;
  51.  
  52.     /**
  53.      * Update the game state according to move/command string from a player.
  54.      * @param str The move or command to process.
  55.      * @return True if str was understood, false otherwise.
  56.      */
  57.     bool processString(const std::string& str);
  58.  
  59.     std::string getGameStateString();
  60.  
  61.     const Position& getPos() const;
  62.  
  63.     /**
  64.      * Get the last played move, or null if no moves played yet.
  65.      */
  66.     Move getLastMove();
  67.  
  68.     enum GameState {
  69.         ALIVE,
  70.         WHITE_MATE,         // White mates
  71.         BLACK_MATE,         // Black mates
  72.         WHITE_STALEMATE,    // White is stalemated
  73.         BLACK_STALEMATE,    // Black is stalemated
  74.         DRAW_REP,           // Draw by 3-fold repetition
  75.         DRAW_50,            // Draw by 50 move rule
  76.         DRAW_NO_MATE,       // Draw by impossibility of check mate
  77.         DRAW_AGREE,         // Draw by agreement
  78.         RESIGN_WHITE,       // White resigns
  79.         RESIGN_BLACK        // Black resigns
  80.     };
  81.  
  82.     /**
  83.      * Get the current state of the game.
  84.      */
  85.     GameState getGameState();
  86.  
  87.     /**
  88.      * Check if a draw offer is available.
  89.      * @return True if the current player has the option to accept a draw offer.
  90.      */
  91.     bool haveDrawOffer();
  92.  
  93.     void getPosHistory(std::vector<std::string> ret);
  94.  
  95.     std::string getMoveListString(bool compressed);
  96.  
  97.     std::string getPGNResultString();
  98.  
  99.     /** Return a list of previous positions in this game, back to the last "zeroing" move. */
  100.     void getHistory(std::vector<Position>& posList);
  101.  
  102. protected:
  103.     /**
  104.      * Handle a special command.
  105.      * @param moveStr  The command to handle
  106.      * @return  True if command handled, false otherwise.
  107.      */
  108.     virtual bool handleCommand(const std::string& moveStr);
  109.  
  110.     /** Swap players around if needed to make the human player in control of the next move. */
  111.     void activateHumanPlayer();
  112.  
  113.  
  114.     std::shared_ptr<Player> whitePlayer;
  115.     std::shared_ptr<Player> blackPlayer;
  116.     std::vector<Move> moveList;
  117.     std::vector<UndoInfo> uiInfoList;
  118.     std::vector<bool> drawOfferList;
  119.     int currentMove;
  120.  
  121. private:
  122.     /** Print a list of all moves. */
  123.     void listMoves();
  124.  
  125.     bool handleDrawCmd(std::string drawCmd);
  126.  
  127.     bool handleBookCmd(const std::string& bookCmd);
  128.  
  129.     bool insufficientMaterial();
  130.  
  131.     /** Compute PerfT value. */
  132.     static U64 perfT(Position& pos, int depth);
  133.  
  134.     Position pos;
  135.  
  136.     std::string drawStateMoveStr; // Move required to claim DRAW_REP or DRAW_50
  137.     GameState resignState;
  138.     bool pendingDrawOffer;
  139.     GameState drawState;
  140.  
  141. };
  142.  
  143. inline
  144. Game::~Game() {
  145. }
  146.  
  147. inline const Position& Game::getPos() const {
  148.     return pos;
  149. }
  150.  
  151. #endif /* GAME_HPP_ */
  152.