Boa tarde,
Estou com alguma dúvidas em relação a um exercício que estou realizando, onde pede para criar o Jogo da velha com a opção de salvar e poder continuar da onde havia parado. O código do jogo já está pronto, porém, falta a parte de salvar e continuar do ponto em que parei, onde estou quebrando a cabeça de como vou fazer.
Preciso criar uma função para salvar e depois chamar no main?
Se puderem me dar uma dica de como posso fazer e de uma forma fácil, agradeço.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <locale.h>
void display(char t[3][3]){
printf("=====================\n");
printf("==| JOGO DA VELHA |==\n");
printf("=====================\n");
printf("\n\n 0 1 2 Y\n");
printf(" 0 %c | %c | %c \n",t[0][0],t[0][1],t[0][2]);
printf(" ---|---|--- \n ");
printf(" 1 %c | %c | %c \n",t[1][0],t[1][1],t[1][2]);
printf(" ---|---|--- \n ");
printf(" 2 %c | %c | %c \n",t[2][0],t[2][1],t[2][2]);
printf("\nX\n");
}
void teste_trinca(int *x, int *y, int *jg, int *teste, int velha, char t[3][3])
{
//TESTE HORIZONTAL//
if(*x==0)
if((t[0][0]==t[0][1]) && (t[0][1]==t[0][2]))
{
display(t);
printf("\n Jogador %i GANHOU!! \n",*jg);
*teste=0;
}
if(*x==1)
if((t[1][0]==t[1][1]) && (t[1][1]==t[1][2]))
{
display(t);
printf("\n Jogador %i GANHOU!! \n",*jg);
*teste=0;
}
if(*x==2)
if((t[2][0]==t[2][1]) && (t[2][1]==t[2][2]))
{
display(t);
printf("\n Jogador %i GANHOU!! \n",*jg);
*teste=0;
}
//TESTE VERTICAL//
if(*y==0)
if((t[0][0]==t[1][0]) && (t[1][0]==t[2][0]))
{
display(t);
printf("\n Jogador %i GANHOU!! \n",*jg);
*teste=0;
}
if(*y==1)
if((t[0][1]==t[1][1]) && (t[1][1]==t[2][1]))
{
display(t);
printf("\n Jogador %i GANHOU!! \n",*jg);
*teste=0;
}
if(*y==2)
if((t[0][2]==t[1][2]) && (t[1][2]==t[2][2]))
{
display(t);
printf("\n Jogador %i GANHOU!! \n",*jg);
*teste=0;
}
//TESTA DIAGONAL//
if(((*x==0) && (*y==0))||((*x==1) && (*y==1))||((*x==2) && (*y==2)))
{
if((t[0][0]==t[1][1]) && (t[0][0]==t[2][2]))
{
display(t);
printf("\n Jogador %i GANHOU!! \n",*jg);
*teste=0;
}
}
if(((*x==0)&&(*y==2))||((*x==1)&&(*y==1))||((*x==2)&&(*y==0)))
{
if((t[0][2]==t[1][1]) && (t[0][2]==t[2][0]))
{
display(t);
printf("\n Jogador %i GANHOU!! \n",*jg);
*teste=0;
}
}
// TESTE VELHA//
if(velha >= 9 && *teste == 1)
{
printf("\n DEU VELHA !");
*teste = 0;
}
}
void play1(char t[3][3], int *x, int *y)
{
display(t);
printf("\nJogador 1: Digite a coordenada X: ");
scanf("%i",&*x);
printf("\nJogador 1: Digite a coordenada Y: ");
scanf("%i",&*y);
}
void play2(int *x, int *y, char t[3][3])
{
display(t);
printf("\nJogador 2: Digite a coordenada X: ");
scanf("%i",&*x);
printf("\nJogador 2: Digite a coordenada Y: ");
scanf("%i",&*y);
}
main()
{
char tela[3][3];
int teste = 1;
int x, y;
int velha = 0;
int jogador;
int jogada;
setlocale(LC_ALL, "Portuguese");
int menu=1;
do{
system("cls");
fflush(stdin);
printf("\n\n -----------------------");
printf("\n 1 - Novo Jogo ");
printf("\n 2 - Continuar Jogo ");
printf("\n 3 - Sair ");
printf("\n\n Escolha uma opção: ");
scanf("%i",&menu);
fflush(stdin);
switch (menu){
case 1: system("cls"); //Novo Jogo
dados=fopen("velha.txt","w");
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
tela[i][j]=' ';
while(teste!=0)
{
if(teste!=0)
{
jogada = 0;
jogador = 1;
play1(tela, &x, &y);
printf("\nX/Y: ");
printf("%i %i", x, y);
getch();
while (jogada == 0)
{
if(tela[x][y]==' ')
{
tela[x][y]='X';
system("cls");
velha++;
teste_trinca (&x, &y, &jogador, &teste, velha, tela);
jogada = 1;
}
else
{
system("cls");
printf("\nPosição já utilizada! \n");
printf("\nPressione qualquer tecla para entrar novo valor\n");
getch();
system("cls");
play1(tela, &x, &y);
}
}
}
if(teste!=0)
{
jogada = 0;
jogador = 2;
play2(&x, &y, tela);
printf("\nX/Y: ");
printf("%i %i", x, y);
getch();
while (jogada == 0)
{
if(tela[x][y]==' ')
{
velha++;
tela[x][y]='O';
system("cls");
teste_trinca(&x, &y, &jogador, &teste, velha, tela);
jogada = 1;
}
else
{
system("cls");
printf("\nPosição já utilizada! \n");
printf("\nPressione qualquer tecla para entrar novo valor!!\n");
getch();
system("cls");
play2(&x, &y, tela);
}
}
}
}
case 3: system("cls");
printf("\nSistema encerrado!!!");
getch();
break;
}
}while(menu!=3);
getch();
}