clorophilla

Jogo da Velha (Complete)

Oct 25th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. char tabuleiro[3][3]; //A ser mostrado na tela.
  8. int tab_interno[3][3]; //Para controle interno.
  9.  
  10. void criar_tab()
  11. {
  12.     int x,y;
  13.     for(x=0;x<3;x++)
  14.     {
  15.         for (y=0;y<3;y++)
  16.         {
  17.             tabuleiro[x][y]=' ';
  18.             tab_interno[x][y]=0;
  19.         }
  20.     }
  21. }
  22.  
  23. void limpar_tela()
  24. {
  25.     if (system("cls"))
  26.     {
  27.         system("reset");
  28.     }
  29.     else
  30.     {
  31.         system ("cls");
  32.     }
  33. }
  34.  
  35. void mostrar_tabuleiro()
  36. {
  37.     int x;
  38.     cout << "    1   2   3" << endl;
  39.     cout << "   --- --- ---" << endl;
  40.     for (x=0;x<3;x++)
  41.     {
  42.         cout << x+1 << " | " << tabuleiro[x][0] << " | " << tabuleiro[x][1] << " | " << tabuleiro[x][2] << " |" << endl;
  43.         cout << "   --- --- ---" << endl;
  44.     }
  45. }
  46.  
  47. int horizontal(int cy)
  48. {
  49.     int soma=0, cx;
  50.     for (cx=0;cx<3;cx++)
  51.     {
  52.         soma=soma + tab_interno[cx][cy];
  53.     }
  54.     if (soma==9)
  55.     {
  56.         return 1;
  57.     }
  58.     else if (soma==21)
  59.     {
  60.         return 2;
  61.     }
  62.     else
  63.     {
  64.         return 0;
  65.     }
  66. }
  67.  
  68. int vertical (int cx)
  69. {
  70.     int cy, soma=0;
  71.     for (cy=0;cy<3;cy++)
  72.     {
  73.         soma=soma + tab_interno[cx][cy];
  74.     }
  75.     if (soma==9)
  76.     {
  77.         return 1;
  78.     }
  79.     else if (soma==21)
  80.     {
  81.         return 2;
  82.     }
  83.     else
  84.     {
  85.         return 0;
  86.     }
  87. }
  88.  
  89. int diagonal_principal()
  90. {
  91.     int soma=0, cx;
  92.     for (cx=0;cx<3;cx++)
  93.     {
  94.         soma=soma + tab_interno[cx][cx];
  95.     }
  96.     if (soma==9)
  97.     {
  98.         return 1;
  99.     }
  100.     else if (soma==21)
  101.     {
  102.         return 2;
  103.     }
  104.     else
  105.     {
  106.         return 0;
  107.     }
  108. }
  109.  
  110. int diagonal_secundaria()
  111. {
  112.     int soma=0, cx;
  113.     for (cx=0;cx<3;cx++)
  114.     {
  115.         soma=soma + tab_interno[cx][2-cx];
  116.     }
  117.     if (soma==9)
  118.     {
  119.         return 1;
  120.     }
  121.     else if (soma==21)
  122.     {
  123.         return 2;
  124.     }
  125.     else
  126.     {
  127.         return 0;
  128.     }
  129. }
  130.  
  131. int cheque_velha(int cx, int cy)
  132. {
  133.     int fim=0;
  134.     //Checar se coordenada está nos lados cima, baixo, esquerda ou direita.
  135.     if (((cx==0) && (cy==1)) || ((cx==1) && (cy==0)) || ((cx==2) && (cy==1)) || ((cx==1) && (cy==2)))
  136.         {
  137.             //Só dois cheques necessários. Horizontal e Vertical.
  138.             fim=horizontal(cy);
  139.             if (fim==0)
  140.             {
  141.                 fim=vertical(cx);
  142.             }
  143.             return fim;
  144.         }
  145.         //Checar se nas pontas cima-esquerda, cima-direita, baixo-esquerda ou baixo-direita.
  146.         else if (((cx==0) && (cy==0)) || ((cx==2) && (cy==2)) || ((cx==0) && (cy==2)) || ((cx==2) && (cy==0)))
  147.         {
  148.             //Três cheques são necessários. Vertical, Horizontal e Diagonal (Principal OU Secundária).
  149.             //Checar se na diagonal principal.
  150.             if (cx==cy)
  151.             {
  152.                 fim=diagonal_principal();
  153.                 if (fim==0)
  154.                 {
  155.                     fim=horizontal(cy);
  156.                 }
  157.                 if (fim==0)
  158.                 {
  159.                     fim=vertical(cx);
  160.                 }
  161.                 return fim;
  162.             }
  163.             else //Diagonal secundária.
  164.             {
  165.                 fim=diagonal_secundaria();
  166.                 if (fim==0)
  167.                 {
  168.                     fim=horizontal(cy);
  169.                 }
  170.                 if (fim==0)
  171.                 {
  172.                     fim=vertical(cx);
  173.                 }
  174.                 return fim;
  175.             }
  176.         }
  177.         else
  178.         {
  179.             //Coordenada está no centro.
  180.             //Quatro cheques são necessários. Vertical, Horizontal e Diagonais Principal e Secundária.
  181.             fim=diagonal_principal();
  182.             if (fim==0)
  183.             {
  184.                 fim=diagonal_secundaria();
  185.             }
  186.             if (fim==0)
  187.             {
  188.                 fim=horizontal(cy);
  189.             }
  190.             if (fim==0)
  191.             {
  192.                 fim=vertical(cx);
  193.             }
  194.             return fim;
  195.         }
  196. }
  197.  
  198. int main()
  199. {
  200.     srand(time(NULL));
  201.     int x=0,y=0;
  202.     int vencedor=0;
  203.     //Criar e limpar os tabuleiros.
  204.  
  205.     criar_tab();
  206.     int celulas_restantes=9;
  207.  
  208.     //Tabuleiros interno e externo criados, nada mais a ser feito. Vamos jogar!
  209.     int fim=0;
  210.  
  211.     cout << "Digite 1 para comecar o jogo ou 0 para sair do programa." << endl;
  212.     cin >> fim;
  213.  
  214.     do
  215.     {
  216.         while (fim==1 && vencedor==0)
  217.         {
  218.             //O jogo acontece aqui.
  219.             limpar_tela();
  220.             mostrar_tabuleiro();
  221.  
  222.             cout << "Digite uma coordenada no formato X(espaco)Y. E.g. 3 2" << endl;
  223.             cin >> x >> y;
  224.             x=x-1;
  225.             y=y-1;
  226.  
  227.             //Checar se as coordenadas foram recebidas incorretamente.
  228.             while ((x<0) || (y<0) || (x>2) || (y>2))
  229.             {
  230.                 //Coordenadas inválidas por representarem um espaço fora do tabuleiro.
  231.                 cout << "Algo deu errado na digitacao das coordenadas. Tente de novo." << endl;
  232.                 cin >> x >> y;
  233.                 x=x-1;
  234.                 y=y-1;
  235.             }
  236.             //Checar se célula já está ocupada com um X ou O.
  237.             while (tab_interno[x][y]!=0)
  238.             {
  239.                 cout << "Esta celula contem uma jogada valida ja realizada. Digite outra coordenada" << endl;
  240.                 cin >> x >> y;
  241.                 x=x-1;
  242.                 y=y-1;
  243.                 //Checar novamente se as coordenadas foram recebidas incorretamente.
  244.                 while ((x<0) || (x>2) || (y<0) || (y>2))
  245.                 {
  246.                     cout << "Algo deu errado na digitacao das coordenadas. Tente de novo." << endl;
  247.                     cin >> x >> y;
  248.                     x=x-1;
  249.                     y=y-1;
  250.                 }
  251.             }
  252.  
  253.             //Usuário fez jogada válida, coloque um X no tabuleiro e um 3 no controle.
  254.             tabuleiro[x][y]='X';
  255.             tab_interno[x][y]=3;
  256.             celulas_restantes=celulas_restantes-1;
  257.  
  258.             //Checar por fim de jogo.
  259.             if(celulas_restantes<5)
  260.             {
  261.                 vencedor=cheque_velha(x, y);
  262.             }
  263.  
  264.             //Vez do computador.
  265.             if(celulas_restantes!=0 && vencedor==0)
  266.             {
  267.                 do
  268.                 {
  269.                     //Sortear uma coordenada.
  270.                     x=rand()%3;
  271.                     y=rand()%3;
  272.                     //Checar se a célula sorteada está ocupada.
  273.                 }while(tab_interno[x][y]!=0); //Forçar o computador a selecionar apenas células vazias.
  274.  
  275.                 //Preencher
  276.                 tabuleiro[x][y]='O';
  277.                 tab_interno[x][y]=7;
  278.                 celulas_restantes=celulas_restantes-1;
  279.  
  280.                 //Checar por fim de jogo.
  281.                 if(celulas_restantes<5)
  282.                 {
  283.                     vencedor=cheque_velha(x, y);
  284.                 }
  285.  
  286.             }
  287.             else //Não há mais células a ser preenchidas, finalize o jogo.
  288.             {
  289.                 fim=0;
  290.             }
  291.         }
  292.         //Pós-jogo. Verificar o ganhador.
  293.         if (vencedor==1)
  294.         {
  295.             cout <<"Voce ganhou! Parabéns!" << endl;
  296.             celulas_restantes=9;
  297.             vencedor=0;
  298.             cout << "Quer jogar de novo? Digite 1 para sim ou 0 para nao" << endl;
  299.             cin >> fim;
  300.             criar_tab();
  301.         }
  302.         else if (vencedor==2)
  303.         {
  304.             cout << "O computador ganhou esta. Tente de novo." << endl;
  305.             celulas_restantes=9;
  306.             vencedor=0;
  307.             cout << "Quer jogar de novo? Digite 1 para sim ou 0 para nao" << endl;
  308.             cin >> fim;
  309.             criar_tab();
  310.         }
  311.         else
  312.         {
  313.             cout << "Deu empate..." << endl;
  314.             celulas_restantes=9;
  315.             vencedor=0;
  316.             cout << "Quer jogar de novo? Digite 1 para sim ou 0 para nao" << endl;
  317.             cin >> fim;
  318.             criar_tab();
  319.         }
  320.     }while (fim!=0);
  321. }
Add Comment
Please, Sign In to add comment