Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* CABEÇALHO
- Arquivo: LISTA 05 - Exercicio 15.c
- Objetivo: Validação de um jogo da velha.
- Autor(a): Ramon Borges
- */
- #include <stdio.h>
- int main()
- {
- char JogoDaVelha[3][3], vencedor;
- int i, j, contX = 0, contY = 0;
- printf("***PREENCHA O JOGO DA VELHA***\n");
- for(i = 0; i < 3; i++)
- for(j = 0; j < 3; j++)
- {
- printf("\nMarque X ou O: ");
- scanf(" %c", &JogoDaVelha[i][j]);
- }
- //LINHAS
- for(i = 0; i < 3; i++)
- {
- for(j = 0; j < 3; j++)
- {
- if(JogoDaVelha[i][j] == 'X')
- contX++;
- else if(JogoDaVelha[i][j] == 'O')
- contY++;
- else
- break;
- }
- if(contX == 3)
- {
- printf("\nJogador X venceu!!!");
- return 0;
- }
- else if(contY == 3)
- {
- printf("\nJogador Y venceu!!!");
- return 0;
- }
- contX = contY = 0;
- }
- //COLUNAS
- for(i = 0; i < 3; i++)
- {
- for(j = 0; j < 3; j++)
- {
- if(JogoDaVelha[j][i] == 'X')
- contX++;
- else if(JogoDaVelha[j][i] == 'O')
- contY++;
- else
- break;
- }
- if(contX == 3)
- {
- printf("\nJogador X venceu!!!");
- return 0;
- }
- else if(contY == 3)
- {
- printf("\nJogador Y venceu!!!");
- return 0;
- }
- contX = contY = 0;
- }
- //DIAGONAL1
- i = j = 0;
- while(j < 4)
- {
- if(JogoDaVelha[i][j] == 'X')
- contX++;
- else if(JogoDaVelha[i][j] == 'O')
- contY++;
- i++;
- j++;
- }
- if(contX == 3)
- {
- printf("\nJogador X venceu!!!");
- return 0;
- }
- else if(contY == 3)
- {
- printf("\nJogador Y venceu!!!");
- return 0;
- }
- contX = contY = 0;
- //DIAGONAL2
- i = 0;
- j = 2;
- while(j >= 0)
- {
- if(JogoDaVelha[i][j] == 'X')
- contX++;
- else if(JogoDaVelha[i][j] == 'O')
- contY++;
- i++;
- j--;
- }
- if(contX == 3)
- {
- printf("\nJogador X venceu!!!");
- return 0;
- }
- else if(contY == 3)
- {
- printf("\nJogador O venceu!!!");
- return 0;
- }
- return 99;
- }
Advertisement
Add Comment
Please, Sign In to add comment