Anyone can help to modified this program?

  • Thread starter Thread starter pajak
  • Start date Start date
  • Tags Tags
    Program
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
5 replies · 3K views
pajak
Messages
4
Reaction score
0
my friend had send to me a coding simulation of bugs life
but this program only can running in TC++ 3.0 or higher

how to modified it able to run in Codeblocks or Devc++...anyone can help me

program:
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int a[20][20],p=0,q=0;
void step(int g,int h,int i,int &u,int &v);

class org
  {
     public :
         int x,y,life;
  };
  class bug:public org
   {
     public:
        void birth(int e,int f)
         {
           x=e;
           y=f;
           life=0;
           a[e][f]=2;
           p++;
         }
        void move();
        void breed();
        void death()
         {
           a[x][y]=0;
         }
   }bugs[400];
   void bug::move()
    {
      int k=0,t1,t2,i;
       life++;
      for(i=0;i<4;i++)
       {
     step(x,y,i,t1,t2);
     if(a[t1][t2]==0)
     k++;
       }
      if(k>0)
       {
     if(k!=1)
       k=(rand()%k+1);
     for(i=0;i<4,k>0;i++)
      {
        step(x,y,i,t1,t2);
        if(a[t1][t2]==0)
        k--;
      }
     a[x][y]=0;
     a[t1][t2]=2;
     x=t1;
     y=t2;
     if(life>=3)
     {
       this->breed();
       life=0;
     }
       }
    }
   void bug::breed()
    {
      int i,k=0,t1,t2;
      for(i=0;i<4;i++)
       {
     step(x,y,i,t1,t2);
     if(a[t1][t2]==0)
        k++;
       }
      if(k!=1)
       k=(rand()%k+1);
      for(i=0;i<4,k>0;i++)
       {
    step(x,y,i,t1,t2);
    if(a[t1][t2]==0)
     k--;
       }
      bugs[p].birth(t1,t2);

    }
  class dood:public org
  {
    public:
       int  steps;
       void birth(int e,int f)
       {
         x=e;
         y=f;
         life=3;
         steps=0;
         a[e][f]=1;
         q++;
       }
       void move();
       void breed();
       void death()
       {
        a[x][y]=0;
       }
  }doodle[400];
  void shiftb();
  void shiftd();
  void dood::move()
   {
     int k=0,t1,t2;
      steps ++;
     for(int i=0;i<4;i++)
      {
    step(x,y,i,t1,t2);
     if(a[t1][t2]==2)
    k++;
      }
     if(k==0)
      life--;
     else
      {
       if(k!=1)
    k=(rand()%k+1);
       for(i=0;i<4,k>0;i++)
    {
     step(x,y,i,t1,t2);
     if(a[t1][t2]==2)
     k--;
    }
       life=3;
       for(int j=0;j<p;j++)
    {
     if((bugs[j].x==t1)&&(bugs[j].y==t2))
       { bugs[j].death();
         shiftb();
       }
    }
       a[this->x][this->y]=0;
       a[t1][t2]=1;
       this->x=t1;
       this->y=t2;

      }
       if(steps>=8)
      this->breed();
       if(life<=0)
    this->death();
   }
  void dood::breed()
   {
     int i,k=0,t1,t2;
     for(i=0;i<4;i++)
      {
    step(x,y,i,t1,t2);
    if(a[t1][t2]==0)
    k++;
      }
     if(k>0)
      {
    if(k!=1)
     k=(rand()%k+1);
    for(i=0;i<4,k>0;i++)
     {
       step(x,y,i,t1,t2);
       if(a[t1][t2]==0)
         k--;
     }
     doodle[q].birth(t1,t2);
      }
   }
void display()
  {
    int  i,j;
    char c;
    clrscr();
    for(i=0;i<20;i++)
     {
       for(j=0;j<20;j++)
    {
      if(a[i][j]==0)
       c='-';
      if(a[i][j]==1)
       c='x';
      if(a[i][j]==2)
       c='o';
      cout<<c<<" ";
    }
       cout<<"\n";
     }
  }
void step(int g,int h,int i,int &u,int &v)
  {
   u=g;
   v=h;
   if(i==0)
    u--;
   if(i==1)
    v++;
   if(i==2)
    u++;
   if(i==3)
    v--;
   if(u<0||v<0||u>19||v>19)
    {
      u=g;
      v=h;
    }
  }
void shiftb()
  { int j,k;
    if(p!=1)
    for( j=0;j<p;)
     {
      if(a[bugs[j].x][bugs[j].y]==0)
       {
    p--;
    for(int k=j;k<p;k++)
    bugs[k]=bugs[k+1];
       }
      else
       j++;
     }
   if(p==1)
     if(a[bugs[0].x][bugs[0].y]==0)
       p--;
  }
void shiftd()
  { int j,k;
    if(q!=1)
    for( j=0;j<q;)
     {
      if(a[doodle[j].x][doodle[j].y]==0)
       {
    q--;
    for(int k=j;k<q;k++)
    doodle[k]=doodle[k+1];
       }
      else
       j++;
     }
   if(q==1)
     if(a[doodle[0].x][doodle[0].y]==0)
       q--;
  }
void main()
{
   clrscr();
   int m,n,i,j,s,t;
   char ch='y';
   cout<<"enter how many doodles & bugs do u wany?"<<endl;
   cin>>s>>t;
   for(i=0;i<20;i++)
    for(j=0;j<20;j++)
     a[i][j]=0;
   randomize();
   for(i=0;i<s;i++)
    {
     do {
     m=rand()%20;
     n=rand()%20;
       }while(a[m][n]!=0);
     doodle[i].birth(m,n);
    }
   for(i=0;i<t;i++)
    {
     do{
    m=rand()%20;
    n=rand()%20;
       }while(a[m][n]!=0);
     bugs[i].birth(m,n);
    }
   display ();
   ch=getch();
   while(ch=='y'||ch=='Y')
   {
     for(i=0;i<q;i++)
      doodle[i].move();
     shiftd();
   for(i=0;i<p;i++)
    bugs[i].move();

   display();
   cout<<"do u want to go to next time step press y for yes";
   ch=getche();
}
}
 
Physics news on Phys.org
Yuck. This code is very difficult to read, and I consider myself a messy coder...

What you want to do is try and compile it with the desired compiler and sift through the errors it spits out one by one, then eliminate them. If you get stuck you can always post those up here for help, although you will get a faster result if you google the error line. The output on error for most compiler errors in most compilers will usually google to the answer you want.

If you were consistent with the spaces, tabs and returns it would look a lot tidier and be easier to read, as it stands I keep getting confused about the scope because of the bad {} placement.. comments would help too.
 
Yikes! What's with the formatting? It looks like an auto-indent gone awry.

I'll see what I can do.
 
I'm almost tempted to give this a code critique...

Here's the Dev-C++ compatible code, with fixed indenting.

Code:
#include <iostream>
#include <time.h>
using namespace std;

int a[20][20], p=0, q=0;
void step(int g, int h, int i, int &u, int &v);

class org
{
    public:
        int x, y, life;
};

class bug:public org
{
    public:
        void birth(int e, int f)
        {
            x = e;
            y = f;
            life = 0;
            a[e][f] = 2;
            p++;
        }
        void move();
        void breed();
        void death()
        {
            a[x][y] = 0;
        }
} bugs[400];

void bug::move()
{
    int k = 0, t1, t2, i;
    life++;
    for(i=0; i<4; i++)
    {
        step(x, y, i, t1, t2);
        if(a[t1][t2] == 0)
            k++;
    }
    if(k > 0)
    {
        if(k != 1)
            k=(rand()%k+1);
        for(i=0; i<4,k>0; i++)
        {
            step(x, y, i, t1, t2);
            if(a[t1][t2] == 0)
                k--;
        }
        a[x][y] = 0;
        a[t1][t2] = 2;
        x = t1;
        y = t2;
        if(life >= 3)
        {
            this->breed();
            life = 0;
        }
    }
}
   
void bug::breed()
{
    int i, k = 0, t1, t2;
    for(i=0; i<4; i++)
    {
        step(x, y, i, t1, t2);
        if(a[t1][t2] == 0)
            k++;
    }
    if(k != 1)
        k = (rand()%k+1);
    for(i=0; i<4, k>0; i++)
    {
        step(x, y, i, t1, t2);
        if(a[t1][t2]==0)
            k--;
    }
    bugs[p].birth(t1,t2);
}

class dood:public org
{
    public:
        int steps;
        void birth(int e, int f)
        {
            x = e;
            y = f;
            life = 3;
            steps = 0;
            a[e][f] = 1;
            q++;
        }
        void move();
        void breed();
        void death()
        {
            a[x][y] = 0;
        }
} doodle[400];

void shiftb();
void shiftd();
void dood::move()
{
    int k = 0, t1, t2, i;
    steps ++;
    for(i=0; i<4; i++)
    {
        step(x, y, i, t1, t2);
        if(a[t1][t2] == 2)
            k++;
    }
    if(k == 0)
        life--;
    else
    {
        if(k != 1)
            k = (rand()%k+1);
        for(i=0; i<4, k>0; i++)
        {
            step(x, y, i, t1, t2);
            if(a[t1][t2] == 2)
                k--;
        }
        life = 3;
        for(int j=0; j<p; j++)
        {
            if(bugs[j].x == t1 && bugs[j].y == t2)
            {
                bugs[j].death();
                shiftb();
            }
        }
        a[this->x][this->y] = 0;
        a[t1][t2] = 1;
        this->x = t1;
        this->y = t2;
    }
    if(steps >= 8)
        this->breed();
    if(life <= 0)
        this->death();
}

void dood::breed()
{
    int i, k=0, t1, t2;
    for(i=0; i<4; i++)
    {
        step(x, y, i, t1, t2);
        if(a[t1][t2] == 0)
        k++;
    }
    if(k > 0)
    {
        if(k != 1)
            k = (rand()%k+1);
        for(i=0;i<4,k>0;i++)
        {
            step(x, y, i, t1, t2);
            if(a[t1][t2] == 0)
                k--;
        }
        doodle[q].birth(t1,t2);
    }
}

void display()
{
    system ("cls");
    int i,j;
    char c;
    for(i=0; i<20; i++)
    {
        for(j=0; j<20; j++)
        {
            if(a[i][j] == 0)
                c = '-';
            if(a[i][j] == 1)
                c = 'x';
            if(a[i][j] == 2)
                c = 'o';
            cout << c << " ";
        }
        cout<<"\n";
    }
}

void step(int g, int h, int i, int &u, int &v)
{
    u = g;
    v = h;
    if(i == 0)
        u--;
    if(i == 1)
        v++;
    if(i == 2)
        u++;
    if(i == 3)
        v--;
    if(u<0 || v<0 || u>19 || v>19)
    {
        u = g;
        v = h;
    }
}

void shiftb()
{ 
    int j, k;
    if(p != 1)
        for( j=0;j<p;)
        {
            if(a[bugs[j].x][bugs[j].y]==0)
            {
                p--;
                for(int k=j; k<p; k++)
                    bugs[k]=bugs[k+1];
            }
            else
                j++;
        }
    if(p == 1)
        if(a[bugs[0].x][bugs[0].y] == 0)
            p--;
}

void shiftd()
{ 
    int j, k;
    if(q != 1)
        for(j=0; j<q; )
        {
            if(a[doodle[j].x][doodle[j].y] == 0)
            {
                q--;
                for(int k=j; k<q; k++)
                    doodle[k]=doodle[k+1];
            }
            else
                j++;
        }
    if(q == 1)
        if(a[doodle[0].x][doodle[0].y] == 0)
            q--;
}

int main()
{
    int m, n, i, j, s, t;
    char ch = 'y';
    cout << "enter how many doodles & bugs do u wany?" << endl;
    cin >> s >> t;
    for(i=0; i<20; i++)
        for(j=0; j<20; j++)
            a[i][j] = 0;
            
    srand ((unsigned)time(NULL));
   
    for(i=0; i<s; i++)
    {
        do 
        {
            m = rand()%20;
            n = rand()%20;
        } while(a[m][n] != 0);
        doodle[i].birth(m,n);
    }
    
    for(i=0; i<t; i++)
    {
        do
        {
            m = rand()%20;
            n = rand()%20;
        } while(a[m][n] != 0);
        bugs[i].birth(m,n);
    }
    display ();
    cin >> ch;
    while(ch == 'y' || ch == 'Y')
    {
        for(i=0; i<q; i++)
            doodle[i].move();
        shiftd();
        for(i=0; i<p; i++)
            bugs[i].move();

        display();
        cout << "do u want to go to next time step press y for yes";
        cin >> ch;
    }
    return 0;
}
 
Jheriko...tq 4 suggestion

Sans...tq 4 ur time


Code:
display();
        cout << "do u want to go to next time step press y for yes";
        cin >> ch;
why the output not continue to this part?
 
It does, it just doesn't display the same cout the first time it prompts you to continue.

If you wish to do that, you must enter the line...
Code:
cout << "do u want to go to next time step press y for yes";
Before the initial prompt, like so (see bolded line at bottom):

Code:
#include <iostream>
#include <time.h>
using namespace std;

int a[20][20], p=0, q=0;
void step(int g, int h, int i, int &u, int &v);

class org
{
    public:
        int x, y, life;
};

class bug:public org
{
    public:
        void birth(int e, int f)
        {
            x = e;
            y = f;
            life = 0;
            a[e][f] = 2;
            p++;
        }
        void move();
        void breed();
        void death()
        {
            a[x][y] = 0;
        }
} bugs[400];

void bug::move()
{
    int k = 0, t1, t2, i;
    life++;
    for(i=0; i<4; i++)
    {
        step(x, y, i, t1, t2);
        if(a[t1][t2] == 0)
            k++;
    }
    if(k > 0)
    {
        if(k != 1)
            k=(rand()%k+1);
        for(i=0; i<4,k>0; i++)
        {
            step(x, y, i, t1, t2);
            if(a[t1][t2] == 0)
                k--;
        }
        a[x][y] = 0;
        a[t1][t2] = 2;
        x = t1;
        y = t2;
        if(life >= 3)
        {
            this->breed();
            life = 0;
        }
    }
}
   
void bug::breed()
{
    int i, k = 0, t1, t2;
    for(i=0; i<4; i++)
    {
        step(x, y, i, t1, t2);
        if(a[t1][t2] == 0)
            k++;
    }
    if(k != 1)
        k = (rand()%k+1);
    for(i=0; i<4, k>0; i++)
    {
        step(x, y, i, t1, t2);
        if(a[t1][t2]==0)
            k--;
    }
    bugs[p].birth(t1,t2);
}

class dood:public org
{
    public:
        int steps;
        void birth(int e, int f)
        {
            x = e;
            y = f;
            life = 3;
            steps = 0;
            a[e][f] = 1;
            q++;
        }
        void move();
        void breed();
        void death()
        {
            a[x][y] = 0;
        }
} doodle[400];

void shiftb();
void shiftd();
void dood::move()
{
    int k = 0, t1, t2, i;
    steps ++;
    for(i=0; i<4; i++)
    {
        step(x, y, i, t1, t2);
        if(a[t1][t2] == 2)
            k++;
    }
    if(k == 0)
        life--;
    else
    {
        if(k != 1)
            k = (rand()%k+1);
        for(i=0; i<4, k>0; i++)
        {
            step(x, y, i, t1, t2);
            if(a[t1][t2] == 2)
                k--;
        }
        life = 3;
        for(int j=0; j<p; j++)
        {
            if(bugs[j].x == t1 && bugs[j].y == t2)
            {
                bugs[j].death();
                shiftb();
            }
        }
        a[this->x][this->y] = 0;
        a[t1][t2] = 1;
        this->x = t1;
        this->y = t2;
    }
    if(steps >= 8)
        this->breed();
    if(life <= 0)
        this->death();
}

void dood::breed()
{
    int i, k=0, t1, t2;
    for(i=0; i<4; i++)
    {
        step(x, y, i, t1, t2);
        if(a[t1][t2] == 0)
        k++;
    }
    if(k > 0)
    {
        if(k != 1)
            k = (rand()%k+1);
        for(i=0;i<4,k>0;i++)
        {
            step(x, y, i, t1, t2);
            if(a[t1][t2] == 0)
                k--;
        }
        doodle[q].birth(t1,t2);
    }
}

void display()
{
    system ("cls");
    int i,j;
    char c;
    for(i=0; i<20; i++)
    {
        for(j=0; j<20; j++)
        {
            if(a[i][j] == 0)
                c = '-';
            if(a[i][j] == 1)
                c = 'x';
            if(a[i][j] == 2)
                c = 'o';
            cout << c << " ";
        }
        cout<<"\n";
    }
}

void step(int g, int h, int i, int &u, int &v)
{
    u = g;
    v = h;
    if(i == 0)
        u--;
    if(i == 1)
        v++;
    if(i == 2)
        u++;
    if(i == 3)
        v--;
    if(u<0 || v<0 || u>19 || v>19)
    {
        u = g;
        v = h;
    }
}

void shiftb()
{ 
    int j, k;
    if(p != 1)
        for( j=0;j<p;)
        {
            if(a[bugs[j].x][bugs[j].y]==0)
            {
                p--;
                for(int k=j; k<p; k++)
                    bugs[k]=bugs[k+1];
            }
            else
                j++;
        }
    if(p == 1)
        if(a[bugs[0].x][bugs[0].y] == 0)
            p--;
}

void shiftd()
{ 
    int j, k;
    if(q != 1)
        for(j=0; j<q; )
        {
            if(a[doodle[j].x][doodle[j].y] == 0)
            {
                q--;
                for(int k=j; k<q; k++)
                    doodle[k]=doodle[k+1];
            }
            else
                j++;
        }
    if(q == 1)
        if(a[doodle[0].x][doodle[0].y] == 0)
            q--;
}

int main()
{
    int m, n, i, j, s, t;
    char ch = 'y';
    cout << "enter how many doodles & bugs do u wany?" << endl;
    cin >> s >> t;
    for(i=0; i<20; i++)
        for(j=0; j<20; j++)
            a[i][j] = 0;
            
    srand ((unsigned)time(NULL));
   
    for(i=0; i<s; i++)
    {
        do 
        {
            m = rand()%20;
            n = rand()%20;
        } while(a[m][n] != 0);
        doodle[i].birth(m,n);
    }
    
    for(i=0; i<t; i++)
    {
        do
        {
            m = rand()%20;
            n = rand()%20;
        } while(a[m][n] != 0);
        bugs[i].birth(m,n);
    }
    display ();
    [b]cout << "do u want to go to next time step press y for yes";[/b]
    cin >> ch;
    while(ch == 'y' || ch == 'Y')
    {
        for(i=0; i<q; i++)
            doodle[i].move();
        shiftd();
        for(i=0; i<p; i++)
            bugs[i].move();

        display();
        cout << "do u want to go to next time step press y for yes";
        cin >> ch;
    }
    return 0;
}