Advertisement
Guest User

kod

a guest
Nov 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include "spis.h"
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <cstdlib>
  5. #include <ctime>
  6.  
  7. using namespace std;
  8.  
  9. int main(){
  10.     sudoku p1;
  11.     p1.wyczysc();
  12.     p1.petle_for();
  13.  
  14.        
  15.        
  16.     p1.wyswietl();
  17.  
  18.    
  19.        
  20.    
  21.  
  22.     return 0;
  23. }
  24.    
  25.  
  26.  
  27.  
  28. #include "spis.h"
  29.  
  30. #include <iostream>
  31. #include <conio.h>
  32. #include <cstdlib>
  33. #include <ctime>
  34.  
  35. using namespace std;
  36.  
  37. void sudoku::wyczysc(){
  38.     for(int cx=8;cx>=0;cx--){
  39.         for(int cy=0;cy<=8;cy++){
  40.             tab[cx][cy]=0;
  41.         }
  42.     }
  43.     l2=0;
  44. }
  45. void sudoku::losuj(){
  46.     tab[y-1][x-1]=rand()%9+1;
  47. }
  48. void sudoku::spr_pion(){
  49.     if(x+y!=2)
  50.     for(int a=2;a<10;a++){
  51.         if(x-a>=0){
  52.             if(tab[y-1][x-1]==tab[y-1][x-a]){
  53.                 x--;licznik++;i=1;
  54.             }
  55.         }
  56.     }
  57. }
  58. void sudoku::spr_poz(){
  59.     if(x+y!=2)
  60.     for(int a=2;a<10;a++){
  61.         if(y-a>=0){
  62.             if(tab[y-1][x-1]==tab[y-a][x-1]){
  63.                 x--;licznik++;i=1;
  64.             }
  65.         }
  66.     }
  67. }
  68. void sudoku::spr_reszte_3x3(){
  69.     if(y%3!=1){
  70.         yz=(y-1)%3;
  71.         xz=(x-1)%3;
  72.         for(int c=1;c<3;c++){
  73.             if(yz-c>=0){
  74.                 if(xz==0){
  75.                     if(tab[y-1][x-1]==tab[y-c-1][x]){
  76.                         x--;
  77.                         licznik++;
  78.                         i=1;
  79.                     }
  80.                     if(tab[y-1][x-1]==tab[y-c-1][x+1]){
  81.                         x--;
  82.                         licznik++;
  83.                         i=1;
  84.                     }
  85.                 }
  86.                 if(xz==1){
  87.                     if(tab[y-1][x-1]==tab[y-c-1][x-2]){
  88.                         x--;
  89.                         licznik++;
  90.                         i=1;
  91.                     }
  92.                     if(tab[y-1][x-1]==tab[y-c-1][x]){
  93.                         x--;
  94.                         licznik++;
  95.                         i=1;
  96.                     }
  97.                 }
  98.                 if(xz==2){
  99.                     if(tab[y-1][x-1]==tab[y-c-1][x-2]){
  100.                         x--;
  101.                         licznik++;
  102.                         i=1;
  103.                     }
  104.                     if(tab[y-1][x-1]==tab[y-c-1][x-3]){
  105.                         x--;
  106.                         licznik++;
  107.                         i=1;
  108.                     }
  109.                 }
  110.             }  
  111.         }  
  112.     }
  113. }
  114. void sudoku::zerowanie(){
  115.     if(licznik==100){cout<<"q";x=0;l2++;}
  116.     if(l2==100){cout<<"!";x=0;y=1;l2=0;}
  117.     if(i==0)licznik=0;
  118. }
  119. void sudoku::petle_for(){
  120.     for(y=1;y<10;y++){
  121.         for(x=1;x<10;x++){
  122.         losuj();
  123.         i=0;
  124.         spr_pion();
  125.         spr_poz(); 
  126.         spr_reszte_3x3();
  127.         zerowanie();
  128.         }
  129.     cout<<endl;
  130.     }
  131. }
  132. void sudoku::wyswietl(){
  133.     for(y=1;y<10;y++){
  134.         if(y==1)cout<<" - - - - - - - - - - - -"<<endl;
  135.         for(x=1;x<10;x++){
  136.             if(x==1)cout<<"|";
  137.                 cout<<" "<<tab[y-1][x-1];
  138.             if(x%3==0)cout<<" |";
  139.         }
  140.         cout<<endl;
  141.         if(y%3==0)cout<<" - - - - - - - - - - - -"<<endl;  
  142.     }
  143. }
  144.  
  145.  
  146. #include <iostream>
  147. #include <conio.h>
  148. #include <cstdlib>
  149. #include <ctime>
  150.  
  151. using namespace std;
  152.  
  153. class sudoku{
  154.     public:
  155.         int r;
  156.         int x,y;
  157.         int tab[9][9];
  158.         int licznik,l2;    
  159.         int i;
  160.         int xz,yz;
  161.         void wyczysc();             // zerowanie tablicy
  162.         void losuj();               // losowanie liczny dla elementu tablicy
  163.         void spr_pion();            // sprawdza czy liczba już jest pionowo 1-9
  164.         void spr_poz();             // sprawdza czy liczba już jest poziomo 1-9
  165.         void spr_reszte_3x3();      // sprawdza czy liczba już jest pozostałych polach niż pionowo i poziomo w kwadratach 3x3 1-9
  166.         void zerowanie();           // zeruje liczniki jeżeli wpisze liczbe do tabeli, jezeli nie znajdzie liczby zaczyna algorytm od nowa
  167.         void petle_for();           // 2 pętle for liczące od 1 do 9
  168.         void wyswietl();            // wyświetla sudoku
  169. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement