Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.98 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <unistd.h>
  5.  
  6. #define DIM 16
  7. #define WALL '#'
  8. #define UP 1
  9. #define RIGHT 2
  10. #define DOWN 3
  11. #define LEFT 4
  12. #define VICTORY '@'
  13.  
  14. int victory = 0;
  15.  
  16. char LEVEL [] [16] = {
  17.                     {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
  18.                     {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  19.                     {'#', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  20.                     {'#', ' ', ' ', '#', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
  21.                     {'#', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  22.                     {'#', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  23.                     {'#', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  24.                     {'#', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  25.                     {'#', ' ', ' ', '#', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  26.                     {'#', ' ', ' ', '#', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  27.                     {'#', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', '#', '#', '#', ' ', '#'},
  28.                     {'@', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  29.                     {'@', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  30.                     {'@', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  31.                     {'#', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
  32.                     {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}
  33.                     };
  34.  
  35. typedef struct{
  36.     int x;
  37.     int y;
  38.     int last_direction;
  39. }ROBOT;
  40.  
  41. ROBOT robot;
  42.  
  43.  
  44. void stampaMatrice(){
  45.     int i,j;
  46.  
  47.     for(i=0;i<DIM;i++){
  48.         for(j=0;j<DIM;j++){
  49.             printf("%c",LEVEL[i][j]);
  50.         }
  51.         printf("\n");
  52.     }
  53. }
  54.  
  55. //Posiziona il robot
  56. void setPosition(int x,int y){
  57.  
  58.     if (LEVEL[y][x] == VICTORY)
  59.         victory = 1;
  60.  
  61.     LEVEL[robot.y][robot.x] = ' ';
  62.     robot.x = x;
  63.     robot.y = y;
  64.     LEVEL[robot.y][robot.x] = 'R';
  65. }
  66.  
  67.  
  68. int longestWay(int steps[]){
  69.      int i,max1,max2,ind_max1,ind_max2;
  70.  
  71.     /*
  72.         Setto max1 e max2 considerando i primi due
  73.         elementi dell'array
  74.     */
  75.  
  76.     if (steps[0] >= steps[1]){
  77.         max1 = steps[0];
  78.         max2 = steps[1];
  79.         ind_max1 = 0;
  80.         ind_max2 = 1;
  81.     }
  82.  
  83.     else{
  84.         max1 = steps[1];
  85.         max2 = steps[0];
  86.         ind_max1 = 1;
  87.         ind_max2 = 0;
  88.     }
  89.  
  90.  
  91.     for(i=2;i<4;i++){
  92.  
  93.         /*Se trovo un numero maggiore di max1*/
  94.         if (steps[i] > max1){
  95.             max2 = max1;
  96.             ind_max2 = ind_max1;
  97.             max1 = steps[i];
  98.             ind_max1 = i;
  99.         }
  100.  
  101.         /*Se trovo un numero maggio di max2 ma minore di max1*/
  102.         else if(steps[i] > max2){
  103.             max2 = steps[i];
  104.             ind_max2 = i;
  105.         }
  106.     }
  107.  
  108.     if(max1 != max2){
  109.         return ind_max1;
  110.     }
  111.  
  112.     if(rand()%2 == 0)
  113.         return ind_max1;
  114.     else
  115.         return ind_max2;
  116. }
  117.  
  118.  
  119. void randomMove(){
  120.     int direction;
  121.     int next_step;
  122.     int flag;
  123.  
  124.     do{
  125.         flag = 0;
  126.  
  127.         direction = rand()%4+1;
  128.  
  129.         printf("%d\n",direction);
  130.  
  131.         switch(direction){
  132.             case UP:
  133.                 if(robot.y == 1 && LEVEL[0][robot.x] != VICTORY)
  134.                     break;
  135.  
  136.                 next_step = LEVEL[robot.y-1][robot.x];
  137.                 flag = 1;
  138.  
  139.                 if(next_step == WALL || robot.last_direction == DOWN)
  140.                     break;
  141.                 else{
  142.                     setPosition(robot.x,robot.y-1);
  143.                     robot.last_direction = direction;
  144.                 }
  145.             break;
  146.  
  147.             case RIGHT:
  148.                 if(robot.x == DIM-1 && LEVEL[robot.y][DIM] != VICTORY)
  149.                     break;
  150.  
  151.                 next_step = LEVEL[robot.y][robot.x+1];
  152.                 flag = 1;
  153.  
  154.                 if(next_step == WALL || robot.last_direction == LEFT)
  155.                     break;
  156.                 else{
  157.                     setPosition(robot.x+1,robot.y);
  158.                     robot.last_direction = direction;
  159.                 }
  160.             break;
  161.  
  162.             case DOWN:
  163.                 if(robot.y == DIM-1 && LEVEL[DIM][robot.x] != VICTORY)
  164.                     break;
  165.  
  166.                 next_step = LEVEL[robot.y+1][robot.x];
  167.                 flag = 1;
  168.  
  169.                 if(next_step == WALL || robot.last_direction == UP)
  170.                     break;
  171.                 else{
  172.                     setPosition(robot.x,robot.y+1);
  173.                     robot.last_direction = direction;
  174.                 }
  175.             break;
  176.  
  177.             case LEFT:
  178.                 if(robot.x == 1 && LEVEL[robot.y][0] != VICTORY)
  179.                     break;
  180.  
  181.                 next_step = LEVEL[robot.y][robot.x-1];
  182.                 flag = 1;
  183.  
  184.                 if(next_step == WALL || robot.last_direction == RIGHT)
  185.                     break;
  186.                 else{
  187.                     setPosition(robot.x-1,robot.y);
  188.                     robot.last_direction = direction;
  189.                 }
  190.             break;
  191.         }
  192.     }while(next_step == WALL || flag==0);
  193. }
  194.  
  195. void longMove(){
  196.  
  197.     int x,y,direction;
  198.  
  199.     int step[5] = {0};
  200.  
  201.  
  202.  
  203.     //UP
  204.     x = robot.x;
  205.     y = robot.y;
  206.  
  207.  
  208.     if(robot.last_direction == DOWN)
  209.         step[UP] = -1;
  210.  
  211.     else{
  212.         while(LEVEL[y][x] != WALL){
  213.             step[UP]++;
  214.             y--;
  215.         }
  216.     }
  217.  
  218. /*
  219.     //RIGHT
  220.     x = robot.x;
  221.     y = robot.y;
  222.  
  223.     if(robot.last_direction == LEFT)
  224.         step[RIGHT] = -1;
  225.  
  226.     else{
  227.         while(LEVEL[y][x] != WALL){
  228.             step[RIGHT]++;
  229.             x++;
  230.         }
  231.     }
  232.  
  233.     //DOWN
  234.     x = robot.x;
  235.     y = robot.y;
  236.  
  237.     if(robot.last_direction == UP)
  238.         step[DOWN] = -1;
  239.  
  240.     else{
  241.         while(LEVEL[y][x] != WALL){
  242.             step[DOWN]++;
  243.             y++;
  244.         }
  245.     }
  246.  
  247.     //LEFT
  248.     x = robot.x;
  249.     y = robot.y;
  250.  
  251.     if(robot.last_direction == RIGHT)
  252.         step[LEFT] = -1;
  253.  
  254.     else{
  255.         while(LEVEL[y][x] != WALL){
  256.             step[LEFT]++;
  257.             x--;
  258.         }
  259.     }
  260.  
  261.    
  262.  
  263.     direction = longestWay(step);
  264.  
  265.     switch(direction){
  266.         case UP:
  267.             setPosition(robot.x,robot.y-1);
  268.         break;
  269.  
  270.         case RIGHT:
  271.             setPosition(robot.x+1,robot.y);
  272.         break;
  273.  
  274.  
  275.         case DOWN:
  276.             setPosition(robot.x,robot.y+1);
  277.         break;
  278.  
  279.  
  280.         case LEFT:
  281.             setPosition(robot.x-1,robot.y);
  282.         break;
  283.  
  284.     }
  285.  
  286. */
  287. }
  288.  
  289.  
  290. int main(){
  291.  
  292.     srand((unsigned int)time(0));
  293.  
  294.     //Setup
  295.     setPosition(2,1);
  296.     int l;
  297.     int cont = 0;
  298.  
  299.     stampaMatrice();
  300.  
  301.     do{
  302.  
  303.         l = rand()%100+1;
  304.  
  305.         // Legge 30%
  306.         if(l <= 30){
  307.             randomMove();
  308.         }
  309.  
  310.         // Legge 70%
  311.         else{
  312.             longMove();
  313.         }
  314.         system("clear");
  315.         stampaMatrice();
  316.        
  317.         cont ++;
  318.     }while(victory == 0);
  319.  
  320.     printf("Tot. mosse = %d\n", cont);
  321.  
  322.  
  323.    
  324.  
  325.     return 0;
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement