update,
This commit is contained in:
9
banson_hker/job1/references/S10/Executable_program/1.txt
Normal file
9
banson_hker/job1/references/S10/Executable_program/1.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
..86.2.1.
|
||||
3.157.8..
|
||||
.6.....29
|
||||
5..8.1.36
|
||||
.3..6..5.
|
||||
68.9.5..1
|
||||
95.....4.
|
||||
..4.965.3
|
||||
.1.2.46..
|
@@ -0,0 +1,9 @@
|
||||
..8..15..
|
||||
.4.37..1.
|
||||
5.19..2.4
|
||||
7...9.18.
|
||||
.2.1.6.5.
|
||||
.15.8...3
|
||||
9.4..23.8
|
||||
.8..43.2.
|
||||
..68..7..
|
@@ -0,0 +1,9 @@
|
||||
398421576
|
||||
642375819
|
||||
571968234
|
||||
763594182
|
||||
829136457
|
||||
415287693
|
||||
954712368
|
||||
187643925
|
||||
236859741
|
@@ -0,0 +1,9 @@
|
||||
.3.8...9.
|
||||
...2...13
|
||||
..27.645.
|
||||
.63......
|
||||
7.......9
|
||||
......86.
|
||||
.945.86..
|
||||
17...4...
|
||||
.5...2.7.
|
@@ -0,0 +1,9 @@
|
||||
437851296
|
||||
685249713
|
||||
912736458
|
||||
863927145
|
||||
741685329
|
||||
529413867
|
||||
294578631
|
||||
176394582
|
||||
356182974
|
@@ -0,0 +1,9 @@
|
||||
1.7....95
|
||||
...58...7
|
||||
598.1...6
|
||||
...6...21
|
||||
2.31.76.9
|
||||
81...5...
|
||||
3...2.964
|
||||
4...73...
|
||||
98....3.2
|
@@ -0,0 +1,9 @@
|
||||
127364895
|
||||
634589217
|
||||
598712436
|
||||
749638521
|
||||
253147689
|
||||
816295743
|
||||
375821964
|
||||
462973158
|
||||
981456372
|
@@ -0,0 +1,9 @@
|
||||
.8.1.7.9.
|
||||
...3.4...
|
||||
3.9...6.1
|
||||
..87.91..
|
||||
1.......7
|
||||
..48.62..
|
||||
4.5...8.2
|
||||
...9.2...
|
||||
.2.4.1.3.
|
@@ -0,0 +1,9 @@
|
||||
582167394
|
||||
617394528
|
||||
349528671
|
||||
258739146
|
||||
163245987
|
||||
974816253
|
||||
495673812
|
||||
731982465
|
||||
826451739
|
@@ -0,0 +1,9 @@
|
||||
52...17..
|
||||
3..78...5
|
||||
.....91..
|
||||
..2.6...4
|
||||
.1.....9.
|
||||
7...2.6..
|
||||
..93.....
|
||||
8...94..3
|
||||
..35...67
|
@@ -0,0 +1,9 @@
|
||||
526431789
|
||||
391786425
|
||||
478259136
|
||||
932165874
|
||||
615847392
|
||||
784923651
|
||||
169372548
|
||||
857694213
|
||||
243518967
|
@@ -0,0 +1,9 @@
|
||||
.691.5..2
|
||||
..2473...
|
||||
.8.6.....
|
||||
..5...719
|
||||
.........
|
||||
416...5..
|
||||
.....8.9.
|
||||
...7412..
|
||||
2..9.637.
|
@@ -0,0 +1,9 @@
|
||||
769185432
|
||||
152473968
|
||||
384629157
|
||||
825364719
|
||||
937512846
|
||||
416897523
|
||||
571238694
|
||||
693741285
|
||||
248956371
|
@@ -0,0 +1,9 @@
|
||||
..21.6...
|
||||
.4....15.
|
||||
75...32..
|
||||
2.63...9.
|
||||
....8....
|
||||
.1...23.7
|
||||
..38...12
|
||||
65.....4.
|
||||
...6.97..
|
@@ -0,0 +1,9 @@
|
||||
832156974
|
||||
649728153
|
||||
751493268
|
||||
286374591
|
||||
397581426
|
||||
514962387
|
||||
973845612
|
||||
165237849
|
||||
428619735
|
@@ -0,0 +1,9 @@
|
||||
57.....21
|
||||
..98..7..
|
||||
2..9.7...
|
||||
9...3.17.
|
||||
....1....
|
||||
.61.9...4
|
||||
...5.4..6
|
||||
..7..32..
|
||||
84.....17
|
@@ -0,0 +1,9 @@
|
||||
578346921
|
||||
439821765
|
||||
216957438
|
||||
984635172
|
||||
725418693
|
||||
361792854
|
||||
192574386
|
||||
657183249
|
||||
843269517
|
@@ -0,0 +1,9 @@
|
||||
.951.387.
|
||||
.........
|
||||
37.....65
|
||||
..97.86..
|
||||
.6..5..2.
|
||||
..49.65..
|
||||
12.....49
|
||||
.........
|
||||
.538.721.
|
@@ -0,0 +1,9 @@
|
||||
495163872
|
||||
681572493
|
||||
372489165
|
||||
519728634
|
||||
867354921
|
||||
234916587
|
||||
128635749
|
||||
746291358
|
||||
953847216
|
@@ -0,0 +1,9 @@
|
||||
.9.1.7.8.
|
||||
...6.2...
|
||||
4.3...6.1
|
||||
..23.85..
|
||||
3.......9
|
||||
..72.14..
|
||||
7.5...2.4
|
||||
...7.4...
|
||||
.1.8.5.9.
|
@@ -0,0 +1,9 @@
|
||||
596147382
|
||||
871632945
|
||||
423589671
|
||||
142398567
|
||||
358476129
|
||||
967251438
|
||||
785963214
|
||||
239714856
|
||||
614825793
|
@@ -0,0 +1,9 @@
|
||||
798642315
|
||||
321579864
|
||||
465318729
|
||||
547821936
|
||||
139467258
|
||||
682935471
|
||||
956783142
|
||||
274196583
|
||||
813254697
|
9
banson_hker/job1/references/S10/Executable_program/2.txt
Normal file
9
banson_hker/job1/references/S10/Executable_program/2.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
..3..15..
|
||||
.4.39..8.
|
||||
6.15..4.3
|
||||
8...3.96.
|
||||
.7.1.2.5.
|
||||
.64.5...7
|
||||
4.8..32.9
|
||||
.3..18.4.
|
||||
..29..6..
|
@@ -0,0 +1,9 @@
|
||||
.8.2....4
|
||||
...6...27
|
||||
23.8.....
|
||||
.5.12.9.8
|
||||
.........
|
||||
1.3.48.7.
|
||||
.....2.89
|
||||
46...5...
|
||||
7....3.6.
|
@@ -0,0 +1,9 @@
|
||||
986271354
|
||||
541639827
|
||||
237854196
|
||||
654127938
|
||||
872396541
|
||||
193548672
|
||||
315462789
|
||||
469785213
|
||||
728913465
|
@@ -0,0 +1,9 @@
|
||||
.4.8.5.6.
|
||||
..27.41..
|
||||
5.......2
|
||||
..3.7.4..
|
||||
9.......6
|
||||
..7.5.3..
|
||||
3.......4
|
||||
..86.15..
|
||||
.9.2.7.3.
|
@@ -0,0 +1,9 @@
|
||||
741825963
|
||||
632794185
|
||||
589163742
|
||||
263978451
|
||||
954312876
|
||||
817456329
|
||||
376589214
|
||||
428631597
|
||||
195247638
|
@@ -0,0 +1,9 @@
|
||||
8...1.29.
|
||||
.9.86.1..
|
||||
.....4...
|
||||
.52...3.4
|
||||
6.......9
|
||||
4.1...85.
|
||||
...4.....
|
||||
..8.27.3.
|
||||
.39.8...2
|
@@ -0,0 +1,9 @@
|
||||
864715293
|
||||
395862147
|
||||
217394568
|
||||
952678314
|
||||
683541729
|
||||
471239856
|
||||
726453981
|
||||
148927635
|
||||
539186472
|
@@ -0,0 +1,9 @@
|
||||
..21...69
|
||||
6..5.....
|
||||
.8.......
|
||||
..9.4..5.
|
||||
.576.928.
|
||||
.6..1.3..
|
||||
.......4.
|
||||
.....2..7
|
||||
83...71..
|
@@ -0,0 +1,9 @@
|
||||
572183469
|
||||
693574812
|
||||
184926573
|
||||
319248756
|
||||
457639281
|
||||
268715394
|
||||
725361948
|
||||
941852637
|
||||
836497125
|
@@ -0,0 +1,9 @@
|
||||
.6...2...
|
||||
4..16.35.
|
||||
8.1....9.
|
||||
.92.4....
|
||||
...3.7...
|
||||
....8.62.
|
||||
.4....7.3
|
||||
.73.95..2
|
||||
...6...4.
|
@@ -0,0 +1,9 @@
|
||||
369852471
|
||||
427169358
|
||||
851734296
|
||||
192546837
|
||||
586327914
|
||||
734981625
|
||||
945218763
|
||||
673495182
|
||||
218673549
|
@@ -0,0 +1,9 @@
|
||||
.....7..4
|
||||
86..4.1..
|
||||
...5.6..8
|
||||
...38.5..
|
||||
7.8...9.3
|
||||
..2.59...
|
||||
3..1.2...
|
||||
..9.3..71
|
||||
1..9.....
|
@@ -0,0 +1,9 @@
|
||||
513897264
|
||||
867243195
|
||||
924516738
|
||||
691384527
|
||||
758621943
|
||||
432759816
|
||||
375162489
|
||||
289435671
|
||||
146978352
|
@@ -0,0 +1,9 @@
|
||||
.....43.2
|
||||
52.81...9
|
||||
.9.......
|
||||
6.1..29..
|
||||
2.......6
|
||||
..76..4.5
|
||||
.......6.
|
||||
3...46.78
|
||||
1.87..7..
|
@@ -0,0 +1,9 @@
|
||||
716594382
|
||||
523817649
|
||||
894263751
|
||||
681452937
|
||||
245379816
|
||||
937681425
|
||||
479128563
|
||||
352946178
|
||||
168735294
|
@@ -0,0 +1,9 @@
|
||||
..7...6..
|
||||
..93.67..
|
||||
4...9...1
|
||||
.769.154.
|
||||
.........
|
||||
.384.216.
|
||||
7...8...6
|
||||
..12.73..
|
||||
..5...2..
|
@@ -0,0 +1,9 @@
|
||||
357814692
|
||||
189326754
|
||||
462795831
|
||||
276931548
|
||||
514678923
|
||||
938452167
|
||||
723589416
|
||||
691247385
|
||||
845163279
|
@@ -0,0 +1,9 @@
|
||||
.1..4.2.9
|
||||
...8.9.73
|
||||
.........
|
||||
.29..4.1.
|
||||
...1.5...
|
||||
.8.7..43.
|
||||
.........
|
||||
34.6.8...
|
||||
1.6.7..85
|
@@ -0,0 +1,9 @@
|
||||
813547269
|
||||
452869173
|
||||
967231854
|
||||
729384516
|
||||
634125798
|
||||
581796432
|
||||
278953641
|
||||
345618927
|
||||
196472385
|
@@ -0,0 +1,9 @@
|
||||
..8.14...
|
||||
..39..1..
|
||||
...5...86
|
||||
..23..4.9
|
||||
.5.....3.
|
||||
6.9..85..
|
||||
71...5...
|
||||
..4..36..
|
||||
...82.7..
|
@@ -0,0 +1,9 @@
|
||||
928614357
|
||||
563987142
|
||||
147532986
|
||||
872351469
|
||||
451769238
|
||||
639248571
|
||||
716495823
|
||||
284173695
|
||||
395826714
|
@@ -0,0 +1,9 @@
|
||||
293481576
|
||||
547396182
|
||||
681527493
|
||||
825734961
|
||||
379162854
|
||||
164859327
|
||||
458673219
|
||||
936218745
|
||||
712945638
|
9
banson_hker/job1/references/S10/Executable_program/3.txt
Normal file
9
banson_hker/job1/references/S10/Executable_program/3.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
5.6...2..
|
||||
.41.326..
|
||||
.8.1.6.9.
|
||||
13.6...4.
|
||||
...815...
|
||||
.2...3.56
|
||||
.5.2.8.1.
|
||||
..274.56.
|
||||
..7...8.9
|
@@ -0,0 +1,9 @@
|
||||
7.5...8..
|
||||
.46.781..
|
||||
.3.4.9.5.
|
||||
4.93...1.
|
||||
...791...
|
||||
.5...4.78
|
||||
.7.9.2.8.
|
||||
..285.74.
|
||||
..9...3.2
|
@@ -0,0 +1,9 @@
|
||||
725613894
|
||||
946578123
|
||||
831429657
|
||||
497385216
|
||||
268791435
|
||||
153264978
|
||||
674932581
|
||||
312856749
|
||||
589147362
|
@@ -0,0 +1,9 @@
|
||||
576984231
|
||||
941532687
|
||||
283176495
|
||||
135627948
|
||||
694815372
|
||||
728493156
|
||||
359268714
|
||||
812749563
|
||||
467351829
|
9
banson_hker/job1/references/S10/Executable_program/4.txt
Normal file
9
banson_hker/job1/references/S10/Executable_program/4.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
98.2...76
|
||||
34.5.69..
|
||||
..6..7..8
|
||||
6.....48.
|
||||
8..4.5..1
|
||||
.73.....9
|
||||
2..3..7..
|
||||
..98.4.25
|
||||
15...9.63
|
@@ -0,0 +1,9 @@
|
||||
981243576
|
||||
347586912
|
||||
526197348
|
||||
615932487
|
||||
892475631
|
||||
473618259
|
||||
268351794
|
||||
739864125
|
||||
154729863
|
9
banson_hker/job1/references/S10/Executable_program/5.txt
Normal file
9
banson_hker/job1/references/S10/Executable_program/5.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
.57...9..
|
||||
...18...7
|
||||
1....2..6
|
||||
..43.7.6.
|
||||
.7..5..4.
|
||||
.2.9.47..
|
||||
8..2....4
|
||||
6...15...
|
||||
..9...21.
|
@@ -0,0 +1,9 @@
|
||||
257436981
|
||||
463189527
|
||||
198572436
|
||||
584327169
|
||||
976851342
|
||||
321964758
|
||||
815293674
|
||||
642715893
|
||||
739648215
|
9
banson_hker/job1/references/S10/Executable_program/6.txt
Normal file
9
banson_hker/job1/references/S10/Executable_program/6.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
..3...1..
|
||||
.5.2.8.7.
|
||||
46.....25
|
||||
..67.98..
|
||||
8.......3
|
||||
..54.12..
|
||||
63.....17
|
||||
.1.3.2.9.
|
||||
..4...5..
|
@@ -0,0 +1,9 @@
|
||||
723954186
|
||||
159268374
|
||||
468173925
|
||||
246739851
|
||||
891526743
|
||||
375481269
|
||||
632895417
|
||||
517342698
|
||||
984617532
|
9
banson_hker/job1/references/S10/Executable_program/7.txt
Normal file
9
banson_hker/job1/references/S10/Executable_program/7.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
.7...4.6.
|
||||
1.46.2..7
|
||||
2.5..39.8
|
||||
9...48...
|
||||
.48...31.
|
||||
...12...9
|
||||
5.14..8.6
|
||||
8..2.91.4
|
||||
.9.5...2.
|
@@ -0,0 +1,9 @@
|
||||
379854261
|
||||
184692537
|
||||
265713948
|
||||
912348675
|
||||
648975312
|
||||
753126489
|
||||
521437896
|
||||
837269154
|
||||
496581723
|
9
banson_hker/job1/references/S10/Executable_program/8.txt
Normal file
9
banson_hker/job1/references/S10/Executable_program/8.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
..8..27..
|
||||
.4.63..2.
|
||||
7.68..1.4
|
||||
6...8.91.
|
||||
.1.7.6.8.
|
||||
.97.4...3
|
||||
1.5..43.2
|
||||
.6..71.5.
|
||||
..95..6..
|
@@ -0,0 +1,9 @@
|
||||
538412796
|
||||
941637528
|
||||
726859134
|
||||
654283917
|
||||
213796438
|
||||
897145263
|
||||
185964372
|
||||
462371859
|
||||
379528641
|
9
banson_hker/job1/references/S10/Executable_program/9.txt
Normal file
9
banson_hker/job1/references/S10/Executable_program/9.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
.4.2.7.8.
|
||||
9.......7
|
||||
..34.82..
|
||||
.5.836.9.
|
||||
.1.....5.
|
||||
.8.145.6.
|
||||
..69.18..
|
||||
8.......5
|
||||
.7.5.2.3.
|
@@ -0,0 +1,9 @@
|
||||
145297683
|
||||
928613549
|
||||
763458219
|
||||
257836491
|
||||
614729358
|
||||
389145762
|
||||
536971824
|
||||
892364175
|
||||
471582936
|
BIN
banson_hker/job1/references/S10/S10.pdf
Normal file
BIN
banson_hker/job1/references/S10/S10.pdf
Normal file
Binary file not shown.
712
banson_hker/job1/references/S10/Source_code/S10.c
Normal file
712
banson_hker/job1/references/S10/Source_code/S10.c
Normal file
@@ -0,0 +1,712 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
//gteet the user
|
||||
void greetintmessage(void);
|
||||
|
||||
//state the rules of playing the game
|
||||
void rules(void);
|
||||
|
||||
//ask the user to select level
|
||||
int levelchoosing(void);
|
||||
|
||||
//verify the correctness of input(length and range)
|
||||
int verifyinput(void);
|
||||
|
||||
//Put content of cell filled with 0 to be a space
|
||||
void initboard(void);
|
||||
|
||||
//print out the game board
|
||||
void printboard(void);
|
||||
|
||||
//fill in some numbers as tips for user by open a sudoku file
|
||||
int generatecontent(int);
|
||||
|
||||
//verify the correctness of option choenn(length and range)
|
||||
int verifyoption(void);
|
||||
|
||||
//accept input from user and modify the array accorgingly
|
||||
//also it checks wether the number is allowed to place at the cell or not
|
||||
void acceptinput(void);
|
||||
|
||||
//enter row and check allowance
|
||||
int enterrow(void);
|
||||
|
||||
//enter column and check allowance
|
||||
int entercolumn(void);
|
||||
|
||||
//give hints to user when requested
|
||||
void givehint(int);
|
||||
|
||||
//delete inputed number before
|
||||
void deletenumber(int);
|
||||
|
||||
//check whether the user has finished the game
|
||||
//by testing wether all the blankets are filled with number
|
||||
//return '1' when all the cell is filled
|
||||
//'o' when not yet finished
|
||||
int checkgameend(void);
|
||||
|
||||
//check if the numbers in each row are unique and digits 1-9 are present
|
||||
//after the user had finished the game
|
||||
int checkrow(void);
|
||||
|
||||
//compare the elements in the same row to check uniqueness
|
||||
//used inside int checkrow
|
||||
int comparerow(int,int a);
|
||||
|
||||
//check if the numbers in each column are unique and digits 1-9 are present
|
||||
//after the user had finished the game
|
||||
int checkcolumn(void);
|
||||
|
||||
//compare the elements in the same column to check uniqueness
|
||||
//used inside int checkcolumn
|
||||
int comparecolumn(int,int a);
|
||||
|
||||
//print out the correct solution if the user'solution is not correct
|
||||
void printsolution(int);
|
||||
|
||||
//congratulation message to the user if solution is correct
|
||||
void congratulation(void);
|
||||
|
||||
//ask wether the user want to play another game
|
||||
int playothergame(void);
|
||||
|
||||
//array used to store value of the game
|
||||
int gameboard[9][9];
|
||||
|
||||
//The following is the main part of this program
|
||||
main()
|
||||
{
|
||||
int level,option;//variable for receive levelchoosing
|
||||
int randnum;//variable for receive random number from generatecontent
|
||||
int crow,ccol;//variables for receive results from checkrow and checkcolumn
|
||||
|
||||
greetintmessage();
|
||||
rules();
|
||||
do{
|
||||
level = levelchoosing();
|
||||
initboard();
|
||||
printf("=================================================================\n\n");
|
||||
randnum = generatecontent(level);
|
||||
do{
|
||||
printboard();
|
||||
printf("1.Enter number\n2.Give hints to current board\n3.Delete input before\n4.Generate new board\n5.Solution to current board\n");
|
||||
printf("Your choice: ");
|
||||
option = verifyoption();
|
||||
if(option == '1')
|
||||
{
|
||||
printf("\nPlease choose the grid you want and input number.");
|
||||
acceptinput();
|
||||
system("CLS");
|
||||
}
|
||||
else if(option == '2')
|
||||
{
|
||||
givehint(randnum);
|
||||
}
|
||||
else if(option == '3')
|
||||
{
|
||||
printf("\nPlease choose the grid you want to delete:");
|
||||
deletenumber(randnum);
|
||||
}
|
||||
else if(option == '4')
|
||||
{
|
||||
randnum = generatecontent(level);
|
||||
system("CLS");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\nCorrect solution is:\n");
|
||||
printsolution(randnum);
|
||||
}
|
||||
}while(checkgameend() != 1);
|
||||
|
||||
if(option != '5'){
|
||||
//after the user have completed the sudoku
|
||||
printf("Great!You have completed the sudoku!\n");
|
||||
printf("Let's check the answers now........\n\n");
|
||||
//check solution
|
||||
printboard();
|
||||
crow = checkrow();
|
||||
ccol = checkcolumn();
|
||||
if(crow == 1 && ccol == 1)
|
||||
{
|
||||
congratulation();
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\nSorry,your solution is not correct.\n");
|
||||
printf("The correct solution should be: \n");
|
||||
printsolution(randnum);
|
||||
}
|
||||
}
|
||||
}while(playothergame() == 1);
|
||||
}
|
||||
|
||||
/*The following is the detail part of all funtions*/
|
||||
/*gteet the user*/
|
||||
void greetintmessage()
|
||||
{
|
||||
printf("\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
|
||||
printf("%18cWelcome to play S-U-D-O-K-U game!\n",' ');
|
||||
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/*state the rules of playing the game*/
|
||||
void rules()
|
||||
{
|
||||
printf("%32cRules :\n",' ');
|
||||
printf("\n%5cFill in all the cells so that every column, row and 3 x 3\n",' ');
|
||||
printf("%5cboxes contains each of the digits 1 to 9 without repetition.\n",' ');
|
||||
}
|
||||
|
||||
/*ask the user to select level*/
|
||||
int levelchoosing()
|
||||
{
|
||||
char input[10];
|
||||
printf("\n=================================================================\n");
|
||||
printf("Please choose the level you want( 1-easy 2-medium 3-hard ) : ");
|
||||
do{
|
||||
scanf("%s",input);
|
||||
if ((strlen(input) != 1) || !( (input[0]<'4') && (input[0]>'0') ))
|
||||
{
|
||||
printf("Input out of range!Input again please: ");
|
||||
}
|
||||
} while ((strlen(input) != 1) || !( (input[0]<'4') && (input[0]>'0') ));
|
||||
|
||||
printf("You have choose level %d\n",input[0]-48);
|
||||
printf("Game start!\nChoose the following options please.\n");
|
||||
|
||||
return input[0];
|
||||
}
|
||||
|
||||
/*verify the correctness of input(length and range)*/
|
||||
int verifyinput()
|
||||
{
|
||||
char input[10];
|
||||
do{
|
||||
scanf("%s",input);
|
||||
if ((strlen(input) != 1) || !( (input[0]<'2') && (input[0]>='0') ))
|
||||
{
|
||||
printf("Input out of range!Input again please: ");
|
||||
}
|
||||
} while ((strlen(input) != 1) || !( (input[0]<'2') && (input[0]>='0') ));
|
||||
return input[0];
|
||||
}
|
||||
|
||||
/*Put content of cell filled with 0 to be a space*/
|
||||
void initboard()
|
||||
{
|
||||
int i,j;
|
||||
for(i = 0;i < 9;i++){
|
||||
for(j = 0;j < 9;j++){
|
||||
gameboard[i][j] = ' ';
|
||||
}//for i
|
||||
}//for j
|
||||
}
|
||||
|
||||
/*print out the game board */
|
||||
void printboard()
|
||||
{
|
||||
char space = ' ';
|
||||
char separator[] = "+- - - -+- - - -+- - - -+";
|
||||
char row = 'a';
|
||||
int i, j;
|
||||
|
||||
printf("%20c 1 2 3 4 5 6 7 8 9\n",space);
|
||||
for (i=0;i<9;i++)
|
||||
{
|
||||
if(i%3 ==0)
|
||||
{
|
||||
printf("%20c%s\n",space,separator);
|
||||
}
|
||||
printf("%18c%c | %c %c %c | %c %c %c | %c %c %c |\n",space,row+i,gameboard[i][0],gameboard[i][1],gameboard[i][2],gameboard[i][3],gameboard[i][4],gameboard[i][5],gameboard[i][6],gameboard[i][7],gameboard[i][8]);
|
||||
}
|
||||
printf("%20c%s\n",space,separator);
|
||||
}
|
||||
|
||||
/*fill in some numbers by open a sudoku file*/
|
||||
int generatecontent(level)
|
||||
{
|
||||
int i,j,randnum;
|
||||
char readchar,filename[20],string[10] = ".txt";
|
||||
FILE *fp;
|
||||
srand(time(NULL));
|
||||
//randomly genereta a game for users according to their request level
|
||||
if(level == '1')
|
||||
{
|
||||
randnum = (rand() %10) + 1;
|
||||
sprintf(filename,"%d%s",randnum,string);//group number and string together
|
||||
}
|
||||
else if(level == '2')
|
||||
{
|
||||
randnum = (rand() %10) + 11;
|
||||
sprintf(filename,"%d%s",randnum,string);
|
||||
}
|
||||
else if(level == '3')
|
||||
{
|
||||
randnum = (rand() %10) + 21;
|
||||
sprintf(filename,"%d%s",randnum,string);
|
||||
}
|
||||
//open file
|
||||
fp=fopen(filename,"r");
|
||||
if(fp==NULL)
|
||||
{
|
||||
printf("cannot open the file!\n");//when failed to open file
|
||||
exit(1);
|
||||
}
|
||||
//read character from the file
|
||||
readchar = fgetc(fp);
|
||||
while(readchar != EOF) {
|
||||
for(i=0;i<9;i++)
|
||||
{
|
||||
for(j=0;j<=9;j++)
|
||||
{
|
||||
gameboard[i][j] = readchar;
|
||||
readchar = fgetc(fp);
|
||||
}//for i
|
||||
}//for j
|
||||
}//for while loop
|
||||
fclose(fp);
|
||||
|
||||
return randnum;
|
||||
}
|
||||
|
||||
/*verify the correctness of option chosen(length and range)*/
|
||||
int verifyoption(void)
|
||||
{
|
||||
char option[10];
|
||||
do{
|
||||
scanf("%s",option);
|
||||
if ((strlen(option) != 1) || !( (option[0]<'6') && (option[0]>'0') ))
|
||||
{
|
||||
printf("Input out of range!Input again please: ");
|
||||
}
|
||||
}while((strlen(option) != 1) || !( (option[0]<'6') && (option[0]>'0') ));
|
||||
return option[0];
|
||||
}
|
||||
|
||||
//accept input from user and modify the array accorgingly
|
||||
//accept input from user and modify the array accorgingly
|
||||
//it checks wether the number is allowed to place at the cell or not*/
|
||||
void acceptinput()
|
||||
{
|
||||
int i , j,row, col, number;
|
||||
char input[20];
|
||||
int count1 = 0,count2 = 0;
|
||||
int startingrow,startingcol;
|
||||
|
||||
//repeatly ask to do input if the cell being chosen is occupied
|
||||
do{
|
||||
//ask to enter column
|
||||
col = entercolumn() - 49;
|
||||
//ask to enter row
|
||||
row = enterrow() - 97;
|
||||
//check wether the cell chosen is already filled or not
|
||||
//if yes,print warning message
|
||||
//if not,fill the cell with number assigned by user
|
||||
if (gameboard[row][col] !='.')
|
||||
{
|
||||
printf("The cell is already occupied!\n");
|
||||
printf("Input again please.");
|
||||
}
|
||||
}while(gameboard[row][col] !='.');
|
||||
//ask to enter number
|
||||
printf("Which number(1-9)do you want to type in? ");
|
||||
do {
|
||||
scanf("%s",input);
|
||||
if ((strlen(input) != 1) || !( (input[0]<='9') && (input[0]>'0') ))
|
||||
{
|
||||
printf("Input out of range! Input again please: ");
|
||||
}
|
||||
} while ((strlen(input) != 1) || !( (input[0]<='9') && (input[0]>'0') ));
|
||||
number = input[0];
|
||||
//checks wether the number is allowed to place at the cell or not
|
||||
//check row and cloumn
|
||||
//by testing wether the same number is presented
|
||||
for(i = 0;i < 9;i++)
|
||||
{
|
||||
if(gameboard[row][i] == number)
|
||||
{
|
||||
count1++;
|
||||
}
|
||||
if(gameboard[i][col] == number)
|
||||
{
|
||||
count1++;
|
||||
}
|
||||
}
|
||||
//check 3x3 boxes
|
||||
//check by dividing the game board into 9 small boxes
|
||||
//after detecting which small box the chosen grid lying on
|
||||
//it test wether same number is presented in other grids within the same box
|
||||
if(row < 3)
|
||||
{
|
||||
startingrow = 0;
|
||||
}
|
||||
else if((row > 2) && (row < 6))
|
||||
{
|
||||
startingrow = 3;
|
||||
}
|
||||
else if((row > 5) && (row < 9))
|
||||
{
|
||||
startingrow = 6;
|
||||
}
|
||||
if(col < 3)
|
||||
{
|
||||
startingcol = 0;
|
||||
}
|
||||
else if((col > 2) && (col < 6))
|
||||
{
|
||||
startingcol = 3;
|
||||
}
|
||||
else if((col > 5) && (col < 9))
|
||||
{
|
||||
startingcol = 6;
|
||||
}
|
||||
for(i = 0;i < 3;i++)
|
||||
{
|
||||
for(j = 0;j < 3;j++)
|
||||
{
|
||||
if(gameboard[startingrow+i][startingcol+j] == number)
|
||||
{
|
||||
count2++;
|
||||
}
|
||||
}
|
||||
}
|
||||
//assign value to the cell
|
||||
if((count1 == 0) && (count2 == 0))
|
||||
{
|
||||
gameboard[row][col] = number;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("=================================================================\n");
|
||||
printf("Sorry,input number is repeated either in row,column or 3x3 box\n");
|
||||
printf("Do you really want to input(1-yes 0-no)? ");
|
||||
input[0] = verifyinput();
|
||||
if(input[0] == '1' )
|
||||
{
|
||||
gameboard[row][col] = number;
|
||||
}
|
||||
}
|
||||
//restore value of two counter
|
||||
count1 = 0;
|
||||
count2 = 0;
|
||||
}
|
||||
|
||||
//enter row and check allowance
|
||||
int enterrow(void)
|
||||
{
|
||||
char input[10];
|
||||
printf("Which row do you want to choose(e.g.a)? ");
|
||||
do {
|
||||
scanf("%s",input);
|
||||
if ((strlen(input) != 1) || !( (input[0]<'j') && (input[0]>='a') ))
|
||||
{
|
||||
printf("Input out of range! Input again please: ");
|
||||
}
|
||||
} while ((strlen(input) != 1) || !( (input[0]<'j') && (input[0]>='a') ));
|
||||
return input[0];
|
||||
}
|
||||
|
||||
//enter column and check allowance
|
||||
int entercolumn(void)
|
||||
{
|
||||
char input[10];
|
||||
printf("\nWhich column do you want to choose(e.g.1)? ");
|
||||
do {
|
||||
scanf("%s",input);
|
||||
if ((strlen(input) != 1) || !( (input[0]<='9') && (input[0]>'0') ))
|
||||
{
|
||||
printf("Input out of range! Input again please: ");
|
||||
}
|
||||
} while ((strlen(input) != 1) || !( (input[0]<='9') && (input[0]>'0') ));
|
||||
return input[0];
|
||||
}
|
||||
|
||||
//give hints to user when requested
|
||||
void givehint(randnum)
|
||||
{
|
||||
int i,j,row,col;
|
||||
char space = ' ';
|
||||
char readchar,filename[20],string[10] = "s.txt";
|
||||
FILE *fp;
|
||||
//ask to enter column
|
||||
col = entercolumn() - 49;
|
||||
//ask to enter row
|
||||
row = enterrow() - 97;
|
||||
sprintf(filename,"%d%s",randnum,string);
|
||||
fp=fopen(filename,"r");
|
||||
if(fp==NULL)
|
||||
{
|
||||
printf("cannot open the file!\n");
|
||||
exit(1);
|
||||
}
|
||||
readchar = fgetc(fp);
|
||||
while(readchar != EOF) {
|
||||
for(i=0;i<9;i++)
|
||||
{
|
||||
for(j=0;j<=9;j++)
|
||||
{
|
||||
if(i ==row && j == col)
|
||||
{
|
||||
if(gameboard[row][col] == '.')
|
||||
{
|
||||
gameboard[i][j] = readchar;
|
||||
printf("=================================================================\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("=================================================================\n");
|
||||
printf("%23c####################\n",space);
|
||||
printf("%23cNumber is presented!\n",space);
|
||||
printf("%23c####################\n",space);
|
||||
}
|
||||
}
|
||||
readchar = fgetc(fp);
|
||||
}//for i
|
||||
}//for j
|
||||
}//for while loop
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
//delete inputed number before
|
||||
void deletenumber(randnum)
|
||||
{
|
||||
int i,j,row,col;
|
||||
char space = ' ';
|
||||
char readchar,filename[20],string[10] = ".txt";
|
||||
FILE *fp;
|
||||
|
||||
//ask to enter column
|
||||
col = entercolumn() - 49;
|
||||
//ask to enter row
|
||||
row = enterrow() - 97;
|
||||
sprintf(filename,"%d%s",randnum,string);
|
||||
fp=fopen(filename,"r");
|
||||
if(fp==NULL)
|
||||
{
|
||||
printf("cannot open the file!\n");
|
||||
exit(1);
|
||||
}
|
||||
readchar = fgetc(fp);
|
||||
while(readchar != EOF) {
|
||||
for(i=0;i<9;i++)
|
||||
{
|
||||
for(j=0;j<=9;j++)
|
||||
{
|
||||
if(i ==row && j == col)
|
||||
{
|
||||
if(readchar == '.' && gameboard[row][col] != '.')
|
||||
{
|
||||
gameboard[row][col] = '.';
|
||||
printf("=================================================================\n");
|
||||
}
|
||||
else if(readchar == '.' && gameboard[row][col] == '.')
|
||||
{
|
||||
printf("=================================================================\n");
|
||||
printf("%21c#######################\n",space);
|
||||
printf("%21cNothing can be deleted!\n",space);
|
||||
printf("%21c#######################\n",space);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("=================================================================\n");
|
||||
printf("%8c########################################################\n",space);
|
||||
printf("%8cThe number can't be cancelled.It is provided originally!\n");
|
||||
printf("%8c########################################################\n",space);
|
||||
}
|
||||
}
|
||||
readchar = fgetc(fp);
|
||||
}//for i
|
||||
}//for j
|
||||
}//for while loop
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
/*check whether the user has finished the game
|
||||
by testing wether all the blankets are filled
|
||||
with number*/
|
||||
//return '1' when all the cell is filled
|
||||
//'o' when not yet finished
|
||||
int checkgameend()
|
||||
{
|
||||
int i,j;
|
||||
int counter = 0;
|
||||
for(i = 0;i < 9;i++){
|
||||
for(j = 0;j < 9;j++){
|
||||
if(gameboard[i][j] != '.'){
|
||||
counter ++;
|
||||
}
|
||||
}//for i
|
||||
}//for j
|
||||
|
||||
if(counter == 81)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
//check if the numbers in each row are unique and digits 1-9 are present
|
||||
int checkrow(void)
|
||||
{
|
||||
int i,j;
|
||||
int sum,counter1 = 0,counter2 = 0,counter3 = 0;
|
||||
for(i = 0;i < 9;i++)
|
||||
{
|
||||
sum = (gameboard[i][0]+gameboard[i][1]+gameboard[i][2]+gameboard[i][3]+gameboard[i][4]+gameboard[i][5]+gameboard[i][6]+gameboard[i][7]+gameboard[i][8])-48*9;
|
||||
if(sum == 45)
|
||||
{
|
||||
counter1 ++;
|
||||
}
|
||||
}
|
||||
//check uniqueness
|
||||
for(i = 0;i < 9;i++){
|
||||
for(j = 1;j < 9;j++){
|
||||
counter3 = comparerow(i,j);
|
||||
counter2 += counter3;
|
||||
counter3 = 0;
|
||||
}//for j
|
||||
}//for i
|
||||
if((counter1 == 9) && (counter2 == 324))//The total combination in comparison is 324(36x9)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
//compare the elements in the same row to check uniqueness
|
||||
//used inside int checkrow
|
||||
int comparerow(i,a)
|
||||
{
|
||||
int j,counter = 0;
|
||||
for(j = a;j < 9;j++)
|
||||
{
|
||||
if(gameboard[i][a-1] != gameboard[i][j])
|
||||
{
|
||||
counter ++;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("More than one %c are found in row %d!\n",gameboard[i][a-1],i+1);
|
||||
}
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
//check if the numbers in each column are unique and digits 1-9 are present
|
||||
int checkcolumn(void)
|
||||
{
|
||||
int i,j;
|
||||
int sum,counter1 = 0,counter2 = 0,counter3 = 0;
|
||||
for(i = 0;i < 9;i++)
|
||||
{
|
||||
sum = (gameboard[0][i]+gameboard[1][i]+gameboard[2][i]+gameboard[3][i]+gameboard[4][i]+gameboard[5][i]+gameboard[6][i]+gameboard[7][i]+gameboard[8][i])-48*9;
|
||||
if(sum == 45)
|
||||
{
|
||||
counter1 ++;
|
||||
}
|
||||
}
|
||||
//check uniqueness
|
||||
for(i = 0;i < 9;i++){
|
||||
for(j = 1;j < 9;j++){
|
||||
counter3 = comparecolumn(i,j);
|
||||
counter2 += counter3;
|
||||
counter3 = 0;
|
||||
}//for j
|
||||
}//for i
|
||||
if((counter1 == 9) && (counter2 == 324))//The total combination in comparison is 324(36x9)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
//compare the elements in the same column to check uniqueness
|
||||
//used inside int checkcolumn
|
||||
int comparecolumn(i,a)
|
||||
{
|
||||
int j,counter = 0;
|
||||
for(j = a;j < 9;j++)
|
||||
{
|
||||
if(gameboard[a-1][i] != gameboard[j][i])
|
||||
{
|
||||
counter ++;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("More than one %c are found in column %d!\n",gameboard[a-1][i],i+1);
|
||||
}
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
/*print out the correct solution if the user'solution is not correct*/
|
||||
void printsolution(randnum)
|
||||
{
|
||||
|
||||
int i,j;
|
||||
char readchar,filename[20],string[10] = "s.txt";
|
||||
FILE *fp;
|
||||
|
||||
sprintf(filename,"%d%s",randnum,string);
|
||||
fp=fopen(filename,"r");
|
||||
if(fp==NULL)
|
||||
{
|
||||
printf("cannot open the file!\n");
|
||||
exit(1);
|
||||
}
|
||||
readchar = fgetc(fp);
|
||||
while(readchar != EOF) {
|
||||
for(i=0;i<9;i++)
|
||||
{
|
||||
for(j=0;j<=9;j++)
|
||||
{
|
||||
gameboard[i][j] = readchar;
|
||||
readchar = fgetc(fp);
|
||||
}//for i
|
||||
}//for j
|
||||
|
||||
}//for while loop
|
||||
fclose(fp);
|
||||
printboard();
|
||||
}
|
||||
|
||||
/*congratulation message to the user if solution is correct*/
|
||||
void congratulation()
|
||||
{
|
||||
char space = ' ';
|
||||
printf("%8c+++++++++++++++++++++++++++++++++++++++++++++++++++\n",space);
|
||||
printf("%8cCONGRATULATION!!You have done the sudoku correctly!\n",space);
|
||||
printf("%8c+++++++++++++++++++++++++++++++++++++++++++++++++++\n",space);
|
||||
}
|
||||
|
||||
/*ask wether the user want to play another game*/
|
||||
int playothergame()
|
||||
{
|
||||
char input[10];
|
||||
printf("=================================================================\n");
|
||||
printf("Do you want to play another game(1-yes 0-no)? ");
|
||||
input[0] = verifyinput();
|
||||
if(input[0] == '1')
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if(input[0] == '0')
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user