Beispiel #1
0
void Edit_DrawBoard ( const ChessBoard &board )
{
    const SQUARE *b = board.queryBoardPointer();
    printf ( "   +--------+\n" );
    for ( int y=9; y >= 2; --y )
    {
        printf ( " %d |", y-1 );
        for ( int x=2; x <= 9; ++x )
        {
            char c = PieceRepresentation ( b[OFFSET(x,y)] );
            printf ( "%c", c );
        }

        printf ( "|" );

        if ( y == 6 )
            printf ( "   ply  = %d", board.GetCurrentPlyNumber() );
        else if ( y == 5 )
            printf ( "   hash = %08lx", (unsigned long)(board.Hash()) );

        printf ( "\n" );
    }
    printf ( "   +--------+\n" );
    printf ( "    abcdefgh\n\n" );
}
Beispiel #2
0
bool InternetChessPlayer::send ( const ChessBoard &board )
{
    char tempString [256];

    UINT32 numPlies = board.GetCurrentPlyNumber();
    if ( numPlies > 0 )
    {
        // Before receiving the move from the remote opponent, we must
        // send him the complete state of the game as it exists locally.

        // Send an 8-byte string to specify what kind of message this is.
        // In this case, it is "history", because we are sending the entire history
        // of moves in the game history so far...

        UINT32 plyBytes = numPlies * sizeof(Move);
        UINT32 packetSize = 8 + sizeof(numPlies) + plyBytes;
        int result = ::send ( connectInfo.commSocket, (const char *)&packetSize, 4, 0 );
        if ( result != 4 )
        {
            sprintf ( tempString, "send psize: %d", WSAGetLastError() );
            userInterface.ReportSpecial (tempString);
            return false;
        }

        result = ::send ( connectInfo.commSocket, "history ", 8, 0 );
        if ( result != 8 )
        {
            sprintf ( tempString, "send 'history': %d", WSAGetLastError() );
            userInterface.ReportSpecial (tempString);
            return false;
        }

        result = ::send ( connectInfo.commSocket, (const char *)&numPlies, 4, 0 );
        if ( result != 4 )
            return false;

        Move *history = new Move [numPlies];
        if ( !history )
        {
            userInterface.ReportSpecial ( "out of memory!" );
            return false;
        }

        for ( UINT32 ply = 0; ply < numPlies; ++ply )
            history[ply] = board.GetPastMove (ply);

        result = ::send ( connectInfo.commSocket, (const char *)history, plyBytes, 0 );
        delete[] history;
        history = NULL;

        if ( UINT32(result) != plyBytes )
        {
            sprintf ( tempString, "send: %d", WSAGetLastError() );
            userInterface.ReportSpecial (tempString);
            return false;
        }
    }

    return true;
}
Beispiel #3
0
void WriteEmail ( const ChessBoard &board, const char *filename )
{
    FILE *f = fopen ( filename, "wt" );
    if ( !f )
    {
        sprintf ( ExitMessage, "Error opening file '%s' for write!\n", filename );
        exit(1);
    }

    ListChessGame ( board, f );
    fprintf ( f, "\n\n%s", ChessDataStart );

    unsigned n = board.GetCurrentPlyNumber();

    fprintf ( f, "%u\n", n );

    for ( unsigned i=0; i < n; i++ )
    {
        Move move = board.GetPastMove ( i );
        // Write source, dest codes as ASCII data...

        fprintf ( f, "%02X %02X\n", unsigned(move.source), unsigned(move.dest) );
    }

    fprintf ( f, "%s", ChessDataEnd );
    fclose ( f );
}
Beispiel #4
0
bool ImportGameFile (LearnTree &tree, const char *filename)
{
    int  updates  = 0;
    int  branches = 0;
    PGN_FILE_STATE state = PGN_FILE_STATE_UNDEFINED;
    bool success  = false;

    tChessMoveStream *stream = tChessMoveStream::OpenFileForRead (filename);
    if (!stream)
    {
        printf("Cannot open file for read: '%s'\n", filename);
    }
    else
    {
        ChessBoard  board;
        Move        move;
        UnmoveInfo  unmove;

        while (stream->GetNextMove(move, state))
        {
            if (state == PGN_FILE_STATE_NEWGAME)
            {
                board.Init();
            }
            if (board.GetCurrentPlyNumber() < (int)MaxLearnDepth)
            {
                int result = tree.rememberPosition (board, move, 0, 0, 1, 0);
                if (result == 0)
                {
                    printf ("??? Could not add move to tree ???\n");
                    break;
                }
                else if (result == 1)
                {
                    ++updates;
                }
                else if (result == 2)
                {
                    ++branches;
                }
                else
                {
                    printf ("??? tree.RememberPosition returned %d\n", result);
                    break;
                }
            }
            board.MakeMove (move, unmove);
        }
        delete stream;
        
        if (state != PGN_FILE_STATE_FINISHED)
        {
            // We don't consider this a fatal error, just a warning.
            printf("??? Error loading PGN file '%s': %s\n", filename, GetPgnFileStateString(state));
        }

        success = true;

        printf(">>> file='%s', branches=%d, updates=%d\n", filename, branches, updates);
    }

    return success;
}
Beispiel #5
0
    //----------------------------------------------------------------
    //  The following function is called by the ChessGame after
    //  each player moves, so that the move can be displayed
    //  in standard chess notation, if the UI desires.
    //  It is helpful to use the ::FormatChessMove() function
    //  for this purpose.
    //  The parameter 'thinkTime' is how long the player thought
    //  about the move, expressed in hundredths of seconds.
    //
    //  NOTE:  This function is called BEFORE the move
    //  has been made on the given ChessBoard.
    //  This is necessary for ::FormatChessMove() to work.
    //----------------------------------------------------------------
    virtual void RecordMove(ChessBoard &board, Move move, INT32 thinkTime)
    {
        TheTerminal.print(BoolSideName(board.WhiteToMove()));
        TheTerminal.print(" move #");
        TheTerminal.printInteger((board.GetCurrentPlyNumber()/2) + 1);
        TheTerminal.print(": ");

        int source, dest;
        SQUARE prom = move.actualOffsets(board, source, dest);
        char sourceFile = XPART(source) - 2 + 'a';
        char sourceRank = YPART(source) - 2 + '1';
        char destFile   = XPART(dest)   - 2 + 'a';
        char destRank   = YPART(dest)   - 2 + '1';
        TheTerminal.print(sourceFile);
        TheTerminal.print(sourceRank);
        TheTerminal.print(destFile);
        TheTerminal.print(destRank);
        if (prom)
        {
            char pchar = '?';
            int pindex = UPIECE_INDEX(prom);
            switch (pindex)
            {
                case Q_INDEX:   pchar = 'Q';    break;
                case R_INDEX:   pchar = 'R';    break;
                case B_INDEX:   pchar = 'B';    break;
                case N_INDEX:   pchar = 'N';    break;
            }
            TheTerminal.print(pchar);
        }

        // Temporarily make the move, so we can display what the board
        // looks like after the move has been made.
        // We also use this to determine if the move causes check,
        // checkmate, stalemate, etc.
        UnmoveInfo unmove;
        board.MakeMove(move, unmove);

        // Report check, checkmate, draws.
        bool inCheck = board.CurrentPlayerInCheck();
        bool canMove = board.CurrentPlayerCanMove();
        if (canMove)
        {
            if (inCheck)
            {
                TheTerminal.print('+');
            }
        }
        else
        {
            // The game is over!
            if (inCheck)
            {
                if (board.WhiteToMove())
                {
                    // White just got checkmated.
                    TheTerminal.print("# 0-1");
                }
                else
                {
                    // Black just got checkmated.
                    TheTerminal.print("# 1-0");
                }
            }
            else
            {
                // The game is a draw (could be stalemate, could be by repetition, insufficient material, ...)
                TheTerminal.print(" 1/2-1/2");
            }
        }

        TheTerminal.printline();

        // Unmake the move, because the caller will make the move itself.
        board.UnmakeMove(move, unmove);
    }
