943,973 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6981
  • C RSS
Oct 21st, 2006
0

Please help! Sudoku in C

Expand Post »
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:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. int x,y;
  5. int matrix[9][9], box[9];
  6. char filename[30];
  7. FILE *fp;
  8. void welcome(void);
  9. void rules(void);
  10. void level(void);
  11. void board(char);
  12. void enter(int);
  13. void check(int);
  14. main()
  15. {
  16. welcome();
  17. rules();
  18. }
  19. void welcome()
  20. {
  21. printf("Welcome to Sudoku Game Version 2.0\n\n");
  22. printf("Thanks for playing this game!\n\nHope you enjoy!\n");
  23. }
  24. void rules()
  25. {
  26. printf("\nRules to play:\n");
  27. printf("Enter a numerical digit from 1 through 9 starting with various digits given in some cells.\n");
  28. printf("Each row, column, and cell must contain only one instance of each numeral.\n");
  29. printf("Are you ready for the game? (1 - Yes, 0 - No)\n");
  30. scanf("%d", &x);
  31. if(x==1)
  32. level();
  33. else
  34. exit(1);
  35. }
  36. void level()
  37. {
  38. printf("Which level do you want to play? (1-simple, 2-medium, 3-hard)\n");
  39. scanf("%d", &y);
  40. switch(y)
  41. {
  42. case 1:
  43. printf("Please choose which puzzle you want to play!\n");
  44. printf("Enter a no within 1-10. Add .txt after the no.\n");
  45. printf("Puzzle to play: (Enter no, eg. 1.txt)\n");
  46. scanf("%s", filename);
  47. board(filename);
  48. break;
  49. case 2:
  50. printf("Please choose which puzzle you want to play.\n");
  51. printf("Enter a no within 11-20. Add .txt after the no.\n");
  52. printf("Puzzle to play: (Enter no, eg. 11.txt)\n");
  53. scanf("%s", filename);
  54. board(filename);
  55. break;
  56. case 3:
  57. printf("Please choose which puzzle you want to play.\n");
  58. printf("Enter a no within 21-30. Add .txt after the no.\n");
  59. printf("Puzzle to play: (Enter no, eg. 21.txt)\n");
  60. board(filename);
  61. break;
  62. default:
  63. printf("Wrong Entering! No must be 1,2 or 3!\n");
  64. level();
  65. fclose(fp);
  66. }
  67. }
  68. void board(char filename[30])
  69. {
  70. int i,j,z;
  71. fp=fopen(filename,"r");
  72. if(fp==NULL)
  73. {
  74. printf("Cannot open file!\n");
  75. level();
  76. }
  77. while((z=fgetc(fp))!=EOF){
  78.  
  79. printf("|-|-|-|-|-|-|-|-|-|\n");
  80. for(i=0;i<9;i++)
  81. {
  82. for(j=0;j<=9;j++)
  83. {
  84. z = fgetc(fp);
  85.  
  86. printf("|%c",z);
  87.  
  88. matrix[i][j] = z;
  89.  
  90. }
  91. }
  92. printf("\n");
  93. printf("|-|-|-|-|-|-|-|-|-|\n");
  94. }
  95. }
  96. void enter(int matrix[9][9])
  97. {
  98. int a,b,c;
  99.  
  100. printf("Enter the position that you want to enter numbers: (eg. 1,1)\n");
  101. scanf("%d,%d", &a,&b);
  102.  
  103. if(matrix[a][b]==0)
  104. {
  105. printf("Wrong entering!\n");
  106. printf("Please enter the position again!\n");
  107. enter(matrix);
  108. }
  109.  
  110. if(matrix[a][b]!=0)
  111. {
  112. printf("Enter the number that you want to put at the position:\n");
  113. scanf("%d", &c);
  114.  
  115. matrix[a][b] = c;
  116.  
  117. check();
  118. }
  119. }

Thank you friends!
Similar Threads
Reputation Points: 18
Solved Threads: 0
Newbie Poster
simmyhp is offline Offline
3 posts
since Oct 2006
Oct 21st, 2006
0

Re: Please help! Sudoku in C

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

at least, I think.
Reputation Points: 129
Solved Threads: 11
Posting Whiz in Training
Anonymusius is offline Offline
223 posts
since Aug 2006
Oct 21st, 2006
0

Re: Please help! Sudoku in C

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.
Reputation Points: 25
Solved Threads: 3
Junior Poster in Training
TylerSBreton is offline Offline
89 posts
since Oct 2006
Oct 22nd, 2006
0

Re: Please help! Sudoku in C

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.
Reputation Points: 18
Solved Threads: 0
Newbie Poster
simmyhp is offline Offline
3 posts
since Oct 2006
Oct 22nd, 2006
0

Re: Please help! Sudoku in C

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)

  1. {
  2. int index = 1 ;
  3. for( index = 1; index <= 9; ++index ) {
  4. if( matrix[row][index] == value_entered ) {
  5. if( index == col ) {
  6. continue ;
  7. }
  8. else {
  9. printf( "The value is already present" ) ;
  10. return 0 ; // return false, condtition not satisfied
  11. }
  12. }
  13. }
  14.  
  15. for( index = 1; index <= 9; ++index ) {
  16. if( matrix[index][col] == value_entered ) {
  17. if( index == row ) {
  18. continue ;
  19. }
  20. else {
  21. printf( "The value is already present" ) ;
  22. return 0 ; // return false, condtition not satisfied
  23. }
  24. }
  25. }
  26.  
  27. return 1 ; // value not present already, so continue adding the new value to matrix
  28. }

Hope it helped, bye.
Last edited by ~s.o.s~; Oct 22nd, 2006 at 2:17 pm.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Oct 12th, 2010
0
Re: Please help! Sudoku in C
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
patticus is offline Offline
2 posts
since Oct 2010
Oct 12th, 2010
0
Re: Please help! Sudoku in C
I use a "step" kind of logic. Each check function returns either 1 (checked ok, or 0, check failed)

  1. if(checkRow(row, col)) {
  2. if(checkCol(row, col)) { //only if row checks out OK, check the col
  3. if(checkBox(row, col)) { //only if row and col check out OK, check box
  4. number is OK
  5. }
  6. }
  7. }
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.
Last edited by Adak; Oct 12th, 2010 at 10:46 pm.
Reputation Points: 416
Solved Threads: 181
Nearly a Posting Virtuoso
Adak is offline Offline
1,463 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Error in For Statement
Next Thread in C Forum Timeline: need help here...about functions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC