Advertisement
leo1553

Tic Tac Toe C

Dec 9th, 2017
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. /* Leonardo Leal
  2.  */
  3.  
  4. #include <stdio.h>
  5.  
  6. int mat[3][3];
  7. int turno;
  8.  
  9. void limparMatriz() {
  10.     int i, j;
  11.     for(i = 0; i < 3; i++){
  12.         for(j = 0; j < 3; j++) {
  13.             mat[i][j] = -1;
  14.         }
  15.     }
  16. }
  17.    
  18. void desenharJogo() {
  19.     int i, j;
  20.     system("cls");
  21.     printf("\n     1   2   3   X\n   +---+---+---+\n");
  22.     for(i = 0; i < 3; i++){
  23.         printf(" %d |", i + 1);
  24.         for(j = 0; j < 3; j++) {
  25.             switch(mat[i][j]) {
  26.                 case -1:
  27.                     printf("   |");
  28.                     break;
  29.                 case 0:
  30.                     printf(" o |");
  31.                     break;
  32.                 case 1:
  33.                     printf(" x |");
  34.                     break;
  35.             }
  36.         }
  37.         printf("\n   +---+---+---+\n");
  38.     }
  39.     printf(" Y\n");
  40. }
  41.  
  42. int verificarGanhador() {
  43.     int i;
  44.     for(i = 0; i < 3; i++) {
  45.         //Verificar Horizontal
  46.         if(mat[i][0] != -1 && mat[i][0] == mat[i][1] && mat[i][1] == mat[i][2])
  47.             return mat[i][0];
  48.         //Verificar Vertical
  49.         if(mat[0][i] != -1 && mat[0][i] == mat[1][i] && mat[1][i] == mat[2][i])
  50.             return mat[0][i];
  51.     }
  52.            
  53.     //Verificar Diagonais
  54.     if(mat[1][1] != -1) {
  55.         if(mat[0][0] == mat[1][1] && mat[1][1] == mat[2][2])
  56.             return mat[1][1];
  57.            
  58.         if(mat[0][2] == mat[1][1] && mat[1][1] == mat[2][0])
  59.             return mat[1][1];
  60.     }
  61.     return -1;
  62. }
  63.  
  64. void lerPosicao(int* x, int* y) {
  65.     printf("  - Jogador %d: Insira a posicao (x y) que voce deseja marcar: ", (turno % 2) + 1);
  66.     scanf("%d %d", y, x);
  67.    
  68.     if(*x < 1 || *x > 3) {
  69.         printf("      Posicao X invalida.\n");
  70.         return lerPosicao(x, y);
  71.     }
  72.     else if(*y < 1 || *y > 3) {
  73.         printf("      Posicao Y invalida.\n");
  74.         return lerPosicao(x, y);
  75.     }
  76.    
  77.     *x = *x - 1;
  78.     *y = *y - 1;
  79.     if(mat[*x][*y] != -1) {
  80.         printf("      Esta posicao ja esta ocupada.\n");
  81.         return lerPosicao(x, y);
  82.     }
  83. }
  84.  
  85. void iniciarJogo() {
  86.     int x, y;
  87.     int ganhador;
  88.     limparMatriz();
  89.     desenharJogo();
  90.    
  91.     for(turno = 0; turno < 9; turno++) {
  92.         lerPosicao(&x, &y);
  93.         mat[x][y] = turno % 2;
  94.        
  95.         ganhador = verificarGanhador();
  96.         desenharJogo();
  97.        
  98.         if(ganhador != -1) {
  99.             printf("\n   O jogador %d ganhou!\n", (turno % 2) + 1);
  100.             break;
  101.         }
  102.     }
  103.    
  104.     if(ganhador == -1) {
  105.         printf("\n   Ninguem ganhou o jogo.\n");
  106.     }
  107.     printf("\n   Insira 1 para jogar novamente.\n   ");
  108.     scanf("%d", &x);
  109.     if(x == 1)
  110.         iniciarJogo();
  111. }
  112.  
  113. int main() {
  114.     iniciarJogo();
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement