help debugging a checkers engine

This is a discussion on help debugging a checkers engine within the C++ Programming forums, part of the General Programming Boards category; The problem is on line 85. The pieces move fine, but can't jump. I'm pretty sure the problem is somewhere ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    9

    help debugging a checkers engine

    The problem is on line 85. The pieces move fine, but can't jump. I'm pretty sure the problem is somewhere in the conditions on my 'if' statement in there. If you want to see the problem in runtime, just compile, and move pieces by using the coordinates on the sides of the board. moving a piece from square (3, 4) to (4, 5) gets inputted to the program as '3445'.

    Code:
    /*
    A checkers engine, for future use in programs
    
    by Alex Borland
    */
    
    #include <iostream>
    using namespace std;
    
    struct square{
           int status;
    };
    square board[12][12];//global multidimensional array of squares
    
    void checkForWin(){
         int blackPieces = 0;
         int redPieces = 0;
         int score = 0;
         int m;
         int n;
         for (m=0; m<12; m++){
             for (n=0; n<12; n++){
                 if (board[m][n].status==1){
                    blackPieces ++;
                    score += 1;
                 } else if (board[m][n].status==2){
                    blackPieces ++;
                    score += 2;
                 } else if (board[m][n].status==-1){
                    redPieces ++;
                    score -= 1;
                 } else if (board[m][n].status==-1){
                    redPieces ++;
                    score -= 2;
                 }
             }
         }
         cout << "score: " << score << endl;
         cout << "black pieces: " << blackPieces << endl;
         cout << "red pieces: " << redPieces << endl;
         if (blackPieces==0){
            cout << "\n!!!!!RED WINS!!!!!" << endl;
         }
         if (redPieces==0){
            cout << "\n!!!!!BLACK WINS!!!!!" << endl;
         }
    }
    
    void printBoard(){
         system("cls");
         cout << "\n";
         checkForWin();
         cout << "\n";
         int q;
         int r;
         for (q=9; q>1; q--){
             cout << q << "  ";
             if (q<10){
                cout << " ";
             }
             for (r=2; r<10; r++){
                 if (board[r][q].status>=0){
                    cout << " ";
                 }
                 cout << board[r][q].status <<  " ";
             }
             cout << "\n\n";
         }
         cout << "\n     2  3  4  5  6  7  8  9";
    }
    
    bool moveBlackPiece(string moveCode){
         bool isJump;
         int x1 = (int)moveCode[0]-48;
         int y1 = (int)moveCode[1]-48;
         int x2 = (int)moveCode[2]-48;
         int y2 = (int)moveCode[3]-48;
         switch (board[x1][y1].status){
                case(1):
                        if ((x1-x2==1||x1-x2==-1) && (y1-y2==-1) && (board[x2][y2].status==0)){
                               board[x1][y1].status = 0;
                               board[x2][y2].status = 1;
                               return true;
    /*the problem is in the below if statement, and all of the similar ones, it marks an attempted jump as illegal*/
                        } else if ((x1-x2==2||x1-x2==-2) && (y1-y2==-2) && (board[x2][y2].status==0) && (board[x1-(x1-x2)/2][y1-(y1-y1)/2].status==-1||board[x1-(x1-x2)/2][y1-(y1-y1)/2].status==-2)){                                  
                               board[x1][y1].status = 0;
                               board[x2][y2].status = 1;
                               board[x1-(x1-x2)][y1-(y1-y1)].status = 0;
                               isJump = true;
                               return true;
                        }
                        cout << board[x1-(x1-x2)/2][y1-(y1-y2)/2].status << endl;
                        break;
                case(2):
                        if ((x1-x2==1||x1-x2==-1) && (y1-y2==1||y1-y2==-1) && (board[x2][y2].status==0)){
                               board[x1][y1].status = 0;
                               board[x2][y2].status = 2;
                               return true;
                        } else if ((x1-x2==2||x1-x2==-2) && (y1-y2==2||x1-y1==-2) && (board[x2][y2].status==0) && (board[x1-(x1-x2)/2][y1-(y1-y1)/2].status==1||board[x1-(x1-x2)/2][y1-(y1-y1)/2].status==2)){
                               board[x1][y1].status = 0;
                               board[x2][y2].status = 2;
                               board[x1-(x1-x2)][y1-(y1-y1)].status = 0;
                               isJump = true;
                               return true;
                        }
                        break;
                default:
                        return false;
                        break;
         }
    
    }
    
    bool moveRedPiece(string moveCode){
         bool isJump;
         int x1 = (int)moveCode[0]-48;
         int y1 = (int)moveCode[1]-48;
         int x2 = (int)moveCode[2]-48;
         int y2 = (int)moveCode[3]-48;
         switch (board[x1][y1].status){
                case(-1):
                        if ((x1-x2==1||x1-x2==-1) && (y1-y2==1) && (board[x2][y2].status==0)){
                               board[x1][y1].status = 0;
                               board[x2][y2].status = -1;
                               return true;
                        } else if ((x1-x2==2||x1-x2==-2) && (y1-y2==2) && (board[x2][y2].status==0) && (board[x1-(x1-x2)/2][y1-(y1-y1)/2].status==1||board[x1-(x1-x2)/2][y1-(y1-y1)/2].status==2)){
                               board[x1][y1].status = 0;
                               board[x2][y2].status = -1;
                               board[x1-(x1-x2)][y1-(y1-y1)].status = 0;
                               isJump = true;
                               return true;
                        }
                        break;
                case(-2):
                        if ((x1-x2==1||x1-x2==-1) && (y1-y2==1||y1-y2==-1) && (board[x2][y2].status==0)){
                               board[x1][y1].status = 0;
                               board[x2][y2].status = -2;
                               return true;
                        } else if ((x1-x2==2||x1-x2==-2) && (y1-y2==2||y1-y2==-2) && (board[x2][y2].status==0) && (board[x1-(x1-x2)/2][y1-(y1-y1)/2].status==1||board[x1-(x1-x2)/2][y1-(y1-y1)/2].status==2)){
                               board[x1][y1].status = 0;
                               board[x2][y2].status = -2;
                               board[x1-(x1-x2)][y1-(y1-y1)].status = 0;
                               isJump = true;
                               return true;
                        }
                        break;
                default:
                        return false;
                        break;
         }
    
    }
    
    int main(){
    //--------------declare board
    int a;
    int b;
    for (a=0; a<12; a++){
        if (1<a && a<10){
        board[a][0].status = 9;
        board[a][1].status = 9;
        board[a][2].status = 0;
        board[a][3].status = 0;
        board[a][4].status = 0;
        board[a][5].status = 0;
        board[a][6].status = 0;
        board[a][7].status = 0;
        board[a][8].status = 0;
        board[a][9].status = 0;
        board[a][10].status = 9;
        board[a][11].status = 9;
        } else {
        board[a][0].status = 9;
        board[a][1].status = 9;
        board[a][2].status = 9;
        board[a][3].status = 9;
        board[a][4].status = 9;
        board[a][5].status = 9;
        board[a][6].status = 9;
        board[a][7].status = 9;
        board[a][8].status = 9;
        board[a][9].status = 9;
        board[a][10].status = 9;
        board[a][11].status = 9;
        }
    }
    
    cout << "board initialized successfully" << endl;
    //-------------------done declaring board, start placing pieces
    /*this section of the program can be edited to manipulate the position the pieces start at
    ~a black piece is a 1, black kings are 2
    ~a red piece is -1, red kings are -2
    */
    board[2][3].status = 1;
    board[3][2].status = 1;
    board[3][4].status = 1;
    board[4][3].status = 1;
    board[5][2].status = 1;
    board[5][4].status = 1;
    board[6][3].status = 1;
    board[7][2].status = 1;
    board[7][4].status = 1;
    board[8][3].status = 1;
    board[9][2].status = 1;
    board[9][4].status = 1;
    
    board[2][9].status = -1;
    board[2][7].status = -1;
    board[3][8].status = -1;
    board[4][9].status = -1;
    board[4][7].status = -1;
    board[5][8].status = -1;
    board[6][9].status = -1;
    board[6][7].status = -1;
    board[7][8].status = -1;
    board[8][9].status = -1;
    board[8][7].status = -1;
    board[9][8].status = -1;
    //----------------------------------done setting up board
    cout << "board set up\n\n" << endl;
    printBoard();
    cout << "\n\n";
    int h = 2;
    while (1){
    string i;
    if (h%2==1){
    cout << "red\'s move" << endl;
    } else {
    cout << "black\'s move" << endl;
    }
    cout << "please enter your desired move's code" << endl;
    cin >> i;
    if (h%2==0){
       while (!moveBlackPiece(i)){
           cout << "illegal move, please try again" << endl;
           cin >> i;
       }
    } else {
       while (!moveRedPiece(i)){
           cout << "illegal move, please try again" << endl;
           cin >> i;
       }
    }
    
    h ++;
    cout << "\n\n";
    printBoard();
    cout << "\n\n";
    }
    return 0;
    }
    I know my indentation is a little sloppy and my commenting is almost nonexistant, but any help is appreciated .

    EDIT: to the guy below me, The first sentance in my post narrows it to one line. I posted the whole program for completeness. If I have to be completely specific, the same issue happens on lines 85, 100, 127, and 140.
    Last edited by Jherry; 08-03-2005 at 05:22 PM.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Have you attempted to narrow down the problem at all? I don't think many people want to read through lines upon lines of code just for you.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Should
    Code:
    y1-(y1-y1)/2
    be
    Code:
    y1-(y1-y2)/2
    ?

    I don't know, I'm just asking, since the first one doesn't seem to make sense.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    9
    >.< I didn't catch that. Thanks for your help. You probably saved me hours of scratching my head looking at the wrong piece of code .

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    I stand corrected.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 10:30 AM
  2. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-25-2006, 11:14 PM
  3. Ultra chess engine contest
    By yodacpp in forum Projects and Job Recruitment
    Replies: 8
    Last Post: 11-21-2004, 06:58 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  5. Game structure, any thoughts?
    By Vorok in forum Game Programming
    Replies: 2
    Last Post: 06-07-2003, 01:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21