Guest User

Project Facul C

a guest
Jun 26th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <conio.h>
  6. #define HAVE_STRUCT_TIMESPEC
  7. #include <pthread.h>
  8.  
  9. char matriz[10][10], c = '#', pos[4],com_a, com_b;
  10. int x, y, a_pos, b_pos, controle = 0,linha_a, coluna_a, linha_b, coluna_b;
  11.  
  12. void espera(int segundos)
  13. {
  14.     // Converting time into milli_seconds
  15.     int milli_segundos = 1000 * segundos;
  16.  
  17.     // Stroing start time
  18.     clock_t start_time = clock();
  19.  
  20.     // looping till required time is not acheived
  21.     while (clock() < start_time + milli_segundos)
  22.         ;
  23. }
  24.  
  25.  
  26. void mostrar_mapa()
  27. {
  28.     system("cls");
  29.     printf("\n\n     %c                 %c\n     ", pos[0], pos[1]);
  30.  
  31.     for(x = 0; x<10;x++)
  32.     {
  33.           for(y = 0; y<10; y++)
  34.           {
  35.                 printf("%c ", matriz[x][y]);
  36.           }
  37.           printf("\n     ");
  38.     }
  39.  
  40.     printf("%c                 %c\n                      \n     ", pos[2], pos[3]);
  41. }
  42.  
  43. void* move_a(void* arg)
  44. {
  45.     //----Mover A
  46.  
  47.     controle = 0;
  48.     while(controle == 0)
  49.     {
  50.         com_a = getch();
  51.  
  52.         if(com_a == 'w' && linha_a >0)
  53.         {
  54.             matriz[linha_a][coluna_a] = '-';
  55.             linha_a--;
  56.         }
  57.         else if(com_a == 's' && linha_a <9)
  58.         {
  59.             matriz[linha_a][coluna_a] = '-';
  60.             linha_a++;
  61.         }
  62.         else if(com_a == 'a' && coluna_a >0)
  63.         {
  64.             matriz[linha_a][coluna_a] = '-';
  65.             coluna_a--;
  66.         }
  67.         else if(com_a == 'd' && coluna_a <9)
  68.         {
  69.             matriz[linha_a][coluna_a] = '-';
  70.             coluna_a++;
  71.         }
  72.         else if(com_a == 'p')
  73.         {
  74.             controle = 1;
  75.         }
  76.  
  77.         matriz[linha_a][coluna_a] = 'A';
  78.  
  79.         mostrar_mapa();
  80.  
  81.         printf("\n\nW, A, S, D move o personagem A   -     as setas movem o personagem B\n\n");
  82.  
  83.     }
  84.     pthread_exit(0);
  85.  
  86. }
  87.  
  88. void* move_b(void* arg)
  89. {
  90.     //----Mover B
  91.  
  92.     controle = 0;
  93.     while(controle == 0)
  94.     {
  95.         com_b = getch();
  96.  
  97.         if(com_b == '8' && linha_b >0)
  98.         {
  99.             matriz[linha_b][coluna_b] = '-';
  100.             linha_b--;
  101.         }
  102.         else if(com_b == '5' && linha_b <9)
  103.         {
  104.             matriz[linha_b][coluna_b] = '-';
  105.             linha_b++;
  106.         }
  107.         else if(com_b == '4' && coluna_b >0)
  108.         {
  109.             matriz[linha_b][coluna_b] = '-';
  110.             coluna_b--;
  111.         }
  112.         else if(com_b == '6' && coluna_b <9)
  113.         {
  114.             matriz[linha_b][coluna_b] = '-';
  115.             coluna_b++;
  116.         }
  117.         else if(com_b == 'p')
  118.         {
  119.             controle = 1;
  120.         }
  121.         matriz[linha_b][coluna_b] = 'B';
  122.  
  123.         mostrar_mapa();
  124.  
  125.     }
  126.  
  127.     pthread_exit(0);
  128.  
  129. }
  130.  
  131. int main()
  132. {
  133.     int arg = 0;
  134.  
  135.     pthread_t t1, t2;
  136.     pthread_attr_t attr;
  137.     pthread_attr_init(&attr);
  138.  
  139.     pos[0] = '1';
  140.     pos[1] = '2';
  141.     pos[2] = '3';
  142.     pos[3] = '4';
  143.  
  144.     for(x = 0; x<10;x++)
  145.     {
  146.           for(y = 0; y<10; y++)
  147.           {
  148.                 matriz[x][y] = c;
  149.           }
  150.     }
  151.  
  152.     while (controle == 0)
  153.     {
  154.         mostrar_mapa();
  155.  
  156.         printf("Jogador A, escolha a sua posicao inicial: ");
  157.         scanf("%d", &a_pos);
  158.         pos[a_pos-1] = ' ';
  159.  
  160.         if(a_pos == 1)
  161.         {
  162.             linha_a = 0;
  163.             coluna_a = 0;
  164.             matriz[linha_a][coluna_a] = 'A';
  165.             controle = 1;
  166.         }
  167.         else if(a_pos == 2)
  168.         {
  169.             linha_a = 0;
  170.             coluna_a = 9;
  171.             matriz[linha_a][coluna_a] = 'A';
  172.             controle = 1;
  173.         }
  174.         else if(a_pos == 3)
  175.         {
  176.             linha_a = 9;
  177.             coluna_a = 0;
  178.             matriz[linha_a][coluna_a] = 'A';
  179.             controle = 1;
  180.         }
  181.         else if(a_pos == 4)
  182.         {
  183.             linha_a = 9;
  184.             coluna_a = 9;
  185.             matriz[linha_a][coluna_a] = 'A';
  186.             controle = 1;
  187.         }
  188.         else
  189.         {
  190.             printf("\n\nValor inválido! Digite um dos numeros que aparecem acima.");
  191.             espera(2);
  192.         }
  193.     }
  194.  
  195.     controle = 0;
  196.     while(controle == 0)
  197.     {
  198.         mostrar_mapa();
  199.  
  200.         printf("Jogador B, escolha a sua posicao inicial: ");
  201.         scanf("%d", &b_pos);
  202.         pos[b_pos-1] = ' ';
  203.  
  204.  
  205.         mostrar_mapa();
  206.  
  207.         if(b_pos == a_pos)
  208.         {
  209.             printf("B não pode iniciar no mesmo lugar que A, escolha ouro valor!");
  210.             espera(2);
  211.         }
  212.         else if(b_pos == 1)
  213.         {
  214.             linha_b = 0;
  215.             coluna_b = 0;
  216.             matriz[linha_b][coluna_b] = 'B';
  217.             controle = 1;
  218.         }
  219.         else if(b_pos == 2)
  220.         {
  221.             linha_b = 0;
  222.             coluna_b = 9;
  223.             matriz[linha_b][coluna_b] = 'B';
  224.             controle = 1;
  225.         }
  226.         else if(b_pos == 3)
  227.         {
  228.             linha_b = 9;
  229.             coluna_b = 0;
  230.             matriz[linha_b][coluna_b] = 'B';
  231.             controle = 1;
  232.         }
  233.         else if(b_pos == 4)
  234.         {
  235.             linha_b = 9;
  236.             coluna_b = 9;
  237.             matriz[linha_b][coluna_b] = 'B';
  238.             controle = 1;
  239.         }
  240.         else
  241.         {
  242.             printf("\n\nValor inválido! Digite um dos numeros que aparecem acima.");
  243.             espera(2);
  244.         }
  245.     }
  246.  
  247.     for(x = 0; x<4; x++)
  248.     {
  249.         pos[x] = ' ';
  250.     }
  251.  
  252.     mostrar_mapa();
  253.  
  254.     printf("\n\nW, A, S, D move o personagem A   -     as setas movem o personagem B\n\n");
  255.  
  256.     pthread_create(&t1, &attr, move_a, &arg);
  257.     pthread_create(&t2, &attr, move_b, &arg);
  258.  
  259.     pthread_join(t1, NULL);
  260.     pthread_join(t2, NULL);
  261.  
  262.     return 0;
  263. }
Advertisement
Add Comment
Please, Sign In to add comment