Create a simulator of chasing animals Requirements 1 Create
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;
 }



