private int getScoreForEndPosition(Player player) { int returnValue = -1; if (player.isInStaleMate() || itsBoard.isThirdOccuranceOfPosition()) returnValue = 500; else if (player.isInCheckMate()) { if (player == itsPlayer) returnValue = 0; else returnValue = 1000; } return returnValue; }
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); } }
private Collection getAllMoves(Player player) { Collection moves = new HashSet(); for (Iterator p = player.getPieces().iterator(); p.hasNext(); ) { Piece piece = (Piece) p.next(); SquareInterface startSquare = piece.getLocation(); for (Iterator s = piece.getMoveOptions().iterator(); s.hasNext(); ) { SquareInterface square = (SquareInterface) s.next(); moves.add(new Move(piece, startSquare, square)); } } return moves; }
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; } }