Thread: Draw and move

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    3

    Draw and move

    Hello. I've been working on simple Chess game. I got troble, when I move figure from one cell to another, the figure wipes off in the old cell, but doesn't appeaer in new one, although there is a pointer to figure in new cell. Can anyone offer some ideas what's is wrong? Thanks) This is part of code, when move realizes.
    Code:
    class Cell
    {
          public:
            int x,y,size,color;
            Figure *figure;
          Cell(int cx=0,int cy=0,int cl=LIGHTGRAY,int csize=40)
                   {
                   x=cx;
                   y=cy;
                   color=cl;
                   size=csize;
                   figure=NULL; 
                   }
          void showC()
          {
                  setfillstyle(1,color);  
                  bar(x,y,x+size,y+size);
                  if (figure!=NULL)
                  figure->draw();  
          }
          int getcolor()
          {
              return color;
          }   
    };
    
    class Field
    {
          protected:
          int x,y,size;
          Cell f[8][8];
          Chose *rama;
          Figure *figure;
          public: 
          Field(int fx,int fy,int fsize=400)
          {
                  x=fx;
                  y=fy;
                  size=fsize;
                  int sc=size/8;
                  if(size%8!=0)
                  size=sc*8;
                  rama=NULL;
          for(int i=0;i<8;i++)
              {
              for(int j=0;j<8;j++) 
                {
                   if((i+j)%2==0)
                      f[i][j]=Cell(j*sc,i*sc,LIGHTGRAY,sc);
                      else
                      f[i][j]=Cell(j*sc,i*sc,DARKGRAY,sc);
                }
              }
              f[0][0].figure=new Rook(BLACK,20,15);
              f[0][1].figure=new Horse(BLACK,70,15);
              f[0][2].figure=new Bishop(BLACK,120,15);
              f[0][3].figure=new King(BLACK,170,15);
              f[0][4].figure=new Queen(BLACK,220,15);
              f[0][5].figure=new Bishop(BLACK,270,15);
              f[0][6].figure=new Horse(BLACK,320,15);
              f[0][7].figure=new Rook(BLACK,370,15);
              
              f[7][0].figure=new Rook(WHITE,20,365);
              f[7][1].figure=new Horse(WHITE,70,365);
              f[7][2].figure=new Bishop(WHITE,120,365);
              f[7][3].figure=new Queen(WHITE,170,365);
              f[7][4].figure=new King(WHITE,220,365);
              f[7][5].figure=new Bishop(WHITE,270,365);
              f[7][6].figure=new Horse(WHITE,320,365);
              f[7][7].figure=new Rook(WHITE,370,365);
              
    
    
             for (int i=0;i<8;i++) 
                {
                x=20+sc*i;
                 f[1][i].figure=new Pawn(BLACK,x,55);
                f[6][i].figure=new Pawn(WHITE,x,305);
                }  
            rama=new Chose(5,5,45,45);  
         }
            
    void Show()
          {
               x=0;y=0;
            rectangle(x,y,x+size,y+size);   
           for(int i=0;i<8;i++)
              {   
              for(int j=0;j<8;j++)
                f[i][j].showC();
              }
            rama->dChose();      
          }    
    
    
               
    void Data(data &mes)
    {   
         mes.color=f[rama->y / rama->size][rama->x / rama->size].getcolor();   
         switch(mes.code)
         {
         case 0: if(f[rama->y / rama->size][rama->x / rama->size].figure!=NULL )
                 {   
                 mes.i=rama->x;
                 mes.j=rama->y;  
                 mes.code=1; break; 
                 }
         case 1: if(getch()==13)
                 { 
                   mes.desti=rama->x;
                   mes.destj=rama->y; 
                   if(f[mes.j / rama->size][mes.i / rama->size].figure->IsLegalMove(mes.i,mes.j,mes.desti,mes.destj)==1)
                   moveto(mes);
                   mes.code=2;
                   break;
                  }           
    
    
         } 
       
            
    }   
       
       
       
    void moveto(data &mes)
    {   
    f[mes.destj / rama->size][mes.desti / rama->size].figure=f[mes.j / rama->size][mes.i / rama->size].figure;
    f[mes.destj / rama->size][mes.desti / rama->size].showC();
    f[mes.j / rama->size][mes.i / rama->size].figure=NULL;
    f[mes.j / rama->size][mes.i / rama->size].showC();          
    }
    
    
    };

  2. #2
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    For readability I would recommend replacing the 1 & 0 constants of the switch statement ( Data( data &mes) ) with either enumerations or other named constants. Also mixing the notation of (x,y) and (i,j) is a bit confusing. Also, remember to use good indention.

    Without knowing how you are rendering, or in what order it's hard to evaluate anything but the logic of how you are moving. It looks like in the function moveto() you could be assigning a pointer to figure to the new position, and then assigning that pointer NULL a few lines later. As your code appears to be doing a NULL check to decide whether to render the figure, it wouldn't show up if this is the case.

    Code:
    f[mes.destj / rama->size][mes.desti / rama->size].figure=f[mes.j / rama->size][mes.i / rama->size].figure;
    f[mes.destj / rama->size][mes.desti / rama->size].showC();
    f[mes.j / rama->size][mes.i / rama->size].figure=NULL;
    f[mes.j / rama->size][mes.i / rama->size].showC(); 
    Edit: On second thought, that looks like a red herring. Maybe check to make sure you aren't rendering over the figure while drawing the board.
    Last edited by Alpo; 12-09-2014 at 12:06 PM.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    3
    Quote Originally Posted by Alpo View Post
    Without knowing how you are rendering, or in what order it's hard to evaluate anything but the logic of how you are moving. It looks like in the function moveto() you could be assigning a pointer to figure to the new position, and then assigning that pointer NULL a few lines later. As your code appears to be doing a NULL check to decide whether to render the figure, it wouldn't show up if this is the case.

    Code:
    f[mes.destj / rama->size][mes.desti / rama->size].figure=f[mes.j / rama->size][mes.i / rama->size].figure;
    f[mes.destj / rama->size][mes.desti / rama->size].showC();
    f[mes.j / rama->size][mes.i / rama->size].figure=NULL;
    f[mes.j / rama->size][mes.i / rama->size].showC(); 
    Edit: On second thought, that looks like a red herring. Maybe check to make sure you aren't rendering over the figure while drawing the board.


    I don't think I render drawing board over figures, I call for Data(Game) after drawing the board.
    Code:
    int main()
    {
    initwindow(400,400);
    Field desk(0,0,400);
    desk.Show();
    data mes;
    mes.code=5;
    while(k!=4)
    {
          desk.Game(mes); // Game includes Data        
    }             
    
    
    getch();
    }
    About function moveto(). What I think is going on there:
    Code:
    f[mes.destj / rama->size][mes.desti / rama->size].figure=f[mes.j / rama->size][mes.i / rama->size].figure; // I assign pointer of figure in the old cell to new cell(now there is a poiner to figure in the new cell
    f[mes.destj / rama->size][mes.desti / rama->size].showC(); // redraw new cell, and now, that there is a pointer, it should be with picture 
    f[mes.j / rama->size][mes.i / rama->size].figure=NULL; // delete pointer in old cell
    f[mes.j / rama->size][mes.i / rama->size].showC(); // redraw old cell, wipes off figure and now it's an empty cell

    Am I right? Or the pointer in a new cell is NULL? Do I assign a pointer to new cell correctly? I have another thought, maybe something's wront with figures' coordinates? For example,
    Code:
    f[0][1].figure=new Horse(BLACK,70,15);
    This figure has these coordinates. Do they change when I move it? Cos I think, I don't change coordinates. Or they change?

    Code:
    virtual void draw()
    {                     
         if(color==BLACK)
          setcolor(WHITE);
         else 
              setcolor(BLACK);
         setbkcolor(color);
         outtextxy(x,y,"H");
    }

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by Oxyis View Post
    I don't think I render drawing board over figures, I call for Data(Game) after drawing the board.
    Code:
    int main()
    {
    initwindow(400,400);
    Field desk(0,0,400);
    desk.Show();
    data mes;
    mes.code=5;
    while(k!=4)
    {
          desk.Game(mes); // Game includes Data        
    }             
    
    
    getch();
    }
    About function moveto(). What I think is going on there:
    Code:
    f[mes.destj / rama->size][mes.desti / rama->size].figure=f[mes.j / rama->size][mes.i / rama->size].figure; // I assign pointer of figure in the old cell to new cell(now there is a poiner to figure in the new cell
    f[mes.destj / rama->size][mes.desti / rama->size].showC(); // redraw new cell, and now, that there is a pointer, it should be with picture 
    f[mes.j / rama->size][mes.i / rama->size].figure=NULL; // delete pointer in old cell
    f[mes.j / rama->size][mes.i / rama->size].showC(); // redraw old cell, wipes off figure and now it's an empty cell

    Am I right? Or the pointer in a new cell is NULL? Do I assign a pointer to new cell correctly?
    This looks correct, I had gotten what you were doing mixed up with the lone Figure* in the Field class.

    For setting the positions and sizes, I would recommend deriving the width and height values you are moving by from the size value the Field class is being initialized with (you are requiring the board be a certain size).


    For instance, in Fields constructor :

    Code:
        const int squaresRoot = 8;
        const int margin = 20;
        /...
        
        int width = fsize / squaresRoot;
    
        f[0][0].figure=new Rook(BLACK,margin ,15);  
        f[0][1].figure=new Horse(BLACK,margin + width ,15);
        width += width;
      // ect.. 
    


    It is hard to tell if the positions are changing on a move, as I don't know what mes::code means (you should definitely be using constants to solve this). You should go through with a debugger (or use printing functions) to check the values. Pay attention to both positions and color of the figure being drawn, and check them against the corresponding cells.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  5. #5
    Registered User
    Join Date
    Dec 2014
    Posts
    3
    Quote Originally Posted by Alpo View Post

    It is hard to tell if the positions are changing on a move, as I don't know what mes::code means (you should definitely be using constants to solve this). You should go through with a debugger (or use printing functions) to check the values. Pay attention to both positions and color of the figure being drawn, and check them against the corresponding cells.
    Thanks for help, but using const didn't help(
    mes.code means evants: mes.cod=0 - evant 1, enter was clicked
    mes.code=1 - evant 2, enter was clicked the second time and here move figure happens

    I tried printf function, and it shoes right values for mes.i, mes.j, mes.desti, mes.destj and for
    f[mes.destj / rama->size][mes.desti / rama->size]
    f[mes.j / rama->size][mes.i / rama->size]

    And debugger shows, thar there is a pionter in
    Code:
    f[mes.destj / rama->size][mes.desti / rama->size], ma->size].showC();
    Could you please look at the pictures.
    Second picture is for line
    Code:
    f[mes.destj / rama->size][mes.desti / rama->size].figure=f[mes.j / rama->size][mes.i / rama->size].figure;
    First picture after moveto()
    Coordinates aren't match.
    Attached Images Attached Images Draw and move-3-png Draw and move-1-png 

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About the use of std::move()...
    By MutantJohn in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2014, 09:58 AM
  2. how to * move????
    By shah18 in forum C Programming
    Replies: 5
    Last Post: 10-04-2010, 11:38 AM
  3. Want to move on
    By the3rror in forum C Programming
    Replies: 7
    Last Post: 12-30-2007, 11:34 AM
  4. Should i move on ?
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 08-05-2002, 01:15 AM
  5. want to move bmp
    By b_amit4u in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 03:40 AM