protected MoveValuePair handleTerminal(Position position, boolean maxTurn, int alpha, int beta) throws IllegalMoveException { MoveValuePair finalMove = new MoveValuePair(); if (position.isTerminal() && position.isMate()) { this.terminalFound = position.isTerminal(); finalMove.eval = (maxTurn ? BE_MATED : MATE); } else if (position.isTerminal() && position.isStaleMate()) finalMove.eval = 0; else { finalMove.eval = quiescence(position, alpha, beta, !maxTurn); } // System.out.print(finalMove.eval + " "); return finalMove; }
@Override protected MoveValuePair ABMaxMinValue( Position position, int depth, int alpha, int beta, boolean maxTurn) throws IllegalMoveException { if (depth <= 0 || position.isTerminal()) { return handleTerminal(position, maxTurn, alpha, beta); } else { MoveValuePair bestMove = new MoveValuePair(); LinkedList<MoveValuePair> sortedMoves = getSortedMoves(position, maxTurn); for (MoveValuePair movepair : sortedMoves) { short move = movepair.move; // collect values from further moves // get and update transposition table if possible position.doMove(move); if (this.p2tte.containsKey(position.getHashCode()) && (this.p2tte.get(position.getHashCode()).depth >= depth)) { // System.out.println("trans table at level: " + depth); TransTableEntry tte = p2tte.get(position.getHashCode()); bestMove.updateMinMax(move, tte.eval, maxTurn); } else { // recursive method MoveValuePair childMove = ABMaxMinValue(position, depth - 1, alpha, beta, !maxTurn); bestMove.updateMinMax(move, childMove.eval, maxTurn); p2tte.put(position.getHashCode(), new TransTableEntry(childMove.eval, depth, move)); } position.undoMove(); // update the alpha beta boundary if (maxTurn) alpha = bestMove.eval; else beta = bestMove.eval; // prune the subtree if needed if (alpha >= beta) return bestMove; } return bestMove; } }