MechaS3K
NEVAR FORGET
I do use printf for out
You should never ever ever do that. It leaves you wide open to overflow exploits.
I do use printf for out
犬夜叉;692166 said:Not that I like it much myself, mind you.
I'm still learning C++, and that program you are showing is just a pain for me. I do use printf for out, and not cout.
delete myvar;
You should never ever ever do that. It leaves you wide open to overflow exploits.
delete[] maze;
#include <iostream>
#include <conio.h>
#include <Windows.h>
#define _WIN32_WINNT 0x0500
using namespace std;
enum Colors {blue=1, green, cyan, red, purple, yellow, grey, dgrey, hblue, hgreen, hred, hpurple, hyellow, hwhite};
//function prototypes
void coutc(int color, char*output);
void generate(int maze[20][20], int &i, int &j);
void initialize(int maze[20][20], int &i, int &j);
void display(int maze[20][20], int &i, int &j);
int step(int maze[20][20], int &i, int &j);
//PURPOSE: Initialize the program, call the other functions,
//and refresh the maze every tenth of a second.
int main()
{
//specifies console dimensions to fit size of maze
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r);
MoveWindow(console, r.left, r.top, 800, 400, TRUE);
int i, j, wait; //counters
int choice; //user choice
int maze[20][20]; //the matrix for the maze itself
//startup message
cout << "Welcome to Maze Navigator!" << endl;
cout << endl;
system("pause");
//startup functions
initialize(maze, i, j);
display(maze, i, j);
while (maze[0][10] != 2)
{
//one move through the maze
step(maze, i, j);
//delays the next move so that the individual moves can be seen
for (wait = 0; wait < 1000000; wait++);
}
//displays upon maze completion
system("cls");
coutc(grey, "Maze Solved!");
cout << endl;
cout << "Do you want to run another maze? (1/0)" << endl;
cin >> choice;
//program exits or restarts depending on player's choice
if (choice == 1)
{
system("cls");
return main();
}
else
{
return 0;
}
}
//PURPOSE: Output a value with a specified text color.
void coutc(int color, char* output)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, color);
cout << output;
SetConsoleTextAttribute(handle, color);
}
//PURPOSE: Generate a randomly structured maze.
void generate(int maze[20][20], int &i, int &j)
{
int spaceFreq;
system("cls");
cout << "Enter a space frequency between 1 and 10: ";
cin >> spaceFreq;
//checks whether the chosen space frequency is valid
if (spaceFreq < 1 || spaceFreq >10)
{
system("cls");
cout << "Error: Invalid space frequency" << endl;
system("pause");
system("cls");
main();
}
for (i = 0; i < 20; i++)
{
for (j = 0; j < 20; j++)
{
maze[i][j] = rand() & spaceFreq;
if (maze[i][j] != 0 && maze[i][j] != 1)
{
maze[i][j] = 0;
}
}
}
//start point
maze[19][9] = 2;
//exit point
maze[0][10] = 0;
}
//PURPOSE: Preliminarily fill in the maze with empty space before
//constructing it proper
void initialize(int maze[20][20], int &i, int &j)
{
for(i = 0; i < 20; i++)
{
for(j = 0; j < 20; j++)
{
maze[i][j] = 0;
}
cout << endl;
}
generate(maze, i, j);
}
//PURPOSE: Display the maze on the console.
void display(int maze[20][20], int &i, int &j)
{
system("cls");
cout << endl << endl;;
coutc(hgreen, " $");
cout << endl;
coutc(green, " X X X X X X X X X X X X X X X X X X X X X");
cout << endl;
for(i = 0; i < 20; i++)
{
coutc(green," X");
//specifies the characters corresponding to different components of the maze
for(j = 0; j < 20; j++)
{
//empty space
if (maze[i][j] == 0)
{
coutc(blue, " ");
}
//wall
else if (maze[i][j] == 1)
{
coutc(green, " X");
}
//dynamic character
else if (maze[i][j] == 2)
{
coutc(hblue, " O");
}
//path used once
else if (maze[i][j] == 3)
{
coutc(blue, " '");
}
//path used twice
else if (maze[i][j] == 4)
{
coutc(blue, " *");
}
//this shouldn't ever be activated
else
{
cout << " " << maze[i][j];
}
}
coutc(green," X");
cout << endl;
}
coutc(green, " X X X X X X X X X X X X X X X X X X X X X");
cout << endl;
coutc(cyan, " @");
cout << endl;
}
//PURPOSE: Make the AI move through the maze.
int step(int maze[20][20], int &i, int &j)
{
int choice;
for(i = 0; i < 20; i++)
{
for(j = 0; j < 20; j++)
{
if(maze[i][j] == 2)
{
//tries to go left
if(maze[i][j - 1] == 0 && j != 0)
{
maze[i][j - 1] = 2;
maze[i][j] = 3;
}
//tries to go up
else if(maze[i - 1][j] == 0 && i != 0)
{
maze[i - 1][j] = 2;
maze[i][j] = 3;
}
//tries to go right
else if(maze[i][j + 1] == 0 && j != 19)
{
while(maze[i][j + 1] == 0 && j != 19)
{
maze[i][j + 1] = 2;
maze[i][j] = 3;
display(maze, i, j);
}
}
//tries to go down
else if(maze[i + 1][j] == 0 && i != 19)
{
while(maze[i + 1][j] == 0 && i != 19)
{
maze[i + 1][j] = 2;
maze[i][j] = 3;
display(maze, i, j);
}
}
//checks for any paths it already crossed and reuses them if necessary
else
{
//tries to go left on a path it already crossed
if(maze[i][j - 1] == 3 && j != 0)
{
maze[i][j - 1] = 2;
maze[i][j] = 4;
}
//tries to go up on a path it already crossed
else if(maze[i - 1][j] == 3 && i != 0)
{
maze[i - 1][j] = 2;
maze[i][j] = 4;
}
//tries to go right on a path it already crossed
else if(maze[i][j + 1] == 3 && j != 19)
{
while(maze[i][j + 1] == 3 && j != 19)
{
maze[i][j + 1] = 2;
maze[i][j] = 4;
display(maze, i, j);
}
}
//tries to go down on a path it already crossed
else if(maze[i + 1][j] == 3 && i != 19)
{
while(maze[i + 1][j] == 3 && i != 19)
{
maze[i + 1][j] = 2;
maze[i][j] = 4;
display(maze, i, j);
}
}
//unable to move at all
else
{
system("cls");
coutc(grey, "Dynamic value is trapped!");
cout << endl;
cout << "Do you want to run another maze? (1/0)" << endl;
cin >> choice;
if (choice == 1)
{
system("cls");
return main();
}
else
{
return 0;
}
}
}
}
}
}
display(maze, i, j);
return 1;
}
Answer me this, though: if the matrix is statically allocated and gets automatically deleted, then why the heck does the program keep using exactly the same maze every time I ask it to generate a new one randomly?You tried to dynamically delete an object that was not dynamically allocated using new.
int maze[20][20];
This was statically allocated. It will by automatically deleted once the program leaves its scope. Do not use the delete or delete[] keywords on it.
My professor implied that switch/case statements are rarely necessary; that's why I didn't use them. I'm so unaccustomed to them that it might be hard, but I agree it makes sense, so I'll try it.Also, you have way too many successive if/else statements. I would replace those with a switch/case statement at the very least.
Eventually, you should go the class-based route. Ideally you would make the maze into a class. This way you could simply issue a statement like Maze *myMaze = new Maze(20, 30); and then you'd get a randomly-generated maze of the specified dimensions, ready to use.
The reason I did that was because I'm used to writing functions with PURPOSE, PRECONDITIONS, and POSTCONDITIONS as documentation, and I was too lazy to come up with something for the other two. I'll try your way, though.Finally, this is more proper style and documentation than anything, but documentation of functions should be done with three slashes ///. This automatically works in with documentation generation programs like Doxygen, and it has much better aesthetics than writing "PURPOSE" before every function.
Answer me this, though: if the matrix is statically allocated and gets automatically deleted, then why the heck does the program keep using exactly the same maze every time I ask it to generate a new one randomly?
#include <ctime>
#include <cstdlib>
void myInitFunction() {
// Initializes the random number generator
srand(time(0));
// I'm not sure if this is needed. From my experience I've had to draw one
// random number for the new random seed to take effect.
rand();
}
My professor implied that switch/case statements are rarely necessary; that's why I didn't use them. I'm so unaccustomed to them that it might be hard, but I agree it makes sense, so I'll try it.
The reason I did that was because I'm used to writing functions with PURPOSE, PRECONDITIONS, and POSTCONDITIONS as documentation, and I was too lazy to come up with something for the other two. I'll try your way, though.
Don't. It's generally better to use iostream instead of cstdio unless you have a very specific reason not to use iostream. You get increased safety, automation, and C++-style error checking if you use iostream.
If it's operating on an array, I'm pretty sure you need to use delete[], not delete. Otherwise you get a memory leak. Also, shouldn't
int[] myvar = new int[20];
Be this:
int *myvar = new int[20];
Answer me this, though: if the matrix is statically allocated and gets automatically deleted, then why the heck does the program keep using exactly the same maze every time I ask it to generate a new one randomly?
Classes will be more of a problem, as I haven't been taught anything about classes yet.
My very specific reason is that some platforms have poor C++ compilers and it auto-flushes the buffer... :)
Probably. C++ syntax is lame.
So, all I have to do is run that before I run generate()? Or is there some more complicated thing involved?This has nothing to do with memory management. It's using the same maze every time because you never initialized the random seed. Ideally, you would initialize the random seed to a value given by the system time:
Code:#include <ctime> #include <cstdlib> void myInitFunction() { // Initializes the random number generator srand(time(0)); // I'm not sure if this is needed. From my experience I've had to draw one // random number for the new random seed to take effect. rand(); }
He doesn't really emphasize switch/case at all. But I think your way is probably better.I don't think he meant for you to use if/else statements in situations which actually call for use of switch/case. The purpose of the switch/case is to test the value of some variable against a number of possibilities, assuming that these are all integers. This is exactly what you are doing in display().
No, that part of the code was totally my own creation. I understand the problem with magic numbers, but I doubt it does harm in this case. I'll try an alternative set of numbers.On a side note, I would avoid using magic numbers. It seems like you managed to do this successfully for the color names. Or, well, maybe you're using someone else's code. In any case, the maze grid values use magic numbers. You should probably fix that.
FYI, my professor was the one who explicitly showed me the PURPOSE/PRECONDITIONS/POSTCONDITIONS format.Your professor might want something different. If he doesn't care, I'd go with the triple-slashes, though that's just me. A lot of editors recognize the triple-slashes and color them appropriately in a different color than regular comments. If you want to include items such as "PURPOSE" or "PRECONDITIONS" I would at least make them lowercase and use hanging indentation. Better aesthetics. If you decide to use automatic documentation generators such as Doxygen, they may use their own syntax for the above. But I wouldn't use those if you're only going include one of them. Seems silly to me...
Why a double asterisk? One works fine. Or do you mean two asterisks for functions and one for parts within functions?Edit: For multiple lines of documentation comments, use /** and **/ to mark the beginning and end of the documentation comments, respectively, rather than using /// on each and every line.
He doesn't really emphasize switch/case at all. But I think your way is probably better.
FYI, my professor was the one who explicitly showed me the PURPOSE/PRECONDITIONS/POSTCONDITIONS format.
Why a double asterisk? One works fine. Or do you mean two asterisks for functions and one for parts within functions?
Nah, the C++ syntax seems cleaner to me. Also, malloc() doesn't play nicely with exceptions, IIRC, if you decide to use those.
Yes, because using '>>' makes perfect sense under normal programming methods...?
Yes, because using '>>' makes perfect sense under normal programming methods...?
#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <cstdlib>
#define _WIN32_WINNT 0x0500
using namespace std;
enum Colors{blue=1, green, cyan, red, purple, yellow, grey, dgrey, hblue, hgreen, hred, hpurple, hyellow, hwhite};
//function prototypes
void coutc(int color, char*output);
void generate(int maze[20][20], int &i, int &j);
void display(int maze[20][20], int &i, int &j);
int restart();
//handles the maze AI
class navigate
{
public:
//master AI function
void step(int maze[20][20], int &i, int &j);
//movement functions
void left(int maze[20][20], int &i, int &j);
void up(int maze[20][20], int &i, int &j);
void right(int maze[20][20], int &i, int &j);
void down(int maze[20][20], int &i, int &j);
//repeat movement functions
void leftAgain(int maze[20][20], int &i, int &j);
void upAgain(int maze[20][20], int &i, int &j);
void rightAgain(int maze[20][20], int &i, int &j);
void downAgain(int maze[20][20], int &i, int &j);
} go; //calls movement functions from the above class
///initializes the program and calls the other functions
int main()
{
//specifies console dimensions to fit the size of the maze
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r);
MoveWindow(console, r.left, r.top, 800, 400, TRUE);
int i, j, wait; //counters
int maze[20][20]; //the matrix for the maze itself
//initialization
cout << "Welcome to Maze Navigator!" << endl << endl;
system("pause");
generate(maze, i, j);
//loop to generate each frame of the maze
while (maze[0][10] != 2)
{
//generates a frame
go.step(maze, i, j);
//pauses before generating the next frame
for (wait = 0; wait < 20000000; wait++);
}
//displays upon maze completion
system("cls");
coutc(grey, "Maze Solved!");
restart();
}
///outputs a value with a specified text color
void coutc(int color, char* output)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, color);
cout << output;
SetConsoleTextAttribute(handle, color);
}
///generates a randomly structured maze
void generate(int maze[20][20], int &i, int &j)
{
int spaceFreq;
srand(GetTickCount());
system("cls");
cout << "Enter a space frequency between 1 and 10: ";
cin >> spaceFreq;
spaceFreq++;
//checks whether the chosen space frequency is valid
if(spaceFreq < 2 || spaceFreq > 10)
{
system("cls");
cout << "Error: Invalid space frequency!" << endl << endl;
system("pause"); system("cls");
main();
}
//builds a randomized maze based on the chosen space frequency
for(i = 0; i < 20; i++)
{
for(j = 0; j < 20; j++)
{
maze[i][j] = rand() % spaceFreq + 1;
if(maze[i][j] != 0 && maze[i][j] != 1)
{
maze[i][j] = 0;
}
}
}
maze[19][9] = 2; //start point
maze[0][10] = 0; //exit point
}
///displays the maze on the console
void display(int maze[20][20], int &i, int &j)
{
system("cls");
cout << endl << endl;
coutc(hgreen, " $");
cout << endl;
coutc(green, " X X X X X X X X X X X X X X X X X X X X X");
cout << endl;
for(i = 0; i < 20; i++)
{
coutc(green," X");
//specifies the characters corresponding to different components of the maze
for(j = 0; j < 20; j++)
{
switch(maze[i][j])
{
case 0: coutc(blue, " "); break; //empty space
case 1: coutc(green, " X"); break; //solid wall
case 2: coutc(hblue, " O"); break; //navigator
case 3: coutc(blue, " '"); break; //path marker
case 4: coutc(blue, " *"); break; //repeated path marker
default: cout << " " << maze[i][j]; //default component
}
}
coutc(green," X");
cout << endl;
}
coutc(green, " X X X X X X X X X X X X X X X X X X X X X");
cout << endl;
coutc(cyan, " @");
cout << endl;
}
///calls the functions to move the character through the maze
void navigate::step(int maze[20][20], int &i, int &j)
{
display(maze, i, j);
for(i = 0; i < 20; i++)
{
for(j = 0; j < 20; j++)
{
if(maze[i][j] == 2)
{
//tries to go left
if(maze[i][j - 1] == 0 && j != 0)
{
go.left(maze, i, j);
}
//tries to go up
else if(maze[i - 1][j] == 0 && i != 0)
{
go.up(maze, i, j);
}
//tries to go right
else if(maze[i][j + 1] == 0 && j != 19)
{
go.right(maze, i, j);
}
//tries to go down
else if(maze[i + 1][j] == 0 && i != 19)
{
go.down(maze, i, j);
}
//checks for any paths it already crossed and reuses them if necessary
else
{
//tries to go left on a path it already crossed
if(maze[i][j - 1] == 3 && j != 0)
{
go.leftAgain(maze, i, j);
}
//tries to go up on a path it already crossed
else if(maze[i - 1][j] == 3 && i != 0)
{
go.upAgain(maze, i, j);
}
//tries to go right on a path it already crossed
else if(maze[i][j + 1] == 3 && j != 19)
{
go.rightAgain(maze, i, j);
}
//tries to go down on a path it already crossed
else if(maze[i + 1][j] == 3 && i != 19)
{
go.downAgain(maze, i, j);
}
//stops the loop if the AI traps itself
else
{
system("cls");
coutc(grey, "The navigator is trapped!");
restart();
break;
}
}
}
}
}
}
///moves left
void navigate::left(int maze[20][20], int &i, int &j)
{
while(maze[i][j - 1] == 0 && j != 0)
{
maze[i][j - 1] = 2;
maze[i][j] = 3;
display(maze, i, j);
}
}
///moves up
void navigate::up(int maze[20][20], int &i, int &j)
{
while(maze[i - 1][j] == 0 && i != 0)
{
maze[i - 1][j] = 2;
maze[i][j] = 3;
display(maze, i, j);
}
}
///moves right
void navigate::right(int maze[20][20], int &i, int &j)
{
while(maze[i][j + 1] == 0 && j != 19)
{
maze[i][j + 1] = 2;
maze[i][j] = 3;
display(maze, i, j);
}
}
///moves down
void navigate::down(int maze[20][20], int &i, int &j)
{
while(maze[i + 1][j] == 0 && i != 19)
{
maze[i + 1][j] = 2;
maze[i][j] = 3;
display(maze, i, j);
}
}
///moves left onto a space it has already crossed
void navigate::leftAgain(int maze[20][20], int &i, int &j)
{
while(maze[i][j - 1] == 3 && j != 0)
{
maze[i][j - 1] = 2;
maze[i][j] = 4;
display(maze, i, j);
}
}
///moves up onto a space it has already crossed
void navigate::upAgain(int maze[20][20], int &i, int &j)
{
while(maze[i - 1][j] == 3 && i != 0)
{
maze[i - 1][j] = 2;
maze[i][j] = 4;
display(maze, i, j);
}
}
///moves right onto a space it has already crossed
void navigate::rightAgain(int maze[20][20], int &i, int &j)
{
while(maze[i][j + 1] == 3 && j != 19)
{
maze[i][j + 1] = 2;
maze[i][j] = 4;
display(maze, i, j);
}
}
///moves down onto a space it has already crossed
void navigate::downAgain(int maze[20][20], int &i, int &j)
{
while(maze[i + 1][j] == 3 && i != 19)
{
maze[i + 1][j] = 2;
maze[i][j] = 4;
display(maze, i, j);
}
}
///exits or restarts program depending on user input
int restart()
{
int choice;
cout << endl << "Do you want to run another maze? (1/0)" << endl;
cin >> choice;
switch(choice)
{
case 0: return 0; break;
case 1: system("cls"); return main(); break;
default: return 0;
}
}
#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <cstdlib>
#define _WIN32_WINNT 0x0500
using namespace std;
enum Colors{blue=1, green, cyan, red, purple, yellow, grey, dgrey, hblue, hgreen, hred, hpurple, hyellow, hwhite};
//function prototypes
void coutc(int color, char*output);
void generate(int maze[20][20], int &i, int &j);
void display(int maze[20][20], int &i, int &j);
void step(int maze[20][20], int &i, int &j);
int restart();
///initializes the program and calls the other functions
int main()
{
//specifies console dimensions to fit the size of the maze
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r);
MoveWindow(console, r.left, r.top, 800, 400, TRUE);
int i, j, wait; //counters
int maze[20][20]; //the matrix for the maze itself
//initialization
cout << "Welcome to Maze Navigator!" << endl << endl;
system("pause");
generate(maze, i, j);
//loop to generate each frame of the maze
while (maze[0][10] != 2)
{
//generates a frame
step(maze, i, j);
//pauses before generating the next frame
for (wait = 0; wait < 20000000; wait++);
}
//displays upon maze completion
system("cls");
coutc(grey, "Maze Solved!");
restart();
}
///outputs a value with a specified text color
void coutc(int color, char* output)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, color);
cout << output;
SetConsoleTextAttribute(handle, color);
}
///generates a randomly structured maze
void generate(int maze[20][20], int &i, int &j)
{
int spaceFreq;
srand(GetTickCount());
system("cls");
cout << "Enter a space frequency between 1 and 10: ";
cin >> spaceFreq;
spaceFreq++;
//checks whether the chosen space frequency is valid
if(spaceFreq < 2 || spaceFreq > 10)
{
system("cls");
cout << "Error: Invalid space frequency!" << endl << endl;
system("pause"); system("cls");
main();
}
//builds a randomized maze based on the chosen space frequency
for(i = 0; i < 20; i++)
{
for(j = 0; j < 20; j++)
{
maze[i][j] = rand() % spaceFreq + 1;
if(maze[i][j] != 0 && maze[i][j] != 1)
{
maze[i][j] = 0;
}
}
}
maze[19][9] = 2; //start point
maze[0][10] = 0; //exit point
}
///displays the maze on the console
void display(int maze[20][20], int &i, int &j)
{
system("cls");
cout << endl << endl;
coutc(hgreen, " $");
cout << endl;
coutc(green, " X X X X X X X X X X X X X X X X X X X X X");
cout << endl;
for(i = 0; i < 20; i++)
{
coutc(green," X");
//specifies the characters corresponding to different components of the maze
for(j = 0; j < 20; j++)
{
switch(maze[i][j])
{
case 0: coutc(blue, " "); break; //empty space
case 1: coutc(green, " X"); break; //solid wall
case 2: coutc(hblue, " O"); break; //navigator
case 3: coutc(blue, " '"); break; //path marker
case 4: coutc(blue, " *"); break; //repeated path marker
default: cout << " " << maze[i][j]; //default component
}
}
coutc(green," X");
cout << endl;
}
coutc(green, " X X X X X X X X X X X X X X X X X X X X X");
cout << endl;
coutc(cyan, " @");
cout << endl;
}
///AI to make the character move through the maze
void step(int maze[20][20], int &i, int &j)
{
display(maze, i, j);
for(i = 0; i < 20; i++)
{
for(j = 0; j < 20; j++)
{
if(maze[i][j] == 2)
{
//tries to go left
if(maze[i][j - 1] == 0 && j != 0)
{
while(maze[i][j - 1] == 0 && j != 0)
{
maze[i][j - 1] = 2;
maze[i][j] = 3;
display(maze, i, j);
}
}
//tries to go up
else if(maze[i - 1][j] == 0 && i != 0)
{
while(maze[i - 1][j] == 0 && i != 0)
{
maze[i - 1][j] = 2;
maze[i][j] = 3;
display(maze, i, j);
}
}
//tries to go right
else if(maze[i][j + 1] == 0 && j != 19)
{
while(maze[i][j + 1] == 0 && j != 19)
{
maze[i][j + 1] = 2;
maze[i][j] = 3;
display(maze, i, j);
}
}
//tries to go down
else if(maze[i + 1][j] == 0 && i != 19)
{
while(maze[i + 1][j] == 0 && i != 19)
{
maze[i + 1][j] = 2;
maze[i][j] = 3;
display(maze, i, j);
}
}
//checks for any paths it already crossed and reuses them if necessary
else
{
//tries to go left on a path it already crossed
if(maze[i][j - 1] == 3 && j != 0)
{
while(maze[i][j - 1] == 3 && j != 0)
{
maze[i][j - 1] = 2;
maze[i][j] = 4;
display(maze, i, j);
}
}
//tries to go up on a path it already crossed
else if(maze[i - 1][j] == 3 && i != 0)
{
while(maze[i - 1][j] == 3 && i != 0)
{
maze[i - 1][j] = 2;
maze[i][j] = 4;
display(maze, i, j);
}
}
//tries to go right on a path it already crossed
else if(maze[i][j + 1] == 3 && j != 19)
{
while(maze[i][j + 1] == 3 && j != 19)
{
maze[i][j + 1] = 2;
maze[i][j] = 4;
display(maze, i, j);
}
}
//tries to go down on a path it already crossed
else if(maze[i + 1][j] == 3 && i != 19)
{
while(maze[i + 1][j] == 3 && i != 19)
{
maze[i + 1][j] = 2;
maze[i][j] = 4;
display(maze, i, j);
}
}
//stops the loop if the AI traps itself
else
{
system("cls");
coutc(grey, "The navigator is trapped!");
restart();
break;
}
}
}
}
}
}
///program exits or restarts depending on user input
int restart()
{
int choice;
cout << endl << "Do you want to run another maze? (1/0)" << endl;
cin >> choice;
switch(choice)
{
case 0: return 0; break;
case 1: system("cls"); return main(); break;
default: return 0;
}
}
Don't arrays automatically pass by reference?
Also, I kind of thought that passing the entire contents of the maze each time was the only way for the program to figure out which values it has to modify.
Plus, I view the slowness as a feature rather than a drawback, seeing as how the progress of the navigator would be hard to follow if the program did things quickly.