Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- char tabuleiro[3][3]; //A ser mostrado na tela.
- int tab_interno[3][3]; //Para controle interno.
- void criar_tab()
- {
- int x,y;
- for(x=0;x<3;x++)
- {
- for (y=0;y<3;y++)
- {
- tabuleiro[x][y]=' ';
- tab_interno[x][y]=0;
- }
- }
- }
- void limpar_tela()
- {
- if (system("cls"))
- {
- system("reset");
- }
- else
- {
- system ("cls");
- }
- }
- void mostrar_tabuleiro()
- {
- int x;
- cout << " 1 2 3" << endl;
- cout << " --- --- ---" << endl;
- for (x=0;x<3;x++)
- {
- cout << x+1 << " | " << tabuleiro[x][0] << " | " << tabuleiro[x][1] << " | " << tabuleiro[x][2] << " |" << endl;
- cout << " --- --- ---" << endl;
- }
- }
- int horizontal(int cy)
- {
- int soma=0, cx;
- for (cx=0;cx<3;cx++)
- {
- soma=soma + tab_interno[cx][cy];
- }
- if (soma==9)
- {
- return 1;
- }
- else if (soma==21)
- {
- return 2;
- }
- else
- {
- return 0;
- }
- }
- int vertical (int cx)
- {
- int cy, soma=0;
- for (cy=0;cy<3;cy++)
- {
- soma=soma + tab_interno[cx][cy];
- }
- if (soma==9)
- {
- return 1;
- }
- else if (soma==21)
- {
- return 2;
- }
- else
- {
- return 0;
- }
- }
- int diagonal_principal()
- {
- int soma=0, cx;
- for (cx=0;cx<3;cx++)
- {
- soma=soma + tab_interno[cx][cx];
- }
- if (soma==9)
- {
- return 1;
- }
- else if (soma==21)
- {
- return 2;
- }
- else
- {
- return 0;
- }
- }
- int diagonal_secundaria()
- {
- int soma=0, cx;
- for (cx=0;cx<3;cx++)
- {
- soma=soma + tab_interno[cx][2-cx];
- }
- if (soma==9)
- {
- return 1;
- }
- else if (soma==21)
- {
- return 2;
- }
- else
- {
- return 0;
- }
- }
- int cheque_velha(int cx, int cy)
- {
- int fim=0;
- //Checar se coordenada está nos lados cima, baixo, esquerda ou direita.
- if (((cx==0) && (cy==1)) || ((cx==1) && (cy==0)) || ((cx==2) && (cy==1)) || ((cx==1) && (cy==2)))
- {
- //Só dois cheques necessários. Horizontal e Vertical.
- fim=horizontal(cy);
- if (fim==0)
- {
- fim=vertical(cx);
- }
- return fim;
- }
- //Checar se nas pontas cima-esquerda, cima-direita, baixo-esquerda ou baixo-direita.
- else if (((cx==0) && (cy==0)) || ((cx==2) && (cy==2)) || ((cx==0) && (cy==2)) || ((cx==2) && (cy==0)))
- {
- //Três cheques são necessários. Vertical, Horizontal e Diagonal (Principal OU Secundária).
- //Checar se na diagonal principal.
- if (cx==cy)
- {
- fim=diagonal_principal();
- if (fim==0)
- {
- fim=horizontal(cy);
- }
- if (fim==0)
- {
- fim=vertical(cx);
- }
- return fim;
- }
- else //Diagonal secundária.
- {
- fim=diagonal_secundaria();
- if (fim==0)
- {
- fim=horizontal(cy);
- }
- if (fim==0)
- {
- fim=vertical(cx);
- }
- return fim;
- }
- }
- else
- {
- //Coordenada está no centro.
- //Quatro cheques são necessários. Vertical, Horizontal e Diagonais Principal e Secundária.
- fim=diagonal_principal();
- if (fim==0)
- {
- fim=diagonal_secundaria();
- }
- if (fim==0)
- {
- fim=horizontal(cy);
- }
- if (fim==0)
- {
- fim=vertical(cx);
- }
- return fim;
- }
- }
- int main()
- {
- srand(time(NULL));
- int x=0,y=0;
- int vencedor=0;
- //Criar e limpar os tabuleiros.
- criar_tab();
- int celulas_restantes=9;
- //Tabuleiros interno e externo criados, nada mais a ser feito. Vamos jogar!
- int fim=0;
- cout << "Digite 1 para comecar o jogo ou 0 para sair do programa." << endl;
- cin >> fim;
- do
- {
- while (fim==1 && vencedor==0)
- {
- //O jogo acontece aqui.
- limpar_tela();
- mostrar_tabuleiro();
- cout << "Digite uma coordenada no formato X(espaco)Y. E.g. 3 2" << endl;
- cin >> x >> y;
- x=x-1;
- y=y-1;
- //Checar se as coordenadas foram recebidas incorretamente.
- while ((x<0) || (y<0) || (x>2) || (y>2))
- {
- //Coordenadas inválidas por representarem um espaço fora do tabuleiro.
- cout << "Algo deu errado na digitacao das coordenadas. Tente de novo." << endl;
- cin >> x >> y;
- x=x-1;
- y=y-1;
- }
- //Checar se célula já está ocupada com um X ou O.
- while (tab_interno[x][y]!=0)
- {
- cout << "Esta celula contem uma jogada valida ja realizada. Digite outra coordenada" << endl;
- cin >> x >> y;
- x=x-1;
- y=y-1;
- //Checar novamente se as coordenadas foram recebidas incorretamente.
- while ((x<0) || (x>2) || (y<0) || (y>2))
- {
- cout << "Algo deu errado na digitacao das coordenadas. Tente de novo." << endl;
- cin >> x >> y;
- x=x-1;
- y=y-1;
- }
- }
- //Usuário fez jogada válida, coloque um X no tabuleiro e um 3 no controle.
- tabuleiro[x][y]='X';
- tab_interno[x][y]=3;
- celulas_restantes=celulas_restantes-1;
- //Checar por fim de jogo.
- if(celulas_restantes<5)
- {
- vencedor=cheque_velha(x, y);
- }
- //Vez do computador.
- if(celulas_restantes!=0 && vencedor==0)
- {
- do
- {
- //Sortear uma coordenada.
- x=rand()%3;
- y=rand()%3;
- //Checar se a célula sorteada está ocupada.
- }while(tab_interno[x][y]!=0); //Forçar o computador a selecionar apenas células vazias.
- //Preencher
- tabuleiro[x][y]='O';
- tab_interno[x][y]=7;
- celulas_restantes=celulas_restantes-1;
- //Checar por fim de jogo.
- if(celulas_restantes<5)
- {
- vencedor=cheque_velha(x, y);
- }
- }
- else //Não há mais células a ser preenchidas, finalize o jogo.
- {
- fim=0;
- }
- }
- //Pós-jogo. Verificar o ganhador.
- if (vencedor==1)
- {
- cout <<"Voce ganhou! Parabéns!" << endl;
- celulas_restantes=9;
- vencedor=0;
- cout << "Quer jogar de novo? Digite 1 para sim ou 0 para nao" << endl;
- cin >> fim;
- criar_tab();
- }
- else if (vencedor==2)
- {
- cout << "O computador ganhou esta. Tente de novo." << endl;
- celulas_restantes=9;
- vencedor=0;
- cout << "Quer jogar de novo? Digite 1 para sim ou 0 para nao" << endl;
- cin >> fim;
- criar_tab();
- }
- else
- {
- cout << "Deu empate..." << endl;
- celulas_restantes=9;
- vencedor=0;
- cout << "Quer jogar de novo? Digite 1 para sim ou 0 para nao" << endl;
- cin >> fim;
- criar_tab();
- }
- }while (fim!=0);
- }
Add Comment
Please, Sign In to add comment