Ejemplo n.º 1
0
bool CMoveGen::isLegal(const CMove& move) const
{
    EField from = move.getFrom();
    EPiece pcFrom = board().getPiece(from);
    if ((eNP == pcFrom) || (getColor(pcFrom) != board().getSide()))
        return(false);

    EField to = move.getTo();
    EPiece pcTo = board().getPiece(to);
    if ((eNP != pcTo) && (getColor(pcTo) == board().getSide()))
        return(false);

    CMoveList moves;
    switch (move.getType())
    {
        case eNormal: genMoves(moves, true, true, create_bit(from), create_bit(to)); break;
        case eCastle: genMoves(moves, false, true, create_bit(from), create_bit(to)); break;
        case eEnPassant: genMoves(moves, true, false, create_bit(from), create_bit(to)); break;
        case ePromotion: genMoves(moves, true, false, create_bit(from), create_bit(to)); break;
    }
    for (int32 i = 0; i < moves.size(); ++i)
        if (move.getMoveData() == moves[i].getMoveData())
            return true;

    return(false);
}
Ejemplo n.º 2
0
void CMoveGen::getEvasions(CMoveList& moves) const
{
    bb_t mask_evasion = bbEmptyBoard;
    EColor side = board().getSide();

    bb_t x = board().getPieceBits(eKing, side);
    while (x)
    {
        EField kingpos = popFirstBit(x);
        bb_t attacks = board().getAttackBy(kingpos, toggleColor(side));

        mask_evasion |= attacks;
        while (attacks)
        {
            EField from = popFirstBit(attacks);
            mask_evasion |= gl_bitboards.getGap(from, kingpos);
        }
    }
    genMoves(moves, true, true, bbFullBoard, mask_evasion);
}
Ejemplo n.º 3
0
void CMoveGen::getQuiets(CMoveList& moves) const
{
    genMoves(moves, false, true, bbFullBoard, bbFullBoard);
}
Ejemplo n.º 4
0
void CMoveGen::getCaptures(CMoveList& moves) const
{
    genMoves(moves, true, false, bbFullBoard, bbFullBoard);
}
Ejemplo n.º 5
0
void CMoveGen::getAll(CMoveList& moves) const
{
    genMoves(moves, true, true, bbFullBoard, bbFullBoard);
}
Ejemplo n.º 6
0
int main()
{
    char line[256];
    char args[4][64];

    /* It mainly calls ComputerThink(maxdepth) to calculate to desired ply */
    char s[256];
    int from;
    int dest;
    int i;

    hashRndInit();
    startgame ();

    maxDepth = 6;		/* Max depth to search */
    MOVE moveBuf[200];
    MOVE theBest;
    int movecnt;

    /* Belka */
    puts (" \n Kitteneitor version June 5th 2013 by Emilio Diaz \n =================================================\n\n");
    puts (" Help overview:");
    puts (" making a move: e.g. e2e4, c7c5, a7a8q, e1g1 etc.");
    puts (" d ............ displaying current board");
    puts (" on ........... forcing the engine to move");
    puts (" sd <n> ....... setting depth to <n> plies");
    puts (" undo ......... taking back last move (ply)");
    puts (" quit ......... quit console application \n\n");
    /* Belka */

    side = WHITE;
    computerSide = BLACK;	/* Human is white side */

    hdp = 0;			/* Current move order */
    for (;;)
    {
        fflush (stdout);
        if (side == computerSide)
        {
            /* Computer's turn */
            theBest = ComputerThink (maxDepth);

            if (theBest.type_of_move > 8)
                printf ("type of move the best %d \n", theBest.type_of_move);

            makeMove (theBest);

            /* Just the move without pawn crown */
            printf("move %c%d%c%d",
                   'a' + COL(theBest.from),
                   8 - ROW(theBest.from),
                   'a' + COL(theBest.dest),
                   8 - ROW(theBest.dest));
            /* Check whether it's a crown */
            switch (theBest.type_of_move)
            {
               case MOVE_TYPE_PROMOTION_TO_QUEEN:
                  printf("q\n");
                  break;
               case MOVE_TYPE_PROMOTION_TO_ROOK:
                  printf("r\n");
                  break;
               case MOVE_TYPE_PROMOTION_TO_BISHOP:
                  printf("b\n");
                  break;
               case MOVE_TYPE_PROMOTION_TO_KNIGHT:
                  printf("n\n");
                  break;
               default:
                  printf("\n");
            }   /* end switch */

            printBoard ();
            printf ("Castle rights: %d\n", castle);
            fflush (stdout);
            continue;
        }

        printf ("k> ");

        /* Get user input */
        if (!fgets (line, 256, stdin))
            return 0;
        if (line[0] == '\n')
            continue;
        sscanf (line, "%s", s);

//        if (scanf ("%s", s) == EOF)	/* Shut down the program */
//            return 0;

        if (!strcmp (s, "d"))
        {
            printBoard ();
            continue;
        }
        if (!strcmp (s, "test1"))
        {
            test1 ();
            printBoard();
            continue;
        }
        if (!strcmp (s, "test2"))
        {
            test2 ();
            printBoard();
            continue;
        }
        if (!strcmp (s, "test3"))
        {
            test3 ();
            printBoard();
            continue;
        }
        if (!strcmp (s, "test4"))
        {
            test4 ();
            printBoard();
            continue;
        }
        if (!strcmp (s, "test5"))
        {
            test5 ();
            printBoard();
            continue;
        }
        if (!strcmp (s, "test6"))
        {
            test6 ();
            printBoard();
            continue;
        }
        if (!strcmp (s, "test7"))
        {
            test7 ();
            printBoard();
            continue;
        }
        if (!strcmp (s, "test8"))
        {
            test8 ();
            printBoard();
            continue;
        }
        if (!strcmp (s, "test9"))
        {
            test9 ();
            printBoard();
            continue;
        }
        if (!strcmp (s, "test10"))
        {
            test10 ();
            printBoard();
            continue;
        }
        if (!strcmp (s, "test11"))
        {
            test11 ();
            printBoard();
            continue;
        }
        if (!strcmp (s, "test12"))
        {
            test12 ();
            printBoard();
            continue;
        }
        if (!strcmp (s, "test13"))
        {
            test13 ();
            printBoard();
            continue;
        }
        if (!strcmp (s, "test14"))
        {
            test14 ();
            printBoard();
            continue;
        }
        if (!strcmp (s, "testMoveGen")) //Belka: McKenzie test position
        {
            testMoveGen();
            printBoard();
            continue;
        }
        if (!strcmp (s, "testEvalSym")) //Belka: McKenzie test position
        {
            testEvalSym();
            continue;
        }
        if (!strcmp (s, "countNodes"))
        {
            countNodes();
            continue;
        }
        if (!strcmp (s, "testWhitePassedPawns"))
        {
            testWhitePassedPawns ();
            continue;
        }
        if (!strcmp (s, "testBlackPassedPawns"))
        {
            testBlackPassedPawns ();
            continue;
        }
        if (!strcmp (s, "testWhiteDoubledPawns"))
        {
            testWhiteDoubledPawns ();
            continue;
        }
        if (!strcmp (s, "testBlackDoubledPawns"))
        {
            testBlackDoubledPawns ();
            continue;
        }
        if (!strcmp (s, "testIsIsolatedPawnWhite"))
        {
            testIsIsolatedPawnWhite ();
            continue;
        }
        if (!strcmp (s, "testIsIsolatedPawnBlack"))
        {
            testIsIsolatedPawnBlack ();
            continue;
        }
        if (!strcmp (s, "showPawnsInfo"))
        {
            showPawnsInfo ();
            continue;
        }
        if (!strcmp (s, "testisSqProtectedByAPawn"))
        {
             testisSqProtectedByAPawn();
            continue;
        }
//        if (!strcmp (s, "testIsSqProtectedByAKnight"))
//        {
//             testIsSqProtectedByAKnight();
//            continue;
//        }
//        if (!strcmp (s, "testIsSqProtectedByABishop"))
//        {
//             testIsSqProtectedByABishop();
//            continue;
//        }
        if (!strcmp (s, "testOpenCols"))
        {
             testOpenCols();
            continue;
        }
        if (!strcmp (s, "undo"))
        {
            takeBack ();
            printBoard ();
            computerSide = (WHITE + BLACK) - computerSide;
            continue;
        }
        if (!strcmp(s,"setboard"))
        {
            strcpy(fenBuf, "");
            sscanf(line, "setboard %s %s %s %s", args[0],args[1],args[2],args[3]);
            strcat(fenBuf, args[0]);
            strcat(fenBuf, args[1]);
            strcat(fenBuf, args[2]);
            strcat(fenBuf, args[3]);
            setBoard(fenBuf);
            continue;
        }
        if (!strcmp (s, "xboard"))
        {
            xboard ();
            return 0;
        }
        if (!strcmp (s, "on"))
        {
            computerSide = side;
            continue;
        }
        if (!strcmp (s, "pass"))
        {
            side = (WHITE + BLACK) - side;
            computerSide = (WHITE + BLACK) - side;
            continue;
        }
        if (!strcmp (s, "sd"))
        {
            sscanf (line, "sd %d", &maxDepth);
            continue;
        }

//        if (!strcmp (s, "fen"))
//        {
//            strcpy (fenstring, "");

//            sscanf (linea, "fen %s %s %s %s", args[0], args[1], args[2],
//                    args[3]);

//            strcat (fenstring, args[0]);
//            strcat (fenstring, args[1]);
//            strcat (fenstring, args[2]);
//            strcat (fenstring, args[3]);

//            fen (fenstring);
//        }

        if (!strcmp (s, "perft"))
        {
            sscanf (line, "perft %d", &maxDepth);
            clock_t start;
            clock_t stop;
            double t = 0.0;
            /* Start timer */
            start = clock ();
            U64 count = perft (maxDepth);
            /* Stop timer */
            stop = clock ();
            t = (double) (stop - start) / CLOCKS_PER_SEC;
//            printf ("nodes = %'llu\n", count);
            printf ("nodes = %8"  PRId64 "\n", count); // Belka
            printf ("time = %.2f s\n", t);
            continue;
        }
        if (!strcmp (s, "quit"))
        {
            printf ("Good bye!\n");
            return 0;
        }

        /* Maybe the user entered a move? */
        from = s[0] - 'a';
        from += 8 * (8 - (s[1] - '0'));
        dest = s[2] - 'a';
        dest += 8 * (8 - (s[3] - '0'));
        ply = 0;
        movecnt = genMoves (side, moveBuf);

        /* Loop through the moves to see whether it's legal */
        for (i = 0; i < movecnt; i++)
            if (moveBuf[i].from == from && moveBuf[i].dest == dest)
            {
                /* Promotion move? */
                if (piece[from] == PAWN && (dest < 8 || dest > 55))
                {
                    switch (s[4])
                    {
                    case 'q':
                        moveBuf[i].type_of_move = MOVE_TYPE_PROMOTION_TO_QUEEN;
                        break;

                    case 'r':
                        moveBuf[i].type_of_move = MOVE_TYPE_PROMOTION_TO_ROOK;
                        break;

                    case 'b':
                        moveBuf[i].type_of_move = MOVE_TYPE_PROMOTION_TO_BISHOP;
                        break;

                    case 'n':
                        moveBuf[i].type_of_move = MOVE_TYPE_PROMOTION_TO_KNIGHT;
                        break;

                    default:
                        puts("Promoting to a McGuffin..., I'll give you a queen");
                        moveBuf[i].type_of_move = MOVE_TYPE_PROMOTION_TO_QUEEN;
                    }
                }
                if (!makeMove (moveBuf[i]))
                {
                    takeBack ();
                    printf ("Illegal move.\n");
                }
                break;
            }
        printBoard ();
    }
}
Ejemplo n.º 7
0
void xboard()
{
    char line[256], command[256], c;
    char args[4][64];
    int from, dest, i;
    MOVE moveBuf[200];
    MOVE theBest;
    int movecnt;
    //int illegal_king = 0;

    signal(SIGINT, SIG_IGN);

    printf ("\n");
//    hashKeyPosition(); /* hash of the initial position */
//    hashRndInit();
    startgame ();

    /* Waiting for a command from GUI */
    for (;;)
    {
        fflush (stdout);
        if (side == computerSide)
        {   /* Computer's turn */
            /* Find out the best move to reply to the current position */
            theBest = ComputerThink (maxDepth);
            if (theBest.type_of_move > 8)
                printf ("type of move the best %d \n", theBest.type_of_move);
            makeMove (theBest);
            /* send move */
            switch (theBest.type_of_move)
            {
            case MOVE_TYPE_PROMOTION_TO_QUEEN:
                c = 'q';
                break;
            case MOVE_TYPE_PROMOTION_TO_ROOK:
                c = 'r';
                break;
            case MOVE_TYPE_PROMOTION_TO_BISHOP:
                c = 'b';
                break;
            case MOVE_TYPE_PROMOTION_TO_KNIGHT:
                c = 'n';
                break;
            default:
                c = ' ';
            }
            printf ("move %c%d%c%d%c\n", 'a' + COL(theBest.from), 8
                    - ROW(theBest.from), 'a' + COL(theBest.dest), 8
                    - ROW(theBest.dest), c);

            fflush(stdout);
            continue;
        }

        if (!fgets (line, 256, stdin))
            return;
        if (line[0] == '\n')
            continue;
        sscanf (line, "%s", command);

        if (!strcmp (command, "xboard"))
        {
            continue;
        }
        if (!strcmp (command, "d"))
        {
            printBoard ();
            continue;
        }
        if (!strcmp (command, "new"))
        {
            startgame ();
            continue;
        }
        if (!strcmp (command, "quit"))
        {
            return;
        }
        if (!strcmp (command, "force"))
        {
            computerSide = EMPTY;
            continue;
        }
        /* If we get a result the engine must stop */
        if (!strcmp(command, "result"))
        {
            computerSide = EMPTY;
            continue;
        }
        if (!strcmp(command, "?")) {
            computerSide = EMPTY;
            continue;
        }
        if (!strcmp(command, ".")) {
            continue;
        }
        if (!strcmp(command, "exit")) {
            continue;
        }
        if (!strcmp(command,"setboard"))
        {
            strcpy(fenBuf, "");
            sscanf(line, "setboard %s %s %s %s", args[0],args[1],args[2],args[3]);
            strcat(fenBuf, args[0]);
            strcat(fenBuf, args[1]);
            strcat(fenBuf, args[2]);
            strcat(fenBuf, args[3]);
            setBoard(fenBuf);
            continue;
        }
        if (!strcmp (command, "white"))
        {
            side = WHITE;
            computerSide = BLACK;
            continue;
        }
        if (!strcmp (command, "black"))
        {
            side = BLACK;
            computerSide = WHITE;
            continue;
        }
        if (!strcmp (command, "sd"))
        {
            sscanf (line, "sd %d", &maxDepth);
            continue;
        }
        if (!strcmp (command, "go"))
        {
            computerSide = side;
            continue;
        }
        /* Taken from TSCP: we receive from the GUI the time we have */
        if (!strcmp(command, "time"))
        {
            sscanf (line, "time %ld", &maxTime);
            /* Convert to miliseconds */
            maxTime *= 10;
            maxTime /= 10;
            maxTime -= 300;
            totalTime = maxTime;

//            if (totalTime < 3000)
//                maxDepth = 6;
//            else
            maxDepth = 32;
            continue;
        }
        if (!strcmp(command, "otim"))
        {
            continue;
        }
        if (!strcmp (command, "undo"))
        {
            if (hdp == 0)
                continue;
            takeBack ();
            continue;
        }
        if (!strcmp (command, "remove"))
        {
            if (hdp <= 1)
                continue;
            takeBack ();
            takeBack ();
            continue;
        }

        /* Maybe the user entered a move? */
        /* Is that a move? */
        if (command[0] < 'a' || command[0] > 'h' ||
                command[1] < '0' || command[1] > '9' ||
                command[2] < 'a' || command[2] > 'h' ||
                command[3] < '0' || command[3] > '9')
        {
            printf("Error (unknown command): %s\n", command); /* No move, unknown command */
            continue;
        }

        from = command[0] - 'a';
        from += 8 * (8 - (command[1] - '0'));
        dest = command[2] - 'a';
        dest += 8 * (8 - (command[3] - '0'));
        ply = 0;
        movecnt = genMoves (side, moveBuf);

        /* Loop through the moves to see if it's legal */
        for (i = 0; i < movecnt; ++i) {
            if (moveBuf[i].from == from && moveBuf[i].dest == dest)
            {
                if (piece[from] == PAWN && (dest < 8 || dest > 55))
                {
                    if (command[4] != 'q' && command[4] != 'r' && command[4] != 'b' && command[4] != 'n')
                    {
                        printf ("Illegal move. Bad letter for promo\n");
                        goto goon;
                    }
                    switch (command[4])
                    {
                    case 'q':
                        moveBuf[i].type_of_move = MOVE_TYPE_PROMOTION_TO_QUEEN;
                        break;
                    case 'r':
                        moveBuf[i].type_of_move = MOVE_TYPE_PROMOTION_TO_ROOK;
                        break;
                    case 'b':
                        moveBuf[i].type_of_move = MOVE_TYPE_PROMOTION_TO_BISHOP;
                        break;
                    case 'n':
                        moveBuf[i].type_of_move = MOVE_TYPE_PROMOTION_TO_KNIGHT;
                        break;
                    }
                }

                if (moveBuf[i].type_of_move > 8)
                    printf ("Type of move the best %d \n", moveBuf[i].type_of_move);

                if (makeMove (moveBuf[i]))
                {
                    goto goon;	/* Legal move */
                }
                else {
                    printf ("Illegal move. King is in check\n");
                    goto goon;
                }
            }
        }
        printf ("Illegal move.\n");  /* illegal move */

goon:
        continue;
    }
}