Chess Mess

This is a discussion on Chess Mess within the C++ Programming forums, part of the General Programming Boards category; I'm making a chess game. I had it pretty much working, but when a player took a piece weird stuff ...

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Chess Mess

    I'm making a chess game. I had it pretty much working, but when a player took a piece weird stuff happened. Anyway the way my object were set up sucked, so I have been trying to redesign it.

    The 'board' object has an 8*8 array which either holds a null value or a pointer to a 'piece' object. When the board is constructed the grid is initialized:
    Code:
           void NewPiece(int x, int y, bool i)
            {
                cell[x][y] = new piece;
                cell[x][y]->SetOwner(i);
                if(y==0 || y==7) 
                    if(x==0 || x==7)      cell[x][y]->SetType('R', 'k');
                    else if(x==1 || x==6) cell[x][y]->SetType('K', 't');
                    else if(x==2 || x==5) cell[x][y]->SetType('B', 's');
                    else if(x==4)         cell[x][y]->SetType('K', 5);
                    else if(x==3)         cell[x][y]->SetType('Q', 6);
                else                  
                    cell[x][y]->SetType('P', 'n');  
            }
            
            board()
            {
                int x, y;
                go=0; turn=1;
                for(y=0; y<2; y++) pl[y] = new player;
                for(y=0; y<8; y++) for(x=0; x<8; x++) cell[x][y]=NULL;
                for(y=0; y<2; y++) for(x=0; x<8; x++) NewPiece(x, y, false);
                for(y=6; y<8; y++) for(x=0; x<8; x++) NewPiece(x, y, true);    
            }
    The chess board is then drawn when the program begins:
    Code:
    void board::DrawBoard()
    {
        for(int y=0; y<18; y++)
            for(int x=0; x<26; x++)
                if(y==0)
                    if(x==25)       printf(" \n");
                    else if(x&#37;3==2) putchar(65+(x/3));   
                    else            putchar(' ');
                else if (y==1)
                    if(x==0)        putchar(' ');
                    else if(x==1)   putchar(218);
                    else if(x==25)  printf("%c\n", 191);
                    else if(x%3==1) putchar(194); 
                    else            putchar(196);
                else if(y==17)
                    if(x==0)        putchar(' ');
                    else if(x==1)   putchar(192);
                    else if(x==25)  printf("%c\n", 217);
                    else if(x%3==1) putchar(193); 
                    else            putchar(196);
                else if(y%2==1)
                    if(x==0)        putchar(' ');
                    else if(x==1)   putchar(195);
                    else if(x==25)  printf("%c\n", 180);
                    else if(x%3==1) putchar(197); 
                    else            putchar(196);
                else  
                    if (x==0)       putchar(48+(y/2));
                    else if(x==25)  printf("%c\n", 179);
                    else if(x%3==1) putchar(179); 
                    else if(x%3==2) DrawCell(x/3, (y/2)-1);    
    }   
    
    void board::DrawCell(int x, int y)
    {  
        int i;
        bool own;
        
        if(cell[x][y]==NULL) //Empty Cell
        {
            if(x%2==0 && y%2==0 || x%2==1 && y%2==1) SetConsoleTextAttribute ( h, BACKGROUND_BLUE | 0 );  
            else                                     SetConsoleTextAttribute ( h, BACKGROUND_GREEN | 0 );  
            printf("  ");   
        }
        else                 //There is a piece in the Cell
        {
            own=GetOwner(x, y);
            if(own == true)//Check owner colour
                if(x%2==0 && y%2==0 || x%2==1 && y%2==1)//Check tile colour
                    SetConsoleTextAttribute ( h, BACKGROUND_BLUE | 8 );  
                else
                    SetConsoleTextAttribute ( h, BACKGROUND_GREEN | 8 );  
            else 
                if(x%2==0 && y%2==0 || x%2==1 && y%2==1)
                    SetConsoleTextAttribute ( h, BACKGROUND_BLUE | 0 );
                else
                    SetConsoleTextAttribute ( h, BACKGROUND_GREEN | 0 );
            
            putchar(cell[x][y]->GetType(0));
            putchar(cell[x][y]->GetType(1));
        }
    
        SetConsoleTextAttribute ( h, 8 ); 
    }
    When the program runs I get a MAV when I try an access a piece through the pointer in the grid cell. (in bold)

    Also here is the get owner function:
    Code:
    bool GetOwner(int x, int y) { return cell[x][y]->GetOwner(); }
    Any suggestions on how I could fix this?

    Cheers.
    Last edited by mike_g; 06-06-2007 at 02:29 PM.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    for(y=0; y<2; y++) for(x=0; x<8; x++) cell[x][y]=NewPiece(x, y, false);
    here you assign the return value of function NewPiece() to a cell.
    Trouble is NewPiece() doesn't return anything useful.
    Kurt
    Last edited by ZuK; 06-06-2007 at 02:24 PM.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yeah, my bad. It was a change I made to it that I forgot to fix again. The NewPiece() functions meant to be a void function and the code to set up the pieces is meant to be:
    Code:
                for(y=0; y<2; y++) for(x=0; x<8; x++) NewPiece(x, y, false);
                for(y=6; y<8; y++) for(x=0; x<8; x++) NewPiece(x, y, true);
    Sorry about that. The problem though, is that the prog still crashes at the same point.

    [edit] I edited the original code so that bits sorted out now. Thanks for pointing it out.[/edit]
    Last edited by mike_g; 06-06-2007 at 02:31 PM.

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Ok I found the problem. Once again its to do with my sloppy indentation and lack of brackets in the new piece function. I think I have to stop pretending I'm clever :/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Chess and Heuristics
    By Happy_Reaper in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-14-2006, 12:05 PM
  2. chess program.
    By myk_raniu in forum C++ Programming
    Replies: 2
    Last Post: 12-19-2005, 02:32 PM
  3. chess ai contest
    By Raven Arkadon in forum Contests Board
    Replies: 7
    Last Post: 07-09-2005, 06:38 AM
  4. Ultra chess engine contest
    By yodacpp in forum Projects and Job Recruitment
    Replies: 8
    Last Post: 11-21-2004, 06:58 AM
  5. Ultra chess engine
    By yodacpp in forum Game Programming
    Replies: 2
    Last Post: 11-19-2004, 11:33 AM

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