#include <stdio.h>
#include <stdlib.h>
char tabuleiro[3][3];
int vitoria;
void deseja();
int give_me_a_random(){
int num;
num=rand() % 3;
return(num+1);
}
void limpar_tabuleiro(){
vitoria=0;
int il,ic;
fflush(stdin);
for(il=0;il<3;il++){
for(ic=0;ic<3;ic++){
tabuleiro[il][ic]='#';
}
}
}
void escrever_tabuleiro(){
system("cls");
system("color 0a");
printf("\n 1 2 3\n");
printf("\n 1 %c| %c |%c ",tabuleiro[0][0],tabuleiro[0][1],tabuleiro[0][2]);
printf("\n --|---|--");
printf("\n 2 %c| %c |%c ",tabuleiro[1][0],tabuleiro[1][1],tabuleiro[1][2]);
printf("\n --|---|--");
printf("\n 3 %c| %c |%c\n\n\n",tabuleiro[2][0],tabuleiro[2][1],tabuleiro[2][2]);
}
int checar_jogada(char jogador){
int teste =0;
int gambiarra;
//Coluuuuuuuuuuuunaaaaaaaaaaaaaaasssssss!!!
if(tabuleiro[0][2]==jogador){
teste++;
}
if(tabuleiro[1][2]==jogador){
teste++;
}
if(tabuleiro[2][2]==jogador){
teste++;
}
if(teste==3){
return(1);
goto fim;
}else{
teste=0;
}
if(tabuleiro[0][1]==jogador){
teste++;
}
if(tabuleiro[1][1]==jogador){
teste++;
}
if(tabuleiro[2][1]==jogador){
teste++;
}
if(teste==3){
return(1);
goto fim;
}else{
teste=0;
}
if(tabuleiro[0][0]==jogador){
teste++;
}
if(tabuleiro[1][0]==jogador){
teste++;
}
if(tabuleiro[2][0]==jogador){
teste++;
}
if(teste==3){
return(1);
goto fim;
}else{
teste=0;
}
//LINHASSSSSSSSSSSSSSSSSSS!!!!
if(tabuleiro[0][0]==jogador){
teste++;
}
if(tabuleiro[0][1]==jogador){
teste++;
}
if(tabuleiro[0][2]==jogador){
teste++;
}
if(teste==3){
return(1);
goto fim;
}else{
teste=0;
}
if(tabuleiro[1][0]==jogador){
teste++;
}
if(tabuleiro[1][1]==jogador){
teste++;
}
if(tabuleiro[1][2]==jogador){
teste++;
}
if(teste==3){
return(1);
goto fim;
}else{
teste=0;
}
if(tabuleiro[2][0]==jogador){
teste++;
}
if(tabuleiro[2][1]==jogador){
teste++;
}
if(tabuleiro[2][2]==jogador){
teste++;
}
if(teste==3){
return(1);
goto fim;
}else{
teste=0;
}
//DIAAGGgooooooooooooooonaaaaaaaaaaaaaaaaiiiiiiiiiiiiiiiiiiissssssss (PRINCIPAL)
if(tabuleiro[0][0]==jogador){
teste++;
}
if(tabuleiro[1][1]==jogador){
teste++;
}
if(tabuleiro[2][2]==jogador){
teste++;
}
if(teste==3){
return(1);
goto fim;
}else{
teste=0;
}
//SECUNDARIA
if(tabuleiro[2][0]==jogador){
teste++;
}
if(tabuleiro[1][1]==jogador){
teste++;
}
if(tabuleiro[0][2]==jogador){
teste++;
}
if(teste==3){
return(1);
goto fim;
}else{
teste=0;
return(0);
}
fim:
gambiarra=0;
}
char trocar_jogador(char jogador){
if(jogador=='X'){
return ('O');
}else{
return ('X');
}
}
void computar_jogada(char jogador, int linha,int coluna){
/*printf("%d",linha);
system("pause");
printf("%d",coluna);
system("pause");*/
tabuleiro[linha-1][coluna-1]=jogador;
}
int realizar_jogada(char jogador,int bot){
int l ,c;
escrever_tabuleiro();
PORINVALIDEZ:
printf("Vez do jogador ' %c '\n",jogador);
if((bot == 1)&&(jogador!='O')){
l=give_me_a_random();
c=give_me_a_random();
}else{
printf("Digite onde deseja marcar %c:\n",jogador);
scanf ("%d %d",&l,&c);
}
if((l<1||l>3)||(c<1||c>3)&&(bot==0)){
system("color 0c");
printf("Jogada invalida(Coordenada)\n");
system("pause");
escrever_tabuleiro();
fflush(stdin);
goto PORINVALIDEZ;
}else
if((l<1||l>3)||(c<1||c>3)&&(bot==1)&&(jogador='O')){
system("color 0c");
printf("Jogada invalida(Coordenada)\n");
system("pause");
escrever_tabuleiro();
fflush(stdin);
goto PORINVALIDEZ;
}else
if((l<1||l>3)||(c<1||c>3)&&(jogador=='X')&&(bot==1)){
escrever_tabuleiro();
fflush(stdin);
goto PORINVALIDEZ;
}
if ((tabuleiro[l-1][c-1]!='#')&&(bot==0)){
system("color 0c");
printf("Jogada invalida(Repetida)\n");
system("pause");
escrever_tabuleiro();
fflush(stdin);
goto PORINVALIDEZ;
}else
if((tabuleiro[l-1][c-1]!='#')&&(jogador=='X')&&(bot==1)){
escrever_tabuleiro();
fflush(stdin);
goto PORINVALIDEZ;
}else
if((tabuleiro[l-1][c-1]!='#')&&(jogador=='O')&&(bot==1)){
system("color 0c");
printf("Jogada invalida(Repetida)\n");
system("pause");
escrever_tabuleiro();
fflush(stdin);
goto PORINVALIDEZ;
}else{
computar_jogada(jogador,l,c);
}
return(checar_jogada(jogador));
}
void comecar_jogo(int bot){
char jogador;
int ok,teste,contador;
contador=0;
ok=0;
jogador='O';
do{
fflush(stdin);
teste=realizar_jogada(jogador,bot);
// printf("%d",teste);
contador++;
if(teste==0){
escrever_tabuleiro();
jogador=trocar_jogador(jogador);
//return (0);
}else{
escrever_tabuleiro();
printf("\n\n\n O JOGADOR %c VENCEU!\n\n\n",jogador);
ok=1;
vitoria=1;
//return(1);
}
if((contador>=9)&&(vitoria==0)){
printf("\n\n\n O JOGO EMPATOU!!!\n\n\n");
ok=1;
}
}while(ok==0);
fflush(stdin);
deseja();
}
/*
void comecar_jogo(){
char jogador;
int ok,teste,contador;
contador=0;
ok=0;
jogador='O';
do{
fflush(stdin);
teste=realizar_jogada(jogador);
contador++;
if(teste==0){
escrever_tabuleiro();
jogador=trocar_jogador(jogador);
}else{
escrever_tabuleiro();
printf("\n\n\n O JOGADOR %c VENCEU!\n\n\n",jogador);
ok=1;
}
if(contador>=9){
printf("\n\n\n O JOGO EMPATOU!!!\n\n\n");
ok=1;
}
}while(ok==0);
fflush(stdin);
deseja();
}
*/
void deseja(){
char escolha, bot;
int botp;
printf("Deseja iniciar um novo jogo? 's' ou 'n'?\n");
scanf("%c",&escolha);
switch(escolha){
case's':
printf("\n Deseja jogar contra bot ou player?\n");
printf("\n p - para player");
printf("\n b - para bot\n\n");
fflush(stdin);
scanf("%c",&bot);
if(bot=='p'){
botp=0;
}else{
botp=1;
}
system("cls");
limpar_tabuleiro();
comecar_jogo(botp);
fflush(stdin);
case 'n':
exit(0);
fflush(stdin);
default:
fflush(stdin);
deseja();
}
}
/*void deseja(){
char escolha;
printf("Deseja iniciar um novo jogo? 's' ou 'n'?\n");
scanf("%c",&escolha);
switch(escolha){
case's':
system("cls");
limpar_tabuleiro();
comecar_jogo();
fflush(stdin);
case 'n':
exit(0);
fflush(stdin);
default:
fflush(stdin);
deseja();
}
}*/
main(){
system("title JOGO DA VELHA");
system("color 0a");
printf("\n Bem Vindo ao Jogo da Velha!\n\n");
printf("\n Os comandos são simples: Deve se digitar a linha e a coluna separador por 1 caractere qualquer ex.'espaço'");
printf("\n Por exemplo: o Jogador O digita '2 2'");
printf("\n Com isso 'O' sera marcada na 'casa do meio';\n\n");
deseja();
}