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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
#include <stack>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
const char *playerMarker = "S"; //What player is marked as on board
const char *wallMarker = "X"; //What the walls are marked as on board
const char *endMarker = "E"; //What the end is marked as on the board
//Vector for player moves
vector<pair<int, int>> playerMovesVect;
// pair and stack for player location
std::pair <int, int> pos;
std::stack<std::pair <int, int> > playerLocation;
//pair and stack for end location
std::pair <int, int> endPos;
std::stack<std::pair <int, int> > EndLocation;
const int SIZE = (15 * 15);
//Variables to hold pair numbers
int pair1;
int pair2;
//Row * Coloumn
char arrMaze [SIZE][SIZE] =
{
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
/*0*/ {'X', 'X', 'X', 'X', 'X', 'X','X', 'X', 'X','X', 'X', 'X','X', 'X', 'X'},
/*1*/ {'X', 'S', 'X', 'X', 'X', 'X','X', 'X', 'X',' ', 'X', 'X','X', 'X', 'X'},
/*2*/ {'X', ' ', ' ', ' ', ' ', 'X','X', 'X', 'X',' ', 'X', ' ','X', 'X', 'X'},
/*3*/ {'X', 'X', ' ', 'X', 'X', 'X','X', 'X', 'X',' ', 'X', ' ','X', 'X', 'X'},
/*4*/ {'X', ' ', ' ', 'X', 'X', 'X','X', 'X', ' ',' ', ' ', ' ','X', 'X', 'X'},
/*5*/ {'X', ' ', 'X', 'X', 'X', 'X','X', ' ', ' ','X', 'X', 'X','X', 'X', 'X'},
/*6*/ {'X', ' ', 'X', ' ', 'X', 'X','X', ' ', ' ',' ', 'X', 'X','X', 'X', 'X'},
/*7*/ {'X', ' ', 'X', ' ', 'X', 'X','X', ' ', 'X',' ', 'X', 'X','X', 'X', 'X'},
/*8*/ {'X', ' ', ' ', ' ', 'X', 'X','X', ' ', 'X',' ', 'X', ' ',' ', ' ', 'X'},
/*9*/ {'X', 'X', ' ', ' ', 'X', 'X','X', ' ', 'X',' ', 'X', ' ','X', ' ', 'X'},
/*0*/ {'X', 'X', ' ', ' ', ' ', ' ',' ', ' ', 'X',' ', 'X', ' ','X', ' ', 'X'},
/*1*/ {'X', 'X', ' ', 'X', 'X', 'X','X', 'X', 'X',' ', 'X', ' ','X', ' ', 'X'},
/*2*/ {'X', 'X', ' ', 'X', 'X', 'X','X', 'X', 'X',' ', ' ', ' ','X', ' ', 'X'},
/*3*/ {'X', 'X', ' ', 'X', 'X', 'X','X', 'X', 'X','X', 'X', ' ','X', 'E', 'X'},
/*4*/ {'X', 'X', 'X', 'X', 'X', 'X','X', 'X', 'X','X', 'X', 'X','X', 'X', 'X'}};
for(int i = 0; i < SIZE; i++)
{
for(int j = 0; j < SIZE; j++)
{
if (arrMaze[i][j] == *playerMarker)
{
pos.first = i;
pos.second = j;
}
if (arrMaze[i][j] == *endMarker)
{
endPos.first = i;
endPos.second = j;
}
else
{
i++;
j++;
}
}
}
//Place end location into stack
//EndLocation.push(endPos);
//Place player location into stack
//playerLocation.push(pos);
//Check to see if N, W, S, E are open
pair1 = pos.first;
pair2 = pos.second;
while(playerLocation != EndLocation)
{
//Check North (- [up] coloumn from Location)
if(arrMaze[pair1][pair2 - 1] != *wallMarker)
{
//Check to see if move has been made
for(int i = 0; i < playerMovesVect.size(); i++)
{
if (playerLocation.top() == playerMovesVect[i])
{
//Remove Direction from stack
playerLocation.pop();
//Make last pair in stack variable
std::pair <int, int> currentLocation = playerLocation.top();
//Make last stack move current location again
arrMaze[currentLocation.first][currentLocation.second];
break;
}
}
do
{
arrMaze[pair1][pair2--];
if (arrMaze[pair1][pair2] == *wallMarker)
{
pair1 = pos.first;
pair2 = pos.second;
playerLocation.push(pos);
playerMovesVect.push_back(pos);
arrMaze[pair1][pair2] = *playerMarker;
}
}
while(arrMaze[pair1][pair2] != *wallMarker);
}
//Check West (- [left] Row from Location)
if(arrMaze[pair1 - 1][pair2] != *wallMarker)
{
for(int i = 0; i < playerMovesVect.size(); i++)
{
if (playerLocation.top() == playerMovesVect[i])
{
//Remove Direction from stack
playerLocation.pop();
//Make last pair in stack variable
std::pair <int, int> currentLocation = playerLocation.top();
//Make last stack move current location again
arrMaze[currentLocation.first][currentLocation.second];
break;
}
}
do
{
arrMaze[pair1--][pair2];
if (arrMaze[pair1][pair2] == *wallMarker)
{
pair1 = pos.first;
pair2 = pos.second;
playerLocation.push(pos);
playerMovesVect.push_back(pos);
arrMaze[pair1][pair2] = *playerMarker;
}
}
while(arrMaze[pair1][pair2] != *wallMarker);
}
//Check South (+ Coloumn [down] from Location)
if(arrMaze[pair1][pair2 + 1] != *wallMarker)
{
for(int i = 0; i < playerMovesVect.size(); i++)
{
if (playerLocation.top() == playerMovesVect[i])
{
//Remove Direction from stack
playerLocation.pop();
//Make last pair in stack variable
std::pair <int, int> currentLocation = playerLocation.top();
//Make last stack move current location again
arrMaze[currentLocation.first][currentLocation.second];
break;
}
}
do
{
arrMaze[pair1][pair2++];
if (arrMaze[pair1][pair2] == *wallMarker)
{
pair1 = pos.first;
pair2 = pos.second;
playerLocation.push(pos);
playerMovesVect.push_back(pos);
arrMaze[pair1][pair2] = *playerMarker;
}
}
while(arrMaze[pair1][pair2] != *wallMarker);
}
//Check East (+ [right] Row from Location)
if(arrMaze[pair1 + 1][pair2] != *wallMarker)
{
for(int i = 0; i < playerMovesVect.size(); i++)
{
if (playerLocation.top() == playerMovesVect[i])
{
//Remove Direction from stack
playerLocation.pop();
//Make last pair in stack variable
std::pair <int, int> currentLocation = playerLocation.top();
//Make last stack move current location again
arrMaze[currentLocation.first][currentLocation.second];
break;
}
}
do
{
arrMaze[pair1++][pair2];
if (arrMaze[pair1][pair2] == *wallMarker)
{
pair1 = pos.first;
pair2 = pos.second;
playerLocation.push(pos);
playerMovesVect.push_back(pos);
arrMaze[pair1][pair2] = *playerMarker;
}
}
while(arrMaze[pair1][pair2] != *wallMarker);
}
}
//Print board
for(int i=0; i<15; i++) //This loops on the rows.
{
for(int j=0; j<15; j++) //This loops on the columns
{
cout << arrMaze[i][j] << " ";
}
cout << endl;
}
return 0;
}
|