Hi, Friends!

I am doing my last year term project that needs me to use C and write a sudoku game. However, I don't know how to write the check part, can some of you help me a bit?

Here is my code:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int x,y;
int matrix[9][9], box[9];
char filename[30];
FILE *fp;
void welcome(void);
void rules(void);
void level(void);
void board(char);
void enter(int);
void check(int);
main()
{
      welcome();
      rules();
}
void welcome()
{
     printf("Welcome to Sudoku Game Version 2.0\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(filename);
  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(filename);
  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(filename);
  break;
  default:
   printf("Wrong Entering! No must be 1,2 or 3!\n");
   level();
    fclose(fp);
    }
}
void board(char filename[30])
{
 int i,j,z;
 fp=fopen(filename,"r");
    if(fp==NULL)
 {
    printf("Cannot open file!\n");
    level();
 }
    while((z=fgetc(fp))!=EOF){
                                                            
         printf("|-|-|-|-|-|-|-|-|-|\n");
         for(i=0;i<9;i++)
         {
               for(j=0;j<=9;j++)
               {
                   z = fgetc(fp);
         
          printf("|%c",z);
                   
                   matrix[i][j] = z;
             
               }                 
         }
    printf("\n");     
    printf("|-|-|-|-|-|-|-|-|-|\n");
    }    
}
void enter(int matrix[9][9])
{
     int a,b,c;
     
     printf("Enter the position that you want to enter numbers: (eg. 1,1)\n");
     scanf("%d,%d", &a,&b);
     
     if(matrix[a][b]==0)
     {
             printf("Wrong entering!\n");
             printf("Please enter the position again!\n");
             enter(matrix);
     }
     
     if(matrix[a][b]!=0)
     {
             printf("Enter the number that you want to put at the position:\n");
             scanf("%d", &c);
             
             matrix[a][b] = c;
             
             check();      
     }
}

Thank you friends!

I can't say much about the game yet, but you should use int main(void) at least, I think.

Well, If you are checking after each entered number (which is what it looks like) you need to compare the entered number to the answer matrix (you could have this as a variable in the program somewhere) for the indicies input by the user. Return 1 if the same, 0 if not.

There are many ways to do this. This is just one possibility.

Well, If you are checking after each entered number (which is what it looks like) you need to compare the entered number to the answer matrix (you could have this as a variable in the program somewhere) for the indicies input by the user. Return 1 if the same, 0 if not.

There are many ways to do this. This is just one possibility.

Hi, I am not checking for the right answer of the sudoku, but I am just checking whether the number that enter by the player has repeated at rows, colums or boxes at the sudoku. Anyway, thanks.

One bug in your program, when the user enters x and y coordinate where he wants to put his number, you dont check to see if those coordinates are within bound (i.e. a < 9 and a > 0) and the same with b. This may allow user to write at a location which doesnt belong to him.

Also you dont check whether the number entered by the user lies in the range of 1 to 9. Perform all the bound checks properly to ensure that your program doesnt crash and perform in unexpected manner.

The check function can be something simple like: (have not checked the function, just my logic)

{
    int index = 1 ;
    for( index = 1; index <= 9; ++index ) {
        if( matrix[row][index] == value_entered ) {
            if( index == col ) {
                continue ;
            }
            else {
                printf( "The value is already present" ) ;
                return 0 ; // return false, condtition not satisfied
            }
        }
    }

    for( index = 1; index <= 9; ++index ) {
        if( matrix[index][col] == value_entered ) {
            if( index == row ) {
                continue ;
            }
            else {
                printf( "The value is already present" ) ;
                return 0 ; // return false, condtition not satisfied
            }
        }
    }

    return 1 ; // value not present already, so continue adding the new value to matrix
}

Hope it helped, bye.

What about checking within each respective box for an additional value? So in the middle square of nine values how would you check that each value is not repeated. I know two nested for loops could cycle through the smaller box of nine coordinates, but I'm not sure how I would move the starting point from the valued entered to the top left corner of the smaller box of nine.

Essentially, what is the mathematical relationship between any coordinate in the smaller box of nine and the upper left hand coordinate of the smaller box of nine values?

I use a "step" kind of logic. Each check function returns either 1 (checked ok, or 0, check failed)

if(checkRow(row, col)) {
  if(checkCol(row, col)) {  //only if row checks out OK, check the col 
    if(checkBox(row, col)) { //only if row and col check out OK, check box
       number is OK
    }
  }
}

To speed things up a bit, I use a 1D Sudoku array, but have also used 2D Sudoku grid arrays in the past.

To handle the checkBox() logic, I use two tables. The first one simply tells me, for any sqr, what box it's in - simple. The second table tells me what 8 squares need to be checked for any box number. (one sqr is the digit being checked).

It's a little extra work to set it up, but it makes your code shorter, and your program, a bit faster.

If you have specific questions, you may want to visit:
http://www.setbb.com/phpbb/?mforum=sudoku

They've forgotten more about programming Sudoku, then most of us will ever care to learn. It's not a very active board, however.

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.