Advertisement
Es7evam

WumpusWorldBaseComplete

Apr 29th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. int l, c,luzfound = 0, score = 0, steps = 0, arrow = 1; // a luz nao foi encontrada ainda, vai ser mudado para 1 quando for.
  7.  
  8. typedef struct celula {
  9.     int tipo;
  10.     int cheiroFraco;
  11.     int cheiroMedio;
  12.     int cheiroForte;
  13.     int brisaFraca;
  14.     int brisaMedia;
  15.     int brisaForte;
  16. } celula;
  17.  
  18. int walk(char dir, int i, int j){ //passa a direcao para andar (w,a,s,d) , a posicao i (linhas) e j (colunas) e o numero de passos.
  19.     steps--;
  20.     score--;
  21.     if (dir == 'w'){
  22.         i--;
  23.     }
  24.     if (dir == 's'){
  25.         i++;
  26.     }
  27.     if (dir == 'a'){
  28.         j--;
  29.     }
  30.     if (dir == 'd'){
  31.         j++;
  32.     }
  33.     if(i < 0){
  34.         printf("Movimento invalido!\n");
  35.         i++;
  36.     }
  37.     if(j < 0){
  38.         printf("Movimento invalido!\n");
  39.         j++;
  40.     }
  41.     if(i > l-1){
  42.         printf("Movimento invalido!\n");
  43.         i--;
  44.     }
  45.     if(j > c-1){
  46.         printf("Movimento invalido!\n");
  47.         j--;
  48.     }
  49.     if(dir == 'w' || dir == 's') return i;
  50.     if(dir == 'a' || dir == 'd') return j;
  51. }
  52.  
  53.  
  54. int sensations(int i, int j, celula ** M){ //sensações no local
  55.     if (i == 0 && j == 0 && luzfound == 1){
  56.         printf("Voce saiu do mundo de Wumpus a salvo!\n");
  57.         score += 1000;
  58.         steps = 0;
  59.         return 0;
  60.     }
  61.     if (M[i][j].tipo == 1){
  62.         printf("Voce encontrou um abismo!\n");
  63.         score -= 1000;
  64.         steps = 0;
  65.         i = j = 0; //volta para o inicio
  66.         return 0;
  67.     }
  68.     if (M[i][j].tipo == 2){
  69.         printf("Voce encontrou um monstro!\n");
  70.         score -=10000;
  71.         steps = 0;
  72.         i = j = 0; //volta para o inicio
  73.         M[i][j].tipo = 0;
  74.         return 0;
  75.     }
  76.     if (M[i][j].tipo == 3){
  77.         printf("Parabens, voce encontrou a luz\n");
  78.         score += 1000;
  79.         luzfound = 1;
  80.         M[i][j].tipo = 0;
  81.         return 0;
  82.     }
  83.     //imprimindo as sensações
  84.     int k;
  85.     for(k = 0;k<M[i][j].cheiroFraco;k++){
  86.         printf("Você sente um cheiro fraco\n");
  87.     }
  88.     for(k = 0;k<M[i][j].cheiroMedio;k++){
  89.         printf("Você sente um cheiro médio\n");
  90.     }
  91.     for(k = 0;k<M[i][j].cheiroForte;k++){
  92.         printf("Você sente um cheiro forte\n");
  93.     }
  94.     for(k = 0;k<M[i][j].brisaFraca;k++){
  95.         printf("Você sente uma brisa fraca\n");
  96.     }
  97.     for(k = 0;k<M[i][j].brisaMedia;k++){
  98.         printf("Você sente uma brisa média\n");
  99.     }
  100.     for(k = 0;k<M[i][j].brisaForte;k++){
  101.         printf("Você sente uma brisa forte\n");
  102.     }
  103.     return 0;
  104. }
  105.  
  106. int shoot(int i, int j, celula ** M, char side){ //retorna 0 se errar ou for inválido, 1 se acertar. - Função flecha
  107.     if (arrow == 0){
  108.         printf("Você já gastou a sua flecha!\n");
  109.         return 0;
  110.     }
  111.     arrow--;
  112.     if (side == 'w'){
  113.         i--;
  114.     }
  115.     if (side == 's'){
  116.         i++;
  117.     }
  118.     if (side == 'a'){
  119.         j--;
  120.     }
  121.     if (side == 'd'){
  122.         j++;
  123.     }
  124.     if (i < 0 || j < 0 || i > l-1 || j > c-1) return 0;
  125.     if (M[i][j].tipo == 2){
  126.         printf("Você matou Wumpus!\n");
  127.         return 1;
  128.     }else{
  129.         printf("Errou!\n");
  130.         return 0;
  131.     }
  132.  
  133. }
  134.  
  135. int askwhat(int i, int j, celula ** M, char side){ //função de pergunta
  136.     score--;
  137.     if (side == 'w'){
  138.         i--;
  139.     }
  140.     if (side == 's'){
  141.         i++;
  142.     }
  143.     if (side == 'a'){
  144.         j--;
  145.     }
  146.     if (side == 'd'){
  147.         j++;
  148.     }
  149.     if (i < 0 || j < 0 || i > l-1 || j > c-1){
  150.         printf("Você encontrou uma parede\n");
  151.         return 0;
  152.     }
  153.     sensations(i, j, M);
  154. }
  155.  
  156.  
  157. int main() {
  158.     int i, j, k = 0;
  159.     int begin, end;
  160.     char side, dir;
  161.  
  162.  //-----Função de leitura--------//
  163.     FILE* fp;
  164.     char* mat_content; //vetor que tem a leitura
  165.  
  166.     celula **M;
  167.  
  168.     fp = fopen("matriz.txt", "r");
  169.    
  170.     if (fp==NULL)
  171.         return -1;
  172.  
  173.     //----Fim leitura arquivo----//
  174.     //Próximas 2 linhas scanf do arquivo
  175.     fscanf(fp, "%d", &l);
  176.     fscanf(fp, "%d", &c);
  177.     steps = l*c;
  178.  
  179.     //-------Declaração da matriz ------//
  180.     M = (celula **)calloc((l), sizeof(celula*)); //declara setando como 0    
  181.  
  182.     for(i=0; i<l; i++)
  183.        M[i] = (celula *)calloc(c, sizeof(celula)); //declara setando como 0
  184.  
  185.     begin = ftell(fp); //começo após os números (é um \n)
  186.  
  187.     fseek(fp, 0, SEEK_END); //vai até o fim
  188.  
  189.     end = ftell(fp); //conta a posição do fim
  190.  
  191.     fseek(fp, begin+1, SEEK_SET); //começa a setar à partir do begin
  192.  
  193.     mat_content = (char*)malloc((end-begin)*sizeof(char)); //alocação
  194.  
  195.     fread(mat_content,sizeof(char), (end-begin), fp); //read
  196.  
  197.     //-------Fim Declaração da Matriz -------//
  198.     //------Atribuição de cada tipo à struct-------//
  199.  
  200.     for(i=0; i<l; i++){
  201.         for(j=0; j<c; j++){
  202.             while(k < (end - begin) && mat_content[k] != '\n'){
  203.                 if (mat_content[k] == 'A')
  204.                     M[i][j].tipo = 1; // Abismo é tipo 1
  205.                 if (mat_content[k] == 'M')
  206.                     M[i][j].tipo = 2; // Monstro é tipo 2
  207.                 if (mat_content[k] == 'L')
  208.                     M[i][j].tipo = 3; // Luz é tipo 3
  209.                 if (mat_content[k] == 'B')
  210.                     M[i][j].brisaForte++; //é B
  211.                 if (mat_content[k] == 'C')
  212.                     M[i][j].cheiroForte++; //é C
  213.                 if (mat_content[k] == 'b'){
  214.                         if(mat_content[k+1] == 'B'){
  215.                             M[i][j].brisaMedia++; //é bB
  216.                             k++;
  217.                         }else{
  218.                             M[i][j].brisaFraca++; //é b
  219.                         }
  220.                 }
  221.                 if (mat_content[k] == 'c'){
  222.                         if(mat_content[k+1] == 'C'){
  223.                             M[i][j].cheiroMedio++; //é cC
  224.                             k++;
  225.                         }else{
  226.                             M[i][j].cheiroFraco++; //é c
  227.                         }
  228.                 }
  229.                 k++;
  230.             }//Duas linhas  abaixo testando o scan
  231.             k++;
  232.             //printf("\ni = %d, j = %d k = %d - [%d %d %d %d %d %d %d]", i, j, k, M[i][j].tipo, M[i][j].brisaFraca, M[i][j].brisaMedia, M[i][j].brisaForte, M[i][j].cheiroFraco, M[i][j].cheiroMedio, M[i][j].cheiroForte);
  233.         }
  234.     }
  235.     //--------Fim da Atribuição-----------//
  236.  
  237.     i = 0;
  238.     j = 0;
  239.  
  240.  
  241.     printf("\n\n\nControles:\n");
  242.     printf("w - Andar para cima\n");
  243.     printf("s - Andar para baixo\n");
  244.     printf("d - Andar para a direita\n");
  245.     printf("a - Andar para a esquerda\n");
  246.     printf("e - Atirar flecha\n");
  247.     printf("q - Perguntar\n");
  248.     printf("c - Acessar o menu de controles\n");
  249.  
  250.     while (steps > 0){
  251.         printf("--------------------------------------------------------------------------------");
  252.         printf("\n\nSua coordenada atual é {%d, %d}\n", i, j);
  253.         printf("Você tem %d passos restantes\n", steps);
  254.         printf("Sua pontuação atual é %d\n\n\n", score);
  255.         sensations(i, j, M);
  256.         printf("Para que lado quer andar? \n");
  257.         scanf("%c%*c",&dir);
  258.         if(dir == 'c'){
  259.             printf("\n\nControles:\n");
  260.             printf("w - Andar para cima\n");
  261.             printf("s - Andar para baixo\n");
  262.             printf("d - Andar para a direita\n");
  263.             printf("a - Andar para a esquerda\n");
  264.             printf("e - Atirar flecha\n");
  265.             printf("q - Perguntar\n");
  266.         }
  267.         if(dir == 'q'){
  268.             printf("Para que lado você quer perguntar?\n");
  269.             scanf("%c%*c", &side);
  270.             askwhat(i, j, M, side);
  271.         }
  272.         if(dir == 'e'){ //q é o modo shoot
  273.             printf("Digite o lado para qual quer atirar\n");
  274.             scanf("%c%*c", &side);
  275.             if(shoot(i, j, M, side) == 1){
  276.                 score += 10000;
  277.             }
  278.             scanf("%c%*c", &dir);
  279.         }
  280.         if(dir == 'w' || dir == 's'){
  281.             i = walk(dir, i, j);
  282.         }
  283.         if(dir == 'a' || dir == 'd'){  
  284.             j = walk(dir, i, j);
  285.         }
  286.         printf("--------------------------------------------------------------------------------");
  287.     }
  288.     printf("Você chegou ao fim do jogo, a sua pontuação é: %d\n\n\n", score);
  289.  
  290.  
  291.     //--------- Dando free no programa--------/
  292.  
  293.     fclose(fp);
  294.  
  295.     for(i=0; i<l; i++)
  296.         free(M[i]);
  297.  
  298.     free(M);
  299.  
  300.     free(mat_content);
  301.  
  302.     //--------- Fim do free ----------//
  303.  
  304.     return 0;
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement