Advertisement
Eng-Mohamed-Essam

Untitled

Sep 11th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. int IsCorrect(char panel[9]);
  5. int main(void){
  6.     FILE *fp; // File pointer
  7.     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
  8.     if (fp == NULL){ // In case we can't open the file
  9.         printf("Could not open file \n");
  10.         return 1;
  11.     }
  12.     char Game[9][9];
  13.     int i, j;
  14.     for (i = 0; i < 9; i++){
  15.         for (j = 0; j < 9; j++){
  16.             Game[i][j] = fgetc(fp);
  17.             if (Game[i][j] < '1' || Game[i][j] > '9'){ // If the character is anything else other than integer
  18.                 printf("Not an integer at [%d,%d]\n", i, j);
  19.                 return 1;
  20.             }
  21.             char NewLineOrSpace = fgetc(fp); // Var used for reading the spaces and new lines
  22.             if (i == 8 && j == 8 && NewLineOrSpace == EOF) break; //  if the Grid is finished break the loop
  23.             else if (j < 8 && NewLineOrSpace != ' '){ // if the Grid isn't finished yet but the Var reads a space
  24.                 printf("Invalid Character at [%d,%d]\n", i, j);
  25.                 return 1;
  26.             }
  27.             else if (j == 8 && NewLineOrSpace != '\n'){ // if the Grid is finished but the Var doesn't read a new line
  28.                 printf("Invalid Character at [%d,%d]\n", i, j);
  29.                 return 1;
  30.             }
  31.         }
  32.     }
  33.     // Rows Check
  34.     for (i = 0; i < 9; i++){
  35.         if (!IsCorrect(Game[i])){
  36.             printf("Dublicated num at : %d\n" ,i);
  37.             return 1;
  38.         }
  39.     }
  40.     // Columns Check
  41.     char Cols[9];
  42.     for (i = 0; i < 9; i++){
  43.         for (j = 0; j < 9; j++){
  44.             Cols[j] = Game[j][i];
  45.             if (!IsCorrect(Cols[j])){
  46.                 printf("Dublicated num at : %d\n",j);
  47.                 return 1;
  48.             }
  49.         }
  50.     }
  51.     // Blocks Check
  52.     char Blocks[9][9]={ // Sorting The Grid of the sudoku to get the blocks as lines in a 2D array called blocks
  53.     {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]},
  54.     {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]},
  55.     {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]},
  56.  
  57.     {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]},
  58.     {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]},
  59.     {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]},
  60.  
  61.     {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]},
  62.     {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]},
  63.     {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]},
  64.     };
  65.     for (i = 0; i < 9; i++){
  66.         if (!IsCorrect(Blocks[i])){
  67.             printf("Invalid Sudoku at: %d\n",i);
  68.             return 1;
  69.         }
  70.     }
  71.     printf("Correct Sudoku\n"); // If all of the above is invalid just print this sudoku is correct in shape
  72.     return 0;
  73. }
  74. int IsCorrect(char Panel[9]){ // Function for Checking if the Given Panel is Correct
  75.     int CheckSum = 0; // initial value for the Checksum var is set to zero
  76.     for (int i = 0; i < 9; i++){ // Checking every module in the panel whether it's the Cols or Rows
  77.         CheckSum |= (int)pow(2,Panel[i] - '0' - 1);
  78.     }
  79.     /* Oring using bitwise |  between the Zero and the Powers of two
  80.     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
  81.     we get the numbers from 1 to 9 else it returns false and changing the double from the power into int  */
  82.     return CheckSum == 0b111111111;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement