ComputerPlayer implements Player { @Override public String getCommand(Position pos, boolean drawOffer, List<Position> history) { long[] posHashList = new long[200 + history.size()]; int posHashListSize = 0; for (Position p : history) { posHashList[posHashListSize++] = p.zobristHash(); } tt.nextGeneration(); History ht = new History(); Search sc = new Search(pos, posHashList, posHashListSize, tt, ht); MoveGen.MoveList moves = new MoveGen().pseudoLegalMoves(pos); MoveGen.removeIllegal(pos, moves); sc.scoreMoveList(moves, 0); if (moves.size == 0) { return "swap"; } if (bookEnabled) { Move bookMove = book.getBookMove(pos); if (bookMove != null) { System.out.printf("Book moves: %s\n", book.getAllBookMoves(pos)); return TextIO.moveToString(pos, bookMove, false); } } currentSearch = sc; sc.setListener(listener); Move bestM; if ((moves.size == 1) && canClaimDraw(pos, posHashList, posHashListSize, moves.m[0]).isEmpty()) { bestM = moves.m[0]; bestM.score = 0; } else if (randomMode) { bestM = findSemiRandomMove(sc, moves); } else { sc.timeLimit(minTimeMillis, maxTimeMillis); bestM = sc.iterativeDeepening(moves, maxDepth, maxNodes, verbose); } currentSearch = null; String strMove = TextIO.moveToString(pos, bestM, false); if (bestM.score <= 0) { String drawClaim = canClaimDraw(pos, posHashList, posHashListSize, bestM); if (!drawClaim.isEmpty()) strMove = drawClaim; } return strMove; } ComputerPlayer(); void setTTLogSize(int logSize); void setListener(Search.Listener listener); @Override String getCommand(Position pos, boolean drawOffer, List<Position> history); @Override boolean isHumanPlayer(); @Override void useBook(boolean bookOn); @Override void timeLimit(int minTimeLimit, int maxTimeLimit, boolean randomMode); @Override void clearTT(); TwoReturnValues<Move, String> searchPosition(Position pos, int maxTimeMillis); static final String engineName; public boolean verbose; }
@Test public void testGetCommand() throws ChessParseError { System.out.println("getCommand"); ArrayList<Position> nullHist = new ArrayList<>(); Position pos = TextIO.readFEN("7k/5Q2/p5K1/8/8/8/8/8 b - - 99 80"); ComputerPlayer cp = new ComputerPlayer(); cp.maxDepth = 1; cp.maxTimeMillis = -1; cp.verbose = false; String result = cp.getCommand(pos, false, nullHist); assertEquals("a5", result); pos = TextIO.readFEN("7k/5Q2/p5K1/8/8/8/8/8 b - - 100 80"); result = cp.getCommand(pos, false, nullHist); assertEquals("draw 50", result); pos = TextIO.readFEN("3k4/1R6/R7/8/8/8/8/1K6 w - - 100 80"); result = cp.getCommand(pos, false, nullHist); assertEquals("Ra8#", result); pos = TextIO.readFEN("8/1R5k/R7/8/8/8/B7/1K6 b - - 99 80"); result = cp.getCommand(pos, false, nullHist); assertEquals("draw 50 Kh8", result); pos = TextIO.readFEN("6Nk/8/5K1R/q7/q7/q7/8/8 b - - 100 80"); result = cp.getCommand(pos, false, nullHist); assertEquals("Kxg8", result); }
@Test public void testDrawRep() { System.out.println("drawRep"); Game game = new Game(new HumanPlayer(), new HumanPlayer()); ComputerPlayer cp = new ComputerPlayer(); cp.maxDepth = 3; cp.maxTimeMillis = -1; cp.verbose = false; game.processString("setpos 7k/5RR1/8/8/8/8/q3q3/2K5 w - - 0 1"); game.processString("Rh7"); game.processString("Kg8"); game.processString("Rhg7"); String result = cp.getCommand(new Position(game.pos), false, game.getHistory()); assertEquals("Kh8", result); game.processString("Kh8"); game.processString("Rh7"); game.processString("Kg8"); game.processString("Rhg7"); result = cp.getCommand(new Position(game.pos), false, game.getHistory()); assertEquals("draw rep Kh8", result); game.processString("setpos 7k/R7/1R6/8/8/8/8/K7 w - - 0 1"); game.processString("Ra8"); game.processString("Kh7"); result = cp.getCommand(new Position(game.pos), false, game.getHistory()); assertEquals("Ra7+", result); game.processString("Ra7"); game.processString("Kh8"); game.processString("Ra8"); game.processString("Kh7"); result = cp.getCommand(new Position(game.pos), false, game.getHistory()); assertTrue(!result.equals("Ra7+")); }