include include include include using namespace std const in

#include<iostream>
#include<time.h>
#include<cstdio>
#include<windows.h>
using namespace std;

const int MAX_SIZE = 35;
int maze[MAX_SIZE][MAX_SIZE];
int currentPositionOfAnimals[6][2];

void display2D(){
for (int i = 0; i < MAX_SIZE; i++)
{
  for (int j = 0; j < MAX_SIZE; j++)
  {
   if (maze[i][j])cout << maze[i][j];
   else cout << \".\";
   cout << \" \";
  }
  cout << \"\ \";
}
}

// initially put the animals in random position in the maze
void allocateInitially(){
time_t t;
srand((unsigned)time(&t));

// assign the 6 animals
for (int i = 1; i <= 6; ++i){
  // get the random x value
  int x = rand() % MAX_SIZE;

  //get the random y value
  int y = rand() % MAX_SIZE;
  // if previously allotted try again
  for (int j = 1; j < i; ++j){
   if (currentPositionOfAnimals[j][0] == x && currentPositionOfAnimals[j][1] == y){
    --i;
    continue;
   }
  }
  maze[x][y] = i;

  // save the current position of animals which could be use for chasing
  currentPositionOfAnimals[i][0] = x;
  currentPositionOfAnimals[i][1] = y;
}
}

void chase(){
int newPos[6][2];// calculate new position of animals
for (int i = 0; i < 6; ++i){
  int j = (i + 1) % 6;
  int x = currentPositionOfAnimals[j + 1][0] - currentPositionOfAnimals[i + 1][0];
  int y = currentPositionOfAnimals[j + 1][1] - currentPositionOfAnimals[i + 1][1];
  if (x)
   x /= abs(x);
  if (y)
   y /= abs(y);


  //eight possible directions
  int absX = abs(x), absY = abs(y);
  int dir[] = { -1, 0, 0, 1, 0, -1, 1, 0, -1, -1, -1, 1, 1, -1, 1, 1 };
  x += currentPositionOfAnimals[i + 1][0];
  y += currentPositionOfAnimals[i + 1][1];
  // cross the boundary of maze
  if (x >= MAX_SIZE || x < 0 || y >= MAX_SIZE || y < 0
   || (absX == 0 && absY == 0)){
   for (int delta = 0; delta < 16; delta += 2){
    x = currentPositionOfAnimals[i + 1][0] + dir[delta];
    y = currentPositionOfAnimals[i + 1][1] + dir[delta + 1];
    //crossed the boundary then continue different location
    if (x >= MAX_SIZE || x < 0 || y >= MAX_SIZE || y < 0)continue;
    // there is already an animal on that place then continue for different location
    for (int mmj = 0; mmj < 6; ++mmj){
     if (currentPositionOfAnimals[mmj][0] == x && currentPositionOfAnimals[mmj][1] == y)continue;
    }
    break;
   }
  }
  else{
   for (int mm = 0; mm < i; ++mm){
    if (currentPositionOfAnimals[mm][0] == x && currentPositionOfAnimals[mm][1] == y){
     for (int delta = 0; delta < 16; delta += 2){
      x = currentPositionOfAnimals[i + 1][0] + dir[delta];
      y = currentPositionOfAnimals[i + 1][1] + dir[delta + 1];
      //crossed the boundary then continue different location
      if (x >= MAX_SIZE || x < 0 || y >= MAX_SIZE || y < 0)continue;
      // there is already an animal on that place then continue for different location
      for (int mmj = 0; mmj < 6; ++mmj){
       if (currentPositionOfAnimals[mmj][0] == x && currentPositionOfAnimals[mmj][1] == y)continue;
      }
      break;
     }
     break;
    }
   }
  }
  newPos[i + 1][0] = x;
  newPos[i + 1][1] = y;
  maze[x][y] = i + 1;
}

// re assign the current positions of the animals
for (int i = 1; i <= 6; ++i){
  currentPositionOfAnimals[i][0] = newPos[i][0];
  currentPositionOfAnimals[i][1] = newPos[i][1];
}
}
int main(){
allocateInitially();// put the animal in the maze
// do at least 20 iterations   
for (int i = 0; i < 20; ++i){
  system(\"cls\"); // if max or linux use this
  //system(\"cls\"; // if windows then use this
  display2D(); // display the movements of the animals
  chase(); // chase other animals

}
return 0;
}

Please help. Did i do something wrong? i couldnt debug my program. There is an error message said the newPos is corrupted ?

Solution

#include <iostream>
#include <time.h>
#include <cstdio>
#include <stdlib.h> // you needed to add this for srand. Now it works perfectly!!
#include <windows.h>
using namespace std;
const int MAX_SIZE = 35;
int maze[MAX_SIZE][MAX_SIZE];
int currentPositionOfAnimals[6][2];
void display2D(){
for (int i = 0; i < MAX_SIZE; i++)
{
for (int j = 0; j < MAX_SIZE; j++)
{
if (maze[i][j])cout << maze[i][j];
else cout << \".\";
cout << \" \";
}
cout << \"\ \";
}
}
// initially put the animals in random position in the maze
void allocateInitially(){
time_t t;
srand((unsigned)time(&t));
// assign the 6 animals
for (int i = 1; i <= 6; ++i){
// get the random x value
int x = rand() % MAX_SIZE;
//get the random y value
int y = rand() % MAX_SIZE;
// if previously allotted try again
for (int j = 1; j < i; ++j){
if (currentPositionOfAnimals[j][0] == x && currentPositionOfAnimals[j][1] == y){
--i;
continue;
}
}
maze[x][y] = i;
// save the current position of animals which could be use for chasing
currentPositionOfAnimals[i][0] = x;
currentPositionOfAnimals[i][1] = y;
}
}

void chase(){
int newPos[6][2];// calculate new position of animals
for (int i = 0; i < 6; ++i){
int j = (i + 1) % 6;
int x = currentPositionOfAnimals[j + 1][0] - currentPositionOfAnimals[i + 1][0];
int y = currentPositionOfAnimals[j + 1][1] - currentPositionOfAnimals[i + 1][1];
if (x)
x /= abs(x);
if (y)
y /= abs(y);

//eight possible directions
int absX = abs(x), absY = abs(y);
int dir[] = { -1, 0, 0, 1, 0, -1, 1, 0, -1, -1, -1, 1, 1, -1, 1, 1 };
x += currentPositionOfAnimals[i + 1][0];
y += currentPositionOfAnimals[i + 1][1];
// cross the boundary of maze
if (x >= MAX_SIZE || x < 0 || y >= MAX_SIZE || y < 0
|| (absX == 0 && absY == 0)){
for (int delta = 0; delta < 16; delta += 2){
x = currentPositionOfAnimals[i + 1][0] + dir[delta];
y = currentPositionOfAnimals[i + 1][1] + dir[delta + 1];
//crossed the boundary then continue different location
if (x >= MAX_SIZE || x < 0 || y >= MAX_SIZE || y < 0)continue;
// there is already an animal on that place then continue for different location
for (int mmj = 0; mmj < 6; ++mmj){
if (currentPositionOfAnimals[mmj][0] == x && currentPositionOfAnimals[mmj][1] == y)continue;
}
break;
}
}
else{
for (int mm = 0; mm < i; ++mm){
if (currentPositionOfAnimals[mm][0] == x && currentPositionOfAnimals[mm][1] == y){
for (int delta = 0; delta < 16; delta += 2){
x = currentPositionOfAnimals[i + 1][0] + dir[delta];
y = currentPositionOfAnimals[i + 1][1] + dir[delta + 1];
//crossed the boundary then continue different location
if (x >= MAX_SIZE || x < 0 || y >= MAX_SIZE || y < 0)continue;
// there is already an animal on that place then continue for different location
for (int mmj = 0; mmj < 6; ++mmj){
if (currentPositionOfAnimals[mmj][0] == x && currentPositionOfAnimals[mmj][1] == y)continue;
}
break;
}
break;
}
}
}
newPos[i + 1][0] = x;
newPos[i + 1][1] = y;
maze[x][y] = i + 1;
}
// re assign the current positions of the animals
for (int i = 1; i <= 6; ++i){
currentPositionOfAnimals[i][0] = newPos[i][0];
currentPositionOfAnimals[i][1] = newPos[i][1];
}
}
int main(){
allocateInitially();// put the animal in the maze
// do at least 20 iterations   
for (int i = 0; i < 20; ++i){
system(\"cls\"); // if max or linux use this
//system(\"cls\"; // if windows then use this
display2D(); // display the movements of the animals
chase(); // chase other animals
}
return 0;
}

#include<iostream> #include<time.h> #include<cstdio> #include<windows.h> using namespace std; const int MAX_SIZE = 35; int maze[MAX_SIZE
#include<iostream> #include<time.h> #include<cstdio> #include<windows.h> using namespace std; const int MAX_SIZE = 35; int maze[MAX_SIZE
#include<iostream> #include<time.h> #include<cstdio> #include<windows.h> using namespace std; const int MAX_SIZE = 35; int maze[MAX_SIZE
#include<iostream> #include<time.h> #include<cstdio> #include<windows.h> using namespace std; const int MAX_SIZE = 35; int maze[MAX_SIZE

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site