SlideShare a Scribd company logo
1 of 7
Download to read offline
#include
#include
#include
#include
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
#include
#include
#include // you needed to add this for srand. Now it works perfectly!!
#include
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;
}

More Related Content

Similar to #includeiostream #includetime.h #includecstdio #include.pdf

include ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfinclude ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdf
contact32
 
Questions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdfQuestions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdf
apexelectronices01
 
AI CHALLENGE ADMIN
AI CHALLENGE ADMINAI CHALLENGE ADMIN
AI CHALLENGE ADMIN
Ankit Gupta
 
SaveI need help with this maze gui that I wrote in java, I am tryi.pdf
SaveI need help with this maze gui that I wrote in java, I am tryi.pdfSaveI need help with this maze gui that I wrote in java, I am tryi.pdf
SaveI need help with this maze gui that I wrote in java, I am tryi.pdf
arihantstoneart
 
Abstract Base Class (C++ Program)Create an abstract base class cal.pdf
Abstract Base Class (C++ Program)Create an abstract base class cal.pdfAbstract Base Class (C++ Program)Create an abstract base class cal.pdf
Abstract Base Class (C++ Program)Create an abstract base class cal.pdf
calderoncasto9163
 
need help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdfneed help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdf
arcotstarsports
 
Include stdio. wps office (1)
Include  stdio. wps office (1)Include  stdio. wps office (1)
Include stdio. wps office (1)
NilayChavhan
 
you will use cellular automata to create a 2D predator–prey simulati.pdf
you will use cellular automata to create a 2D predator–prey simulati.pdfyou will use cellular automata to create a 2D predator–prey simulati.pdf
you will use cellular automata to create a 2D predator–prey simulati.pdf
FashionBoutiquedelhi
 
Im trying again -Okay, Im in need of some help - this is the c.pdf
Im trying again -Okay, Im in need of some help - this is the c.pdfIm trying again -Okay, Im in need of some help - this is the c.pdf
Im trying again -Okay, Im in need of some help - this is the c.pdf
eyeonsecuritysystems
 

Similar to #includeiostream #includetime.h #includecstdio #include.pdf (20)

Numerical Methods with Computer Programming
Numerical Methods with Computer ProgrammingNumerical Methods with Computer Programming
Numerical Methods with Computer Programming
 
Vcs16
Vcs16Vcs16
Vcs16
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
include ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfinclude ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdf
 
Functional JS for everyone - 4Developers
Functional JS for everyone - 4DevelopersFunctional JS for everyone - 4Developers
Functional JS for everyone - 4Developers
 
Cocos2d Performance Tips
Cocos2d Performance TipsCocos2d Performance Tips
Cocos2d Performance Tips
 
Ocr code
Ocr codeOcr code
Ocr code
 
A simple snake game project
A simple snake game projectA simple snake game project
A simple snake game project
 
Questions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdfQuestions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdf
 
AI CHALLENGE ADMIN
AI CHALLENGE ADMINAI CHALLENGE ADMIN
AI CHALLENGE ADMIN
 
SaveI need help with this maze gui that I wrote in java, I am tryi.pdf
SaveI need help with this maze gui that I wrote in java, I am tryi.pdfSaveI need help with this maze gui that I wrote in java, I am tryi.pdf
SaveI need help with this maze gui that I wrote in java, I am tryi.pdf
 
Abstract Base Class (C++ Program)Create an abstract base class cal.pdf
Abstract Base Class (C++ Program)Create an abstract base class cal.pdfAbstract Base Class (C++ Program)Create an abstract base class cal.pdf
Abstract Base Class (C++ Program)Create an abstract base class cal.pdf
 
need help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdfneed help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdf
 
In a galaxy far, far away - A procedural generation tale
In a galaxy far, far away - A procedural generation taleIn a galaxy far, far away - A procedural generation tale
In a galaxy far, far away - A procedural generation tale
 
Include stdio. wps office (1)
Include  stdio. wps office (1)Include  stdio. wps office (1)
Include stdio. wps office (1)
 
you will use cellular automata to create a 2D predator–prey simulati.pdf
you will use cellular automata to create a 2D predator–prey simulati.pdfyou will use cellular automata to create a 2D predator–prey simulati.pdf
you will use cellular automata to create a 2D predator–prey simulati.pdf
 
Im trying again -Okay, Im in need of some help - this is the c.pdf
Im trying again -Okay, Im in need of some help - this is the c.pdfIm trying again -Okay, Im in need of some help - this is the c.pdf
Im trying again -Okay, Im in need of some help - this is the c.pdf
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
 
数学カフェ 確率・統計・機械学習回 「速習 確率・統計」
数学カフェ 確率・統計・機械学習回 「速習 確率・統計」数学カフェ 確率・統計・機械学習回 「速習 確率・統計」
数学カフェ 確率・統計・機械学習回 「速習 確率・統計」
 
Hitchhiker's Guide to Functional Programming
Hitchhiker's Guide to Functional ProgrammingHitchhiker's Guide to Functional Programming
Hitchhiker's Guide to Functional Programming
 

More from ARORACOCKERY2111

How has technology impacted our societySolutionBy the followi.pdf
How has technology impacted our societySolutionBy the followi.pdfHow has technology impacted our societySolutionBy the followi.pdf
How has technology impacted our societySolutionBy the followi.pdf
ARORACOCKERY2111
 
Give definitions for comparative anatomy and embryology.Why do evo.pdf
Give definitions for comparative anatomy and embryology.Why do evo.pdfGive definitions for comparative anatomy and embryology.Why do evo.pdf
Give definitions for comparative anatomy and embryology.Why do evo.pdf
ARORACOCKERY2111
 
Exercise 1. Comparative Anatomy Type of Circulatory outstanding Featu.pdf
Exercise 1. Comparative Anatomy Type of Circulatory outstanding Featu.pdfExercise 1. Comparative Anatomy Type of Circulatory outstanding Featu.pdf
Exercise 1. Comparative Anatomy Type of Circulatory outstanding Featu.pdf
ARORACOCKERY2111
 
Discuss the characteristics that make something a protist as well as.pdf
Discuss the characteristics that make something a protist as well as.pdfDiscuss the characteristics that make something a protist as well as.pdf
Discuss the characteristics that make something a protist as well as.pdf
ARORACOCKERY2111
 
compare and contrast the morphology of Cestodes, Trematodes, and Nem.pdf
compare and contrast the morphology of Cestodes, Trematodes, and Nem.pdfcompare and contrast the morphology of Cestodes, Trematodes, and Nem.pdf
compare and contrast the morphology of Cestodes, Trematodes, and Nem.pdf
ARORACOCKERY2111
 
An important part of electrical engineering is PCB design. One impor.pdf
An important part of electrical engineering is PCB design. One impor.pdfAn important part of electrical engineering is PCB design. One impor.pdf
An important part of electrical engineering is PCB design. One impor.pdf
ARORACOCKERY2111
 
Alternate Electron acceptors Know what these electron acceptors will.pdf
Alternate Electron acceptors Know what these electron acceptors will.pdfAlternate Electron acceptors Know what these electron acceptors will.pdf
Alternate Electron acceptors Know what these electron acceptors will.pdf
ARORACOCKERY2111
 
You have been exposed to each of the 8 microbes below.After a coup.pdf
You have been exposed to each of the 8 microbes below.After a coup.pdfYou have been exposed to each of the 8 microbes below.After a coup.pdf
You have been exposed to each of the 8 microbes below.After a coup.pdf
ARORACOCKERY2111
 
Which statement below regarding membrane structure and membrane tran.pdf
Which statement below regarding membrane structure and membrane tran.pdfWhich statement below regarding membrane structure and membrane tran.pdf
Which statement below regarding membrane structure and membrane tran.pdf
ARORACOCKERY2111
 

More from ARORACOCKERY2111 (20)

Imagine a factorial design in which there are two factors. Each fact.pdf
Imagine a factorial design in which there are two factors. Each fact.pdfImagine a factorial design in which there are two factors. Each fact.pdf
Imagine a factorial design in which there are two factors. Each fact.pdf
 
How has technology impacted our societySolutionBy the followi.pdf
How has technology impacted our societySolutionBy the followi.pdfHow has technology impacted our societySolutionBy the followi.pdf
How has technology impacted our societySolutionBy the followi.pdf
 
How can you find DNA and RNA on a scene using spectroscopyHow c.pdf
How can you find DNA and RNA on a scene using spectroscopyHow c.pdfHow can you find DNA and RNA on a scene using spectroscopyHow c.pdf
How can you find DNA and RNA on a scene using spectroscopyHow c.pdf
 
Give definitions for comparative anatomy and embryology.Why do evo.pdf
Give definitions for comparative anatomy and embryology.Why do evo.pdfGive definitions for comparative anatomy and embryology.Why do evo.pdf
Give definitions for comparative anatomy and embryology.Why do evo.pdf
 
Find the area of the shaded region. The graph depicts the standard no.pdf
Find the area of the shaded region. The graph depicts the standard no.pdfFind the area of the shaded region. The graph depicts the standard no.pdf
Find the area of the shaded region. The graph depicts the standard no.pdf
 
Exercise 1. Comparative Anatomy Type of Circulatory outstanding Featu.pdf
Exercise 1. Comparative Anatomy Type of Circulatory outstanding Featu.pdfExercise 1. Comparative Anatomy Type of Circulatory outstanding Featu.pdf
Exercise 1. Comparative Anatomy Type of Circulatory outstanding Featu.pdf
 
Explain how the high heat capacity water that makes it a vital substa.pdf
Explain how the high heat capacity water that makes it a vital substa.pdfExplain how the high heat capacity water that makes it a vital substa.pdf
Explain how the high heat capacity water that makes it a vital substa.pdf
 
Discuss the characteristics that make something a protist as well as.pdf
Discuss the characteristics that make something a protist as well as.pdfDiscuss the characteristics that make something a protist as well as.pdf
Discuss the characteristics that make something a protist as well as.pdf
 
Describe the purpose for carrying out a DNAase Hypersensitivity assa.pdf
Describe the purpose for carrying out a DNAase Hypersensitivity assa.pdfDescribe the purpose for carrying out a DNAase Hypersensitivity assa.pdf
Describe the purpose for carrying out a DNAase Hypersensitivity assa.pdf
 
compare and contrast the morphology of Cestodes, Trematodes, and Nem.pdf
compare and contrast the morphology of Cestodes, Trematodes, and Nem.pdfcompare and contrast the morphology of Cestodes, Trematodes, and Nem.pdf
compare and contrast the morphology of Cestodes, Trematodes, and Nem.pdf
 
Carbon dioxide transport Drag each label to the appropriate location.pdf
Carbon dioxide transport  Drag each label to the appropriate location.pdfCarbon dioxide transport  Drag each label to the appropriate location.pdf
Carbon dioxide transport Drag each label to the appropriate location.pdf
 
An important part of electrical engineering is PCB design. One impor.pdf
An important part of electrical engineering is PCB design. One impor.pdfAn important part of electrical engineering is PCB design. One impor.pdf
An important part of electrical engineering is PCB design. One impor.pdf
 
Assume ND=NA=1E+15cm-3, in two different slabs of semiconductor (1 e.pdf
Assume ND=NA=1E+15cm-3, in two different slabs of semiconductor (1 e.pdfAssume ND=NA=1E+15cm-3, in two different slabs of semiconductor (1 e.pdf
Assume ND=NA=1E+15cm-3, in two different slabs of semiconductor (1 e.pdf
 
Alternate Electron acceptors Know what these electron acceptors will.pdf
Alternate Electron acceptors Know what these electron acceptors will.pdfAlternate Electron acceptors Know what these electron acceptors will.pdf
Alternate Electron acceptors Know what these electron acceptors will.pdf
 
A man with blood type A and a woman with blood type B have three chi.pdf
A man with blood type A and a woman with blood type B have three chi.pdfA man with blood type A and a woman with blood type B have three chi.pdf
A man with blood type A and a woman with blood type B have three chi.pdf
 
You have been exposed to each of the 8 microbes below.After a coup.pdf
You have been exposed to each of the 8 microbes below.After a coup.pdfYou have been exposed to each of the 8 microbes below.After a coup.pdf
You have been exposed to each of the 8 microbes below.After a coup.pdf
 
Which of the four levels of measurement is most appropriate for Wei.pdf
Which of the four levels of measurement is most appropriate for Wei.pdfWhich of the four levels of measurement is most appropriate for Wei.pdf
Which of the four levels of measurement is most appropriate for Wei.pdf
 
Which statement below regarding membrane structure and membrane tran.pdf
Which statement below regarding membrane structure and membrane tran.pdfWhich statement below regarding membrane structure and membrane tran.pdf
Which statement below regarding membrane structure and membrane tran.pdf
 
Why do cyanobacteria possess heterocystsA- Heterocysts shield the.pdf
Why do cyanobacteria possess heterocystsA- Heterocysts shield the.pdfWhy do cyanobacteria possess heterocystsA- Heterocysts shield the.pdf
Why do cyanobacteria possess heterocystsA- Heterocysts shield the.pdf
 
Which of the following isare true regarding hex digitsA. Hex dig.pdf
Which of the following isare true regarding hex digitsA. Hex dig.pdfWhich of the following isare true regarding hex digitsA. Hex dig.pdf
Which of the following isare true regarding hex digitsA. Hex dig.pdf
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 

#includeiostream #includetime.h #includecstdio #include.pdf

  • 1. #include #include #include #include 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;
  • 2. } } 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;
  • 3. } 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
  • 4. 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 #include #include #include // you needed to add this for srand. Now it works perfectly!! #include 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(){
  • 5. 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];
  • 6. 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; } } }
  • 7. 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; }