Advertisement
monoteen

Maze_Tast

Nov 19th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. #include <conio.h>
  5.  
  6. #define MAZE_BOARD_HEIGHT 10
  7. #define MAZE_BOARD_WIDTH 6
  8. #define POINT_X 4  //보드 시작좌표 x
  9. #define POINT_Y 2  //보드 시작좌표 y
  10.  
  11. #define LEFT 75
  12. #define RIGHT 77
  13. #define UP 72
  14. #define DOWN 80
  15.  
  16. #define DELAY 100
  17. #define EXIT 50
  18.  
  19.  
  20. int currunt_stage = 1;
  21.  
  22. int maze[MAZE_BOARD_HEIGHT][MAZE_BOARD_WIDTH]={
  23.     {'1','1','1','1','1','1'},
  24.     {'e','0','0','0','0','1'},
  25.     {'1','0','1','1','1','1'},
  26.     {'1','0','0','0','1','1'},
  27.     {'1','0','1','0','0','1'},
  28.     {'1','0','0','1','0','1'},
  29.     {'1','1','1','0','0','1'},
  30.     {'1','0','1','0','1','1'},
  31.     {'1','0','0','0','0','x'},
  32.     {'1','1','1','1','1','1'}
  33. };
  34.  
  35. int maze2[MAZE_BOARD_HEIGHT][MAZE_BOARD_WIDTH]={
  36.     {'1','1','1','1','1','1'},
  37.     {'e','0','0','1','1','1'},
  38.     {'1','1','0','1','1','1'},
  39.     {'1','1','0','1','1','1'},
  40.     {'1','1','0','0','1','1'},
  41.     {'1','1','0','0','1','1'},
  42.     {'1','1','1','0','1','1'},
  43.     {'1','1','1','0','1','1'},
  44.     {'1','1','1','0','0','x'},
  45.     {'1','1','1','1','1','1'}
  46. };
  47.  
  48. int maze3[MAZE_BOARD_HEIGHT][MAZE_BOARD_WIDTH]={
  49.     {'1','1','1','1','1','1'},
  50.     {'e','0','0','0','0','1'},
  51.     {'1','1','1','1','0','1'},
  52.     {'1','0','0','0','0','1'},
  53.     {'1','0','1','1','1','1'},
  54.     {'1','0','0','0','0','1'},
  55.     {'1','1','1','1','0','1'},
  56.     {'1','0','0','0','0','1'},
  57.     {'1','0','1','1','1','1'},
  58.     {'1','x','1','1','1','1'}
  59. };
  60. void setCursor(int x,int y)
  61. {
  62.     COORD pos;
  63.     pos.X=x;
  64.     pos.Y=y;
  65.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
  66. }
  67.  
  68. COORD getCursor(void)
  69. {
  70.     COORD curPoint;
  71.     CONSOLE_SCREEN_BUFFER_INFO pos;
  72.     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&pos);
  73.     curPoint.X=pos.dwCursorPosition.X;
  74.     curPoint.Y=pos.dwCursorPosition.Y;
  75.     return curPoint;
  76. }
  77.  
  78. void removeCursor(void)
  79. {
  80.     CONSOLE_CURSOR_INFO cur;
  81.     GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cur);
  82.     cur.bVisible=0;
  83.     SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cur);
  84. }
  85.  
  86. void showBoard(void)
  87. {
  88.     int x,y;
  89.     COORD cur=getCursor();
  90.  
  91.     for(y=0; y<MAZE_BOARD_HEIGHT; y++)
  92.     {
  93.         for(x=0; x<MAZE_BOARD_WIDTH; x++)
  94.         {
  95.             setCursor(cur.X+(x*2), cur.Y+y);
  96.             switch(currunt_stage)
  97.             {
  98.             case 1:
  99.                 if(maze[y][x]=='1')
  100.                     printf("■");
  101.                 if(maze[y][x]=='x')
  102.                     printf("→");
  103.                 break;
  104.             case 2:
  105.                 if(maze2[y][x]=='1')
  106.                     printf("■");
  107.                 if(maze2[y][x]=='x')
  108.                     printf("→");
  109.                 break;
  110.             case 3:
  111.                 if(maze3[y][x]=='1')
  112.                     printf("■");
  113.                 if(maze3[y][x]=='x')
  114.                     printf("↓");
  115.                 break;
  116.             default:
  117.                 break;
  118.             }
  119.         }
  120.     }
  121.     setCursor(cur.X,cur.Y);
  122. }
  123. void showCharacter(void)
  124. {
  125.     COORD cur=getCursor();
  126.     printf("◎");
  127.    
  128.     setCursor(cur.X, cur.Y);
  129.  
  130. }
  131. int detect(int x,int y)
  132. {
  133.     int x1=0;
  134.     int y1=0;
  135.  
  136.     // 커서 위치 얻기
  137.     COORD cur=getCursor();
  138.  
  139.     // 미로내에서의 위치 계산.
  140.     x1=cur.X+x;
  141.     y1=cur.Y+y;
  142.     x1=x1/2-2;
  143.     y1=y1-2;
  144.  
  145.     // 미로 밖에 있느냐?
  146.     if(!((x1 >=0 && x1 <MAZE_BOARD_WIDTH) && (y1 >=0 && y1 <MAZE_BOARD_HEIGHT)))
  147.     {
  148.         return 1;
  149.     }
  150.     switch(currunt_stage)
  151.     {
  152.     case 1:
  153.         //배열을 넘어가지 않는이유?
  154.         if(maze[y1][x1]=='1') //++
  155.             return 1;
  156.         //미션성공
  157.         else if(maze[y1][x1]=='x')
  158.             return EXIT;
  159.         else
  160.             return 0;
  161.         break;
  162.     case 2:
  163.         if(maze2[y1][x1]=='1') //++
  164.             return 1;
  165.         //미션성공
  166.         else if(maze2[y1][x1]=='x')
  167.             return EXIT;
  168.         else
  169.             return 0;
  170.         break;
  171.     case 3:
  172.         if(maze3[y1][x1]=='1') //++
  173.             return 1;
  174.         //미션성공
  175.         else if(maze3[y1][x1]=='x')
  176.             return EXIT;
  177.         else
  178.             return 0;
  179.         break;
  180.     default:
  181.         break;
  182.     }
  183.     /*
  184.     //배열을 넘어가지 않는이유?
  185.     if(maze2[y1][x1]=='1')
  186.         return 1;
  187.     //미션성공
  188.     else if(maze2[y1][x1]=='x')
  189.         return EXIT;
  190.     else
  191.         return 0;
  192.         */
  193. }
  194. int RemoveCharacter_Set(int x, int y)
  195. {
  196.     int value=detect(x,y);
  197.    
  198.     if(value==0)
  199.     {
  200.         COORD cur=getCursor();
  201.        
  202.         printf("  ");
  203.         setCursor(cur.X+x, cur.Y+y);   
  204.     }
  205.     else if(value==EXIT)
  206.     {
  207.         setCursor(10,15);
  208.         printf("ENTER를 두번 누르세요!\n\n\n\n");
  209.         system("pause");
  210.  
  211.         currunt_stage += 1;
  212.         return 1;
  213.         //exit(1);
  214.     }
  215.  
  216.     return 0;
  217. }
  218.  
  219.  
  220. void character_static(void)
  221. {
  222.     int kb;
  223.     int ret = 0;
  224.     setCursor(4,3);  //케릭터 시작위치
  225.     while(1)
  226.     {
  227.         while(!_kbhit())
  228.         {
  229.             showCharacter();
  230.             Sleep(DELAY);
  231.         }
  232.         kb=_getch();
  233.         switch(kb)
  234.         {
  235.             case UP:
  236.                 ret = RemoveCharacter_Set(0,-1);
  237.                 break;
  238.             case DOWN:
  239.                 ret = RemoveCharacter_Set(0,1);
  240.                 break;
  241.             case RIGHT:
  242.                 ret = RemoveCharacter_Set(2,0);
  243.                 break;
  244.             case LEFT:
  245.                 ret = RemoveCharacter_Set(-2,0);
  246.                 break;
  247.         }
  248.        
  249.         if(ret) break;
  250.     }
  251. }
  252.            
  253.        
  254.        
  255. int main()
  256. {
  257.     while(1)
  258.     {
  259.         system("cls");
  260.         printf("Test\n");
  261.         removeCursor(); //커서 깜박이 지우기
  262.         setCursor(POINT_X, POINT_Y); //보드 시작좌표
  263.         showBoard(); //미로판 보여주기
  264.    
  265.         character_static(); //케릭터 움직이기
  266.         getchar();
  267.     }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement