Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. char tabuleiro[10][10];
  5.  
  6. void resetar_tabuleiro() {
  7.     int i;
  8.     int j;
  9.  
  10.     for (i = 0; i < 10; ++i)
  11.         for (j = 0; j < 10; ++j)
  12.             tabuleiro[i][j] = ' ';
  13. }
  14.  
  15. void imprimir_tabuleiro() {
  16.     int i;
  17.     int j;
  18.  
  19.     for (i = 0; i < 10; ++i) {
  20.         printf("+---+---+---+---+---+---+---+---+---+---+\n");
  21.  
  22.         for (j = 0; j < 10; ++j) {
  23.             printf("| %c ", tabuleiro[i][j]);
  24.         }
  25.         printf("|\n");
  26.     }
  27.  
  28.     printf("+---+---+---+---+---+---+---+---+---+---+\n");
  29. }
  30.  
  31. int coluna(int i, int j) {
  32.     if (tabuleiro[i][j] == tabuleiro[i+1][j] && tabuleiro[i][j] == tabuleiro[i+2][j] && tabuleiro[i][j] == tabuleiro[i+3][j]) {
  33.         if (tabuleiro[i][j] == '#')
  34.             return 1;
  35.         else if (tabuleiro[i][j] == '@')
  36.             return 2;
  37.         else
  38.             return 0;
  39.     }
  40. }
  41.  
  42. int linha(int i, int j) {
  43.     if (tabuleiro[i][j] == tabuleiro[i][j+1] && tabuleiro[i][j] == tabuleiro[i][j+2] && tabuleiro[i][j] == tabuleiro[i][j+3]) {
  44.         if (tabuleiro[i][j] == '#')
  45.             return 1;
  46.         else if (tabuleiro[i][j] == '@')
  47.             return 2;
  48.         else
  49.             return 0;
  50.     }
  51. }
  52.  
  53. int diagonal_principal(int i, int j) {
  54.     if (tabuleiro[i][j] == tabuleiro[i+1][j+1] && tabuleiro[i][j] == tabuleiro[i+2][j+2] && tabuleiro[i][j] == tabuleiro[i+3][j+3]) {
  55.         if (tabuleiro[i][j] == '#')
  56.             return 1;
  57.         else if (tabuleiro[i][j] == '@')
  58.             return 2;
  59.         else
  60.             return 0;
  61.     }
  62. }
  63.  
  64. int diagonal_secundaria(int i, int j) {
  65.     if (tabuleiro[i][j] == tabuleiro[i+1][j-1] && tabuleiro[i][j] == tabuleiro[i+2][j-2] && tabuleiro[i][j] == tabuleiro[i+3][j-3]) {
  66.         if (tabuleiro[i][j] == '#')
  67.             return 1;
  68.         else if (tabuleiro[i][j] == '@')
  69.             return 2;
  70.         else
  71.             return 0;
  72.     }
  73. }
  74.  
  75. int verificar_vencedor() {
  76.     int i;
  77.     int j;
  78.  
  79.     for (i = 0; i < 10; ++i) {
  80.         for (j = 0; j < 10; ++j) {
  81.             if (i < 7) {
  82.                 if (coluna(i, j) == 1)
  83.                     return 1;
  84.                 else if (coluna(i, j) == 2)
  85.                     return 2;
  86.             }
  87.  
  88.             if (j < 7) {
  89.                 if (linha(i, j) == 1)
  90.                     return 1;
  91.                 else if (linha(i, j) == 2)
  92.                     return 2;
  93.             }
  94.  
  95.             if (j < 7 && i < 7) {
  96.                 if (diagonal_principal(i, j) == 1)
  97.                     return 1;
  98.                 else if (diagonal_principal(i, j) == 2)
  99.                     return 2;
  100.             }
  101.  
  102.             if (j > 2 && i < 7) {
  103.                 if (diagonal_secundaria(i, j) == 1)
  104.                     return 1;
  105.                 else if (diagonal_secundaria(i, j) == 2)
  106.                     return 2;
  107.             }
  108.         }
  109.     }
  110.  
  111.     return 0;
  112. }
  113.  
  114. int jogar(char jogador) {
  115.     int tmp;
  116.     int i;
  117.  
  118.     printf("coluna: ");
  119.     scanf("%d", &tmp);
  120.  
  121.     for (i = 0; i < 10; ++i) {
  122.         if (tabuleiro[i][tmp] != ' ')
  123.             break;
  124.     }
  125.  
  126.     if (tabuleiro[0][tmp] != ' ' || tmp < 0 || tmp > 9) {
  127.         printf("nao eh possivel jogar nessa posicao\n");
  128.         return 0;
  129.     }
  130.     else {
  131.         for (i = 0; i < 10; ++i) {
  132.             if (tabuleiro[i][tmp] != ' ')
  133.                 break;
  134.         }
  135.  
  136.         tabuleiro[i - 1][tmp] = jogador;
  137.         return 1;
  138.     }
  139. }
  140.  
  141. int main() {
  142.  
  143.  
  144.  
  145.     resetar_tabuleiro();
  146.     int vencedor = 0;
  147.     char jogador = '#';
  148.     int jogada_ok = 0;
  149.     char * nome1;
  150.     char * nome2;
  151.     int idade1;
  152.     int idade2;
  153.    
  154.     nome1 = malloc(10 * sizeof(char));
  155. printf("Digite o nome do Primeiro jogador:\n");
  156. scanf("%s", nome1);
  157. printf("Digite sua idade:\n");
  158. scanf("%i", &idade1);
  159.  
  160.     nome2 = malloc(10 * sizeof(char));
  161. printf("Digite o nome do Segundo jogador:\n");
  162. scanf("%s", nome2);
  163. printf("Digite sua idade:\n");
  164. scanf("%i", &idade2);
  165.  
  166. printf("\n");  
  167.  
  168.     printf("Hora de começar o jogo!\n");
  169.    
  170.     printf("\n");
  171.    
  172.     printf("O jogador %s ficara com a peça # e o jogador %s ficara com a peça @ \n", nome1, nome2);
  173.    
  174.     printf("\n");
  175.  
  176.     while (vencedor == 0) {
  177.         imprimir_tabuleiro();
  178.         printf("Eh a vez do jogador que esta com a peça %c\n", jogador);
  179.         jogada_ok = 0;
  180.  
  181.         while (jogada_ok == 0) {
  182.             jogada_ok = jogar(jogador);
  183.         }
  184.  
  185.         vencedor = verificar_vencedor();
  186.  
  187.         if (jogador == '#')
  188.             jogador = '@';
  189.         else
  190.             jogador = '#';
  191.        
  192.     }
  193.  
  194.     imprimir_tabuleiro();
  195.     printf("jogador %d venceu!\n", vencedor);
  196.    
  197.  
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement