Advertisement
F_THIAGO

Jogo da velha simples - Windows

May 15th, 2019
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.34 KB | None | 0 0
  1. /*
  2. *   JOGO DA VELHA - V.1.0
  3. * 2018 Novembro.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <conio.h>
  9.  
  10. #if defined(WIN3) || defined(_WIN32)
  11.     void limparTela()
  12.     {
  13.         system("@cls || clear");
  14.     }
  15.    
  16.     void dimensiona( int x, int y )
  17.     {
  18.         char command[50];
  19.         sprintf( command, "mode %d, %d", x, y );
  20.         system(command);
  21.     }
  22. #endif
  23.  
  24. // Constantes e Macros
  25. #define PLAYER_1    'x'
  26. #define PLAYER_2    'o'
  27. #define FREE_FIELD  '.'
  28.  
  29. #define ENTER       0x0D
  30. #define ESCAPE      0x1B
  31. #define LEFT        0x61
  32. #define RIGHT       0x64
  33. #define UP          0x77
  34. #define BOTTOM      0x73
  35.  
  36. // Prototipos das funcoes
  37. void imprimeTabuleiro();
  38. int  verificaJogo();
  39. void jogar( int, int );
  40.  
  41. // Variaveis globais
  42. char cursor = 0x00;
  43. short x, y;
  44. unsigned short jogadas = 0;
  45.    
  46. unsigned short tabuleiro[3][3] = {
  47.     { FREE_FIELD, FREE_FIELD, FREE_FIELD },
  48.     { FREE_FIELD, FREE_FIELD, FREE_FIELD },
  49.     { FREE_FIELD, FREE_FIELD, FREE_FIELD }
  50. };
  51.  
  52. int main()
  53. {
  54.     // Variaveis Locais
  55.     unsigned short sair = 0;
  56.     char entrada = 0x00;
  57.     dimensiona( 50, 30 );
  58.    
  59.     x = 0, y = 0;
  60.    
  61.     while( !sair )
  62.     {
  63.         limparTela();
  64.        
  65.         cursor = ((jogadas%2) == 0)?'x':'o';
  66.         printf(" Jogadas: %2d -- (%2d,%2d)\n\n", jogadas, x, y );
  67.        
  68.         imprimeTabuleiro();
  69.        
  70.         // Entrada de dados
  71.         entrada = getch();
  72.         fflush(stdin);
  73.        
  74.         // Tratamento dos dados
  75.         switch( entrada )
  76.         {
  77.             case UP:
  78.                 x = ( (x-1) < 0 )?2:(x-1);
  79.                 break;
  80.                
  81.             case BOTTOM:
  82.                 x = ( (x+1) > 2 )?0:(x+1);
  83.                 break;
  84.                
  85.             case LEFT:
  86.                 y = ( (y-1) < 0 )?2:(y-1);
  87.                 break;
  88.                
  89.             case RIGHT:
  90.                 y = ( (y+1) > 2 )?0:(y+1);
  91.                 break;
  92.                
  93.             case ENTER:
  94.                 jogar( x, y );
  95.                 break;
  96.                
  97.             case ESCAPE:
  98.                 sair = 1;
  99.                 break;
  100.                
  101.             default:
  102.                 break;
  103.         }
  104.        
  105.         // Verifica o estado do jogo
  106.         if( verificaJogo() == 1 )
  107.         {
  108.             printf("\n\t[!] Vitoria do jogador 1 >-<\n");
  109.             sair = 1;
  110.             continue;
  111.         }
  112.         else if( verificaJogo() == 2 )
  113.         {
  114.             printf("\n\t[!] Vitoria do jogador 2 >-<\n");
  115.             sair = 1;
  116.             continue;
  117.         }
  118.         else if( verificaJogo() < 0 )
  119.         {
  120.             printf("\n\t[!] Empate ;-;\n");
  121.             sair = 1;
  122.             continue;
  123.         }
  124.     }
  125.    
  126.     system("pause>nul");
  127.     return 0;
  128. }
  129.  
  130. void imprimeTabuleiro()
  131. {
  132.     int i, j;
  133.    
  134.     for( i=0; i<3; i++ )
  135.     {
  136.         printf("\t\t\t");
  137.         for( j=0; j<3; j++ )
  138.             printf("%c ", ((i == x) && (j == y))?cursor:tabuleiro[i][j] );
  139.         printf("\n");
  140.     }
  141. };
  142.  
  143. int  verificaJogo()
  144. {
  145.     if(
  146.         ( (tabuleiro[0][0] == PLAYER_1) && (tabuleiro[0][1] == PLAYER_1) && (tabuleiro[0][2] == PLAYER_1)  ) || \
  147.         ( (tabuleiro[1][0] == PLAYER_1) && (tabuleiro[1][1] == PLAYER_1) && (tabuleiro[1][2] == PLAYER_1)  ) || \
  148.         ( (tabuleiro[2][0] == PLAYER_1) && (tabuleiro[2][1] == PLAYER_1) && (tabuleiro[2][2] == PLAYER_1)  ) || \
  149.  
  150.         ( (tabuleiro[0][0] == PLAYER_1) && (tabuleiro[1][0] == PLAYER_1) && (tabuleiro[2][0] == PLAYER_1)  ) || \
  151.         ( (tabuleiro[0][1] == PLAYER_1) && (tabuleiro[1][1] == PLAYER_1) && (tabuleiro[2][1] == PLAYER_1)  ) || \
  152.         ( (tabuleiro[0][2] == PLAYER_1) && (tabuleiro[1][2] == PLAYER_1) && (tabuleiro[2][2] == PLAYER_1)  ) || \
  153.  
  154.         ( (tabuleiro[0][0] == PLAYER_1) && (tabuleiro[1][1] == PLAYER_1) && (tabuleiro[2][2] == PLAYER_1)  ) || \
  155.         ( (tabuleiro[0][2] == PLAYER_1) && (tabuleiro[1][1] == PLAYER_1) && (tabuleiro[2][0] == PLAYER_1)  )    
  156.     )
  157.         return 1;
  158.        
  159.     else if(
  160.         ( (tabuleiro[0][0] == PLAYER_2) && (tabuleiro[0][1] == PLAYER_2) && (tabuleiro[0][2] == PLAYER_2)  ) || \
  161.         ( (tabuleiro[1][0] == PLAYER_2) && (tabuleiro[1][1] == PLAYER_2) && (tabuleiro[1][2] == PLAYER_2)  ) || \
  162.         ( (tabuleiro[2][0] == PLAYER_2) && (tabuleiro[2][1] == PLAYER_2) && (tabuleiro[2][2] == PLAYER_2)  ) || \
  163.  
  164.         ( (tabuleiro[0][0] == PLAYER_2) && (tabuleiro[1][0] == PLAYER_2) && (tabuleiro[2][0] == PLAYER_2)  ) || \
  165.         ( (tabuleiro[0][1] == PLAYER_2) && (tabuleiro[1][1] == PLAYER_2) && (tabuleiro[2][1] == PLAYER_2)  ) || \
  166.         ( (tabuleiro[0][2] == PLAYER_2) && (tabuleiro[1][2] == PLAYER_2) && (tabuleiro[2][2] == PLAYER_2)  ) || \
  167.  
  168.         ( (tabuleiro[0][0] == PLAYER_2) && (tabuleiro[1][1] == PLAYER_2) && (tabuleiro[2][2] == PLAYER_2)  ) || \
  169.         ( (tabuleiro[0][2] == PLAYER_2) && (tabuleiro[1][1] == PLAYER_2) && (tabuleiro[2][0] == PLAYER_2)  )    
  170.     )
  171.         return 2;
  172.        
  173.     else if( jogadas == 9 )
  174.         return -1;
  175.    
  176.     else
  177.         return 0;
  178.    
  179. };
  180.  
  181. void jogar( int x, int y )
  182. {
  183.     if( tabuleiro[x][y] == FREE_FIELD )
  184.     {
  185.         tabuleiro[x][y] = ((jogadas%2) == 0)?PLAYER_1:PLAYER_2;
  186.         jogadas++;
  187.     }
  188. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement