# Huggge Question

• 12-03-2002
tetraflare
Huggge Question
ok first of all , greets to you all. secondly, i have a project due, now, before you all unsheif your swords.....i come bearing code!!! :) i know how you guys feel bout the " oh im stuck do it for me" policy. anyways heres the objective.

make a 40 by 40 maze that travels the fastest way to the end as possible, and randomly generate walls( im having loads of trouble with this 1) and as you get closer, your chances of getting there are better than if you were far away..... btw the computer runs this game, its just showing how its done, movement will be another thread :)

here is what i have, it prints the box, and displays the move, but it moves all over the box..... i apprciate any help in advance....

Code:

``` #include <iostream> #include <ctime> #include <cstdlib> #include <windows.h> using namespace std; unsigned long stepInt = 0; const int size = 40; int arraybox[size][size]; int r = 1, c = 1; void print(void); void move(void); void walls(void); int main() { int yo=1;                 for (int i = 0; i < size + 1; i++)        {         arraybox[i][0] =177;         arraybox[i][size-1] = 177;         arraybox[0][i] = 177;         arraybox[size-1][i] = 177;         }         walls();         print();         cout << "\nProccessing\n";         for (int y = 0; y < 50; y++)        {                                 Sleep(10);                                 cout << '.';         }         move();         cout << endl << "Steps: " << stepInt << endl;         cin >> yo;         return 0; } void print() {         for (int l = 0; l < size; l++)        {                 cout << endl;                         for(int g = 0; g < size; g++)                                 cout<<static_cast<char>(arraybox[ l ] [g]);                                 } } void move() {         int num, end = 0;         srand(time(0));         while (end != 1)        {                 num = 1 + rand() % 4;                 if ( num == 1 && arraybox[r-1][c] != 177)        {                         arraybox[r][c] = 27;                         r = r - 1;                         stepInt += 1;                 }                 else if ( num == 2 && arraybox[r+1][c] != 177)        {                         arraybox[r][c] = 26;                         r = r + 1;                         stepInt += 1;                 }                 else if ( num == 3 && arraybox[r][c-1] != 177)        {                         arraybox[r][c] = 24;                         c = c - 1;                         stepInt += 1;                 }                 else if ( num == 4 && arraybox[r][c+1] != 177)        {                         arraybox[r][c] = 25;                         c = c + 1;                         stepInt += 1;                 }                                 if (r == size-2 && c == size-2)        {                         end = 1;                         cout << endl << endl << endl << endl;                         arraybox[r][c] = 4;                         print();                 }         } } void walls() {         int randR, randH, done = 0;         srand(time(0));         while (done != size / 1 + rand() % 4)        {                 randR = 1 + rand() % size - 1;                 randH = 1 + rand() % size - 1;                                                 if ( arraybox[randR][randH] != 177 ||                     arraybox[randR-1][randH] != 177 ||                     arraybox[randR+1][randH] != 177 ||                     arraybox[randR][randH+1] != 177 ||                     arraybox[randR][randH-1] != 177 ||                     arraybox[randR+1][randH+1] != 177 ||                     arraybox[randR-1][randH-1] != 177)        {                 arraybox[randR][randH] = 177;                 arraybox[randR-1][randH] = 177;                 arraybox[randR+1][randH] = 177;                 arraybox[randR-1][randH-1] = 177;                 arraybox[randR+1][randH+1] = 177;                 arraybox[randR][randH+1] = 177;                 arraybox[randR+1][randH-1] = 177;                 done += 1;                 }         } }```

thanks again.

&#91;code]&#91;/code]tagged by Salem
• 12-03-2002
Stoned_Coder
First tip..... code should be commented and self-documenting.

Your code has no comments and too many magic numbers(i.e. 177). Replace those with symbolic constants to make it easier to see what you are trying to achieve.

Then use code tags when writing code as it makes it so much more readable. More help when this is done.
• 12-04-2002
tetraflare
sorry i was kinda on the fly here it is :)

btw 177 = the ascii character used for my walls.

Quote:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <windows.h>
using namespace std;
unsigned long stepInt = 0;
const int size = 40;
int arraybox[size][size];
int r = 1, c = 1;
void print(void);
void move(void);
void walls(void);
int main()
{
int yo=1;

for (int i = 0; i < size + 1; i++) {
arraybox[i][0] =177;
arraybox[i][size-1] = 177; //makes the box with
arraybox[0][i] = 177;
arraybox[size-1][i] = 177; // ascii char 177 (wall)
}
walls();
print();
cout << "\nProccessing\n";
for (int y = 0; y < 50; y++) {

Sleep(10);
//processes the maze
cout << '.';
}
move();
cout << endl << "Steps: " << stepInt << endl; //shows
cin>>yo; //number of steps needed
//to complete the maze.
return 0;

}

void print()
{

for (int l = 0; l < size; l++) {
cout << endl;
for(int g = 0; g < size; g++)
cout<<static_cast<char>(arraybox[ l ] [g]);

}
}

void move() // just makes sure you cant run into walls
{
int num, end = 0;
srand(time(0));
while (end != 1) {

num = 1 + rand() % 4;

if ( num == 1 && arraybox[r-1][c] != 177) {
arraybox[r][c] = 27;
r = r - 1;
stepInt += 1;
}
else if ( num == 2 && arraybox[r+1][c] != 177) {
arraybox[r][c] = 26;
r = r + 1;
stepInt += 1;
}
else if ( num == 3 && arraybox[r][c-1] != 177) {
arraybox[r][c] = 24;
c = c - 1;
stepInt += 1;
}
else if ( num == 4 && arraybox[r][c+1] != 177) {
arraybox[r][c] = 25;
c = c + 1;
stepInt += 1;
}

if (r == size-2 && c == size-2) {
end = 1;
cout << endl << endl << endl << endl;
arraybox[r][c] = 4;
print();
}

}
}

void walls() // defines the walls
{
int randR, randH, done = 0;

srand(time(0));

while (done != size / 1 + rand() % 4) {

randR = 1 + rand() % size - 1;
randH = 1 + rand() % size - 1;

if ( arraybox[randR][randH] != 177 ||
arraybox[randR-1][randH] != 177 ||
arraybox[randR+1][randH] != 177 ||
arraybox[randR][randH+1] != 177 ||
arraybox[randR][randH-1] != 177 ||
arraybox[randR+1][randH+1] != 177 ||
arraybox[randR-1][randH-1] != 177) {
arraybox[randR][randH] = 177;
arraybox[randR-1][randH] = 177;
arraybox[randR+1][randH] = 177;
arraybox[randR-1][randH-1] = 177;
arraybox[randR+1][randH+1] = 177;
arraybox[randR][randH+1] = 177;
arraybox[randR+1][randH-1] = 177;
done += 1;
}
}
}

once again, i need to find a way to make the box reach the end of the map in the shortest distance possible. then from their, i need to make walls for the maze to go through...

any help is greatly appreciated.

thanks
• 12-04-2002
Stoned_Coder
consider const int WALL=177; or similar. Maybe an enum. Code should have no magic numbers. If it said WALL everytime u see 177 the code will automatically become more readable.I knew what 177 was but you have to learn to write self documenting code.
• 12-04-2002
tetraflare
ok sorry i see im new to programming, sorry if i confused any 1 :(

but the fact remains, how is it possible to make it move to the end in the shortest distance?
• 12-04-2002
Stoned_Coder
• 12-04-2002
tetraflare
wow thanks. i appreciate it, and i thankyou for being patient with me, i didnt realize it was such a deep algorithm :) ah well time to read that! :)
• 12-04-2002
moi
Quote:

Originally posted by tetraflare
sorry i was kinda on the fly here it is :)

btw 177 = the ascii character used for my walls.

thanks

