Maze.docx

#include <iostream> #include <Windows.h> #include <conio.h> #include <stdlib.h> #include <fstream> #include <iomanip>

using namespace std;

//global variables char inputKey; const int a = 16; const int b = 33; int mapArrayOne[a][b] = {  { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },  { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },  { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,0,1 },  { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },  { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },  { 1,0,0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1 },  { 1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1 },  { 1,0,0,1,0,1,1,0,1,0,0,1,0,0,1,1,1,1,0,0,1,0,0,1,0,0,1,1,1,1,0,0,1 },  { 1,0,0,1,0,1,3,0,1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,1,0,0,1,0,0,1 },  { 1,0,0,1,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,1,1,1,1,1,1,1,0,0,1,0,0,1 },  { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 },  { 1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1 },  { 1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1 },  { 1,0,0,1,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,1,1,1 },  { 1,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1 },  { 1,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1 },  { 1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1 },  { 1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1 },  { 1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1 },  { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }  

};

void DrawMaze(); void gotoXY(int, int); void movement(); void DrawMaze2(); void movement2(); void DrawMaze3(); void movement3(); void readFile(); //void movementCustom();

void main() {  float w;  cout << "Select your level number and press enter! Or select 5 to input your own file." << endl;  cout << "1     2     3     4     5" << endl;  cin >> w;

 cin.get();    if (w == 1) {   DrawMaze();   movement();  }  if (w == 2) {   DrawMaze2();   movement2();  }  if (w == 3) {   DrawMaze3();   movement3();  }  if (w == 5) {   readFile();  }  if (w == 9) {   return;  }  if (w != 1, 2, 3, 4, 5, 9) {   system("CLS");   cout << "*Error: Invalid Level*     " << flush;   main();  }

 return; }

void DrawMaze() {  for (int counter = 0; counter < a; counter++)  {   for (int newCounter = 0; newCounter<b; newCounter++)   {    switch (mapArrayOne[counter][newCounter])    {    case 0:     cout << " ";     break;    case 1:     cout << char(219);     break;    case 2:     cout << " ";     break;    case 3:     cout << " ";     break;        }   }   cout << " " << endl;  }

}

void DrawMaze2() {  for (int counter = 0; counter < a; counter++)  {   for (int newCounter = 0; newCounter < b; newCounter++)   {    switch (mapArrayTwo[counter][newCounter])    {    case 0:     cout << " ";     break;    case 1:     cout << char(219);     break;    case 2:     cout << " ";     break;    case 3:     cout << " ";     break;    }   }   cout << " " << endl;  } }

void DrawMaze3() {  for (int counter = 0; counter < a; counter++)  {   for (int newCounter = 0; newCounter < b; newCounter++)   {    switch (mapArrayTeapot[counter][newCounter])    {    case 0:     cout << " ";     break;    case 1:     cout << char(219);     break;    case 2:     cout << " ";     break;    case 3:     cout << " ";     break;    }   }   cout << " " << endl;  } }

void DrawMaze5(int *fileArray) {                    //a lot of problems with this at the moment, but it compiles and doesn't disturb the other code  for (int i = 0; i < 20; i++) {   for (int j = 0; j < 20; j++) {    switch (*fileArray) {    case 0:     cout << " ";     break;    case 1:     cout << char(219);     break;    case 2:     cout << " ";     break;    case 3:     cout << " ";     break;        }   }   cout << " " << endl;  }

}

void gotoXY(int x, int y) {  COORD coord;  coord.X = x;  coord.Y = y + 3;  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }

void movement() {  int x = 1;  int y = 1;  int xBefore = 1;  int yBefore = 1;  while (true)  {   // 2 lines below = makes previous char disapear to simulate movement of only one char.   gotoXY(xBefore, yBefore);   cout << " ";   gotoXY(x, y);   cout << char(1);   inputKey = _getch();   xBefore = x;   yBefore = y;   if (inputKey == 'w')    y -= 1;   else if (inputKey == 's')    y += 1;   else if (inputKey == 'a')    x -= 1;   else if (inputKey == 'd')    x += 1;   // below = makes it not go through walls   if (mapArrayOne[y][x] == 1)   {    x = xBefore;    y = yBefore;   }   COORD coord;   coord.X = x;   coord.Y = y;

  if (mapArrayOne[y][x] == 3)    cout << "You found the secret teapot!" << endl;

  // Map Initilizer Code:   if (mapArrayOne[y][x] == 2) {    float a;    cout << "Great gob! Press '8' to return to the main menu." << endl;    cin >> a;    cin.get();    if (a == 8) {

    system("CLS");     cout << "Level 1 Complete!     " << flush;     main();    }    if (a == 9) {     return;    }       }  } }

void movement2() {  int x = 1;  int y = 1;  int xBefore = 1;  int yBefore = 1;  while (true)  {   // 2 lines below = makes previous char disapear to simulate movement of only one char.   gotoXY(xBefore, yBefore);   cout << " ";   gotoXY(x, y);   cout << char(1);   inputKey = _getch();   xBefore = x;   yBefore = y;   if (inputKey == 'w')    y -= 1;   else if (inputKey == 's')    y += 1;   else if (inputKey == 'a')    x -= 1;   else if (inputKey == 'd')    x += 1;   // below = makes it not go through walls   if (mapArrayTwo[y][x] == 1)   {    x = xBefore;    y = yBefore;   }   COORD coord;   coord.X = x;   coord.Y = y;

  if (mapArrayTwo[y][x] == 2) {    float a;    cout << "Great job! Press '8' to return to the main menu." << endl;    cin >> a;    cin.get();    if (a == 8) {     system("CLS");     cout << "Level 2 Complete!     " << flush;     main();    }

   if (a == 9) {     return;    }       }  }

 if (mapArrayTwo[y][x] == 3)   cout << "You found the secret teapot!" << endl; }

void movement3() {  int x = 1;  int y = 1;  int xBefore = 1;  int yBefore = 1;  while (true)  {   // 2 lines below = makes previous char disapear to simulate movement of only one char.   gotoXY(xBefore, yBefore);   cout << " ";   gotoXY(x, y);   cout << char(1);   inputKey = _getch();   xBefore = x;   yBefore = y;   if (inputKey == 'w')    y -= 1;   else if (inputKey == 's')    y += 1;   else if (inputKey == 'a')    x -= 1;   else if (inputKey == 'd')    x += 1;   // below = makes it not go through walls   if (mapArrayTeapot[y][x] == 1)   {    x = xBefore;    y = yBefore;   }   COORD coord;   coord.X = x;   coord.Y = y;

  if (mapArrayTeapot[y][x] == 3)    cout << "You found the secret teapot; within the giant teapot! Sick." << endl;

  // Map Initilizer Code:   if (mapArrayTeapot[y][x] == 2) {    float a;    cout << "Great job! Press '8' to return to the main menu." << endl;    cin >> a;    if (a == 8) {

    system("CLS");     cout << "Teapot Level Complete!     " << flush;     main();    }

   if (a == 9) {     return;    }

   cin.get();   }  } }

//Movement for custom file disabled until custom file is properly working /* void movementCustom() {  int x = 1;  int y = 1;  int xBefore = 1;  int yBefore = 1;  while (true)  {   // 2 lines below = makes previous char disapear to simulate movement of only one char.   gotoXY(xBefore, yBefore);   cout << " ";   gotoXY(x, y);   cout << char(1);   inputKey = _getch();   xBefore = x;   yBefore = y;   if (inputKey == 'w')    y -= 1;   else if (inputKey == 's')    y += 1;   else if (inputKey == 'a')    x -= 1;   else if (inputKey == 'd')    x += 1;   // below = makes it not go through walls   if (--[y][x] == 1)   {    x = xBefore;    y = yBefore;   }   COORD coord;   coord.X = x;   coord.Y = y;

  if (--[y][x] == 3)    cout << "You found the secret teapot; within the giant teapot! Sick." << endl;

  // Map Initilizer Code:   if (--[y][x] == 2) {    float a;    cout << "Would you like to play another map? Press '8' for yes, or '9' for no" << endl;    cin >> a;    if (a == 8) {

    system("CLS");     cout << "Teapot Level Complete!     " << flush;     main();    }    else if (a == 9)     return;

   cin.get();   }  } } */

void readFile(void) {  int x;  int i = 0;  int j = 0;  int n;  int fileArray[20][20];

 ifstream inFile;  inFile.open("Z:\\CGT 215\\ReadFileTest\\maze1.txt");        //need to get correct file extension before class starts!!!!

 if (!inFile) {   system("CLS");   cout << "Unable to open file maze1.txt     " << flush;   main();                                             }

 if (inFile.is_open()) {   cout << "File opened successfully" << endl;  }

 cout << "How many columns does the maze have? ";        //eventually this will be able to take user input for how large the array is, for now values are hardcoded in  cin >> n;

 while (inFile >> x) {   if (j == n) {    i++;    j = 0;    fileArray[i][j] = x;    j++;   }   else {    fileArray[i][j] = x;    j++;   }  }  inFile.close();  if (!inFile.is_open()) {   cout << "File closed sucessfully" << endl;  }  DrawMaze5(*fileArray);  movement();  return; }