Ejemplo n.º 1
0
  private int alphaBeta(int alpha, int beta, int depthleft) {
    int ttAplha = alpha;
    nodeCount++;

    TableEntry tableEntry = transpositionTable.get(pos.key);
    if (tableEntry != null) {
      if (tableEntry.getDepth() >= depthleft) {
        int type = tableEntry.getType();
        int eval = tableEntry.getEval();
        if (type == TableEntry.EXACT) {
          return eval;
        } else if (type == TableEntry.LOWER) {
          alpha = Math.max(alpha, eval);
        } else if (type == TableEntry.UPPER) {
          beta = Math.min(beta, eval);
        }
        if (alpha >= beta) {
          return eval;
        }
      }
    }

    if (depthleft == 0) {
      return Evaluator.evaluate(pos);
    }

    Move[] moves = new Move[MAX_NUMBER_OF_MOVES];
    int numberOfMoves = moveGen.findLegalMoves(pos, moves);
    // orderMoves(moves, numberOfMoves);
    int best = -10000;
    Move bestMove = null;
    for (int i = 0; i < numberOfMoves; i++) {
      final byte result = moveGen.makeMove(pos, moves[i]);
      if (!PerftStat.isLegalPosition(result)) {
        moveGen.unMakeMove(pos, moves[i]);
        return Integer.MIN_VALUE;
      } else if (!PerftStat.isLegalMove(result)) {
        moveGen.unMakeMove(pos, moves[i]);
        continue;
      }
      int score = -alphaBeta(-beta, -alpha, depthleft - 1);
      moveGen.unMakeMove(pos, moves[i]);
      if (score > best) {
        best = score;
        bestMove = moves[i];
      }
      if (score > alpha) {
        alpha = score;
      }
      if (alpha >= beta) {
        break;
      }
    }
    transpositionTable.store(pos.key, depthleft, best, ttAplha, beta, age, bestMove);
    return best;
  }