Thread: debug assertion failed?!?

  1. #1
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179

    debug assertion failed?!?

    OK, so I'm working on a checkers ai that works by getting all the possible boards a certain number of turns out and playing towards the best one. My code doesn't get any compile time errors, just this weird "debug assertion failed" error that I've never seen before. Does anyone know what this means or how I can fix it? thx
    Illusion and reality become impartiality and confidence.

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    it could mean your forgetting to allocate some memory or are accessing memory that is not yours. can you post some code?

  3. #3
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    Here are the two functions that have anything to do with memory:
    Code:
    void aimove(board_t& board, char player, int difficulty) {
        char enemy = 'X';
        if (player == 'X') enemy = 'O';
        move_t *moves;
        int nummoves = 0, i, j, k, x, y;
        //first level
        for (i = 0; i < boardsize; ++i)
            for (j = 0; j < boardsize; ++j)
                if (board[i][j] == player) {
                    int destx[2], desty[2], vert = -1;
                    if (player == 'X') vert = 1;
                    destx[0] = i - 1;
                    desty[0] = j + vert;
                    destx[1] = i + 1;
                    desty[1] = j + vert;
                    for (k = 0; k < 2; ++k) {
                        if (board[destx[k]][desty[k]] && board[destx[k]][desty[k]] != player && 
                            !board[destx[k]+destx[k]-i][desty[k]+desty[k]-j]) {
                            destx[k] += destx[k] - i;
                            desty[k] += desty[k] - j;
                        }
                        if (destx[k] >= 0 && destx[k] < boardsize &&
                            desty[k] >= 0 && desty[k] < boardsize &&
                            !board[destx[k]][desty[k]]) {
                            if (!nummoves) {
                                moves = new move_t[1];
                                moves[0].fromx = i;
                                moves[0].fromy = j;
                                moves[0].tox = destx[k];
                                moves[0].toy = desty[k];
                                for (x = 0; x < boardsize; ++x)
                                    for (y = 0; y < boardsize; ++y)
                                        moves[0].board[x][y] = board[x][y];
                                ++nummoves;
                            } else {
                                addmove(board,nummoves,moves,i,j,destx[k],desty[k]);
                            }
                        }
                    }
                }
        //other levels
        char players[2] = {'X','O'};
        if (player == 'O') {
            players[0] = 'O';
            players[1] = 'X';
        }
        int playercount = 1, diffcount, move;
        for (diffcount = 0; diffcount < difficulty * 2; ++diffcount) {
            for (move = 0; move < nummoves; ++move) {
                board_t oldboard;
                for (i = 0; i < boardsize; ++i)
                    for (j = 0; j < boardsize; ++j)
                        oldboard[i][j] = moves[move].board[i][j];
                bool changed = false;
                for (i = 0; i < boardsize; ++i)
                    for (j = 0; j < boardsize; ++j) {
                        int destx[2], desty[2], vert = -1;
                        if (players[playercount] == 'X') vert = 1;
                        destx[0] = i - 1;
                        desty[0] = j + vert;
                        destx[1] = i + 1;
                        desty[1] = j + vert;
                        for (k = 0; k < 2; ++k) {
                            if (board[destx[k]][desty[k]] && 
                                board[destx[k]][desty[k]] != players[playercount] && 
                                !board[destx[k]+destx[k]-i][desty[k]+desty[k]-j]) {
                                destx[k] += destx[k] - i;
                                desty[k] += desty[k] - j;
                            }
                            if (destx[k] >= 0 && destx[k] < boardsize &&
                                desty[k] >= 0 && desty[k] < boardsize &&
                                !board[destx[k]][desty[k]]) {
                                if (!changed) {
                                    moves[move].board[destx[k]][desty[k]] = 
                                        moves[move].board[i][j];
                                    moves[move].board[i][j] = ' ';
                                    if (abs(destx[k]-i) > 1)
                                        moves[move].board[destx[k]-(destx[k]-i)/2]
                                            [desty[k]-(desty[k]-j)/2] = ' ';
                                    changed = true;
                                } else {
                                    addmove(oldboard,nummoves,moves,i,j,destx[k],desty[k]);
                                }
                            }
                        }
                    }
            }
            playercount = !playercount;
        }
        int maxdiff = INT_MIN, bestpos = rand()%nummoves;
        for (move = 0; move < nummoves; ++move) {
            int numplayer = 0, numenemy = 0;
            for (i = 0; i < boardsize; ++i)
                for (j = 0; j < boardsize; ++j) {
                    if (moves[move].board[i][j] == player) ++numplayer;
                    if (moves[move].board[i][j] == enemy) ++numenemy;
                }
            int diff = numplayer - numenemy;
            if (diff > maxdiff) {
                maxdiff = diff;
                bestpos = move;
            }
        }
        board[moves[bestpos].tox][moves[bestpos].toy] = 
            board[moves[bestpos].fromx][moves[bestpos].fromy];
        board[moves[bestpos].fromx][moves[bestpos].fromy] = ' ';
        if (abs(moves[bestpos].tox-moves[bestpos].fromx) > 1)
            board[(moves[bestpos].tox-moves[bestpos].fromx)/2]
                [(moves[bestpos].toy-moves[bestpos].fromy)/2] = ' ';
        delete [] moves;
    }
    
    void addmove(board_t oldboard,int& nummoves,move_t* moves, 
                 int fromx, int fromy, int tox, int toy) {
        int i, j;
        move_t newmove;
        newmove.fromx = fromx;
        newmove.fromy = fromy;
        newmove.tox = tox;
        newmove.toy = toy;
        for (i = 0; i < boardsize; ++i)
            for (j = 0; j < boardsize; ++j)
                newmove.board[i][j] = oldboard[i][j];
        newmove.board[tox][toy] = newmove.board[fromx][fromy];
        newmove.board[fromx][fromy] = ' ';
        if (abs(tox-fromx) > 1)
            newmove.board[fromx+(tox-fromx)/2][fromy+(toy-fromy)/2] = ' ';
    
        move_t* tempmoves = new move_t[nummoves];
        for (i = 0; i < nummoves; ++i)
            tempmoves[i] = moves[i];
        delete [] moves;
        ++nummoves;
        moves = new move_t[nummoves];
        for (i = 0; i < nummoves-1; ++i)
            moves[i] = tempmoves[i];
        delete [] tempmoves;
        moves[nummoves-1] = newmove;
    }
    this all seems pretty solid to me, I think maybe I'm just running out of memory?
    Illusion and reality become impartiality and confidence.

  4. #4
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    Code:
    addmove( oldboard, nummoves, moves, i, j, destx[k], desty[k] );
    This function modifies nummoves and moves.

    You pass a reference to nummoves, I suggest that you do the same with moves as well.
    moves is a pointer to a dynamic array of move_t structs, should this be move_t*& moves instead of move_t* moves?
    Illusion and reality become impartiality and confidence.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Assertion Failed!
    By IndioDoido in forum C Programming
    Replies: 31
    Last Post: 03-25-2008, 11:07 AM
  2. debug assertion failed!
    By chintugavali in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2007, 04:05 AM
  3. Visual Studio 2005 Debug Assertion Failed
    By natmas in forum C++ Programming
    Replies: 7
    Last Post: 07-17-2007, 04:28 PM
  4. debug assertion failed !
    By blue_gene in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2004, 11:23 AM
  5. Debug Assertion Failed!
    By Ray Schmidt in forum C++ Programming
    Replies: 3
    Last Post: 02-21-2003, 09:58 PM