Beispiel #6
0
void ListChessGame ( const ChessBoard &theBoard, FILE *out )
{
    fprintf ( out, "\n      WHITE               BLACK\n\n" );

    ChessBoard temp;
    unsigned n = theBoard.GetCurrentPlyNumber();
    for ( unsigned i=0; i < n; i++ )
    {
        Move move = theBoard.GetPastMove(i);
        char movestr [MAX_MOVE_STRLEN + 1];
        FormatChessMove ( temp, move, movestr );
        if ( temp.WhiteToMove() )
        {
            fprintf ( out, "%3u.  %-20s", i/2 + 1, movestr );
        }
        else
        {
            fprintf ( out, "%s\n", movestr );
        }

        UnmoveInfo unmove;
        temp.MakeMove ( move, unmove );
    }

    if ( n & 1 )
    {
        fprintf ( out, "\n" );
    }

    fprintf ( out, "\n\n" );

    for ( int y=7; y >= 0; y-- )
    {
        fprintf ( out, "      " );

        for ( int x=0; x <= 7; x++ )
        {
            SQUARE p = theBoard.GetSquareContents ( x, y );
            char c = '?';
            switch ( p )
            {
            case EMPTY:      c = '.';   break;
            case WPAWN:      c = 'P';   break;
            case WKNIGHT:    c = 'N';   break;
            case WBISHOP:    c = 'B';   break;
            case WROOK:      c = 'R';   break;
            case WQUEEN:     c = 'Q';   break;
            case WKING:      c = 'K';   break;
            case BPAWN:      c = 'p';   break;
            case BKNIGHT:    c = 'n';   break;
            case BBISHOP:    c = 'b';   break;
            case BROOK:      c = 'r';   break;
            case BQUEEN:     c = 'q';   break;
            case BKING:      c = 'k';   break;
            }

            fprintf ( out, "%c", c );
        }

        fprintf ( out, " %d", y+1 );

        if ( y == 3 )
        {
            const char *side = theBoard.WhiteToMove() ? "White" : "Black";
            fprintf ( out, "    %s to move", side );
        }

        fprintf ( out, "\n" );
    }

    fprintf ( out, "\n      abcdefgh\n\n" );
}