Guest User

A maze!

a guest
Oct 7th, 2010
3,672
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // http://feliam.wordpress.com/2010/10/07/the-symbolic-maze/ ‎
  2. // twitter.com/feliam
  3. /*
  4.  * It's a maze!
  5.  * Use a,s,d,w to move "through" it.
  6.  */
  7.  
  8. #include<string.h>
  9. #include<stdio.h>
  10. #include<stdlib.h>
  11.  
  12.  
  13. /**
  14.   * Maze hardcoded dimensions
  15.   */
  16. #define H 7
  17. #define W 11
  18. /**
  19.   * Tha maze map
  20.   */
  21. char maze[H][W] = { "+-+---+---+",
  22.                     "| |     |#|",
  23.                     "| | --+ | |",
  24.                     "| |   | | |",
  25.                     "| +-- | | |",
  26.                     "|     |   |",
  27.                     "+-----+---+" };
  28.  
  29. /**
  30.   * Draw the maze state in the screen!
  31.   */
  32. void draw ()
  33. {
  34.     int i, j;
  35.     for (i = 0; i < H; i++)
  36.       {
  37.           for (j = 0; j < W; j++)
  38.                   printf ("%c", maze[i][j]);
  39.           printf ("\n");
  40.       }
  41.     printf ("\n");
  42. }
  43.  
  44.  
  45. /**
  46.   * The main function
  47.   */
  48. int
  49. main (int argc, char *argv[])
  50. {
  51.     int x, y;     //Player position
  52.     int ox, oy;   //Old player position
  53.     int i = 0;    //Iteration number
  54.     #define ITERS 28
  55.     char program[ITERS];
  56.  
  57. //Initial position
  58.     x = 1;
  59.     y = 1;
  60.     maze[y][x]='X';
  61.  
  62. //Print some info
  63.     printf ("Maze dimensions: %dx%d\n", W, H);
  64.     printf ("Player pos: %dx%d\n", x, y);
  65.     printf ("Iteration no. %d\n",i);
  66.     printf ("Program the player moves with a sequence of 'w', 's', 'a' and 'd'\n");
  67.     printf ("Try to reach the price(#)!\n");
  68.  
  69. //Draw the maze
  70.     draw ();    
  71. //Read the directions 'program' to execute...
  72.     read(0,program,ITERS);
  73.  
  74. //Iterate and run 'program'
  75.     while(i < ITERS)
  76.       {
  77.           //Save old player position
  78.           ox = x;
  79.           oy = y;
  80.           //Move polayer position depending on the actual command
  81.           switch (program[i])
  82.             {
  83.             case 'w':
  84.                 y--;
  85.                 break;
  86.             case 's':
  87.                 y++;
  88.                 break;
  89.             case 'a':
  90.                 x--;
  91.                 break;
  92.             case 'd':
  93.                 x++;
  94.                 break;
  95.             default:
  96.                 printf("Wrong command!(only w,s,a,d accepted!)\n");
  97.                 printf("You loose!\n");
  98.                 exit(-1);
  99.             }
  100.  
  101.           //If hit the price, You Win!!        
  102.           if (maze[y][x] == '#')
  103.             {
  104.                 printf ("You win!\n");
  105.                 printf ("Your solution <%42s>\n",program);
  106.                 exit (1);
  107.             }
  108.           //If something is wrong do not advance
  109.           if (maze[y][x] != ' '
  110.               &&
  111.               !((y == 2 && maze[y][x] == '|' && x > 0 && x < W)))
  112.             {
  113.                 x = ox;
  114.                 y = oy;
  115.             }
  116.          
  117.           //Print new maze state and info...
  118.           printf ("Player pos: %dx%d\n", x, y);
  119.           printf ("Iteration no. %d. Action: %c. %s\n",i,program[i], ((ox==x && oy==y)?"Blocked!":""));
  120.          
  121.           //If crashed to a wall! Exit, you loose
  122.           if (ox==x && oy==y){
  123.                 printf("You loose\n");
  124.                 exit(-2);
  125.           }
  126.           //put the player on the maze...
  127.           maze[y][x]='X';
  128.           //draw it
  129.           draw ();
  130.           //increment iteration
  131.           i++;
  132.           //me wait to human
  133.           sleep(1);
  134.       }
  135. //You couldn't make it! You loose!   
  136. printf("You loose\n");
  137. }
RAW Paste Data