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
|
// Maze Traverse.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void printMaze(char maze[][12]);
void mazeTraverse(char maze[][12], int startPointY, int startPointX);
int _tmain(int argc, _TCHAR* argv[])
{
int startPointY = 2;
int startPointX = 0;
char maze[12][12] =
{ { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' },
{ '.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' },
{ '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.' },
{ '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' },
{ '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }
};
printMaze(maze);
system("Pause");
mazeTraverse(maze, startPointY, startPointX);
system("Pause");
return 0;
}
void printMaze(char maze[][12])
{
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
cout << maze[i][j];
}
cout << endl;
}
cout << endl;
}
void mazeTraverse(char maze[][12], int startPointY, int startPointX)
{
int previousPointY;
int previousPointX;
int currentPointY = startPointY;
int currentPointX = startPointX;
maze[startPointY][startPointX] = 'X';
previousPointX = startPointX;
previousPointY = startPointY;
if(currentPointX != 11 || currentPointY != 4)
{
if (maze[currentPointY + 1][currentPointX] == '.') // Looks south first, if there is space to move, moves that way, marks previous space with X and moves on.
{
maze[currentPointY][currentPointX] = 'X';
currentPointY = currentPointY + 1;
currentPointX = currentPointX;
}
if (maze[currentPointY + 1][currentPointX] != '.')
{
if (maze[currentPointY][currentPointX + 1] == '.') // Looks west next, if there is space to move, moves that way, marks previous space with X and moves on.
{
maze[currentPointY][currentPointX] = 'X';
currentPointY = currentPointY;
currentPointX = currentPointX + 1;
}
if (maze[currentPointY][currentPointX + 1] != '.')
{
if (maze[currentPointY - 1][currentPointX] == '.') // Looks north next, if there is space to move, moves that way, marks previous space with X and moves on.
{
maze[currentPointY][currentPointX] = 'X';
currentPointY = currentPointY - 1;
currentPointX = currentPointX;
}
if (maze[currentPointY - 1][currentPointX] != '.')
{
if (maze[currentPointY][currentPointX - 1] == '.') // Looks east last, if there is space to move, moves that way, marks previous space with X and moves on.
{
maze[currentPointY][currentPointX] = 'X';
currentPointY = currentPointY;
currentPointX = currentPointX - 1;
}
}
}
}
/* if (maze[currentPointY - 1][currentPointX] != '.')
{
if (maze[currentPointY + 1][currentPointX] == 'X') // Looks south first, if there is space to move, moves that way, marks previous space with X and moves on.
{
maze[currentPointY][currentPointX] = 'X';
currentPointY = currentPointY + 1;
currentPointX = currentPointX;
}
else if (maze[currentPointY][currentPointX + 1] == 'X') // Looks west next, if there is space to move, moves that way, marks previous space with X and moves on.
{
maze[currentPointY][currentPointX] = 'X';
currentPointY = currentPointY;
currentPointX = currentPointX + 1;
}
else if (maze[currentPointY - 1][currentPointX] == 'X') // Looks north next, if there is space to move, moves that way, marks previous space with X and moves on.
{
maze[currentPointY][currentPointX] = 'X';
currentPointY = currentPointY - 1;
currentPointX = currentPointX;
}
else if (maze[currentPointY][currentPointX - 1] == 'X') // Looks east last, if there is space to move, moves that way, marks previous space with X and moves on.
{
maze[currentPointY][currentPointX] = 'X';
currentPointY = currentPointY;
currentPointX = currentPointX - 1;
}
}*/
system("CLS");
printMaze(maze);
mazeTraverse(maze, currentPointY, currentPointX);
}
system("CLS");
printMaze(maze);
}
|