Package nl.zoidberg.calculon.model

Source Code of nl.zoidberg.calculon.model.PerftTest

/**
* Calculon - A Java chess-engine.
*
* Copyright (C) 2008-2009 Barry Smith
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package nl.zoidberg.calculon.model;

import nl.zoidberg.calculon.engine.BitBoard;
import nl.zoidberg.calculon.engine.BitBoard.BitBoardMove;
import nl.zoidberg.calculon.engine.MoveGenerator;
import nl.zoidberg.calculon.notation.FENUtils;
import org.junit.Test;

import java.util.Iterator;

import static org.junit.Assert.assertEquals;

/**
* These tests verify the move generation software. The number of possible moves at various depths
* is tested against the known correct values. If these don't match exactly, something is wrong
* in the move generation logic. The correct values from any position can be found using crafty
* with the 'perft' command.
*/
public class PerftTest {

    @Test
  public void testPosition1() {
    BitBoard board = new BitBoard().initialise();
    executeBoard(board, new int[] { 20, 400, 8902, 197281, 4865609, });
  }
 
    @Test
  public void testPosition2() {
    BitBoard board = FENUtils.getBoard("r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1");
    executeBoard(board, new int[] { 48, 2039, 97862, 4085603, });
//    executeBoard(board, new int[] { 48, 2039, 97862, 4085603, 193690690, });
  }

    @Test
  public void testPosition3() {
    // Some very complex enpassant moves in this one.
    BitBoard board = FENUtils.getBoard("8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1");

    executeBoard(board, new int[] { 14, 191, 2812, 43238, 674624, 11030083, });
//    executeBoard(board, new int[] { 14, 191, 2812, 43238, 674624, 11030083, 178633661, });
  }
 
    @Test
  public void testPosition4() {
    // Tests all castling moves...
    BitBoard board = FENUtils.getBoard("r3k2r/pppq1ppp/2nbbn2/3pp3/3PP3/2NBBN2/PPPQ1PPP/R3K2R w KQkq - 0 8");
    executeBoard(board, new int[] { 41, 1680, 69126, 2833127, });
   
    board = FENUtils.getBoard("r3k2r/ppp2p1p/3bbnpB/n2Np3/q2PP3/2PB1N2/PP1Q1PPP/R3K2R w KQkq - 0 11");
    executeBoard(board, new int[] { 47, 2055, 93774, });
  }
 
    @Test
  public void testPosition5() {
    BitBoard board = FENUtils.getBoard("8/PPP4k/8/8/8/8/4Kppp/8 w - - 0 1");
    executeBoard(board, new int[] { 18, 290, 5044, 89363, 1745545, });
  }
 
    @Test
  public void testPosition6() {
    BitBoard board = FENUtils.getBoard("8/3K4/2p5/p2b2r1/5k2/8/8/1q6 b - - 0 1");
    executeBoard(board, new int[] { 50, 279, 13310, 54703, 2538084});
  }

    @Test
  public void testPosition7() {
    BitBoard board = FENUtils.getBoard("8/p3kp2/6p1/3r1p1p/7P/8/3p2P1/3R1K2 w - - 0 1");
    executeBoard(board, new int[] { 10, 218, 2886, 63771, 927197, });
  }
 
  private void executeBoard(BitBoard board, int[] expect) {
    for(int x = 0; x < expect.length; x++) {
      assertEquals(expect[x], generateToDepth(x+1, board));
    }
  }

  private int generateToDepth(int depth, BitBoard bitBoard) {
    if(depth == 1) {
            return new MoveGenerator(bitBoard).getAllRemainingMoves().size();
    }

        int count = 0;
    for(Iterator<BitBoardMove> moveItr = new MoveGenerator(bitBoard); moveItr.hasNext(); ) {
      BitBoardMove move = moveItr.next();
      byte x = bitBoard.getFlags();
      long cs = bitBoard.getChecksum();
      bitBoard.makeMove(move);
      count += generateToDepth(depth-1, bitBoard);
      bitBoard.unmakeMove();
      if(x != bitBoard.getFlags() || cs != bitBoard.getChecksum()) {
                throw new IllegalStateException("make/unmake caused differences");
      }
    }
        return count;
  }
 
//  private void generateForCrafty(BitBoard bitBoard, int depth) {
//    System.out.println("ponder off");
//    for(Iterator<BitBoardMove> moveItr = new MoveGenerator(bitBoard); moveItr.hasNext(); ) {
//      BitBoardMove move = moveItr.next();
//      bitBoard.makeMove(move);
//      System.out.println(move.getAlgebraic() + " " + move);
//      System.out.println("setboard " + FENUtils.generate(bitBoard));
//      System.out.println("perft " + depth);
//      generateToDepth(depth, bitBoard);
//      bitBoard.unmakeMove();
//    }
//  }
}
TOP

Related Classes of nl.zoidberg.calculon.model.PerftTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.