Hi, I'm kinda new here, and I've only been programming for 6 or 8 months, so please forgive me if I sound like a total noob or an idiot with programming.

I've been trying to code for my school assignment two AIs that are supposed to navigate around a maze. Right now I'm having trouble with the navigation program.

First off, let me explain what "squareid"s are. Well, first I have an ordinary maze on a number plane (x and y coordinates), assigned to each coordinate (like for example (2,1)), is a "square id", which says how many walls are in that square and where. The first digit is how many walls in that square, the second is what it looks like because there are different permutations possible, like for example a one walled square can either have a single wall north, east, south or west.

Everything in my program works, the AI moves along the paths in the maze, except when it is in a square that only has one wall, and because of that it gets stuck in square (3,1).

In my code I have a switch that performs tasks depending on the square ID. The "one-walled" series of squareIDs are when the switch tests 11,12,13, and 14.

Please check my code, any comments or criticisms would be greatly appreciated.

//2nd attempt at maze program

#include <iostream>
using namespace std;

//Declaration of all variables goes here

int const oneone=23; int const onetwo=26; int const onethree=26; int const onefour=24;  // all these are the square ids of the    // coordinates in the maze
int const twoone=25; int const twotwo=33; int const twothree=26; int const twofour=11;
int const threeone=13; int const threetwo=24; int const threethree=34; int const threefour=25;
int const fourone=32; int const fourtwo=22; int const fourthree=21; int const fourfour=32;

int xgeneral=3; int ygeneral=3; // These variables store the position of the AI general
// I usually specify what value they are to say what they
// will initially be at the beginning of the maze.

int xtarget=2; int ytarget=2;  //These are the x and y coordinates of the point of interest
//They won't ever change because they are a static location.

int xenemy=4; int yenemy=4; //I won't use this in the first testing of the scouting program just yet.
// but these values declare the x and y position of the enemy unit.

int maze02 (int p, int q) //This function returns a "square ID" depending on the x and y           coordinates of the AI
{
int r;

if (p==1 && q==4)
r=onefour;

if (p==2 && q==4)
r=twofour;

if (p==3 && q==4)
r=threefour;

if (p==4 && q==4)
r=fourfour;

if (p==1 && q==3)
r=onethree;

if (p==2 && q==3)
r=twothree;

if (p==3 && q==3)
r=threethree;

if (p==4 && q==3)
r=fourthree;

if (p==1 && q==2)
r=onetwo;

if (p==2 && q==2)
r=twotwo;

if (p==3 && q==2)
r=threetwo;

if (p==4 && q==2)
r=fourtwo;

if (p==1 && q==1)
r=oneone;

if (p==2 && q==1)
r=twoone;

if (p==3 && q==1)
r=threeone;

if (p==4 && q==1)
r=fourone;

return (r);

}

int xnode (int a, int b) //these values are used to calculate distance in the maze
{
int xn;

if (a == 1 && b == 1)
xn=1;

if (a == 2 && b == 1)
xn=1;

if (a == 3 && b == 1)
xn=1;

if (a == 4 && b == 1)
xn=1;

if (a == 1 && b == 2)
xn=1;

if (a == 2 && b == 2)
xn=3;

if (a == 3 && b == 2)
xn=2;

if (a == 4 && b == 2)
xn=3;

if (a == 1 && b == 3)
xn=1;

if (a == 2 && b == 3)
xn=2;

if (a == 3 && b == 3)
xn=5;

if (a == 4 && b == 3)
xn=4;

if (a == 1 && b == 4)
xn=1;

if (a == 2 && b == 4)
xn=1;

if (a == 3 && b == 4)
xn=1;

if (a == 4 && b == 4)
xn=1;

return (xn);
}

int ynode (int c, int d) //this is the same as the xnode function, but they are both
//working fine.
{
int yn;

if (c == 1 && d == 1)
yn=4;

if (c == 2 && d == 1)
yn=3;

if (c == 3 && d == 1)
yn=2;

if (c == 4 && d == 1)
yn=1;

if (c == 1 && d == 2)
yn=5;

if (c == 2 && d == 2)
yn=8;

if (c == 3 && d == 2)
yn=2;

if (c == 4 && d == 2)
yn=2;

if (c == 1 && d == 3)
yn=6;

if (c == 2 && d == 3)
yn=8;

if (c == 3 && d == 3)
yn=2;

if (c == 4 && d == 3)
yn=2;

if (c == 1 && d == 4)
yn=7;

if (c == 2 && d == 4)
yn=8;

if (c == 3 && d == 4)
yn=9;

if (c == 4 && d == 4)
yn=10;

return (yn);
}

int distance (int e, int f, int g, int h) //now this calculates distance using
//the xnode and ynode numbers
{
int xdist1=e-g;

if (xdist1<0)
xdist1=xdist1*-1;

int ydist1=f-h;

if (ydist1<0)
ydist1=ydist1*-1;

int dist1;

if (e>=1 && g>=1)
dist1=g+e+ydist1-1;
else
dist1=xdist1+ydist1;

return (dist1);
}

int main()
{
int prompt; cin>>prompt;

while(xgeneral !=xtarget || ygeneral != ytarget)
{

int squareid=maze02(xgeneral,ygeneral); //this gives the square ID based on
//the coordinates of the AI.

int xtar=xnode(xtarget,ytarget);        //this gets the xnode and ynode
int ytar=ynode(xtarget,ytarget);        //coordinates of the point of interest
//which is 2,2.

int xgen=xnode(xgeneral,ygeneral);      //this gets the xnode and ynode
int ygen=ynode(xgeneral,ygeneral);      //coordinates of the AI general
//the AI general is located at 3,3.

int xright=xnode(xgeneral+1,ygeneral);  //now this predicts the values of xnode
int xleft=xnode(xgeneral-1,ygeneral);   //and ynode coordinates "if" the AI
int yup=ynode(xgeneral,ygeneral+1);     //were to move up,down,right or left.
int ydown=ynode(xgeneral,ygeneral-1);

int distright=distance(xright,ygen,xtar,ytar); //this calculates the distance
//"if" the AI move right

int distleft=distance(xleft,ygen,xtar,ytar);   //this calculates the distance
//"if" the AI move left

int distup=distance(xgen,yup,xtar,ytar);       //this calculates the distance
//"if" the AI move up

int distdown=distance(xgen,ydown,xtar,ytar);   //this calculates the distance
//"if" the AI moves down

cout<<"squareid:"<<squareid<<"     ";          //this is just so the program
cin>>prompt;                                   //doesn't run too quickly

switch (squareid) { //from here on the computer doesn't execute the
//if statements
case 11: //one wall at the north of the square
//here it's possible to move left, down and right

if (distright<distleft && distright<distdown)
xgeneral+=1;

if (distdown<distright && distdown<distleft)
ygeneral-=1;

if (distleft<distright && distleft<distdown)
xgeneral-=1;
break;

case 12: //one wall at the east of the square
//here it's possible to move up, left and down

if (distup<distdown && distup<distleft)
ygeneral+=1;

if (distdown<distup && distdown<distleft)
ygeneral-=1;

if (distleft<distup && distleft<distdown)
xgeneral-=1;
break;

case 13: //one wall at the south of the square
//here it's possible to move up, right and left.

if (distup<distright && distup<distleft)
ygeneral+=1;

if (distright<distleft && distright<distup)
xgeneral+=1;

if (distleft<distright && distleft<distup)
xgeneral-=1;
break;

case 14: //one wall at the west of the square
//here it's possible to move up, right and down.

if (distup<distdown && distup<distright)
ygeneral+=1;

if (distright<distup && distright<distdown)
xgeneral+=1;

if (distdown<distup && distdown<distright)
ygeneral-=1;
break;
//after this point everything does work
case 21:
if (distleft<distdown)
xgeneral-=1;

if (distdown<distleft)
ygeneral-=1;
break;

case 22:
if (distup<distleft)
ygeneral+=1;

if (distleft<distup)
xgeneral-=1;
break;

case 23:
if (distup<distright)
ygeneral+=1;

if (distright<distup)
xgeneral+=1;
break;

case 24:
if (distright<distdown)
xgeneral+=1;

if (distdown<distright)
ygeneral-=1;
break;
//for some reason the computer is stuck when the case is 25
//square ID 25 means that there is one wall north and
//one wall south. So this square looks like an "equals" sign.
// kinda like "=".

case 25:
if (distright<distleft)
xgeneral+=1;

if (distleft<distright)
xgeneral-=1;
break;

case 26:
if (distup<distdown)
ygeneral+=1;

if (distdown<distup)
ygeneral-=1;
break;

case 31:
ygeneral=ygeneral-1;
break;

case 32:
xgeneral=xgeneral-1;
break;

case 33:
ygeneral=ygeneral+1;
break;

case 34:
xgeneral=xgeneral+1;
break;

default:
cout<<"squarid unassigned. please fix the program.";
}

cout<<"New x coordinate of AI general:"<<xgeneral;
cout<<"\nNew y coordinate of AI general:"<<ygeneral;

cout<<"\nPress anykey to continue";
cin>>prompt;
}
return 0;
}

Please pardon me if it is a little messy when the program runs. I know its crude, but I lost my programming book a while ago, so I don't know much.

Thanks.

• line 266 case: 12
• line 292 case: 14

:-/
Are these supposed to correspond with lines 8, 9, 10, and 11? I only scanned quickly through this. Maybe it could be a typo. Apologies if I am way off bases, because I am only a beginner programmer. Why are their coordinates for 25 and 26 if your maze is only 4X4? Good luck. :)