Create a simulator of chasing animals Requirements 1 Create

Create a simulator of chasing animals Requirements (1) Create a function that displays a two dimensional array. The example uses a 35 X 35 array. If the stored value is 0, show “.”, otherwise show the stored number. Hint: Use two dimensional array, nested loops, and if statement (2) In the main program, set the randomly selected starting locations for each number /animal. The example uses number 1 through number 6 (3) Move each number/animal so that an animal chases the next animal. For example, animal # 1 chases animal # 2, animal # 2 chases animal # 3, and animal # 6 (the last animal) chases animal # 1 (the first animal). Hint: Use modulus division, as well as if statements. You may choose the direction of the movement to cither four directions or eight directions. (4) Repeat Number 3 at least 20 times. Show the trace of each animal\'s numbers. Hint: Use a loop. You may use system( CLS\") to clear the screen, and system(\"pause\") to pause after each iteration Please Submit 1. A flowchart of your program 2. Printout of your C+ program with a heading comment (Do not forget to 4 points) add comments within your program) 3. Copy of a screenshot after your program is executed (4 points) (2 points)

Solution

Solution:.

C++ code for the given program simulation.

#include<iostream>
#include<cstdlib.>
#include<math.h>
#include<time.h>
#include<string>
#include<windows.h>

using namespace std;
//global 2d array variables
int animal[35][35];
int Positn[6][2];


//To initailze the initial position of animals
void randAllocation(){
    time_t tim;
    srand((unsigned)time(&tim));
  
   for(int vi = 1;vi <= 6;++vi){
      
       int x_co = rand() % 35;
       int y_co = rand() % 35;
        //If the number is already allotted
       cout<<endl;
       cout<<x_co<<\" \"<<y_co;
       for(int vj = 1;vj < vi; ++vj){
            if(Positn[vj][0] == x_co && Positn[vj][1] == y_co)
           {
                --vi;
                continue;
            }
        }
        animal[x_co][y_co] = vi;
       //save the initial position of animals
       Positn[vi][0] = x_co;
        Positn[vi][1] = y_co;
      
    }
}

//Function to display the grid
void disp(){
    for (int vi = 0; vi < 35; vi++)
    {
        for (int vj = 0; vj < 35; vj++)
        {
            cout << \" \";
            if(animal[vi][vj]!=0)
               cout << animal[vi][vj];
            else cout << \".\";
        }
        cout << \"\ \";
    }
    Sleep(1);
}


void Chasesimulation(){
   //Array to update the position of the animals with new position
    int Updatedposition[6][2];
   for(int vi = 0;vi < 6;++vi)
   {
        int vj = (vi + 1) % 6;
        int x_co = Positn[vj + 1][0] - Positn[vi + 1][0];
        int y_co = Positn[vj + 1][1] - Positn[vi + 1][1];
        x_co=x_co/abs(x_co);
        y_co=y_co/abs(y_co);
       //chekc for all the eight possible directions
       int abs_X = abs(x_co), abs_Y = abs(y_co);
        int posiblespots[] = {-1, 0, 0, 1, 0, -1, 1, 0, -1, -1, -1, 1, 1, -1, 1, 1};
        x_co += Positn[vi + 1][0];
        y_co += Positn[vi + 1][1];
        //if cross the boundary of animal
       if(x_co >= 35 || x_co < 0 || y_co >= 35 || y_co < 0|| (abs_X == 0 && abs_Y == 0))
       {
            for(int differen = 0;differen < 16;differen += 2)
           {
                x_co = Positn[vi + 1][0] + posiblespots[differen];
                y_co = Positn[vi + 1][1] + posiblespots[differen + 1];
                //Chek for the boundary
               if(x_co >= 35 || x_co < 0 || y_co >= 35 || y_co < 0)
                   continue;
                //Already an animal is there
               for(int vk = 0;vk < 6;++vk)
               {
                    if(Positn[vk][0] == x_co && Positn[vk][1] == y_co)
                       continue;
                }
                break;
            }
        }else{
            for(int v = 0;v < vi; ++v){
                if(Positn[v][0] == x_co && Positn[v][1] == y_co){
                    for(int differen = 0;differen < 16;differen += 2){
                        x_co = Positn[vi + 1][0] + posiblespots[differen];
                        y_co = Positn[vi + 1][1] + posiblespots[differen + 1];
                        //crossed the boundary then continue different location
                       if(x_co >= 35 || x_co < 0 || y_co >= 35 || y_co < 0)continue;
                        // there is already an animal on that place then continue for different location
                       for(int vk = 0;vk < 6;++vk){
                            if(Positn[vk][0] == x_co && Positn[vk][1] == y_co)continue;
                        }
                        break;
                    }
                    break;
                }
            }
        }
        Updatedposition[vi + 1][0] = x_co;
        Updatedposition[vi + 1][1] = y_co;
        animal[x_co][y_co] = vi + 1;
    }

    //Assign the new posotion of the animals
   for(int vi = 1;vi <= 6;++vi){
        Positn[vi][0] = Updatedposition[vi][0];
        Positn[vi][1] = Updatedposition[vi][1];
    }
}
int main(){
    randAllocation();
   for(int vi = 0;vi < 20; ++vi){
       //To clear screen on every itteration
          system(\"cls\");
       disp();
       Chasesimulation();
}
   system(\"pause\");
    return 0;
}

 Create a simulator of chasing animals Requirements (1) Create a function that displays a two dimensional array. The example uses a 35 X 35 array. If the stored
 Create a simulator of chasing animals Requirements (1) Create a function that displays a two dimensional array. The example uses a 35 X 35 array. If the stored
 Create a simulator of chasing animals Requirements (1) Create a function that displays a two dimensional array. The example uses a 35 X 35 array. If the stored

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site