Example #1
0
Value value_from_tt(Value v, int ply) {
  if(v >= value_mate_in(100))
    return v - ply;
  else if(v <= value_mated_in(100))
    return v + ply;
  else
    return v;
}
// probe_egtb() does the actual probing. On failure it returns VALUE_NONE.
Value probe_egtb(Position &pos, const int ply, const bool hard, const bool exact)
{
  // Conversion variables
  Bitboard occ;
  int count;

  // stockfish -> egtb
  int stm, epsquare, castling;
  unsigned int  ws[17], bs[17];
  unsigned char wp[17], bp[17];

  // egtb -> stockfish
  int tb_available;
  unsigned info = tb_UNKNOWN;
  unsigned pliestomate;

  // Prepare info for white (stockfish -> egtb)
  occ = pos.pieces_of_color(WHITE);
  count = 0;
  while (occ)
  {
      Square s = pop_1st_bit(&occ);
      ws[count] = s;
      wp[count] = (unsigned char) pos.type_of_piece_on(s);
      count++;
  }
  ws[count] = tb_NOSQUARE;
  wp[count] = tb_NOPIECE;

  // Prepare info for black (stockfish -> egtb)
  occ = pos.pieces_of_color(BLACK);
  count = 0;
  while (occ)
  {
      Square s = pop_1st_bit(&occ);
      bs[count] = s;
      bp[count] = (unsigned char) pos.type_of_piece_on(s);
      count++;
  }
  bs[count] = tb_NOSQUARE;
  bp[count] = tb_NOPIECE;

  // Prepare general info
  stm      = pos.side_to_move();
  epsquare = pos.ep_square();
  castling = tb_NOCASTLE;

  if (pos.can_castle(WHITE) || pos.can_castle(BLACK))
  {
      if (Chess960)
          return VALUE_NONE;

      if (pos.can_castle_kingside(WHITE))
          castling |= tb_WOO;
      if (pos.can_castle_queenside(WHITE))
          castling |= tb_WOOO;
      if (pos.can_castle_kingside(BLACK))
          castling |= tb_BOO;
      if (pos.can_castle_queenside(BLACK))
          castling |= tb_BOOO;
  }

  // Do the actual probing
  if (hard)
  {
      if (exact)
          tb_available = tb_probe_hard (stm, epsquare, castling, ws, bs, wp, bp, &info, &pliestomate);
      else
          tb_available = tb_probe_WDL_hard (stm, epsquare, castling, ws, bs, wp, bp, &info);
  }
  else
  {
      if (exact)
          tb_available = tb_probe_soft (stm, epsquare, castling, ws, bs, wp, bp, &info, &pliestomate);
      else
          tb_available = tb_probe_WDL_soft (stm, epsquare, castling, ws, bs, wp, bp, &info);
  }

  // Return probing info (if available)
  if (tb_available)
  {
    pos.set_tb_hits(pos.tb_hits() + 1);
    if (info == tb_DRAW)
        return VALUE_DRAW;
    else if (info == tb_WMATE && stm == tb_WHITE_TO_MOVE)
        return (exact ? value_mate_in(pliestomate+ply) : VALUE_KNOWN_WIN);
    else if (info == tb_BMATE && stm == tb_BLACK_TO_MOVE)
        return (exact ? value_mate_in(pliestomate+ply) : VALUE_KNOWN_WIN);
    else if (info == tb_WMATE && stm == tb_BLACK_TO_MOVE)
        return (exact ? value_mated_in(pliestomate+ply) : -VALUE_KNOWN_WIN);
    else if (info == tb_BMATE && stm == tb_WHITE_TO_MOVE)
        return (exact ? value_mated_in(pliestomate+ply) : -VALUE_KNOWN_WIN);
    else
        return VALUE_NONE;
  }
  else
      return VALUE_NONE;
}