This code does not have any errors, however the program does not solve the game (it does not print answers on the screen):
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#define N 9
#define D 4
void facil(void);
void medio(void);
void dificil(void);
int isValueInLineOrColumn(int val, int arr[N][N], int line, int column);
int isValueInLineOrColum(int val, int arr[D][D], int line, int column);
int conferencia;
int main()
{
int opc=0;
printf("1-facil\t2-medio\t3-dificil");
scanf("%d", &opc);
conferencia=opc;
switch(opc)/*para a função do nivel escolhido*/
{
case 1:
facil();
break;
case 2:
medio();
break;
case 3:
dificil();
break;
}
return 0;
}
void facil(void)
{
FILE *arquivo;
int mesa[D][D];
int i, j;
arquivo=fopen("facil.txt","r");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
while((mesa[i][j]=fgetc(arquivo))!=EOF)
{
fflush(stdin);
printf( "%c", mesa[i][j]);
}
}
}
fclose(arquivo);
arquivo=fopen("facil.txt","a");
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
{
int val = 1+rand()%N;
while((mesa[i][j]=0))
{
if(isValueInLineOrColum(val, mesa, i, j))
mesa[i][j]=0;
else
mesa[i][j]=val;
}
}
}
fclose(arquivo);
}
void medio(void)
{
FILE *arquivo;
int mesa[N][N];
int i, j;
arquivo=fopen("medio.txt","r");
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
{
while((mesa[i][j]=fgetc(arquivo))!=EOF)
{
fflush(stdin);
printf( "%c", mesa[i][j]);
}
}
}
fclose(arquivo);
arquivo=fopen("medio.txt","a");
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
{
int val = 1+rand()%N;
while((mesa[i][j]=0))
{
if(isValueInLineOrColumn(val, mesa, i, j))
mesa[i][j]=0;
else
mesa[i][j]=val;
}
}
}
fclose(arquivo);
}
void dificil(void)
{
FILE *arquivo;
int mesa[N][N];
int i, j;
arquivo=fopen("dificil.txt","r");
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
{
while((mesa[i][j]=fgetc(arquivo))!=EOF)
{
fflush(stdin);
printf( "%c", mesa[i][j]);
}
}
}
fclose(arquivo);
arquivo=fopen("dificil.txt","a");
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
{
int val = 1+rand()%N;
while((mesa[i][j]=0))
{
if(isValueInLineOrColumn(val, mesa, i, j))
mesa[i][j]=0;
else
mesa[i][j]=val;
}
}
}
fclose(arquivo);
}
int isValueInLineOrColumn(int val, int arr[N][N], int line, int column)
{
int i;
for (i=0; i < N; i++)
{
if(arr[line][i] == val)
return 1;
if (arr[i][column] == val)
return 1;
}
return 0;
}
int isValueInLineOrColum(int val, int arr[D][D], int line, int column)
{
int i;
for (i=0; i < N; i++)
{
if(arr[line][i] == val)
return 1;
if (arr[i][column] == val)
return 1;
}
return 0;
}
Down to original post
I have this sudoku here and I need to make the PC play alone. I made the rules but I can not do a way to print this and save it to the file.
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#define N 9
void facil(void);
void medio(void);
void dificil(void);
void play(void);
int isValueInLineOrColumn(int val, int arr[N][N], int line, int column);
int conferencia;
int main()
{
int opc=0;
printf("1-facil\t2-medio\t3-dificil");
scanf("%d", &opc);
conferencia=opc;
switch(opc)/*para a função do nivel escolhido*/
{
case 1:
facil();
break;
case 2:
medio();
break;
case 3:
dificil();
break;
}
return 0;
}
void facil(void)
{
FILE *arquivo;
char mesa[4][4];
int i, j;
arquivo=fopen("facil.txt","r");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
while((mesa[i][j]=fgetc(arquivo))!=EOF)
{
fflush(stdin);
printf("%c", mesa[i][j]);
}
}
}
}
void medio(void)
{
FILE *arquivo;
char mesa[N][N];
int i, j;
arquivo=fopen("medio.txt","r");
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
{
while((mesa[i][j]=fgetc(arquivo))!=EOF)
{
fflush(stdin);
printf("%c", mesa[i][j]);
}
}
}
}
void dificil(void)
{
FILE *arquivo;
char mesa[N][N];
int i, j;
arquivo=fopen("dificil.txt","r");
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
{
while((mesa[i][j]=fgetc(arquivo))!=EOF)
{
fflush(stdin);
printf("%c", mesa[i][j]);
}
}
}
}
int isValueInLineOrColumn(int val, int arr[N][N], int line, int column)
{
int i,j;
for (i=0; i < N; i++)
{
if(arr[line][i] == val)
return 1;
if (arr[i][column] == val)
return 1;
}
return 0;
}
You have to continue on file.