Sudoku does not print answers on screen

Asked

Viewed 299 times

-2

This code has no error, but the program does not solve the game (does not print the 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;
}

Below the original post

I have this sudoku here and I need to get the PC to play by itself. I made the rules but I can’t make a means to print it 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 go on file.

  • 1

    Don’t forget that every random generation of numbers doesn’t work

  • 2

    There are more rules of sudoku... You’d better read

2 answers

2

You are misreading the file, and typing as well. Because you are reading the | and the -. Do you understand?

Your file has to only have numbers to read and write, like,:

easy file

0240
1003
4002
0130

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

I also made an improvement in the code: http://ideone.com/F7JHXU

  • this kind of mistake is my worst dyslexic but I’m training it. I’ll test it here

  • then Play B. it still does not solve the sudoku there is no reason to print in the file the answers tb I will change to r at the opening of the archive

  • bash: line 1: 229 segmentation fault (core dumped) ./program your gave it there and showed nothing, I do not know what it is, but errors and warnings had not

  • I must have forgotten something, then I confirm

  • 1

    ok then, no problem I am stuck in the program because I made the corrections in my code and now it has no error and it only prints the q has inside the txt and does not solve Go =/

  • 1

    @Leonardov.Degasperin where is the part that solves the game?

Show 1 more comment

0

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.