Hi, I have written the code of checking already, but I have got two problems here.

1. I don't know why when I enter the number in, when it prints out the number, it becomes smilies or some kind of maths code. Please tell me what I have done wrongly and how should I correct it.

2. I don't know whether my checking part can really check the numbers have repeated or not. Please help me to have a look at it.

Thanks.

The following is my code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int h,i,j,k,p,r,s,x,y,z;
char matrix[10][10],answer[10][10],box[10];
char filename[30], filename1[30];
FILE *fp, *fp1, *fp2;
void welcome(void);
void rules(void);
void level(void);
void board(void);
void reprint(void);
void enter(void);
void check(void);
void repeat(void);
void choice(void);
void action(void);
void printans(void);
void compare(void);
void printboth(void);
void save(void);
main()
{
      welcome();
      rules();
}
void welcome()
{
     printf("Welcome to Sudoku Game Version 2.0.0.6!\n\n");
     printf("Thanks for playing this game!\n\nHope you enjoy!\n");
}
void rules()
{
     printf("\nRules to play:\n");
     printf("Enter a numerical digit from 1 through 9 starting with various digits given in some cells.\n");
     printf("Each row, column, and cell must contain only one instance of each numeral.\n");
     printf("Are you ready for the game? (1-Yes, 0-No)\n");
     scanf("%d", &x);
     if(x==1)
         level();
     else
         exit(1);
}
void level()
{
 printf("Which level do you want to play? (1-simple, 2-medium, 3-hard)\n");
 scanf("%d", &y);
 switch(y)
 {
  case 1:
   printf("Please choose which puzzle you want to play!\n");
   printf("Enter a no within 1-10. Add .txt after the no.\n");
   printf("Puzzle to play: (Enter no, eg. 1.txt)\n");
   scanf("%s", filename);
   board();
  break;
  case 2:
   printf("Please choose which puzzle you want to play.\n");
   printf("Enter a no within 11-20. Add .txt after the no.\n");
   printf("Puzzle to play: (Enter no, eg. 11.txt)\n");
   scanf("%s", filename);
   board();
  break;
  case 3:
   printf("Please choose which puzzle you want to play.\n");
   printf("Enter a no within 21-30. Add .txt after the no.\n");
   printf("Puzzle to play: (Enter no, eg. 21.txt)\n");
   board();
  break;
  default:
   printf("Wrong Entering!\nNo must be 1,2 or 3!\n");
   level();
    fclose(fp);
    }
}
void board()
{
 int d;
 fp=fopen(filename,"r");
    if(fp==NULL)
 {
    printf("Cannot open file!\n");
    level();
 }
    while((z=fgetc(fp))!=EOF){
         printf("|-|-|-|-|-|-|-|-|-|\n");
         for(i=1;i<10;i++)
         {
               for(j=1;j<=10;j++)
               {
                   z = fgetc(fp);
          printf("|%c",z);
                   matrix[i][j] = z;
               }
               for(d=i;d%3==0;d++)
               {
                   if(i!=9)
                   printf("|-|-|-|-|-|-|-|-|-|\n");
               }
         }
         printf("\n");
         printf("|-|-|-|-|-|-|-|-|-|\n");
    }
    enter();
}
void reprint()
{
     int d;
     
     printf("|-|-|-|-|-|-|-|-|-|\n");
     for(i=1;i<10;i++)
     {
             for(j=1;j<=10;j++)
             {
                     printf("|%c",matrix[i][j]);
             }
             
             for(d=i;d%3==0;d++)
             {
                   if(i!=9)
                   printf("|-|-|-|-|-|-|-|-|-|\n");
             }
         }
         printf("\n");
         printf("|-|-|-|-|-|-|-|-|-|\n");
}    
void enter()
{
 int a,b,c;
 char v;
 printf("Enter the position that you want to enter numbers: (eg. 1,1)\n");
 scanf("%d,%d", &a,&b);
 if(a<0 || a>10)
    {
           if(b<0 || b>10)
        {
                  printf("Wrong Entering!\n");
                  printf("You should enter position that is from 1-9!\n");
                  printf("Please enter the position again!\n");
                  reprint();
                  enter();
           }
    }
    if(matrix[a][b]!=' ')
 {
  printf("Wrong Entering!\n");
  printf("Position that entered should be without numbers!\n");
  printf("Please enter the position again!\n");
  reprint();
        enter();
 }
 if(matrix[a][b]==' ')
 {
  printf("Enter the number that you want to put at the position:\n");
  scanf("%d", &c);
  if(c<0 || c>10)
  {
   printf("Wrong entering!\nNumber should be 1-9!\n");
   reprint();
            enter();
  }
  else
  {
   int v = c;
            matrix[a][b] = v;
   reprint();
            for(i=1;i<10;i++)
            {
                    for(j=1;j<10;j++)
                    {
                            if(matrix[i][j]!=0)
                                     check();
                    }
            }
  }
 }
}
void check()
{
     int e=0,q,m,n;
     /*check each row*/
     for(i=1;i<10;i++)
     {
             for(j=1;j<10;j++)
             {
                     for(k=1;k<10;k++)
                     {
                             if(matrix[i][j]==k)
                             {
                                   box[k]=k;
                                   h += box[k];
                             }
                     }
                     if(h!=55)
                          repeat();
             }
     }
     /*check each column*/
     for(j=1;j<10;j++)
     {
             for(i=1;i<10;i++)
             {
                     for(k=1;k<10;k++)
                     {
                             if(matrix[i][j]==k)
                             {
                                   box[k]=k;
                                   h += box[k];
                             }
                     }
                     if(h!=55)
                          repeat();
             }
     }
     /*check each cell*/
     for(i=1;i<10;i*=3)
     {
             for(j=1;j<10;j*=3)
             {
                     do{
                               for(m=1;m<4;m++)
                               {
                                       for(n=1;n<4;n++)
                                       {
                                               for(k=1;k<10;k++)
                                               {
                                                       if(matrix[m][n]==k)
                                                           box[k]=k;
                                                       else
                                                       {
                                                           e++;
                                                           
                                                           if(e>10)
                                                               repeat();
                                                       }
                                                       h+=box[k];
                                                       
                                                       if(h!=55)
                                                            repeat();
                                               }
                                       }
                               }
                     }while(i%3==0);
             }
     }
     choice();
}
void repeat()
{
     printf("Number has repeated!\n");
     reprint();
     enter();
}
void choice()
{
     printf("What do you want to do now? (1-View answer, 2-Save, 3-Exit, 4-New game)\n");
     scanf("%d", &y);
     
     switch(y)
     {
          case 1:
               printans();
          break;
          
          case 2:
               save();
          break;
          
          case 3:
               exit(1);
          break;
          
          case 4:
               level();
          break;
          
          default:
               printf("Wrong entering!\n");
               choice();
     }
}
void action()
{
    printf("What do you want to do now? (1-Save, 2-Return, 3-Exit, 4-New game)\n");
    scanf("%d", &y);
     
    switch(y)
    {
        case 1:
             save();
        break;
        
        case 2:
             return;
        
        case 3:
             exit(1);
        
        case 4:
             level();
        break;
        
        default:
             printf("Wrong entering!\n");
             action();
    }
}
void printans()
{
    int f,t,u;
    
    t = strlen(filename);
    strncpy(filename1, filename, t-4);
    
    fp2=filename1;
    u = fgetc(fp2);
    u += 100;
    strncpy(filename1, u, 3);
    strncat(filename1, ".txt", 4);
    
    fp1=fopen(filename1,"r");
    if(fp1==NULL)
 {
    printf("please wait...\nCalculating the answer.\n");
    printans();
 }
    while((p=fgetc(fp1))!=EOF)
    {
         printf("|-|-|-|-|-|-|-|-|-|\n");
         for(r=1;r<10;r++)
         {
               for(s=1;s<=10;s++)
               {
                   p = fgetc(fp1);
          printf("|%c",p);
                   answer[r][s] = p;
               }
               
               for(f=i;f%3==0;f++)
               {
                   if(r!=9)
                   printf("|-|-|-|-|-|-|-|-|-|\n");
               }
         }
         printf("\n");
         printf("|-|-|-|-|-|-|-|-|-|\n");
    }
    compare();
    action();
    fclose(fp1);
}
void compare()
{
     int g;
     
     for(r=1;r<10;r++)
     {
             for(s=1;s<10;s++)
             {
                     r=i;
                     s=j;
                     
                     if(answer[r][s]==matrix[i][j])
                          g+1;                     
             }
     }
     printf("You have %d mark(s).\n", g);
     
}
void printboth()
{
     int d,f;
     
     printf("Sudoku puzzle\n");
     reprint();
         
     printf("Solved sudoku\n");
     printans();
     
     compare();
     action();
}
void save()
{
     printf("Save puzzle as: (Enter filename, eg. puzzle1.txt)\n");
     scanf("%s", filename1);
     
     fp2=fopen(filename1,"w");
     
     if(fp2==NULL)
     {
                  printf("Cannot open file!\n");
                  choice();
     }
     
     printf("Save method: 1-Only puzzle, 2-Only answer, 3-Puzzle and answer\n");
     scanf("%d", &y);
     
     switch(y)
     {
              case 1:
                   board();
              break;
              
              case 2:
                   printans();
              break;
              
              case 3:
                   printboth();
              break;
              
              default:
                   printf("Wrong entering!\n");
                   exit(1);
     }
     action();     
}

Thanks, friends!

Ancient Dragon commented: thanks for using code tags correctly +8

Recommended Answers

All 3 Replies

>>1. I don't know why when I enter the number in, when it prints out the number

which function has that problem?

>>2. I don't know whether my checking part can really check the numbers have repeated or not

Why not make that check at the time a number is entered, then you won't need another function to do that.

And BTW: Thanks for using code tags to make your program easier to read. :)

As Ancient Dragon implied, with 400+ lines of code, could you just post the section(s) you're having trouble with?

1. I don't know why when I enter the number in, when it prints out the number, it becomes smilies or some kind of maths code. Please tell me what I have done wrongly and how should I correct it.

Dozens of printfs, can't tell where.

There is one thing I did notice, though. You're using strncpy() and strncmp():

strncpy(filename1, u, 3);
    strncat(filename1, ".txt", 4);

Run this program and see why you should switch to strcpy() and strcmp():

#include <stdio.h>
#include <string.h>

int main()
{
    char *t1 = "123456789\0ABCDEFGH";
    char *t2 = "zxcvbn";
    char *t3 = "asdfg";
    
    printf("%s\n", t1);
    strncpy(t1,t2,3);
    strncat(t1,t3,3);
    printf("%s\n", t1);
    
    return (0);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.