Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.93 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int jogada = 1;
  4. int inGame = 1;
  5. char A1, A2, A3, B1, B2, B3, C1, C2, C3;
  6.  
  7.  
  8. void limparTela(){
  9.     int i;
  10.     for(i = 0; i < 100; i++){
  11.         printf("\n");
  12.     }
  13. }
  14.  
  15. int menu(){
  16.     int opcao;
  17.  
  18.     printf("\n\n\n\n\n\n1 - Jogar\n");
  19.     printf("0 - Sair\n");
  20.     printf("\nEscolha uma opcao: ");
  21.  
  22.     scanf("%d", &opcao);
  23.  
  24.     switch(opcao){
  25.         case 1:
  26.             inGame = 1;
  27.             break;
  28.         case 0:
  29.             inGame = 0;
  30.             break;
  31.         default:
  32.             limparTela();
  33.             printf("Opcao inválida. \n");
  34.     }
  35.  
  36.     return opcao;
  37. }
  38.  
  39. void zerarValores(){
  40.     A1 = ' ';
  41.     A2 = ' ';
  42.     A3 = ' ';
  43.     B1 = ' ';
  44.     B2 = ' ';
  45.     B3 = ' ';
  46.     C1 = ' ';
  47.     C2 = ' ';
  48.     C3 = ' ';
  49. }
  50.  
  51. void zerarPartida(){
  52.     zerarValores();
  53.     jogada = 1;
  54. }
  55.  
  56. int inserirValor(int n, int c){
  57.  
  58.     char playerSimbolo;
  59.  
  60.     if((n < 1 || n > 3) || (c < 1 || c > 3) ){
  61.         printf("Valor inserido invalido. Número fora do limite\n");
  62.         return 0;
  63.     }else {
  64.  
  65.  
  66.         int linha = n;
  67.         int coluna = c;
  68.  
  69.         if (jogada % 2 == 0) {
  70.             playerSimbolo = 'X';
  71.         } else {
  72.             playerSimbolo = 'O';
  73.         }
  74.  
  75.         printf("\n\nLinha: %d, Coluna: %c\n\n", linha, coluna);
  76.  
  77.         if (linha == 1 && coluna == 1 && A1 == ' ') {
  78.             A1 = playerSimbolo;
  79.             return 1;
  80.         } else if (linha == 1 && coluna == 2 && B1 == ' ') {
  81.             B1 = playerSimbolo;
  82.             return 1;
  83.         } else if (linha == 1 && coluna == 3 && C1 == ' ') {
  84.             C1 = playerSimbolo;
  85.             return 1;
  86.         } else if (linha == 2 && coluna == 1 && A2 == ' ') {
  87.             A2 = playerSimbolo;
  88.             return 1;
  89.         } else if (linha == 2 && coluna == 2 && B2 == ' ') {
  90.             B2 = playerSimbolo;
  91.             return 1;
  92.         } else if (linha == 2 && coluna == 3 && C2 == ' ') {
  93.             C2 = playerSimbolo;
  94.             return 1;
  95.         } else if (linha == 3 && coluna == 1 && A3 == ' ') {
  96.             A3 = playerSimbolo;
  97.             return 1;
  98.         } else if (linha == 3 && coluna == 2 && B3 == ' ') {
  99.             B3 = playerSimbolo;
  100.             return 1;
  101.         } else if (linha == 3 && coluna == 3 && C3 == ' ') {
  102.             C3 = playerSimbolo;
  103.             return 1;
  104.         } else {
  105.             printf("Valor inserido invalido. \n");
  106.             return 0;
  107.         }
  108.  
  109.     }
  110.  
  111. }
  112.  
  113. void verificarVitoria(){
  114.  
  115.     //Verificar se jogador 1 venceu
  116.  
  117.     if( (A1 == 'O' && B1 == 'O' && C1 == 'O') ||
  118.         (A2 == 'O' && B2 == 'O' && C2 == 'O') ||
  119.         (A3 == 'O' && B3 == 'O' && C3 == 'O') ||
  120.         (A1 == 'O' && A2 == 'O' && A3 == 'O') ||
  121.         (B1 == 'O' && B2 == 'O' && B3 == 'O') ||
  122.         (C1 == 'O' && C2 == 'O' && C3 == 'O') ||
  123.         (A1 == 'O' && B2 == 'O' && C3 == 'O') ||
  124.         (A3 == 'O' && B2 == 'O' && C1 == 'O')) {
  125.  
  126.         printf("\n\nJogador 1 venceu!\n");
  127.         zerarPartida();
  128.     }
  129.  
  130.     //Verificar se jogador 2 venceu
  131.  
  132.     if( (A1 == 'X' && B1 == 'X' && C1 == 'X') ||
  133.         (A2 == 'X' && B2 == 'X' && C2 == 'X') ||
  134.         (A3 == 'X' && B3 == 'X' && C3 == 'X') ||
  135.         (A1 == 'X' && A2 == 'X' && A3 == 'X') ||
  136.         (B1 == 'X' && B2 == 'X' && B3 == 'X') ||
  137.         (C1 == 'X' && C2 == 'X' && C3 == 'X') ||
  138.         (A1 == 'X' && B2 == 'X' && C3 == 'X') ||
  139.         (A3 == 'X' && B2 == 'X' && C1 == 'X')) {
  140.  
  141.         printf("\n \n Jogador 2 venceu! Jogador 1 é horrível\n");
  142.         zerarPartida();
  143.     }
  144.  
  145.     if(A1 != ' ' && A2 != ' ' && A3 != ' ' && B1 != ' ' && B2 != ' ' && B3 != ' ' &&
  146.        C1 != ' ' && C2 != ' ' && C3 != ' '){
  147.  
  148.         printf("\n \n Empatou, seus lixos!\n");
  149.         zerarPartida();
  150.     }
  151.  
  152. }
  153.  
  154. void jogar(){
  155.  
  156.     int linha, coluna, jogador;
  157.     int resultado = 0;
  158.  
  159.     if(jogada % 2 == 0){
  160.         jogador = 2;
  161.     }else{
  162.         jogador = 1;
  163.     }
  164.  
  165.     printf("Vez do jogador: %d\n\n\n\n\n", jogador);
  166.  
  167.     while(resultado != 1){
  168.         printf("Insira a linha e a coluna: \n");
  169.         scanf("%d %d", &linha, &coluna);
  170.  
  171.         resultado = inserirValor(linha, coluna);
  172.     }
  173.  
  174.     jogada = jogada + 1;
  175.  
  176. }
  177.  
  178.  
  179. void desenhar(){
  180.  
  181.     printf("   |   |   \n");
  182.     printf(" %c | %c | %c \n", A1, B1, C1);
  183.     printf("---|---|---\n");
  184.     printf(" %c | %c | %c \n", A2, B2, C2);
  185.     printf("---|---|---\n");
  186.     printf(" %c | %c | %c \n", A3, B3, C3);
  187.     printf("   |   |   \n");
  188.  
  189. }
  190.  
  191.  
  192. int main(){
  193.  
  194.     zerarValores();
  195.     limparTela();
  196.  
  197.     while (inGame != 0){
  198.         if(jogada == 1){
  199.  
  200.             if(menu() == 1){
  201.                 limparTela();
  202.                 desenhar();
  203.                 jogar();
  204.                 limparTela();
  205.                 desenhar();
  206.             }else{
  207.                 break;
  208.             }
  209.  
  210.         }else{
  211.             jogar();
  212.             limparTela();
  213.             desenhar();
  214.             verificarVitoria();
  215.         }
  216.  
  217.     }
  218.  
  219.     return 0;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement