Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <conio.h>
- #define HAVE_STRUCT_TIMESPEC
- #include <pthread.h>
- char matriz[10][10], c = '#', pos[4],com_a, com_b;
- int x, y, a_pos, b_pos, controle = 0,linha_a, coluna_a, linha_b, coluna_b;
- void espera(int segundos)
- {
- // Converting time into milli_seconds
- int milli_segundos = 1000 * segundos;
- // Stroing start time
- clock_t start_time = clock();
- // looping till required time is not acheived
- while (clock() < start_time + milli_segundos)
- ;
- }
- void mostrar_mapa()
- {
- system("cls");
- printf("\n\n %c %c\n ", pos[0], pos[1]);
- for(x = 0; x<10;x++)
- {
- for(y = 0; y<10; y++)
- {
- printf("%c ", matriz[x][y]);
- }
- printf("\n ");
- }
- printf("%c %c\n \n ", pos[2], pos[3]);
- }
- void* move_a(void* arg)
- {
- //----Mover A
- controle = 0;
- while(controle == 0)
- {
- com_a = getch();
- if(com_a == 'w' && linha_a >0)
- {
- matriz[linha_a][coluna_a] = '-';
- linha_a--;
- }
- else if(com_a == 's' && linha_a <9)
- {
- matriz[linha_a][coluna_a] = '-';
- linha_a++;
- }
- else if(com_a == 'a' && coluna_a >0)
- {
- matriz[linha_a][coluna_a] = '-';
- coluna_a--;
- }
- else if(com_a == 'd' && coluna_a <9)
- {
- matriz[linha_a][coluna_a] = '-';
- coluna_a++;
- }
- else if(com_a == 'p')
- {
- controle = 1;
- }
- matriz[linha_a][coluna_a] = 'A';
- mostrar_mapa();
- printf("\n\nW, A, S, D move o personagem A - as setas movem o personagem B\n\n");
- }
- pthread_exit(0);
- }
- void* move_b(void* arg)
- {
- //----Mover B
- controle = 0;
- while(controle == 0)
- {
- com_b = getch();
- if(com_b == '8' && linha_b >0)
- {
- matriz[linha_b][coluna_b] = '-';
- linha_b--;
- }
- else if(com_b == '5' && linha_b <9)
- {
- matriz[linha_b][coluna_b] = '-';
- linha_b++;
- }
- else if(com_b == '4' && coluna_b >0)
- {
- matriz[linha_b][coluna_b] = '-';
- coluna_b--;
- }
- else if(com_b == '6' && coluna_b <9)
- {
- matriz[linha_b][coluna_b] = '-';
- coluna_b++;
- }
- else if(com_b == 'p')
- {
- controle = 1;
- }
- matriz[linha_b][coluna_b] = 'B';
- mostrar_mapa();
- }
- pthread_exit(0);
- }
- int main()
- {
- int arg = 0;
- pthread_t t1, t2;
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pos[0] = '1';
- pos[1] = '2';
- pos[2] = '3';
- pos[3] = '4';
- for(x = 0; x<10;x++)
- {
- for(y = 0; y<10; y++)
- {
- matriz[x][y] = c;
- }
- }
- while (controle == 0)
- {
- mostrar_mapa();
- printf("Jogador A, escolha a sua posicao inicial: ");
- scanf("%d", &a_pos);
- pos[a_pos-1] = ' ';
- if(a_pos == 1)
- {
- linha_a = 0;
- coluna_a = 0;
- matriz[linha_a][coluna_a] = 'A';
- controle = 1;
- }
- else if(a_pos == 2)
- {
- linha_a = 0;
- coluna_a = 9;
- matriz[linha_a][coluna_a] = 'A';
- controle = 1;
- }
- else if(a_pos == 3)
- {
- linha_a = 9;
- coluna_a = 0;
- matriz[linha_a][coluna_a] = 'A';
- controle = 1;
- }
- else if(a_pos == 4)
- {
- linha_a = 9;
- coluna_a = 9;
- matriz[linha_a][coluna_a] = 'A';
- controle = 1;
- }
- else
- {
- printf("\n\nValor inválido! Digite um dos numeros que aparecem acima.");
- espera(2);
- }
- }
- controle = 0;
- while(controle == 0)
- {
- mostrar_mapa();
- printf("Jogador B, escolha a sua posicao inicial: ");
- scanf("%d", &b_pos);
- pos[b_pos-1] = ' ';
- mostrar_mapa();
- if(b_pos == a_pos)
- {
- printf("B não pode iniciar no mesmo lugar que A, escolha ouro valor!");
- espera(2);
- }
- else if(b_pos == 1)
- {
- linha_b = 0;
- coluna_b = 0;
- matriz[linha_b][coluna_b] = 'B';
- controle = 1;
- }
- else if(b_pos == 2)
- {
- linha_b = 0;
- coluna_b = 9;
- matriz[linha_b][coluna_b] = 'B';
- controle = 1;
- }
- else if(b_pos == 3)
- {
- linha_b = 9;
- coluna_b = 0;
- matriz[linha_b][coluna_b] = 'B';
- controle = 1;
- }
- else if(b_pos == 4)
- {
- linha_b = 9;
- coluna_b = 9;
- matriz[linha_b][coluna_b] = 'B';
- controle = 1;
- }
- else
- {
- printf("\n\nValor inválido! Digite um dos numeros que aparecem acima.");
- espera(2);
- }
- }
- for(x = 0; x<4; x++)
- {
- pos[x] = ' ';
- }
- mostrar_mapa();
- printf("\n\nW, A, S, D move o personagem A - as setas movem o personagem B\n\n");
- pthread_create(&t1, &attr, move_a, &arg);
- pthread_create(&t2, &attr, move_b, &arg);
- pthread_join(t1, NULL);
- pthread_join(t2, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment