1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int SIZE = 10;
enum value {space, wall, ext, robot};
// Easier way to visualize maze, and to make changes, quicker
int maze[SIZE][SIZE] =
{
{1,1,3,1,1,1,1,1,1,1},
{1,1,0,0,0,1,1,1,1,1},
{1,1,1,0,0,0,1,1,1,1},
{1,1,1,0,1,0,1,1,1,1},
{1,0,0,0,1,0,1,1,1,1},
{1,1,0,1,1,0,1,1,0,2},
{1,1,0,1,1,0,0,0,0,1},
{1,1,0,1,1,1,1,1,1,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,0,2}
};
struct loc
{
int x;
int y;
};
loc roblo;
void createmaze();
void showmaze();
bool robmov();
int main ()
{
srand((unsigned)time(0));
// Robot starting position
roblo.x = 0;
roblo.y = 2;
bool out = false;
do
{
out = robmov();
showmaze();
}while(!out);
cout << "Exiting the maze.." << endl;
return 0;
}
void showmaze(){
for (int i=0; i<SIZE; i++)
{
for (int j=0; j<SIZE; j++)
{
if (maze[i][j] == wall)
{
cout << "+";
}
if (maze[i][j] == space || maze[i][j] == 5)
{
cout << " ";
}
if (maze[i][j] == ext)
{
cout << "}";
}
if (maze[i][j]==robot)
{
cout << "*";
}
}
cout << endl;
}
cout << endl << endl;
}
bool robmov()
{
int move = rand()%50; // To take a different route, sometimes
int ok = 0;
if(maze[roblo.x+1][roblo.y] == ext || maze[roblo.x][roblo.y+1] == ext)
{
maze[roblo.x][roblo.y] = 5;
if(maze[roblo.x+1][roblo.y] == ext)
maze[roblo.x+1][roblo.y] = robot;
else if(maze[roblo.x][roblo.y+1] == ext)
maze[roblo.x][roblo.y+1] = robot;
return true;
}
if(maze[roblo.x][roblo.y+1] == space && roblo.y+1 < SIZE && move < 25)
{
maze[roblo.x][roblo.y] = 5;
roblo.y++;
ok = 1;
}
else if(maze[roblo.x+1][roblo.y] == space && roblo.x+1 < SIZE)
{
maze[roblo.x][roblo.y] = 5;
roblo.x++;
ok = 1;
}
else if(maze[roblo.x-1][roblo.y] == space && roblo.x-1 >=0 )
{
maze[roblo.x][roblo.y] = 5;
roblo.x--;
ok = 1;
}
else if(maze[roblo.x][roblo.y-1] == space && roblo.y-1 >=0)
{
maze[roblo.x][roblo.y] = 5;
roblo.y--;
ok = 1;
}
if(!ok)
{
if(maze[roblo.x+1][roblo.y] == 5 && roblo.x+1 < SIZE)
{
maze[roblo.x+1][roblo.y] = space;
}
else if(maze[roblo.x][roblo.y+1] == 5 && roblo.y+1 < SIZE)
{
maze[roblo.x][roblo.y+1] = space;
}
else if(maze[roblo.x-1][roblo.y] == 5 && roblo.x-1 >= 0)
{
maze[roblo.x-1][roblo.y] = space;
}
if(maze[roblo.x][roblo.y-1] == 5 && roblo.y-1 >= 0)
{
maze[roblo.x][roblo.y-1] = space;
}
}
maze[roblo.x][roblo.y] = robot;
return false;
}
|