Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.01 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.  
  69.  
  70. int longestWay(int steps[]){
  71.      int i,max1,max2,ind_max1,ind_max2;
  72.  
  73.     /*
  74.         Setto max1 e max2 considerando i primi due
  75.         elementi dell'array
  76.     */
  77.  
  78.     if (steps[UP] >= steps[RIGHT]){
  79.         max1 = steps[UP];
  80.         max2 = steps[RIGHT];
  81.         ind_max1 = UP;
  82.         ind_max2 = RIGHT;
  83.     }
  84.  
  85.     else{
  86.         max1 = steps[RIGHT];
  87.         max2 = steps[UP];
  88.         ind_max1 = RIGHT;
  89.         ind_max2 = UP;
  90.     }
  91.  
  92.  
  93.     for(i=3;i<5;i++){
  94.  
  95.         /*Se trovo un numero maggiore di max1*/
  96.         if (steps[i] > max1){
  97.             max2 = max1;
  98.             ind_max2 = ind_max1;
  99.             max1 = steps[i];
  100.             ind_max1 = i;
  101.         }
  102.  
  103.         /*Se trovo un numero maggio di max2 ma minore di max1*/
  104.         else if(steps[i] > max2){
  105.             max2 = steps[i];
  106.             ind_max2 = i;
  107.         }
  108.     }
  109.  
  110.     if(max1 != max2){
  111.         return ind_max1;
  112.     }
  113.  
  114.     if(rand()%2 == 0)
  115.         return ind_max1;
  116.     else
  117.         return ind_max2;
  118. }
  119.  
  120.  
  121. void randomMove(){
  122.     int direction;
  123.     int next_step;
  124.     int flag;
  125.  
  126.     do{
  127.         flag = 0;
  128.  
  129.         direction = rand()%4+1;
  130.  
  131.         printf("%d\n",direction);
  132.  
  133.         switch(direction){
  134.             case UP:
  135.                 if(robot.y == 1 && LEVEL[0][robot.x] != VICTORY)
  136.                     break;
  137.  
  138.                 next_step = LEVEL[robot.y-1][robot.x];
  139.                 flag = 1;
  140.  
  141.                 if(next_step == WALL || robot.last_direction == DOWN)
  142.                     break;
  143.                 else{
  144.                     setPosition(robot.x,robot.y-1);
  145.                     robot.last_direction = direction;
  146.                 }
  147.             break;
  148.  
  149.             case RIGHT:
  150.                 if(robot.x == DIM-1 && LEVEL[robot.y][DIM] != VICTORY)
  151.                     break;
  152.  
  153.                 next_step = LEVEL[robot.y][robot.x+1];
  154.                 flag = 1;
  155.  
  156.                 if(next_step == WALL || robot.last_direction == LEFT)
  157.                     break;
  158.                 else{
  159.                     setPosition(robot.x+1,robot.y);
  160.                     robot.last_direction = direction;
  161.                 }
  162.             break;
  163.  
  164.             case DOWN:
  165.                 if(robot.y == DIM-1 && LEVEL[DIM][robot.x] != VICTORY)
  166.                     break;
  167.  
  168.                 next_step = LEVEL[robot.y+1][robot.x];
  169.                 flag = 1;
  170.  
  171.                 if(next_step == WALL || robot.last_direction == UP)
  172.                     break;
  173.                 else{
  174.                     setPosition(robot.x,robot.y+1);
  175.                     robot.last_direction = direction;
  176.                 }
  177.             break;
  178.  
  179.             case LEFT:
  180.                 if(robot.x == 1 && LEVEL[robot.y][0] != VICTORY)
  181.                     break;
  182.  
  183.                 next_step = LEVEL[robot.y][robot.x-1];
  184.                 flag = 1;
  185.  
  186.                 if(next_step == WALL || robot.last_direction == RIGHT)
  187.                     break;
  188.                 else{
  189.                     setPosition(robot.x-1,robot.y);
  190.                     robot.last_direction = direction;
  191.                 }
  192.             break;
  193.         }
  194.     }while(next_step == WALL || flag==0);
  195. }
  196.  
  197. void longMove(){
  198.  
  199.     int x,y,direction;
  200.  
  201.     int step[5] = {0};
  202.  
  203.  
  204.  
  205.     //UP
  206.     x = robot.x;
  207.     y = robot.y;
  208.  
  209.  
  210.     if(robot.last_direction == DOWN)
  211.         step[UP] = -1;
  212.  
  213.     else{
  214.         while(LEVEL[y][x] != WALL){
  215.             step[UP]++;
  216.             y--;
  217.         }
  218.     }
  219.  
  220.  
  221.     //RIGHT
  222.     x = robot.x;
  223.     y = robot.y;
  224.  
  225.     if(robot.last_direction == LEFT)
  226.         step[RIGHT] = -1;
  227.  
  228.     else{
  229.         while(LEVEL[y][x] != WALL){
  230.             step[RIGHT]++;
  231.             x++;
  232.         }
  233.     }
  234.  
  235.     //DOWN
  236.     x = robot.x;
  237.     y = robot.y;
  238.  
  239.     if(robot.last_direction == UP)
  240.         step[DOWN] = -1;
  241.  
  242.     else{
  243.         while(LEVEL[y][x] != WALL){
  244.             step[DOWN]++;
  245.             y++;
  246.         }
  247.     }
  248.  
  249.     //LEFT
  250.     x = robot.x;
  251.     y = robot.y;
  252.  
  253.     if(robot.last_direction == RIGHT)
  254.         step[LEFT] = -1;
  255.  
  256.     else{
  257.         while(LEVEL[y][x] != WALL){
  258.             step[LEFT]++;
  259.             x--;
  260.         }
  261.     }
  262.  
  263.    
  264.  
  265.     direction = longestWay(step);
  266.     switch(direction){
  267.         case UP:
  268.             setPosition(robot.x,robot.y-1);
  269.         break;
  270.  
  271.         case RIGHT:
  272.             setPosition(robot.x+1,robot.y);
  273.         break;
  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.         //sleep(1);
  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