@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; } }
// protected MoveValuePair handleTerminal(Position position, boolean maxTurn) { // 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 = (int) ( (position.getMaterial() + position.getDomination())); // } //// System.out.print(finalMove.eval + " "); // return finalMove; // } protected LinkedList<MoveValuePair> getCapturingSortedMoves(Position position, boolean maxTurn) throws IllegalMoveException { LinkedList<MoveValuePair> sortedMoves = new LinkedList<MoveValuePair>(); short[] moves = position.getAllCapturingMoves(); MoveValuePair theMove = null; ASCENDING = maxTurn ? false : true; for (short move : moves) { position.doMove(move); if (p2tte.containsKey(position.getHashCode())) { theMove = new MoveValuePair(move, p2tte.get(position.getHashCode()).eval); } else { // for max turn, I assign worst values those unvisited positions // theMove = new MoveValuePair(move, maxTurn ? BE_MATED : MATE); int eval = (int) ((maxTurn ? -1 : 1) * (position.getMaterial() + position.getDomination())); theMove = new MoveValuePair(move, eval); } position.undoMove(); sortedMoves.add(theMove); } Collections.sort( sortedMoves, new Comparator<MoveValuePair>() { @Override public int compare(MoveValuePair c1, MoveValuePair c2) { // System.out.println(c1.eval + " vs " + c2.eval); return (int) ((ASCENDING ? 1 : -1) * Math.signum(c1.eval - c2.eval)); // use your logic } }); return sortedMoves; }
/** * Returns whether the given position occurs in the main line of this game. * * @param position the position to look for, must not be null * @return whether the given position occurs in the main line of this game */ public boolean containsPosition(ImmutablePosition position) { boolean res = false; int index = getCurNode(); gotoStart(true); for (; ; ) { if (m_position.getHashCode() == position.getHashCode()) { res = true; break; } if (!hasNextMove()) break; goForward(true); } gotoNode(index, true); return res; }