3 Replies - 618 Views - Last Post: 08 December 2014 - 10:23 AM Rate Topic: -----

#1 DemonessaAdda   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 12-October 14

Problem with pointer

Posted 25 November 2014 - 11:14 AM

Hello. I have problem with moving objects. I have chess desk (8x8 cells - massive), figures(Pawns, Kings and etc) with coordinates and color, and square(rectangle), that I move and with it I choose, what figure I want to move. I click enter and if there are figure and square in this cell, programm remembers coordinates of figure(origin coordinats), and when i click enter other time, programm checks if there is a figure in that cell, deletes it and remembers destanation coordinates.Promlem is with moving pointers.
Function void Data, class Field. When I run the programm nothing happens when i click enter, square(class Chose moves, figures stay on there place)
class Figure and her "children" classes Pawns, Quenn and etc. 
...........
class Cell
{
      public:
        int x,y,size,color;
        Figure *figure;
      public:
      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)
              figure->draw();  
      }
      
};

struct data
{
       int code,i,j,desti,destj;
};       

class Field
{
      protected:
      int x,y,size;
      Cell f[8][8];
      Chose *ramka;
      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;
              ramka=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,65);
			f[6][i].figure=new Pawn(WHITE,x,315);
            }  
        ramka=new Chose(5,5,45,45);  
     }
        
void Data(data &mes)
{   
     switch(mes.code)
     {
     case 0: if(f[rama->y / rama->size][rama->x / rama->size].figure!=NULL) 
             {     
             mes.i=x;
             mes.j=y;  
             mes.code=1; break; 
             }
     case 1: if(mes.code==1 && getch()==13)
             { 
               mes.desti=x;
               mes.destj=y; 
               mes.code=2; break;
              }
     case 2: if(f[mes.j / 50][mes.i / 50].figure->IsLegalMove(mes.i,mes.j,mes.desti,mes.destj)==1 || f[mes.j / 50][mes.i / 50].figure->firstmove(mes.i,mes.j,mes.desti,mes.destj)==1)
             mes.code=3; break;
     }  
if(mes.code==3 && f[mes.j / 50][mes.i / 50].figure)
{
delete f[mes.j / 50][mes.i / 50].figure; // delete original and destination cells
delete f[mes.destj / 50][mes.desti / 50].figure;
delete [];
f[mes.destj / 50][mes.desti / 50].figure;  //put new pointer
f[mes.j / 50][mes.i / 50].showC();
f[mes.destj / 50][mes.desti / 50].showC(); // redraw 2 cells
}                                                                  
}

        
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();
          }
      }          

void Step(data &mes)
{
     switch(char c=getch())
     {
         case 0: k=5; break;
         case 80: k=0;rama->move(k); break;
         case 72: k=1;rama->move(k); break;
         case 77: k=2;rama->move(k); break;
         case 75: k=3;rama->move(k); break;
         case 27: k=4; break;
         case 13: if (mes.code!=1) mes.code=0; break; 
      }
}
         
};

int main()
{
initwindow(400,400);
Field desk(0,0,400);
desk.Show();
data mes;
Chose object=Chose(5,5,45,45);
object.dChose();
while(k!=4)
{
    desk.Step(mes);
}   

getch();
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with pointer

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

Re: Problem with pointer

Posted 25 November 2014 - 11:18 PM

http://en.wikipedia....ki/Indent_style
This is for line 100 onwards.

I've no idea why you should want (or need) so many delete calls just to move.

Yes, you need one delete call when one piece takes another piece.
But moving should simply be
f[newy][newx].figure = f[oldy][oldx].figure;
f[oldy][oldx].figure = 0;


And these lines should be encapsulated into a member function of Field, so you say
movePiece(oldx,oldy,newx,newy);


Was This Post Helpful? 1
  • +
  • -

#3 DemonessaAdda   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 12-October 14

Re: Problem with pointer

Posted 26 November 2014 - 12:04 PM

View PostSalem_c, on 25 November 2014 - 11:18 PM, said:

http://en.wikipedia....ki/Indent_style
I've no idea why you should want (or need) so many delete calls just to move.

This style is very unusual to me.
If I don't delete the old cell, the picture of figure stays there... Or no?
I did what you suggest, but still nothing happens when I click enter. I added function moveto(), I sent there my struct( struct includes coordinates new and old). I edited lies: 79-102 and in function main I added line 144.

struct data
{
       int code,i,j,desti,destj;
};       

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;
              ramka=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,65);
			f[6][i].figure=new Pawn(WHITE,x,315);
            }  
        rama=new Chose(5,5,45,45);  
     }
        
void Data(data &mes)
{   
     switch(mes.code)
     {
     case 0: if(getch()==13 && f[rama->y / rama->size][rama->x / rama->size].figure!=NULL) 
             {     
             mes.i=x;
             mes.j=y;  
             mes.code=1; break; 
             }
     case 1: if(mes.code==1 && getch()==13)
             { 
               mes.desti=x;
               mes.destj=y; 
               mes.code=2; break;
              }
     case 2: if(f[mes.j / 50][mes.i / 50].figure->IsLegalMove(mes.i,mes.j,mes.desti,mes.destj)==1 || f[mes.j / 50][mes.i / 50].figure->firstmove(mes.i,mes.j,mes.desti,mes.destj)==1)
             {
             mes.code=3;
             moveto(mes);
             break;
             }
             
     /*case 3: if(mes.code==3 && f[mes.destj / 50][mes.desti / 50].figure!=NULL)
             {
             {               
             delete f[mes.destj / 50][mes.desti / 50].figure;      
             delete f[mes.j / 50][mes.i / 50].figure;     
             }
             delete [] f;   
             mes.code==4; break;
             } */          
      }    
}   

void moveto(data &mes)
{   
f[mes.destj / 50][mes.desti / 50].figure=f[mes.j / 50][mes.i / 50].figure;
f[mes.j / 50][mes.i / 50].figure=0;
f[mes.destj / 50][mes.desti / 50].showC();
//f[mes.j / 50][mes.i / 50].showC();            
}

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();
          }
      }          

void Step(data &mes)
{
     switch(char c=getch())
     {
         case 0: k=5; break;
         case 80: k=0;rama->move(k); break;
         case 72: k=1;rama->move(k); break;
         case 77: k=2;rama->move(k); break;
         case 75: k=3;rama->move(k); break;
         case 27: k=4; break;
         //case 13: if (mes.code!=1) mes.code=0; break; 
      }
}
         
};

int main()
{
initwindow(400,400);
Field desk(0,0,400);
desk.Show();
data mes;
Chose object=Chose(5,5,45,45);
object.dChose();
while(k!=4)
{
    desk.Step(mes);
    
}  
desk.Data(mes); 
getch();
}

Was This Post Helpful? 0
  • +
  • -

#4 DemonessaAdda   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 12-October 14

Re: Problem with pointer

Posted 08 December 2014 - 10:23 AM

Hello) I dealt with moving problem, but I have a new one(( With redead figure. When figure is moved to another cell, I redraw this 2 cells, the old one is redraw good, but there is no figure in new one(both cells are empty). Can you,please help me with it?)
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() // draw cell with figure
      {
              setfillstyle(1,color);  
              bar(x,y,x+size,y+size);
              if (figure)
              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;
              ramka=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);
            }  
        ramka=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 / ramka->size][mes.i / ramka->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.destj / rama->size][mes.desti / rama->size].figure->draw();    
f[mes.j / rama->size][mes.i / rama->size].figure=NULL;
f[mes.j / rama->size][mes.i / rama->size].showC();          
}




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1