private int examineMove(AbstractMove theMove, Player player, int depth, String position) { Player player2 = player.getOpponent(); player2.findAllMoves(theMove.isCheck()); int endPositionScore = getScoreForEndPosition(player2); if (endPositionScore != -1) { itsTable.add(position, endPositionScore); return endPositionScore; } else if (depth == 1) { int score = itsScorer.getScore(itsBoard, itsPlayer); itsTable.add(position, score); return score; } else { int bestScore = 1234; Collection moves = getAllMoves(player2); for (Iterator i = moves.iterator(); i.hasNext(); ) { AbstractMove move = (AbstractMove) i.next(); move.getPiece().findMoves_safe(); move = player2.movePiece(move); String newPosition = itsBoard.getPositionCode() + player2.getColor(); int thisScore = -321; boolean hasIt = itsTable.has(newPosition); if (hasIt) { thisScore = itsTable.getScore(); } else { thisScore = examineMove(move, player2, depth - 1, newPosition); itsTable.add(newPosition, thisScore); } bestScore = compareScores(player2, bestScore, thisScore); player2.reverseMove(move); } return bestScore; } }
private Collection getMovesOfValue(int target) { Collection moves = new HashSet(); for (Iterator i = itsMoves.iterator(); i.hasNext(); ) { AbstractMove move = (AbstractMove) i.next(); if (move.getScore() == target) moves.add(move); } return moves; }
private AbstractMove findHighestScoringMove() { AbstractMove bestMove = new IllegalMove(); for (Iterator i = itsMoves.iterator(); i.hasNext(); ) { AbstractMove move = (AbstractMove) i.next(); if (move.getScore() > bestMove.getScore()) bestMove = move; } return bestMove; }
public void search(ChessBoard board, Player player) { itsTable = new PositionHashTable(); itsMoves.clear(); itsPlayer = player; itsBoard = board; Collection moves = getAllMoves(player); for (Iterator i = moves.iterator(); i.hasNext(); ) { AbstractMove move = (AbstractMove) i.next(); move.getPiece().findMoves_safe(); move = player.movePiece(move); itsBoard.recordPosition(); String positionCode = itsBoard.getPositionCode() + player.getColor(); int score = examineMove(move, player, itsSearchQuality, positionCode); move.setScore(score); itsMoves.add(move); itsBoard.removeLastPosition(); player.reverseMove(move); } }
public Collection getBestMoves() { AbstractMove high = findHighestScoringMove(); return getMovesOfValue(high.getScore()); }