Please help! Sudoku in C

Reply

Join Date: Oct 2006
Posts: 3
Reputation: simmyhp is an unknown quantity at this point 
Solved Threads: 0
simmyhp simmyhp is offline Offline
Newbie Poster

Please help! Sudoku in C

 
0
  #1
Oct 21st, 2006
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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 223
Reputation: Anonymusius is on a distinguished road 
Solved Threads: 10
Anonymusius's Avatar
Anonymusius Anonymusius is offline Offline
Posting Whiz in Training

Re: Please help! Sudoku in C

 
0
  #2
Oct 21st, 2006
I can't say much about the game yet, but you should use int main(void)

at least, I think.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 89
Reputation: TylerSBreton is an unknown quantity at this point 
Solved Threads: 3
TylerSBreton's Avatar
TylerSBreton TylerSBreton is offline Offline
Junior Poster in Training

Re: Please help! Sudoku in C

 
0
  #3
Oct 21st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 3
Reputation: simmyhp is an unknown quantity at this point 
Solved Threads: 0
simmyhp simmyhp is offline Offline
Newbie Poster

Re: Please help! Sudoku in C

 
0
  #4
Oct 22nd, 2006
Originally Posted by TylerSBreton View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Please help! Sudoku in C

 
0
  #5
Oct 22nd, 2006
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC