0
I’m making a Sudoku that the computer plays, I already have all the logic programmed, but I can’t print the answer in the file and on the screen, if someone can take a look and tested in my code I would be very grateful.
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#define N 9
void facil(void);
void medio(void);
void dificil(void);
int isValueInLineOrColumn(int val, char 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]);
}
}
}
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(isValueInLineOrColumn(val, mesa, i, j))
fprintf(arquivo ,"%c", mesa[i][j]=0);
else
fprintf(arquivo ,"%c", mesa[i][j]=val);
}
}
}
}
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]);
}
}
}
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))
fprintf(arquivo ,"%c", mesa[i][j]=0);
else
fprintf(arquivo ,"%c", mesa[i][j]=val);
}
}
}
}
void dificil(void)
{
FILE *arquivo;
char mesa[N][N];
int i, j;
srand( (unsigned)time(NULL) );
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))
fprintf(arquivo ,"%c", mesa[i][j]=0);
else
fprintf(arquivo ,"%c", mesa[i][j]=val);
}
}
}
}
int isValueInLineOrColumn(int val, char 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;
}
easy file:
---------
|0|2|4|0|
|1|0|0|3|
|4|0|0|2|
|0|1|3|0|
---------
middle file:
-------------------
|0|2|0|0|3|9|0|0|6|
|0|4|0|1|0|0|5|0|0|
|0|3|2|0|1|0|4|0|0|
|8|0|0|2|0|0|0|0|5|
|0|0|7|0|0|0|9|6|0|
|3|0|0|0|5|0|0|0|4|
|4|0|0|7|8|0|0|5|0|
-------------------
hard file:
-------------------
|8|0|0|4|0|6|0|0|7|
|0|0|0|0|0|0|4|0|0|
|5|0|9|0|3|0|7|8|0|
|0|0|0|0|7|0|0|0|0|
|0|4|8|0|2|0|1|0|3|
|0|5|2|0|0|0|0|9|0|
|0|0|1|0|0|0|0|0|0|
|3|0|0|9|0|2|0|0|5|
-------------------
Leonardo what’s the problem? Don’t print anything to the screen? Gives error?
– Jorge B.
Leonardo, your role in creating sudokus should be the same for all difficulties
void criarSudoku(int dificuldade);
with that signature.– Jorge B.
Buying Jorge, however as you say, do not print on the screen neither the matrix nor the answer I made with file call with the matrix already created I will post above, on the function I will arrange.
– Leonardo V. De Gasperin
http://ideone.com/F7JHXU
– Jorge B.
You are misreading the file, and typing as well. Because you are reading the
|
and the-
. Do you understand?– Jorge B.
I answered here: http://answall.com/a/40489/7210
– Jorge B.