Advertisement
uopspop

maze

May 30th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <windows.h>
  5. #define ROW 7
  6. #define COL 15
  7. using namespace std;
  8.  
  9. // 堆疊結構的宣告
  10. class stack_node
  11. {
  12.     friend class Maze;
  13.     public:
  14.         stack_node(int x_, int y_);
  15.     private:
  16.         int x;
  17.         int y;
  18.         stack_node *next;
  19. };
  20. typedef class stack_node node;
  21.  
  22. class Maze{
  23.     public:
  24.  
  25.         Maze(int mazeCopy[][COL]);
  26.         void print();
  27.         void push(int x, int y);
  28.         void pop(int& x, int& y);
  29.         void clearStack(); // not Done yet
  30.  
  31.         bool goLeft(int x, int y);
  32.         bool goRight(int x, int y);
  33.         bool goUp(int x, int y);
  34.         bool goDown(int x, int y);
  35.         void walk(int x, int y, int xEnd, int yEnd);
  36.     private:
  37.         int maze[ROW][COL];
  38.         node* head;
  39.         enum Status{PASSED = 2,DEADLOCK = 3, CLEAR = 0, BLOCK = 1};;
  40.  
  41. };
  42.  
  43.  
  44. // 主程式
  45. int main()
  46. {
  47.  
  48.     // 輸入
  49.     int mazeCopy[ROW][COL] = //
  50.     {
  51.         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  52.         1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1,
  53.         1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1,
  54.         1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1,
  55.         1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1,
  56.         1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
  57.         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  58.     };
  59.  
  60.     Maze myMaze(mazeCopy);
  61.  
  62.     myMaze.walk(13,5,1,1);// (xStart, yStart, xEnd, yEnd)
  63.     myMaze.print(); cout << endl;
  64.  
  65.     return 0;
  66. }
  67.  
  68. stack_node::stack_node(int x_, int y_):x(0),y(0),next(NULL){
  69.     x = x_;
  70.     y = y_;
  71. }
  72.  
  73. Maze::Maze(int mazeCopy[][COL]){
  74.     for (int i = 0; i < ROW; i++){
  75.         for (int j = 0; j < COL; j++){
  76.             maze[i][j] = mazeCopy[i][j];
  77.         }
  78.     }
  79. }
  80.  
  81. void Maze::print(){
  82.     for (int i = 0; i < ROW; i++){
  83.         for (int j = 0; j < COL; j++){
  84.             cout << maze[i][j] << " ";
  85.         }
  86.         cout << endl;
  87.     }
  88. }
  89.  
  90. void Maze::push(int x, int y){
  91.     node* new_node = new node(x,y);
  92.         if (!new_node) exit(0); // 記憶體配置失敗!
  93.     new_node->next = head;
  94.     head = new_node;
  95. }
  96.  
  97. void Maze::pop(int& x, int& y){
  98.     node *top;
  99.  
  100.     if (head != NULL){
  101.         top = head;
  102.         head = head->next;
  103.         x = top->x;
  104.         y = top->y;
  105.         delete top;
  106.     }else{
  107.         x = -1;
  108.         y = -1;
  109.     }
  110. }
  111.  
  112. bool Maze::goLeft(int x, int y){
  113.     return maze[y][x-1] <= 0;
  114. }
  115. bool Maze::goRight(int x, int y){
  116.     return maze[y][x+1] <= 0;
  117. }
  118. bool Maze::goUp(int x, int y){
  119.     return maze[y-1][x] <= 0; // 注意:往上是y-1
  120. }
  121. bool Maze::goDown(int x, int y){
  122.     return maze[y+1][x] <= 0; // 注意:往下是y+1
  123. }
  124.  
  125.  
  126.  
  127. // 重點:之後還有有路走時,才會把現在的位置放入堆疊中!
  128. void Maze::walk(int x, int y, int xEnd, int yEnd)
  129. {
  130.     while(1)
  131.     {
  132.         if(x == xEnd && y == yEnd) // 是否為出口
  133.         {
  134.             push(x, y);
  135.             maze[y][x] = PASSED; // 標示最後一點
  136.             break;
  137.         }
  138.         maze[y][x] = PASSED; // 標示為走過的路
  139.         if( goUp(x,y) ) // 往上方走
  140.             push(x, y--);
  141.         else if( goDown(x,y) ) // 往下方走
  142.             push(x, y++);
  143.         else if( goLeft(x,y)) // 往左方走
  144.             push(x--, y);
  145.         else if( goRight(x,y) ) // 往右方走
  146.             push(x++, y);
  147.         else // 無路可走 => 回朔
  148.         {
  149.             maze[y][x] = DEADLOCK;
  150.             pop(x, y); // 退回一步
  151.         }
  152.         print(); cout << endl;
  153.         //system("pause");
  154.         Sleep(1000);
  155.         system("cls");
  156.     }// end while
  157. }
  158.  
  159. void Maze::clearStack(){
  160. node* ptr = head;
  161. while (!head){
  162.     ptr = head;
  163.     head = head->next;
  164.     cout << "[" << ptr->x << "," << ptr->y << "]" << endl;
  165.     delete ptr;
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement