Advertisement
Guest User

Untitled

a guest
May 4th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4.  
  5. #define BOLA 1
  6. #define PAREDE 2
  7. #define OBJETIVO 3
  8.  
  9. void usleep(int);
  10.  
  11. typedef struct{
  12.     int tam;
  13.     char bola;
  14.     char parede;
  15.     char objetivo;
  16. } config_;
  17.  
  18. int readIntInRange(int a,int b){
  19.     int x;
  20.     while(a<=b){
  21.         scanf("%d",&x);
  22.         while(getchar()!='\n');
  23.         if(x>=a && x<=b)return x;
  24.         printf("Digite um valor valido (%d>=x>=%d)\n",a,b);
  25.     }
  26.     return 0;
  27. }
  28.  
  29. void preencheFase(int* fase,char* str){
  30.     for(int i=0;str[i];i++){
  31.         if(str[i]==' ')fase[i]=0;
  32.         if(str[i]=='o')fase[i]=BOLA;
  33.         if(str[i]=='#')fase[i]=PAREDE;
  34.         if(str[i]=='0')fase[i]=OBJETIVO;
  35.     }
  36. }
  37.  
  38. void printFase(int* fase,int dir,config_ config){
  39.     if(dir==0){//w
  40.         for(int y=0;y<(7+5*(config.tam));y++){
  41.             for(int x=0;x<(7+5*(config.tam));x++){
  42.                 if(!(x+y))printf("▓▓");
  43.                 else if(fase[x+((7+5*(config.tam))*y)]==BOLA)    printf("%c ",config.bola);
  44.                 else if(fase[x+((7+5*(config.tam))*y)]==PAREDE)  printf("%c%c",config.parede,config.parede);
  45.                 else if(fase[x+((7+5*(config.tam))*y)]==OBJETIVO)printf("%c ",config.objetivo);
  46.                 else printf("  ");
  47.                 // printf("%d", fase[x+((7+5*(config.tam))*y)]);
  48.             }
  49.             printf("\n");
  50.         }
  51.     }
  52.     if(dir==1){//a
  53.         for(int x=(7+5*(config.tam))-1;x>=0;x--){
  54.             for(int y=0;y<(7+5*(config.tam));y++){
  55.                 if(!(x+y))printf("▓▓");
  56.                 else if(fase[x+((7+5*(config.tam))*y)]==BOLA)    printf("%c ",config.bola);
  57.                 else if(fase[x+((7+5*(config.tam))*y)]==PAREDE)  printf("%c%c",config.parede,config.parede);
  58.                 else if(fase[x+((7+5*(config.tam))*y)]==OBJETIVO)printf("%c ",config.objetivo);
  59.                 else printf("  ");
  60.             }
  61.             printf("\n");
  62.         }
  63.     }
  64.     if(dir==2){//s
  65.         for(int y=(7+5*(config.tam))-1;y>=0;y--){
  66.             for(int x=(7+5*(config.tam))-1;x>=0;x--){
  67.                 if(!(x+y))printf("▓▓");
  68.                 else if(fase[x+((7+5*(config.tam))*y)]==BOLA)    printf("%c ",config.bola);
  69.                 else if(fase[x+((7+5*(config.tam))*y)]==PAREDE)  printf("%c%c",config.parede,config.parede);
  70.                 else if(fase[x+((7+5*(config.tam))*y)]==OBJETIVO)printf("%c ",config.objetivo);
  71.                 else printf("  ");
  72.             }
  73.             printf("\n");
  74.         }
  75.     }
  76.     if(dir==3){//d
  77.         for(int x=0;x<(7+5*(config.tam));x++){
  78.             for(int y=(7+5*(config.tam))-1;y>=0;y--){
  79.                 if(!(x+y))printf("▓▓");
  80.                 else if(fase[x+((7+5*(config.tam))*y)]==BOLA)    printf("%c ",config.bola);
  81.                 else if(fase[x+((7+5*(config.tam))*y)]==PAREDE)  printf("%c%c",config.parede,config.parede);
  82.                 else if(fase[x+((7+5*(config.tam))*y)]==OBJETIVO)printf("%c ",config.objetivo);
  83.                 else printf("  ");
  84.             }
  85.             printf("\n");
  86.         }
  87.     }
  88. }
  89.  
  90. void moveBola(int* fase,int dir,config_ config,int* x,int* y){
  91.     //printf("x%d,y%d\n",*x,*y);
  92.     //printf("fase x,y+1 = %d\n",fase[(*x)+((7+5*config.tam)*((*y)+1))]);
  93.     system("clear");
  94.     printFase(fase,dir,config);
  95.     printf("Digite 'a' para virar para a esquerda ou 'd' para virar para a direita ou 'q' para sair.\n");
  96.     usleep(500000);
  97.     if(dir==0 && fase[(*x)+((7+5*config.tam)*((*y)+1))]!=PAREDE){
  98.         fase[(*x)+((7+5*config.tam)*(*y))] = 0;
  99.         (*y)++;
  100.     }
  101.     else if(dir==1 && fase[((*x)-1)+((7+5*config.tam)*(*y))]!=PAREDE){
  102.         fase[(*x)+((7+5*config.tam)*(*y))] = 0;
  103.         (*x)--;
  104.     }
  105.     else if(dir==2 && fase[(*x)+((7+5*config.tam)*((*y)-1))]!=PAREDE){
  106.         fase[(*x)+((7+5*config.tam)*(*y))] = 0;
  107.         (*y)--;
  108.     }
  109.     else if(dir==3 && fase[((*x)+1)+((7+5*config.tam)*(*y))]!=PAREDE){
  110.         fase[(*x)+((7+5*config.tam)*(*y))] = 0;
  111.         (*x)++;
  112.     }
  113.     else return;
  114.     if(fase[(*x)+((7+5*config.tam)*(*y))] == OBJETIVO)return;
  115.     fase[(*x)+((7+5*config.tam)*(*y))] = BOLA;
  116.     moveBola(fase,dir,config,x,y);
  117. }
  118.  
  119. void jogar(config_ config){
  120.     int fase[(7+5*(config.tam))*(7+5*(config.tam))],x,y,dir=0;
  121.     char c;
  122.     if(config.tam==1){
  123.         preencheFase(fase,"#############o#    #   ## # #      ##     #    ###         ##          ##     0    ##  #    #  ##          ##    #     ##          #############");
  124.         x=1;
  125.         y=1;
  126.         //############
  127.         //#o#    #   #
  128.         //# # #      #
  129.         //#     #    #
  130.         //##         #
  131.         //#          #
  132.         //#     0    #
  133.         //#  #    #  #
  134.         //#          #
  135.         //#    #     #
  136.         //#          #
  137.         //############
  138.     }
  139.     if(config.tam==2){
  140.         preencheFase(fase,"################## #    #        ##   #      #   ###     #         ###      #       ##  #   #   #    ##o              ##  #    #    #  ##               ##    #     #    ##    #      #   ##    #   #      ##    #          ##    #          ##    #          ##        #0     ##################");
  141.         x=1;
  142.         y=6;
  143.         //#################
  144.         //# #    #        #
  145.         //#   #      #   ##
  146.         //#     #         #
  147.         //##      #       #
  148.         //#  #   #   #    #
  149.         //#o              #
  150.         //#  #    #    #  #
  151.         //#               #
  152.         //#    #     #    #
  153.         //#    #      #   #
  154.         //#    #   #      #
  155.         //#    #          #
  156.         //#    #          #
  157.         //#    #          #
  158.         //#        #0     #
  159.         //#################
  160.     }
  161.     if(config.tam==3){
  162.         preencheFase(fase,"####################### #    #             ##   #      #        ###     #              ##     #              ##    #               ## #    #             ###      #            ##  #   #   #         ##o                   ## #    #             ##  #    #    #       ##                    ##    #     #         ##    #      #        ##     #              ##    #   #           ##    #               ##    #               ##    #               ##        #0          #######################");
  163.         x=1;
  164.         y=9;
  165.         //######################
  166.         //# #    #             #
  167.         //#   #      #        ##
  168.         //#     #              #
  169.         //#     #              #
  170.         //#    #               #
  171.         //# #    #             #
  172.         //##      #            #
  173.         //#  #   #   #         #
  174.         //#o                   #
  175.         //# #    #             #
  176.         //#  #    #    #       #
  177.         //#                    #
  178.         //#    #     #         #
  179.         //#    #      #        #
  180.         //#     #              #
  181.         //#    #   #           #
  182.         //#    #               #
  183.         //#    #               #
  184.         //#    #               #
  185.         //#        #0          #
  186.         //######################
  187.     }
  188.     moveBola(fase,dir,config,&x,&y);
  189.     while(fase[x+((7+5*config.tam) * y)]!=OBJETIVO){
  190.         system("clear");
  191.         //printf("x=%d y=%d\n",x,y);
  192.         printFase(fase,dir,config);
  193.         printf("Digite 'a' para virar para a esquerda ou 'd' para virar para a direita ou 'q' para sair.");
  194.         do{
  195.             c = getchar();
  196.             while(getchar()!='\n');
  197.         }while(c!='a' && c!='d' && c!='A' && c!='D' && c!='q' && c!='Q');
  198.         if(c=='a' || c=='A')dir= (dir+1)%4;
  199.         if(c=='d' || c=='D')dir= (dir+3)%4;
  200.         if(c=='q' || c=='Q')return;
  201.         moveBola(fase,dir,config,&x,&y);
  202.     }
  203.     system("clear");
  204.     printFase(fase,dir,config);
  205.     printf("Batata, enter pra voltar.\n");
  206.     while(getchar()!='\n');
  207. }
  208.  
  209. void configura(config_ *config){
  210.     int x;
  211.     do{
  212.         system("clear");
  213.         printf("1 - tamanho = %d(%dx%d)\n",config->tam,5+5*(config->tam),5+5*(config->tam));
  214.         printf("2 - bola = '%c'\n",config->bola);
  215.         printf("3 - parede = '%c'\n",config->parede);
  216.         printf("4 - objetivo = '%c'\n",config->objetivo);
  217.         printf("0 - sair\n");
  218.         x=readIntInRange(0,4);
  219.         if(x==1)config->tam = readIntInRange(1,3);
  220.         if(x==2)do{
  221.             config->bola = getchar();
  222.             while(getchar()!='\n');
  223.         }while(config->bola=='\n' || config->bola==' ' || config->bola==config->parede || config->bola==config->objetivo);
  224.         if(x==3)do{
  225.             config->parede = getchar();
  226.             while(getchar()!='\n');
  227.         }while(config->parede=='\n' || config->parede==' ' || config->parede==config->bola || config->parede==config->objetivo);
  228.         if(x==4)do{
  229.             config->objetivo = getchar();
  230.             while(getchar()!='\n');
  231.         }while(config->objetivo=='\n' || config->objetivo==' ' || config->objetivo==config->parede || config->objetivo==config->bola);
  232.     }while(x);
  233. }
  234.  
  235. int main(){
  236.     int x;
  237.     config_ config;
  238.     config.tam=1;
  239.     config.bola='o';
  240.     config.parede='#';
  241.     config.objetivo='0';
  242.     do{
  243.         system("clear");
  244.         printf("╔═════════════════════════════════╗\n");
  245.         printf("║MAZE RUNNER MAZE RUNNER MAZE RUNN║\n");
  246.         printf("║ER MAZE RUNNER MAZE RUNNER MAZE R║\n");
  247.         printf("║UNNE╔═════════════════════╗ER MAZ║\n");
  248.         printf("║E RU║      1 - Jogar      ║RUNNER║\n");
  249.         printf("║MAZE╚═════════════════════╝E RUNN║\n");
  250.         printf("║ER MAZE RUNNER MAZE RUNNER MAZE R║\n");
  251.         printf("║UNNE╔═════════════════════╗ER MAZ║\n");
  252.         printf("║E RU║  2 - Configurações  ║RUNNER║\n");
  253.         printf("║MAZE╚═════════════════════╝E RUNN║\n");
  254.         printf("║ER MAZE RUNNER MAZE RUNNER MAZE R║\n");
  255.         printf("║UNNE╔═════════════════════╗ER MAZ║\n");
  256.         printf("║E RU║      0 - Sair       ║RUNNER║\n");
  257.         printf("║MAZE╚═════════════════════╝E RUNN║\n");
  258.         printf("║ER MAZE RUNNER MAZE RUNNER MAZE R║\n");
  259.         printf("║UNNER MAZE RUNNER MAZE RUNNER MAZ║\n");
  260.         printf("╚═════════════════════════════════╝\n");
  261.         x=readIntInRange(0,2);
  262.         if(x==1)jogar(config);
  263.         if(x==2)configura(&config);
  264.     }while(x);
  265.     return 0;
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement