Advertisement
Guest User

sudokuCleared

a guest
Mar 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define max 9
  6.  
  7. using namespace std;
  8.  
  9. void kreirajTablu(int grid[max][max], int size)
  10. {
  11.     int i,j;
  12.     int random, randbr;
  13.     srand(time(0));
  14.     for(i = 0; i < size; i++)
  15.     {
  16.         for(j = 0; j < size; j++)
  17.         {
  18.             random = rand() % 100;
  19.             if(random < 35)
  20.             {
  21.                 randbr = 1 + rand() % 9;
  22.                 grid[i][j] = randbr;
  23.             }
  24.         }
  25.     }
  26. }
  27.  
  28. void ispisTable(int grid[max][max], int size)
  29. {
  30.     int a,b;
  31.     cout << "Evo je trenutna tabla" << endl;
  32.     cout << "Prvo unosis poziciju pa broj" << endl << endl;;
  33.     cout << "   1 2 3 4 5 6 7 8 9" << endl;
  34.     cout << "  __________________" << endl;
  35.     for(a = 0; a < size; a++)
  36.     {
  37.         cout << a+1 << "| ";
  38.         for(b = 0; b < size; b++)
  39.         {
  40.             cout << grid[a][b] << " ";
  41.         }
  42.             cout << endl;
  43.     }
  44. }
  45.  
  46. void proveraTable(int grid[max][max], int size)
  47. {
  48.     int j,k,z;
  49.     for(k = 0; k < size; k++)
  50.     {
  51.         for(j = 0; j < size; j++)
  52.         {
  53.             for(z = 0; z < size; z++)
  54.             {
  55.                 if(grid[k][j] == grid[k][z])
  56.                 {
  57.                     if(j == z){
  58.                         continue;
  59.                     }
  60.                     grid[k][z] = 0;
  61.                 }
  62.             }
  63.         }
  64.     }
  65.  
  66.     for(k = 0; k < size; k++)
  67.     {
  68.         for(j = 0; j < size; j++)
  69.         {
  70.             for(z = 0; z < size; z++)
  71.             {
  72.                 if(grid[j][k] == grid[z][k])
  73.                 {
  74.                     if(j == z)
  75.                     {
  76.                         continue;
  77.                     }
  78.                     grid[k][z] = 0;
  79.                 }
  80.             }
  81.         }
  82.     }
  83. }
  84.  
  85. void kraj(int grid[max][max], int size)
  86. {
  87.     int i,j;
  88.     int brpoz = 0;
  89.     for(i = 0; i < size; i++)
  90.     {
  91.         for(j = 0; j < size; j++)
  92.         {
  93.             if(grid[i][j] != 0)
  94.             {
  95.                 brpoz++;
  96.             }
  97.         }
  98.         if(brpoz == 81)
  99.         {
  100.             cout << "Kraj. Pobedio si." << endl;
  101.         }
  102.     }
  103. }
  104.  
  105. void check(int grid[max][max], int size, bool *T, int x, int y, int v)
  106. {
  107.     int i,j;
  108.     *T = true;
  109.     for(i = 0; i < size; i++)
  110.         {
  111.             for(j = 0; j < size; j++)
  112.             {
  113.                 if(grid[x][y] == grid[x][j])
  114.                 {
  115.                     if(x == j)
  116.                     {
  117.                         continue;
  118.                     }
  119.                     cout << "Ne moze." << endl;
  120.                     grid[x][j] = 0;
  121.                     *T = false;
  122.                     break;
  123.                }
  124.                if(grid[x][y] == grid[j][y])
  125.                {
  126.                    if(y == j)
  127.                    {
  128.                        continue;
  129.                    }
  130.                    cout << "Ne moze." << endl;
  131.                    grid[j][y] = 0;
  132.                    *T = false;
  133.                    break;
  134.                }
  135.             }
  136.         }
  137. }
  138.  
  139. void proveraPozicija(int x, int y, int v, bool *pT)
  140. {
  141.     if((x >= 10) || (y >= 10) || (v >= 10))
  142.         {
  143.             cout << "Unosis pogresno" << endl;  //PROVERA UNOSA
  144.             pT = false;
  145.         }
  146. }
  147.  
  148. class pozicija
  149. {
  150.     public:
  151.     int x;
  152.     int y;
  153.     int broj;
  154. };
  155.  
  156. pozicija poz;
  157.  
  158. int main()
  159. {
  160.     int tabla[9][9] = {0};
  161.     kreirajTablu(tabla,9);
  162.     proveraTable(tabla,9);
  163.     ispisTable(tabla, 9);
  164.     bool T = true;
  165.     bool pT = true;
  166.  
  167.     while(1)
  168.     {
  169.         pocetak:
  170.         cout << "Izaberi poziciju. Prvo koordinate pa broj." << endl;
  171.         cin >> poz.x >> poz.y >> poz.broj;
  172.         poz.x--; poz.y--;
  173.         tabla[poz.x][poz.y] = poz.broj;
  174.         proveraPozicija(poz.x, poz.y, poz.broj, &pT);
  175.         if(pT == false)
  176.         {
  177.             goto pocetak;
  178.         }
  179.         check(tabla, 9, &T, poz.x, poz.y, poz.broj);
  180.         if(T == false)
  181.         {
  182.             goto pocetak;
  183.         }
  184.         ispisTable(tabla,9);
  185.         kraj(tabla,9);
  186.     }
  187.    
  188.  
  189.     return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement