Sudoku does not print screen responses

-2

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.

    
asked by anonymous 10.11.2014 / 21:47

2 answers

2

You're reading badly from the file, and writing it too. Because you're reading | and - . Do you understand?

Your file must have only the numbers to read and write, type:

easy file

0240
1003
4002
0130

Of course, you can also read how the files are currently, but give it a little more work.

I also did an improvement on the code: link

    
14.11.2014 / 16:29
0

It seems to me that your code is "almost there". Use fprintf to write to a file. It works the same way as printf , but has one more parameter that is the file. Remember to use fclose to close the open files.

    
12.11.2014 / 01:17