highly unlikely, considering the ascii character set ends at 127.
• 12-04-2002
tetraflare
woah dude what ever your tokin off of get me some! there are way way more than 127, try making a program that prints out all the ascii chars. form 0 to 255 thats right pal, 255....... yeh.....
• 12-04-2002
Stoned_Coder
the top 128 chars are implementation specific. What may be a wall to you on a different operating system it could be something completely different.Only the first 127 chars are the same across all ascii operating systems
• 12-04-2002
tetraflare
Quote:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <windows.h>
using namespace std;
unsigned long stepInt = 0;
const int size = 40;
int arraybox[size][size]={177,177,177,177,177,177,177,177,177,177,177,177, 177,177,177,177,177,177,177,177,177,177,177,177,17 7,177,177,177,177,177,177,177,177,177,177,177,177, 177,177,177,
177, 0,0,0,177,0,0,0,0,0,0,0,177,0,0,0,177,0,0,0,177,0, 0,0,0,0,177,0,0,0,177,0,0,0,0,0,0,0,0,177,
177,177,177,0,0,0,177,177,177,177,177,0,0,0,177,0, 177,0,177,0,177,0,177,177,177,177,177,0,177,0,177, 0,177,177,177,177,177,177,0,177,
177,0,0,0,177,0,177,0,0,0,177,177,177,177,177,0,0, 0,177,0,177,0,177,0,0,0,177,0,177,0,0,0,177,0,0,0, 0,177,0,177,
177,0,177,177,0,0,0,0,177,0,177,0,0,0,177,177,177, 177,177,0,177,0,0,0,177,0,177,0,177,177,177,177,17 7,0,177,0,0,177,0,177,
177,0,0,177,0,177,177,177,177,0,0,0,177,0,0,177,0, 177,0,0,177,0,177,177,0,0,177,0,0,0,0,0,177,0,177, 0,0,177,0,177,
177,177,177,177,0,0,0,0,177,177,177,177,177,177,0, 177,0,0,0,177,177,0,177,0,0,177,177,177,177,177,17 7,0,177,0,177,0,177,177,0,177,
177,0,0,0,0,177,177,0,0,0,0,0,0,177,0,177,0,177,17 7,177,0,0,177,0,177,177,0,0,0,177,177,0,177,0,177, 0,0,0,0,177,
177,0,177,177,177,177,177,177,177,177,177,177,0,17 7,177,177,177,177,177,0,0,177,177,0,177,0,0,177,0, 177,0,0,177,0,177,177,177,177,177,177,
177,0,177,0,0,0,0,0,0,0,0,177,0,177,0,0,0,0,177,0, 177,177,0,0,177,0,177,177,0,177,0,177,177,0,0,0,0, 0,0,177,
177,0,177,0,177,177,177,177,177,177,0,177,0,177,0, 177,177,0,177,0,177,0,0,177,177,0,177,0,0,177,0,0, 177,0,177,177,177,177,0,177,
177,0,177,0,177,0,0,0,0,177,0,177,0,177,0,0,177,0, 177,0,177,0,177,177,0,0,177,0,177,177,177,0,177,0, 177,0,0,0,0,177,
177,0,177,0,177,0,177,177,0,177,0,177,0,177,177,0, 177,0,177,0,177,0,177,0,0,177,177,0,0,177,0,0,177, 0,177,0,177,177,177,177,
177,0,177,0,177,0,0,177,0,177,0,177,0,0,0,0,177,0, 0,0,177,0,177,0,177,177,177,177,0,177,0,177,177,0, 177,0,0,0,0,177,
177,0,177,0,177,177,0,177,0,177,0,177,0,177,177,17 7,177,177,177,177,177,0,177,0,177,0,0,0,0,177,0,0, 177,0,177,177,177,177,0,177,
177,0,177,0,0,0,0,177,0,177,0,177,0,0,0,0,0,177,0, 0,0,0,177,0,177,0,177,177,177,177,177,0,177,0,177, 0,0,0,0,177,
177,0,177,177,177,177,177,177,0,177,0,177,177,177, 177,177,0,177,0,177,177,177,177,0,177,0,177,0,0,0, 177,0,177,0,177,0,177,177,177,177,
177,0,0,0,0,0,0,0,0,177,0,177,0,0,0,0,0,177,0,0,0, 0,0,0,177,0,0,0,177,0,0,0,177,0,177,0,0,0,0,177,
177,177,177,177,177,177,177,177,177,177,0,177,0,17 7,177,177,177,177,177,177,177,177,177,177,177,177, 177,177,177,177,177,177,177,177,177,177,177,177,0, 177,
177,0,0,0,0,0,0,0,0,0,0,177,0,177,0,0,0,177,0,0,0, 0,0,0,0,177,0,0,0,0,0,0,0,177,0,0,0,177,0,177,
177,0,177,177,177,177,177,177,177,177,177,177,0,17 7,0,177,0,0,0,177,177,177,177,177,0,177,0,177,177, 177,177,177,0,177,0,177,0,177,0,177,
177,0,177,0,0,0,0,0,0,0,0,177,0,177,0,177,177,177, 177,177,0,0,0,0,0,177,0,177,0,0,0,177,0,177,0,177, 0,0,0,177,
177,0,177,0,177,177,177,177,177,177,0,177,0,177,0, 0,0,177,0,0,0,177,177,177,177,177,0,177,0,177,0,17 7,0,177,0,177,177,177,177,177,
177,0,177,0,177,0,0,0,0,177,0,177,0,177,177,177,0, 177,0,177,177,177,0,0,0,177,0,0,0,177,0,177,0,0,0, 0,0,0,0,177,
177,0,0,0,177,0,177,177,0,177,0,177,0,0,0,0,0,177, 0,0,0,0,0,177,0,177,177,177,177,177,177,177,177,17 7,177,177,177,177,0,177,
177,177,177,177,177,0,177,177,0,177,0,177,177,177, 177,177,177,177,177,177,177,177,177,177,0,0,0,0,0, 177,0,0,0,0,0,0,0,177,0,177,
177,0,0,0,0,0,177,177,0,177,0,177,0,0,0,177,0,0,0, 0,0,0,0,177,177,177,177,177,0,177,0,177,177,177,17 7,177,0,177,0,177,
177,0,177,177,177,177,177,177,0,177,0,177,0,177,0, 177,0,177,177,177,177,177,0,0,0,0,0,177,0,177,0,17 7,0,0,0,177,0,177,0,177,
177,0,0,177,0,0,0,177,0,0,0,177,0,177,0,177,0,177, 0,0,0,177,177,177,177,177,0,177,0,177,0,177,0,177, 0,0,0,177,0,177,
177,177,0,177,0,177,0,177,177,177,177,177,0,177,0, 177,0,177,0,177,0,177,0,0,0,177,0,177,0,177,0,177, 0,177,177,177,177,177,0,177,
177,0,0,177,0,177,0,177,0,0,0,0,0,177,0,177,0,0,0, 177,0,177,0,177,0,0,0,0,0,177,0,177,0,0,0,0,0,0,0, 177,
177,0,177,177,0,177,0,177,0,177,177,177,177,177,0, 177,177,177,177,177,0,177,0,177,177,177,177,177,17 7,177,0,177,177,177,177,177,177,177,177,177,
177,0,0,177,0,177,0,177,0,177,0,0,0,0,0,177,0,0,0, 177,0,177,0,0,0,0,0,0,0,177,0,0,0,0,0,0,0,0,0,177,
177,177,0,177,0,177,0,177,0,177,0,177,177,177,177, 177,0,177,0,177,0,177,177,177,177,177,177,177,0,17 7,177,177,177,177,177,177,177,177,0,177,
177,0,0,177,0,177,0,177,0,177,0,177,0,0,0,177,0,17 7,0,177,0,0,0,177,0,0,0,177,0,177,0,0,0,0,0,177,0, 0,0,177,
177,0,177,177,0,177,0,177,0,177,0,177,0,177,0,177, 0,177,0,177,177,177,0,177,0,177,0,177,0,177,0,177, 177,177,0,177,0,177,177,177,
177,0,177,0,0,177,0,177,0,177,0,177,0,177,0,177,0, 177,0,0,0,0,0,177,0,177,0,0,0,177,0,0,0,177,0,177, 0,0,0,177,
177,0,177,0,177,177,0,177,0,177,0,177,0,177,0,177, 177,177,177,177,177,177,177,177,177,177,177,177,17 7,177,177,177,0,177,0,177,177,177,177,177,
177,0,0,0,177,177,0,0,0,177,0,0,0,177,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,177,0,0,0,0, 4,177,
177,177,177,177,177,177,177,177,177,177,177,177,17 7,177,177,177,177,177,177,177,177,177,177,177,177, 177,177,177,177,177,177,177,177,177,177,177,177,17 7,177,177};
int r = 1, c = 1;
void print(void);
void move(void);

int main()
{
int yo=1;

for (int i = 0; i < size + 1; i++) {
arraybox[i][0] =177;
arraybox[i][size-1] = 177;
arraybox[0][i] = 177;
arraybox[size-1][i] = 177;
}

print();
cout << "\nProccessing\n";
for (int y = 0; y < 50; y++) {

Sleep(10);

cout << '.';
}
move();
cout << endl << "Steps: " << stepInt << endl;
cin>>yo;

return 0;

}

void print()
{

for (int l = 0; l < size; l++) {
cout << endl;
for(int g = 0; g < size; g++)
cout<<static_cast<char>(arraybox[ l ] [g]);

}
}

void move() // just makes sure you cant run into walls
{
int num, end = 0;
srand(time(0));
while (end != 1) {

num = 1 + rand() % 4;

if ( num == 1 && arraybox[r-1][c] != 177) {
arraybox[r][c] = 27;
r = r - 1;
stepInt += 1;
}
else if ( num == 2 && arraybox[r+1][c] != 177 ) {
arraybox[r][c] = 26;
r = r + 1;
stepInt += 1;
}
else if ( num == 3 && arraybox[r][c-1] != 177 ) {
arraybox[r][c] = 24;
c = c - 1;
stepInt += 1;
}
else if ( num == 4 && arraybox[r][c+1] != 177 ) {
arraybox[r][c] = 25;
c = c + 1;
stepInt += 1;
}

if (r == size-2 && c == size-2) {
end = 1;
cout << endl << endl << endl << endl;
arraybox[r][c] = 4;
print();
}

}
}

• 12-04-2002
tetraflare
ok now i have a serious question... on my maze ( posted below)

i need to have the maze tracel to the end in the FASTEST possible path... ( how would i make a pointer to do this...or do i even need a pointer? )

a few things first off...

1) the number 177 is the acsii character for wall in my maze. all other numbers are arrows except 40, which is the end.

2) right now it makes like 7000 moves to get to the end, how can i make it so it wont backtrack? or try to go through walls?

****** I strongly reccomend you compile and run, it may not work the first time or so, ust keep trying, a rand is messed up somewhere :) so please beae with me. :) ****************

here it is

Quote:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <windows.h>
using namespace std;
unsigned long stepInt = 0;
const int size = 40;
int arraybox[size][size];
int r = 1, c = 1;
void print(void);
void move(void);
void walls(void);
int main()
{
int yo=1;

for (int i = 0; i < size + 1; i++) { //intiailizes box
arraybox[i][0] =177;
arraybox[i][size-1] = 177;
arraybox[0][i] = 177;
arraybox[size-1][i] = 177;
}
walls();
print();
cout << "\nProccessing\n";
for (int y = 0; y < 50; y++) {

Sleep(10);

cout << '.';
}
move();
cout << endl << "Steps: " << stepInt << endl;
//steps counts # of moves it takes
cin >> yo;

return 0;

}

void print()
{

for (int l = 0; l < size; l++) {
cout << endl;
for(int g = 0; g < size; g++)
cout<<static_cast<char>(arraybox[ l ] [g]);

}
}

void move()
{
int num, end = 0;
srand(time(0));
while (end != 1) {

num = 1 + rand() % 4;

if ( num == 1 && arraybox[r-1][c] != 177) {
arraybox[r][c] = 27;
r = r - 1;
stepInt += 1;
}
else if ( num == 2 && arraybox[r+1][c] != 177) {
arraybox[r][c] = 26;
r = r + 1;
stepInt += 1;
}
else if ( num == 3 && arraybox[r][c-1] != 177) {
arraybox[r][c] = 24;
c = c - 1;
stepInt += 1;
}
else if ( num == 4 && arraybox[r][c+1] != 177) {
arraybox[r][c] = 25;
c = c + 1;
stepInt += 1;
}

if (r == size-2 && c == size-2) {
end = 1;
cout << endl << endl << endl << endl;
arraybox[r][c] = 4;
print();
}

}
}

void walls()
{
int randR, randH, done = 0;

srand(time(0));

while (done != size / 1 + rand() % 4) {

randR = 1 + rand() % size - 1;
randH = 1 + rand() % size - 1;

if ( arraybox[randR][randH] != 177 ||
arraybox[randR-1][randH] != 177 ||
arraybox[randR+1][randH] != 177 ||
arraybox[randR][randH+1] != 177 ||
arraybox[randR][randH-1] != 177 ||
arraybox[randR+1][randH+1] != 177 ||
arraybox[randR-1][randH-1] != 177) {
arraybox[randR][randH] = 177;
arraybox[randR-1][randH] = 177;
arraybox[randR+1][randH] = 177;
arraybox[randR-1][randH-1] = 177;
arraybox[randR+1][randH+1] = 177;
arraybox[randR][randH+1] = 177;
arraybox[randR+1][randH-1] = 177;
done += 1;
}
}
}

• 12-04-2002
moi
Quote:

Originally posted by tetraflare
woah dude what ever your tokin off of get me some! there are way way more than 127, try making a program that prints out all the ascii chars. form 0 to 255 thats right pal, 255....... yeh.....
shut up. you dont have a clue what you are talking about. i should punch your face in, but i'm slightly nice. ansi C specifies that a char can hold from at least 0 to 127, and the ascii character tables map from 0 to 127. hell, i'll even tell you what you are using: the ms-dos standard extended character set. sure as hell aint ascii tho.