Objective I’m supposed to create a sudoku program using these implementations. i
ID: 3698503 • Letter: O
Question
Objective
I’m supposed to create a sudoku program using these implementations. input/output, variables, conditionals, loops, file reading, methods, arrays, and recursion.
Sudoku rules. Traditional Sudoku is a 9x9 puzzle grid composed by 3x3 subgrids. The
objective is to fill the grid with three rules: 1) each column, 2) each row, and 3) each of the 3x3
subgrids should contain all the digits from 1 to 9 only once (the order of digits does not
matter).
Your program should have the following menu options:
1. Load Sudoku
2. Play Sudoku
3. Exit
Option 1 - Load Sudoku
This option will enable the user to input the name of a file where a traditional Sudoku puzzle is
given. The digits of a row in a Sudoku use a 0 to represent an empty sell, a comma to separate
the nine digits in a row, and a new line to separate rows. As an example, the puzzle1.txt file
provided in this assignment contains the digits of the puzzle shown in Figure 1 (left) as:
5,3,0,0,7,0,0,0,0
6,0,0,1,9,5,0,0,0
0,9,8,0,0,0,0,6,0
8,0,0,0,6,0,0,0,3
4,0,0,8,0,3,0,0,1
7,0,0,0,2,0,0,0,6
0,6,0,0,0,0,2,8,0
0,0,0,4,1,9,0,0,5
0,0,0,0,8,0,0,7,9
The option 1 should use a method called loadSudoku that receives the name of the file (e.g. ,
puzzle1.txt) as a parameter, reads the file, creates a 2-D array called Puzzle[][] based on the
content of the file, and lastly returns the 2D array. This array will contain the digits for the
puzzle.
Option 2 - Play Sudoku
This option will display the puzzle and enable the user to input a digit into the puzzle by
providing the name position of a cell in the grid, for example, the position [1][3] represents the
cell in row 1, column 3, illustrated with red color in Figure 2. Only cells with the digit 0 can be
changed for a digit between 1 and 9. However, the digit 0 can only be replaced by a digit from 1
to 9 if it does not break the rules explained in 3.1.
Every time a new digit is entered, the puzzle has to be displayed again, showing the newly
entered value. When the puzzle is solved, i.e., it does not contain a 0 digit, the following
message should be displayed: “Congratulations! You have solved the puzzle!” and the program
should return to the main menu provided in 2.1., and option 2 will be disabled. You should also
provide an option to the user to exit option 2 at any time and return to the main menu.
For option 2, you should implement and use the method isValidInput that receives a 2-D array
Puzzle[][], a row number, a column number, and the digit to be added in that row-column cell,
as parameters. The method isValidInput returns true if the digit can be added without breaking
the Sudoku rules. Feel free to also create the methods getValue or setValue to retrieve or
replace the values of a cell as well as any other methods you consider necessary.
Option 3. Exit
When option 3 is selected, the program should end and print the message “Good bye”.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
void loadSudoku(char fileName[20], int Puzzle[9][9])
{
int i = 0, j = 0, ch;
char c;
FILE *fp;
fp = fopen(fileName, "r"); //Scanning the file contents
while(1)
{
fscanf(fp, "%c", &c);
switch(c) //Copying the Sudoku
{
case '0': Puzzle[i][j] = 0;
break;
case '1': Puzzle[i][j] = 1;
break;
case '2': Puzzle[i][j] = 2;
break;
case '3': Puzzle[i][j] = 3;
break;
case '4': Puzzle[i][j] = 4;
break;
case '5': Puzzle[i][j] = 5;
break;
case '6': Puzzle[i][j] = 6;
break;
case '7': Puzzle[i][j] = 7;
break;
case '8': Puzzle[i][j] = 8;
break;
case '9': Puzzle[i][j] = 9;
break;
}
if(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9') //Updating i and j
{
if(j == 8)
{
j = 0;
i++;
}
else
j++;
if(i == 9) //Sudoku is loaded
break;
}
}
fclose(fp);
printf("1. Load Sudoku 2. Play Sudoku 3. Exit "); //Now, Play Sudoku is activated
scanf("%d", &ch);
switch(ch)
{
case 1: printf(" Enter the file name form which Sudoku is to be loaded: ");
scanf("%s", fileName);
loadSudoku(fileName, Puzzle);
break;
case 2: Display(Puzzle);
PlaySudoku(Puzzle);
break;
case 3: printf("Good Bye!");
exit(0);
default:printf("Wrong choice");
}
}
void PlaySudoku(int Puzzle[9][9])
{
int i, j, a, flag = 0;
do
{
do
{
if(flag == -1)
printf("Can not be inserted.");
printf(" Enter the cell(i and j): ");
scanf("%d%d", &i, &j);
printf(" Enter the number to be inserted.: ");
scanf("%d", &a);
flag = isValidInput(Puzzle, i, j, a);
}while(flag == -1);
Puzzle[i][j] = a;
Display(Puzzle);
}while(flag == 0);
if(flag == 1)
{
int ch;
char fileName[20];
printf(" Congratulations! You have solved the puzzle!");
printf(" 1. Load Sudoku 2. Exit"); //Play Sudoku is again disabled. New sudoku game has to be loaded first
scanf("%d", &ch);
switch(ch)
{
case 1: printf(" Enter the file name form which Sudoku is to be loaded: ");
scanf("%s", fileName);
loadSudoku(fileName, Puzzle);
break;
case 2: printf("Good Bye!");
exit(0);
default:printf("Wrong choice");
}
}
}
int isValidInput(int Puzzle[9][9], int x, int y, int a) //Returns -1 if number can not be inserted, 0 if number is successfully inserted and 1 if user filled all the spots
{
if(Puzzle[x][y] != 0)
return(-1);
else
{
int i, j;
for(i = 0; i < 9 ;i++) //Checking the row
if(Puzzle[x][i] == a && i != y)
return(-1);
for(i = 0; i < 9 ;i++) //Checking the column
if(Puzzle[i][y] == a && i != x)
return(-1);
for(i = x / 3; i < x / 3 + 3; i++) //Checking the 3*3 box
for(j = y / 3; j < y / 3 + 3; j++)
if(Puzzle[i][j] == a && i != x && j != y)
return(-1);
for(i = 0; i < 9 ;i++) //Chekcing if all the spaces are filled
for(j = 0; j < 9; j++)
if(Puzzle[i][j] == 0)
return(0);
}
return(1);
}
void Display(int Puzzle[9][9])
{
int i, j;
for(i = 0; i < 9; i++)
{
printf(" ");
for(j = 0; j < 9; j++)
printf("%d ", Puzzle[i][j]);
}
}
int main()
{
char fileName[20];
int Puzzle[9][9], ch;
printf("1. Load Sudoku 2. Exit ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf(" Enter the file name form which Sudoku is to be loaded: "); //There will not be option of Play Sudoku initially. First, it has to be loaded.
scanf("%s", fileName);
loadSudoku(fileName, Puzzle);
break;
case 2: printf("Good Bye!");
exit(0);
default:printf("Wrong choice");
}
return(0);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.