Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. void main()
  2. {
  3.  
  4.    
  5.    
  6.     char maze[80][80];
  7.     int row, col, rIndex, cIndex;
  8.     int MazeCount = 0;
  9.     bool EndProgram = false;
  10.     ifstream fin ("maze.dat");
  11.     ofstream fout ("maze.out");
  12.  
  13.     Coordinate CurrentPosition;
  14.  
  15.     Stack MoveStack;
  16.  
  17.     fout << "Solutions to A-MAZE-ING Stacks Assignment" << endl;
  18.     fout << "Name: Cody Chambers" << endl << endl;
  19.  
  20.     while(!EndProgram)
  21.     {
  22.     //read dimensions
  23.     fin >> row >> col;
  24.  
  25.     if(row == -1 && col == -1)
  26.     {
  27.         EndProgram = true;
  28.         fout << "End of program!!" << endl;
  29.         exit(0);
  30.     }
  31.     //read maze into twoD array
  32.     for (rIndex = 0; rIndex < row; rIndex++)
  33.         for (cIndex = 0; cIndex < col; cIndex++)
  34.             fin >> maze[rIndex][cIndex];
  35.    
  36.    
  37.  
  38.     fin >> CurrentPosition.Row >> CurrentPosition.Column;
  39.    
  40.    
  41.  
  42.     fin.close();
  43.  
  44.     while (maze[CurrentPosition.Row][CurrentPosition.Column] == '0')
  45.         {
  46.         //create west and north coordinates
  47.         Coordinate West, North, South, East;
  48.    
  49.         West.Row = CurrentPosition.Row;
  50.         West.Column = CurrentPosition.Column - 1;
  51.  
  52.         North.Row = CurrentPosition.Row - 1;
  53.         North.Column = CurrentPosition.Column;
  54.  
  55.         South.Row = CurrentPosition.Row + 1;
  56.         South.Column = CurrentPosition.Column;
  57.    
  58.         East.Row = CurrentPosition.Row;
  59.         East.Column = CurrentPosition.Column + 1;
  60.  
  61.         //Is west available? If so, push it on the move stack
  62.         if (MoveStack.AvailableMove(West, maze))
  63.         {
  64.             if (!MoveStack.Push(West))
  65.             {
  66.                 cout << "Memory error" << endl;
  67.                 exit(0);
  68.             }
  69.         }
  70.  
  71.         //Is north available? If so, push it on the move stack
  72.         if (MoveStack.AvailableMove(North,maze))
  73.         {
  74.             if (!MoveStack.Push(North))
  75.             {
  76.                 cout << "Memory error" << endl;
  77.                 exit(0);
  78.             }
  79.         }
  80.  
  81.         if (MoveStack.AvailableMove(South,maze))
  82.         {
  83.             if (!MoveStack.Push(South))
  84.             {
  85.                 cout << "Memory error" << endl;
  86.                 exit(0);
  87.             }
  88.         }
  89.  
  90.         if (MoveStack.AvailableMove(East,maze))
  91.         {
  92.             if (!MoveStack.Push(East))
  93.             {
  94.                 cout << "Memory error" << endl;
  95.                 exit(0);
  96.             }
  97.         }
  98.  
  99.         maze[CurrentPosition.Row][CurrentPosition.Column] = '.';
  100.  
  101.         if (!MoveStack.Pop(CurrentPosition))
  102.         {
  103.             cout << "Stack Empty" << endl;
  104.  
  105.             fout << "MAZE NUMBER:" << MazeCount << endl << endl;
  106.             for (rIndex = 0; rIndex < row; rIndex++)
  107.             {
  108.                 for (cIndex = 0; cIndex < col; cIndex++)
  109.                 {
  110.                     fout << maze[rIndex][cIndex];
  111.                 }
  112.                 fout << endl;
  113.             }
  114.  
  115.             fout << "There IS NOT a way out!!" << endl;
  116.             break;
  117.         }
  118.  
  119.     }
  120.  
  121.  
  122.         if (maze[CurrentPosition.Row][CurrentPosition.Column] == 'E')
  123.             {
  124.                 for (rIndex = 0; rIndex < row; rIndex++)
  125.                 {
  126.                     for (cIndex = 0; cIndex < col; cIndex++)
  127.                         fout << maze[rIndex][cIndex];
  128.                     fout << endl;
  129.                 }
  130.                 fout << "There IS a way out!" << endl;
  131.  
  132.             }
  133.         else
  134.             {
  135.                 for (rIndex = 0; rIndex < row; rIndex++)
  136.                 {
  137.                     for (cIndex = 0; cIndex < col; cIndex++)
  138.                         fout << maze[rIndex][cIndex];
  139.                     fout << endl;
  140.                 }
  141.                 fout << "There IS NOT a way out!" << endl;
  142.             }
  143.  
  144.        
  145.     fout.close();
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement