Advertisement
Guest User

Sudoku

a guest
Oct 16th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <fstream>
  4. #include <string>
  5.  
  6. #include "Sudoku.h"
  7.  
  8. using namespace std;
  9.  
  10. node::node()
  11. {
  12.     x = -1;
  13.     y = -1;
  14.     next = NULL;
  15.     prev = NULL;
  16.     val = NULL;
  17.     for (int i = 0; i < 9; i++)
  18.         arrint[i] = 0;
  19. }
  20.  
  21.  
  22. node::~node(){}
  23.  
  24. Sudoku::Sudoku() { head = NULL; tail = NULL; }
  25.  
  26. Sudoku::~Sudoku() {}
  27.  
  28.  
  29. void Sudoku::read_File(char TowD[9][9]) {
  30.     ifstream inFile;
  31.  
  32.     inFile.open("TextFile2.txt");
  33.     if (!inFile)
  34.         cout << "error opening the file";
  35.  
  36.     else {
  37.         char x[100];
  38.         inFile >> x;
  39.        
  40.         int ind = 0;
  41.         for (int i = 0; i < 9; i++)
  42.             for (int j = 0; j < 9; j++) {
  43.                 TowD[i][j] = x[ind];
  44.                 ind++;
  45.  
  46.             }
  47.     }
  48. }
  49. //Read File----------------------------------------------------------------------------------------------------------------------------
  50.  
  51. void Sudoku::search(int x, int y, char TowD[9][9], char arr[9]) {
  52.     int t = 0;
  53.     if (x >= 0 && x <= 2 && y >= 0 && y <= 2) {                     // box 1
  54.         for (int i = 0; i <= 2; i++) {
  55.             for (int j = 0; j <= 2; j++) {
  56.                 if (TowD[i][j] != '0') {
  57.                     arr[t] = TowD[i][j];
  58.                     t++;
  59.                 }
  60.             }
  61.         }
  62.     }
  63.     else if (x >= 0 && x <= 2 && y >= 3 && y <= 5) {                 // box 2
  64.         for (int i = 0; i <= 2; i++) {
  65.             for (int j = 3; j <= 5; j++) {
  66.                 if (TowD[i][j] != '0') {
  67.                     arr[t] = TowD[i][j];
  68.                     t++;
  69.                 }
  70.             }
  71.         }
  72.     }
  73.  
  74.     else if (x >= 0 && x <= 2 && y >= 6 && y <= 8) {                  // box 3
  75.         for (int i = 0; i <= 2; i++) {
  76.             for (int j = 6; j <= 8; j++) {
  77.                 if (TowD[i][j] != '0') {
  78.                     arr[t] = TowD[i][j];
  79.                     t++;
  80.                 }
  81.             }
  82.         }
  83.     }
  84.  
  85.     else if (x >= 3 && x <= 5 && y >= 0 && y <= 2) {                   // box 4
  86.         for (int i = 3; i <= 5; i++) {
  87.             for (int j = 0; j <= 2; j++) {
  88.                 if (TowD[i][j] != '0') {
  89.                     arr[t] = TowD[i][j];
  90.                     t++;
  91.                 }
  92.             }
  93.         }
  94.     }
  95.  
  96.     else if (x >= 3 && x <= 5 && y >= 3 && y <= 5) {                    // box 5
  97.         for (int i = 3; i <= 5; i++) {
  98.             for (int j = 3; j <= 5; j++) {
  99.                 if (TowD[i][j] != '0') {
  100.                     arr[t] = TowD[i][j];
  101.                     t++;
  102.                 }
  103.             }
  104.         }
  105.     }
  106.  
  107.     else if (x >= 3 && x <= 5 && y >= 6 && y <= 8) {                    // box 6
  108.         for (int i = 3; i <= 5; i++) {
  109.             for (int j = 6; j <= 8; j++) {
  110.                 if (TowD[i][j] != '0') {
  111.                     arr[t] = TowD[i][j];
  112.                     t++;
  113.                 }
  114.             }
  115.         }
  116.     }
  117.  
  118.     else if (x >= 6 && x <= 8 && y >= 0 && y <= 2) {                     // box 7
  119.         for (int i = 6; i <= 8; i++) {
  120.             for (int j = 0; j <= 2; j++) {
  121.                 if (TowD[i][j] != '0') {
  122.                     arr[t] = TowD[i][j];
  123.                     t++;
  124.                 }
  125.             }
  126.         }
  127.     }
  128.  
  129.     else if (x >= 6 && x <= 8 && y >= 3 && y <= 5) {                      // box 8
  130.         for (int i = 6; i <= 8; i++) {
  131.             for (int j = 3; j <= 5; j++) {
  132.                 if (TowD[i][j] != '0') {
  133.                     arr[t] = TowD[i][j];
  134.                     t++;
  135.                 }
  136.             }
  137.         }
  138.     }
  139.  
  140.     else if (x >= 6 && x <= 8 && y >= 6 && y <= 8) {                      // box 9
  141.         for (int i = 6; i <= 8; i++) {
  142.             for (int j = 6; j <= 8; j++) {
  143.                 if (TowD[i][j] != '0') {
  144.                     arr[t] = TowD[i][j];
  145.                     t++;
  146.                 }
  147.             }
  148.         }
  149.     }
  150.  
  151.     bool test = false;
  152.     for (int i = 0; i < 9; i++) {
  153.         if (TowD[i][y] != '0') {
  154.             for (int j = 0; j < t; j++) {
  155.                 if (TowD[i][y] != arr[j]) {
  156.                     test = true;
  157.                 }
  158.             }
  159.             if (test == false)
  160.                 arr[t] = TowD[i][y];
  161.             t++;
  162.         }
  163.     }
  164.     for (int j = 0; j < 9; j++) {
  165.         if (TowD[x][j] != '0') {
  166.             for (int i = 0; i < t; i++) {
  167.                 if (TowD[x][i] != arr[j]) {
  168.                     test = true;
  169.                 }
  170.             }
  171.             if (test == false)
  172.                 arr[t] = TowD[x][j];
  173.             t++;
  174.         }
  175.     }
  176. }
  177. //Search-------------------------------------------------------------------------------------------------------------------------------
  178.  
  179. void Sudoku::Store(int i, int j, char TowD[9][9],char chance[9]) {
  180.     char arr[9] = {'0'};
  181.     search(i, j, TowD,arr);
  182.    
  183.     char temp = '1';
  184.     bool test = false;
  185.     int index = 0;
  186.     for (int i = 0; i < 9; i++) {
  187.  
  188.         for (int t = 0; t < 9; t++)
  189.             if(temp==arr[t])test=true;
  190.  
  191.         if (test == false) {
  192.             chance[index]=temp;
  193.             index++;
  194.         }
  195.         temp++;
  196.  
  197.     }
  198. }
  199. //Store Available Number---------------------------------------------------------------------------------------------------------------
  200.  
  201. void Sudoku::addNode(char towd[9][9],int i,int j,char chance[9]) {
  202.    
  203.  
  204.    
  205.  
  206.     if (head == NULL) {
  207.         node* new_node = new node;
  208.         new_node->prev = head;
  209.         head = new_node;
  210.         tail = new_node;
  211.         new_node->x = i;
  212.         new_node->y = j;
  213.  
  214.         for (int i = 0; i < 9; i++)
  215.         new_node->arrint[i] = chance[i];
  216.         new_node->val = &new_node->arrint[0];
  217.         towd[i][j] = *(new_node->val);
  218.     }
  219.     else {
  220.        
  221.         node* new_node = new node;
  222.         for (int i = 0; i < 9; i++)
  223.         new_node->arrint[i] = chance[i];
  224.         new_node->val = &new_node->arrint[0];
  225.         new_node->x = i;
  226.         new_node->y = j;
  227.         new_node->prev = tail;
  228.         tail->next = new_node;
  229.         tail = new_node;
  230.         towd[i][j] = * (new_node->val);
  231.     }
  232. }
  233.  
  234. void Sudoku::backTrack(char towd[9][9],int i, int j) {
  235.     node* P;
  236.     P = tail;
  237.     delete tail;
  238.     P = P->prev;
  239. }
  240.  
  241. void Sudoku::solve(char towd[9][9]) {
  242.  
  243.     char chance[9] = { '0' };
  244.  
  245.     for (int i = 0; i < 9; i++) {
  246.         for (int j = 0; j < 9; j++) {
  247.  
  248.             if (towd[i][j] == '0') {
  249.                 Store(i, j, towd, chance);
  250.  
  251.                 if (chance[0] == '0')
  252.                     backTrack(towd,i,j);
  253.  
  254.                 else {
  255.                     addNode(towd, i, j, chance);
  256.                 }
  257.             }
  258.            
  259.         }
  260.     }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement