Anyone can help to modified this program?

  • Thread starter pajak
  • Start date
  • Tags
    Program
In summary, the conversation is about a coding simulation of the game "Bugs Life" that can only run in TC++ 3.0 or higher. The person is looking for help in modifying the program to run in Codeblocks or Devc++. The program includes code for bugs and doodles, and functions for their movement, breeding, and death. There is also a display function mentioned.
  • #1
pajak
4
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();
}
}
 
Technology news on Phys.org
  • #2
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.
 
  • #3
Yikes! What's with the formatting? It looks like an auto-indent gone awry.

I'll see what I can do.
 
  • #4
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;
}
 
  • #5
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?
 
  • #6
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;
}
 

1. Can I modify this program even if I have no coding experience?

Yes, anyone can help to modify this program, regardless of their coding experience. The level of modification you can do may vary depending on your coding knowledge, but even small changes can be valuable and helpful.

2. Do I need special software or tools to modify this program?

Most likely, yes. Depending on the programming language and tools used to create the program, you may need certain software or tools to make modifications. If the program is open-source, the necessary software or tools may be provided by the creators.

3. Will modifying this program make me a collaborator or co-author?

Not necessarily. Modifying a program does not automatically make you a collaborator or co-author. However, if your changes are significant and you work closely with the original creators, you may be credited as a collaborator or co-author.

4. Can I share my modifications with others?

Yes, you can share your modifications with others. However, if the program is licensed under specific terms, you may need to follow those terms when sharing your modifications. It is always best to consult the original creators before sharing any modifications.

5. Is it necessary to get permission from the creators before modifying this program?

It depends on the program's license and the terms set by the creators. If the program is open-source, you may not need explicit permission to modify it. However, it is always respectful to inform and consult the creators before making any modifications.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
610
  • Programming and Computer Science
Replies
1
Views
942
  • Programming and Computer Science
Replies
6
Views
1K
Replies
1
Views
1K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top