Advertisement
Guest User

jogodavelha

a guest
Apr 17th, 2012
1,256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.70 KB | None | 0 0
  1. /* ***********************************
  2.    * Autor: Renato R. Leme
  3.    * Data: 14/04/2012 (atualizado em 17/04/2012)
  4.    * e-mail: rntreisleme@gmail.com
  5.    * site: http://argcv.wordpress.com
  6.    ***********************************
  7.    Qualquer dúvida ou sugestão, pode entrar em contato comigo pelo meu e-mail
  8.    ou pelo meu blog.
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. #define LINHAS  3
  15. #define COLUNAS 3
  16. #define CASAS   9
  17.  
  18. void init_tabuleiro(void);
  19. void mostra_tabuleiro(void);
  20. void jogada_player(void);
  21. void jogada_cpu(int rodada);
  22. int update_tabuleiro(int x, int y); // retorna 0 se a operação for bem sucedida
  23. int testa_vencedor(void); // 2 - vitória cpu, 1 - vitória player, 0 - nada aconteceu
  24. int velha(void); // conta o número de casas vazias no tabuleiro
  25.  
  26. enum vez { PLAYER, CPU } jogador;
  27. enum niveis { NOOB, MEDIO, GLADIADOR, ZEUS } nivel;
  28.  
  29. int tabuleiro[LINHAS][COLUNAS];
  30.  
  31. void main() {
  32.     int dificuldade, escolha, status;
  33.     static int rodada;
  34.     rodada = 0;
  35.     jogador = PLAYER; // seta o primeiro jogador para player
  36.     init_tabuleiro(); // inicia o tabuleiro com 0s
  37.  
  38.     while(1) {
  39.         __fpurge(stdin); // limpa o buffer do teclado
  40.         printf("\nEscolha o nível em que deseja jogar \n(1 - Noob, 2 - Médio, 3 - Gladiador, 4 - Zeus): ");
  41.         scanf("%d", &dificuldade);
  42.         if (dificuldade == 1) { nivel = NOOB; break; }
  43.         else if (dificuldade == 2) { nivel = MEDIO; break; }
  44.         else if (dificuldade == 3) { nivel = GLADIADOR; break; }
  45.         else if (dificuldade == 4) { nivel = ZEUS; break; }
  46.         else printf("\nEscolha invalida!");
  47.     }
  48.  
  49.     while(1) {
  50.         ++rodada;
  51.         if (jogador == PLAYER) {
  52.             if (!velha()) break;
  53.             mostra_tabuleiro();
  54.             jogada_player();
  55.             status = testa_vencedor();
  56.             if (status) break;
  57.         }
  58.         if (jogador == CPU) {
  59.             if (!velha()) break;
  60.             jogada_cpu(rodada);
  61.             status = testa_vencedor();
  62.             if (status) break;
  63.         }
  64.     }
  65.  
  66.     mostra_tabuleiro();
  67.  
  68.     if (status == 1) printf("\nParabéns, você venceu!\n");
  69.     else if (status == 2) printf("\nVocê perdeu! :(\n");
  70.     else printf("\nDeu velha!\n");
  71.  
  72.     while(1) {
  73.         __fpurge(stdin);
  74.         printf("\nJogar novamente? (1 = sim / 2 = não) : ");
  75.         scanf("%d", &escolha);
  76.         if (escolha == 1) main();
  77.         else if (escolha == 2) exit(0);
  78.         else printf("\nEscolha invalida!");
  79.     }
  80. }
  81.  
  82. void init_tabuleiro() {
  83.     register int i;
  84.     int *p;
  85.     p = (int *) tabuleiro;
  86.  
  87.     for (i = 0; i < CASAS; i++ ) *(p+i) = 0;
  88. }
  89.  
  90. int velha() {
  91.     register int i;
  92.     int *p, zeros = 0;
  93.     p = (int *) tabuleiro;
  94.  
  95.     for (i = 0; i < CASAS; i++ ) if (*(p+i) == 0) zeros++;
  96.  
  97.     if (!zeros) return 0;
  98.     else return 1;
  99. }
  100.  
  101. void mostra_tabuleiro() {
  102.     register int i, j;
  103.     char m_tabuleiro[LINHAS][COLUNAS];
  104.  
  105.     for (i = 0; i < 3; i++) {
  106.         for (j = 0; j < 3; j++) {
  107.             if (tabuleiro[i][j] == 0) m_tabuleiro[i][j] = ' ';
  108.             if (tabuleiro[i][j] == 1) m_tabuleiro[i][j] = 'X';
  109.             if (tabuleiro[i][j] == 2) m_tabuleiro[i][j] = 'O';
  110.         }
  111.     }
  112.  
  113.     printf("\n");
  114.  
  115.     for (i = 0; i < 3; i++) {
  116.         for (j = 0; j < 3; j++) {
  117.             if (i < 2) printf("|_%c_|", m_tabuleiro[i][j]);
  118.             else printf("| %c |", m_tabuleiro[i][j]);
  119.         }
  120.         printf("\n");
  121.     }
  122. }
  123.  
  124. int update_tabuleiro(int x, int y) {
  125.     if (jogador == PLAYER) {
  126.             tabuleiro[x][y] = 1;
  127.             jogador = CPU;
  128.             return 0;
  129.         }
  130.     if (jogador == CPU) {
  131.         if (tabuleiro[x][y] != 0) return 1;
  132.         else {
  133.             tabuleiro[x][y] = 2;
  134.             jogador = PLAYER;
  135.             return 0;
  136.         }
  137.     }
  138. }
  139.  
  140. void jogada_player() {
  141.     int x, y;
  142.     __fpurge(stdin);
  143.     printf("\nDigite a linha e a coluna em que deseja jogar: ");
  144.     scanf("%d %d", &x, &y);
  145.  
  146.     x--, y--;
  147.  
  148.     if (tabuleiro[x][y] != 0 || x < 0 || x > 2 || y < 0 || y > 2) {
  149.         printf("\nJogada inválida.\n");
  150.         jogada_player();
  151.     }
  152.     else update_tabuleiro(x, y);
  153. }
  154.  
  155. void jogada_cpu(int rodada) {
  156.     register int i;
  157.     int x, y;
  158.     static int diagonais; // será alterada caso o player comece a jogar pelas diagonais
  159.     int ad; // ataque e defesa. Quando 'ad' vale 2, a cpu testa a possibilidade de ataque.
  160.             // se valer 1, ela testa por defesa.
  161.  
  162.     diagonais = (rodada == 1) ? 0 : diagonais; // será zerada toda vez que o jogo reinicia
  163.  
  164.     // série de testes que verificará se há possibilidade de o jogador ou a CPU vencer o jogo em
  165.     // alguma linha, alguma coluna ou alguma diagonal.
  166.     // serão executados de acordo com o nivel do jogo.
  167.  
  168.     if (nivel == MEDIO || nivel == GLADIADOR || nivel == ZEUS) {
  169.         for (ad = 2; ad >= 1; ad--) {
  170.             for (i = 0; i < 3; i++) { // para todas as linhas e colunas
  171.                 if (tabuleiro[i][1] == ad && tabuleiro[i][2] == ad) if (!update_tabuleiro(i, 0)) return;
  172.                 if (tabuleiro[i][0] == ad && tabuleiro[i][2] == ad) if (!update_tabuleiro(i, 1)) return;
  173.                 if (tabuleiro[i][0] == ad && tabuleiro[i][1] == ad) if (!update_tabuleiro(i, 2)) return;
  174.                 if (tabuleiro[1][i] == ad && tabuleiro[2][i] == ad) if (!update_tabuleiro(0, i)) return;
  175.                 if (tabuleiro[0][i] == ad && tabuleiro[2][i] == ad) if (!update_tabuleiro(1, i)) return;
  176.                 if (tabuleiro[0][i] == ad && tabuleiro[1][i] == ad) if (!update_tabuleiro(2, i)) return;
  177.             }
  178.  
  179.             // para as diagonais
  180.             if (tabuleiro[0][0] == ad && tabuleiro[2][2] == ad) if (!update_tabuleiro(1, 1)) return;
  181.             if (tabuleiro[0][2] == ad && tabuleiro[2][0] == ad) if (!update_tabuleiro(1, 1)) return;
  182.             if (tabuleiro[0][2] == ad && tabuleiro[1][1] == ad) if (!update_tabuleiro(2, 0)) return;
  183.             if (tabuleiro[2][2] == ad && tabuleiro[1][1] == ad) if (!update_tabuleiro(0, 0)) return;
  184.             if (tabuleiro[2][0] == ad && tabuleiro[1][1] == ad) if (!update_tabuleiro(0, 2)) return;
  185.             if (tabuleiro[0][0] == ad && tabuleiro[1][1] == ad) if (!update_tabuleiro(2, 2)) return;
  186.         }
  187.  
  188.         if (nivel == GLADIADOR || nivel == ZEUS) {
  189.             if (rodada == 1 && tabuleiro[1][1] == 1) if (!update_tabuleiro(2, 0)) return;
  190.             if ((!diagonais && rodada == 2) && tabuleiro[0][2] == 1) if (!update_tabuleiro(0, 0)) return;
  191.  
  192.             if (tabuleiro[0][1] == 1  || tabuleiro[1][0] == 1 || tabuleiro[1][2] == 1 || tabuleiro[2][1] == 1) {
  193.             if (rodada == 1 && !update_tabuleiro(1, 1)) return;
  194.                 if (rodada == 2) {
  195.                     if (tabuleiro[1][2] == 1 || tabuleiro[1][0] == 1) {
  196.                         if (tabuleiro[2][0] == 1 && !update_tabuleiro(2, 2)) return;
  197.                         if (tabuleiro[0][0] == 1 && !update_tabuleiro(0, 2)) return;
  198.                         if (tabuleiro[0][1] == 1 && !update_tabuleiro(0, 0)) return;
  199.                         if (tabuleiro[2][1] == 1 && !update_tabuleiro(2, 0)) return;
  200.                     }
  201.                     if (tabuleiro[0][1] == 1 || tabuleiro[2][1] == 1) {
  202.                         if ((tabuleiro[2][0] == 1 || tabuleiro[0][0] == 1) && !update_tabuleiro(1, 2)) return;
  203.                         if ((tabuleiro[2][2] == 1 || tabuleiro[0][2] == 1) && !update_tabuleiro(1, 0)) return;
  204.                     }
  205.                 }
  206.             }
  207.         }
  208.  
  209.         if (nivel == ZEUS) {
  210.             if (tabuleiro[0][0] == 1 || tabuleiro[0][2] == 1 || tabuleiro[2][0] == 1 || tabuleiro[2][2] == 1) {
  211.                 diagonais++;
  212.                 if (!update_tabuleiro(1, 1)) return;
  213.                 if (rodada == 2) if (!update_tabuleiro(1, 0)) return;
  214.             }
  215.         }
  216.     }
  217.  
  218.     // caso nenhum dos testes acima sejam satisfeitos, a cpu joga aleatoriamente
  219.  
  220.     srand(time(NULL));
  221.  
  222.     while(1) {
  223.         x = rand() % 3;
  224.         y = rand() % 3;
  225.         if (!update_tabuleiro(x,y)) return;
  226.     }
  227.  
  228. }
  229.  
  230. int testa_vencedor() {
  231.     register int i;
  232.     int a = (jogador == CPU) ? 1 : 2; // 'a' varia de acordo com quem o chama (player ou cpu)
  233.  
  234.     for (i = 0; i < 3; i++) if (tabuleiro[i][0] == a && tabuleiro[i][1] == a && tabuleiro[i][2] == a) return a; // testa linha
  235.     for (i = 0; i < 3; i++) if (tabuleiro[0][i] == a && tabuleiro[1][i] == a && tabuleiro[2][i] == a) return a; // testa coluna
  236.     if (tabuleiro[2][0] == a && tabuleiro[1][1] == a && tabuleiro[0][2] == a) return a; // testa diagonal 1
  237.     if (tabuleiro[0][0] == a && tabuleiro[1][1] == a && tabuleiro[2][2] == a) return a; // testa diagonal 2
  238.  
  239.     return 0; // se não houver retorno até aqui, é sinal de que ninguém venceu. Portanto, a função retorna 0
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement