Advertisement
Guest User

commit sudoku

a guest
Mar 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 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, )
  106. {
  107.     int i,j;
  108.     for(i = 0; i < size; i++)
  109.         {
  110.             for(j = 0; j < size; j++)
  111.             {
  112.                 if(grid[poz.x][poz.y] == grid[poz.x][j])
  113.                 {
  114.                     if(poz.x == j)
  115.                     {
  116.                         continue;
  117.                     }
  118.                     cout << "Ne moze." << endl;
  119.                     grid[poz.x][j] = 0;
  120.                     *T = false;
  121.                }
  122.                if(grid[poz.x][poz.y] == grid[j][poz.y])
  123.                {
  124.                    if(poz.y == j)
  125.                    {
  126.                        continue;
  127.                    }
  128.                    cout << "Ne moze." << endl;
  129.                    grid[j][poz.y] = 0;
  130.                    *T = false;
  131.                }
  132.             }
  133.         }
  134. }
  135.  
  136. class pozicija
  137. {
  138.     public:
  139.     int x;
  140.     int y;
  141.     int broj;
  142. };
  143.  
  144. pozicija poz;
  145.  
  146. int main()
  147. {
  148.     int tabla[9][9] = {0};
  149.     kreirajTablu(tabla,9);
  150.     proveraTable(tabla,9);
  151.     ispisTable(tabla, 9);
  152.  
  153.     while(1)
  154.     {
  155.         pocetak:
  156.         cout << "Izaberi poziciju. Prvo koordinate pa broj." << endl;
  157.         cin >> poz.x >> poz.y >> poz.broj;
  158.         poz.x--; poz.y--;
  159.         tabla[poz.x][poz.y] = poz.broj;
  160.         if((poz.x >= 10) || (poz.y >= 10) || (poz.broj >= 10))
  161.         {
  162.             cout << "Unosis pogresno" << endl;  //PROVERA UNOSA
  163.             goto pocetak;
  164.         }
  165.         check();
  166.         ispisTable(tabla,9);
  167.         kraj(tabla,9);
  168.     }
  169.    
  170.  
  171.     return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement