Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- int IsCorrect(char panel[9]);
- int main(void){
- FILE *fp; // File pointer
- fp = fopen("C:\\Users\\carniavl\\Desktop\\MIA-Task5\\Task 5\\Part A test cases\\valid1.txt", "r"); // Function for opening the file and setting it to read only
- if (fp == NULL){ // In case we can't open the file
- printf("Could not open file \n");
- return 1;
- }
- char Game[9][9];
- int i, j;
- for (i = 0; i < 9; i++){
- for (j = 0; j < 9; j++){
- Game[i][j] = fgetc(fp);
- if (Game[i][j] < '1' || Game[i][j] > '9'){ // If the character is anything else other than integer
- printf("Not an integer at [%d,%d]\n", i, j);
- return 1;
- }
- char NewLineOrSpace = fgetc(fp); // Var used for reading the spaces and new lines
- if (i == 8 && j == 8 && NewLineOrSpace == EOF) break; // if the Grid is finished break the loop
- else if (j < 8 && NewLineOrSpace != ' '){ // if the Grid isn't finished yet but the Var reads a space
- printf("Invalid Character at [%d,%d]\n", i, j);
- return 1;
- }
- else if (j == 8 && NewLineOrSpace != '\n'){ // if the Grid is finished but the Var doesn't read a new line
- printf("Invalid Character at [%d,%d]\n", i, j);
- return 1;
- }
- }
- }
- // Rows Check
- for (i = 0; i < 9; i++){
- if (!IsCorrect(Game[i])){
- printf("Dublicated num at : %d\n" ,i);
- return 1;
- }
- }
- // Columns Check
- char Cols[9];
- for (i = 0; i < 9; i++){
- for (j = 0; j < 9; j++){
- Cols[j] = Game[j][i];
- if (!IsCorrect(Cols[j])){
- printf("Dublicated num at : %d\n",j);
- return 1;
- }
- }
- }
- // Blocks Check
- char Blocks[9][9]={ // Sorting The Grid of the sudoku to get the blocks as lines in a 2D array called blocks
- {Game[0][0],Game[0][1],Game[0][2], Game[1][0],Game[1][1],Game[1][2], Game[2][0],Game[2][1],Game[2][2]},
- {Game[0][3],Game[0][4],Game[0][5], Game[1][3],Game[1][4],Game[1][5], Game[2][3],Game[2][4],Game[2][5]},
- {Game[0][6],Game[0][7],Game[0][8], Game[1][6],Game[1][7],Game[1][8], Game[2][6],Game[2][7],Game[2][8]},
- {Game[3][0],Game[3][1],Game[3][2], Game[4][0],Game[4][1],Game[4][2], Game[5][0],Game[5][1],Game[5][2]},
- {Game[3][3],Game[3][4],Game[3][5], Game[4][3],Game[4][4],Game[4][5], Game[5][3],Game[5][4],Game[5][5]},
- {Game[3][6],Game[3][7],Game[3][8], Game[4][6],Game[4][7],Game[4][8], Game[5][6],Game[5][7],Game[5][8]},
- {Game[6][0],Game[6][1],Game[6][2], Game[7][0],Game[7][1],Game[7][2], Game[8][0],Game[8][1],Game[8][2]},
- {Game[6][3],Game[0][4],Game[6][5], Game[7][3],Game[7][4],Game[7][5], Game[8][3],Game[8][4],Game[8][5]},
- {Game[6][6],Game[6][7],Game[6][8], Game[7][6],Game[7][7],Game[7][8], Game[8][6],Game[8][7],Game[8][8]},
- };
- for (i = 0; i < 9; i++){
- if (!IsCorrect(Blocks[i])){
- printf("Invalid Sudoku at: %d\n",i);
- return 1;
- }
- }
- printf("Correct Sudoku\n"); // If all of the above is invalid just print this sudoku is correct in shape
- return 0;
- }
- int IsCorrect(char Panel[9]){ // Function for Checking if the Given Panel is Correct
- int CheckSum = 0; // initial value for the Checksum var is set to zero
- for (int i = 0; i < 9; i++){ // Checking every module in the panel whether it's the Cols or Rows
- CheckSum |= (int)pow(2,Panel[i] - '0' - 1);
- }
- /* Oring using bitwise | between the Zero and the Powers of two
- The last Power of two it can reach is 256 and the next power is 512 so 511 will be the binary of it to return in case
- we get the numbers from 1 to 9 else it returns false and changing the double from the power into int */
- return CheckSum == 0b111111111;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement