<commit_before>#ifndef SEARCH_H
#define SEARCH_H

#include "board.h"
#include "defs.h"
#include "movegen.h"
#include "transptable.h"
#include <limits>

class Search {
public:
  Search(const Board&, bool=true);
  void perform(int);

  Move getBestMove();
  int getBestScore();

private:
  static const int INF = std::numeric_limits<int>::max();

  void _rootMax(const Board&, int);
  int _negaMax(const Board&, int, int, int);

  int _qSearch(const Board&, int=-INF, int=INF);

  void _orderMoves(MoveBoardList&);
  void _orderMovesQSearch(MoveBoardList&);

  bool _compareMovesTt(MoveBoard, MoveBoard);
  bool _compareMovesMvvLva(MoveBoard, MoveBoard);
  bool _compareMovesPromotionValue(MoveBoard, MoveBoard);

  void _logUciInfo(const MoveBoardList&, int, Move, int);

  int _getPieceValue(PieceType);

  MoveBoardList _getPv(const Board&, int);

  Board _board;

  bool _logUci;
  TranspTable _tt;
  Move _bestMove;
  int _bestScore;
};

#endif<commit_msg>Document Search class<commit_after>#ifndef SEARCH_H
#define SEARCH_H

#include "board.h"
#include "defs.h"
#include "movegen.h"
#include "transptable.h"
#include <limits>

/**
 * @brief Represents a search through a minmax tree.
 *
 * Searches are performed using recursive alpha-beta search, followed by a
 * quiescence search. The Search class contains no iterative deepening
 * functionality, that must be implemented by the caller via multiple calls
 * to perform().
 *
 */
class Search {
public:
  /**
   * @brief Construct a new Search with the given board.
   *
   * @param Board The board to search
   * @param bool If logUci is set, UCI info commands about the search will be printed
   * to standard output in real time.
   */
  Search(const Board&, bool=true);

  /**
   * @brief Perform a search to the given depth.
   * @param int Depth to search to
   */
  void perform(int);

  /**
   * @brief Returns the best move obtained through the last search performed.
   * @return The best move obtained through the last search
   */
  Move getBestMove();

  /**
   * @brief Return the best score obtained through the last search performed.
   * @return The best score obtained through the last search performed.
   */
  int getBestScore();

private:
  static const int INF = std::numeric_limits<int>::max();

  /**
   * @brief Initial board being used in this search.
   */
  Board _board;

  /**
   * @brief True if UCI output should be logged.
   */
  bool _logUci;

  /**
   * @brief Transposition Table used while searching.
   */
  TranspTable _tt;

  /**
   * @brief Best move found on last search.
   */
  Move _bestMove;

  /**
   * @brief Score corresponding to _bestMove
   */
  int _bestScore;

  /**
   * @brief Root negamax function.
   *
   * Starts performing a search to the given depth using recursive minimax
   * with alpha-beta pruning.
   *
   * @param Board Board to search through
   * @param int Maximum depth to search to
   */
  void _rootMax(const Board&, int);

  /**
   * @brief non root negamax function, should only be called by _rootMax()
   *
   *
   * @param  Board Board to search
   * @param  int   Plys remaining to search
   * @param  int   Alpha value
   * @param  int   Beta value
   * @return The score of the given board
   */
  int _negaMax(const Board&, int, int, int);

  /**
   * @brief Performs a quiescence search
   *
   * _qSearch only takes into account captures (checks, promotions are not
   * considered)
   *
   * @param  Board  Board to perform a quiescence search on
   * @param  int    Alpha value
   * @param  int    Beta value
   * @return        The score of the given board
   */
  int _qSearch(const Board&, int=-INF, int=INF);

  /**
   * @brief Orders a MoveBoardList for negamax search.
   *
   * Move ordering is as follows:
   * 1. Best moves from Transposition Table (PV is implicitly placed first by this)
   * 2. Captures sorted by MVV/LVA
   * 3. Promotions sorted by value
   * 4. All other moves
   *
   * @param MoveBoardList MoveBoardList to sort
   */
  void _orderMoves(MoveBoardList&);

  /**
   * @brief Orders a MoveBoardList for quiescence search
   *
   * Places captures first sorted by MVV/LVA and non captures after.
   *
   * @param MoveBoardList MoveBoardList to sort
   */
  void _orderMovesQSearch(MoveBoardList&);

  /**
   * @brief Transposition table comparison function for moves
   *
   * Returns true if a is a worse better than b according to the transposition table.
   *
   * If a or b are not in the transposition table, returns false or true respectively.
   * If a and b are both not in the transposition table, returns false.
   *
   * @param  MoveBoard First MoveBoard to compare
   * @param  MoveBoard Second MoveBoard to compare
   * @return true if a is a better move than b according to the transposition table.
   */
  bool _compareMovesTt(MoveBoard, MoveBoard);

  /**
   * @brief MVV/LVA comparison function for moves
   *
   * Returns true if a is a better move than b according to MVV/LVA.
   *
   * @param  MoveBoard First MoveBoard to compare
   * @param  MoveBoard Second MoveBoard to compare
   * @return true if a is a better move than b according to MVV/LVA, false otherwise
   */
  bool _compareMovesMvvLva(MoveBoard, MoveBoard);

  /**
   * @brief Promotion comparison function for moves.
   *
   * Returns true if a is a better promotion than b according to the value of the
   * promotion piece.
   *
   * @param  MoveBoard First moveboard to compare
   * @param  MoveBoard Second moveboard to compare.
   * @return true if a is a better move than b according to promotion piece value.
   */
  bool _compareMovesPromotionValue(MoveBoard, MoveBoard);

  /**
   * @brief Log info about a search according to the UCI protocol.
   *
   * @param MoveBoardList MoveBoardList representing the Principal Variation (first moves at index 0)
   * @param int           Depth of search
   * @param Move          Best move obtained from search
   * @param int           Score corresponding to the best move
   */
  void _logUciInfo(const MoveBoardList&, int, Move, int);

  /**
   * @brief Returns the value of a pieceType that can be used for comparisons.
   *
   * @param  PieceType PieceType to get value for
   * @return Value of the given piece type.
   */
  int _getPieceValue(PieceType);

  /**
   * @brief Extracts and returns the Principal Variation of the last search from the transposition table
   *
   * Can only be called after a search has been called via _rootMax().
   *
   * @param  Board  Initial board used in the last search
   * @param  int    Depth of the last search
   * @return        MoveBoardList representing the Principal Variation of the last search
   */
  MoveBoardList _getPv(const Board&, int);
};

#endif
{'fraction_non_alphanumeric': 0.08323406789755806, 'fraction_numerical': 0.0007444907683144729, 'mean_word_length': 3.095731707317073, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4686661', 'n_tokens_mistral': 2082, 'n_tokens_neox': 2006, 'n_words': 803}