|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <time.h> |
| 4 | +//GENERATORE SUDOKU |
| 5 | + |
| 6 | + |
| 7 | +void azzera(int matrice[9][9]); |
| 8 | +void stampa(int matrice[9][9]); |
| 9 | +int antiriga(int i, int j, int matrice[9][9]); |
| 10 | +int anticolonna(int i, int j, int matrice[9][9]); |
| 11 | +int antiquadrato(int i, int j, int matrice[9][9]); |
| 12 | +void genera(int matrice[9][9]); |
| 13 | + |
| 14 | +main() |
| 15 | +{ |
| 16 | + int sudoku[9][9]; |
| 17 | + int sc; |
| 18 | + |
| 19 | + while(1){ |
| 20 | + system("cls"); |
| 21 | + printf("Scegli un'opzione:\n1. Genera 3x3\n2. Esci\n\n-¯ "); |
| 22 | + scanf("%d",&sc); |
| 23 | + system("cls"); |
| 24 | + switch(sc){ |
| 25 | + case 1: |
| 26 | + azzera(sudoku); |
| 27 | + genera(sudoku); |
| 28 | + stampa(sudoku); |
| 29 | + break; |
| 30 | + |
| 31 | + case 2: |
| 32 | + exit(0); |
| 33 | + break; |
| 34 | + |
| 35 | + default: |
| 36 | + printf("Scelta non valida."); |
| 37 | + break; |
| 38 | + } |
| 39 | + printf("\n\n"); |
| 40 | + system("pause"); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +void azzera(int matrice[9][9]) //crea un 9x9 di zeri |
| 45 | +{ |
| 46 | + int i,j; |
| 47 | + for(i=0;i<9;i++) |
| 48 | + { |
| 49 | + for(j=0;j<9;j++) |
| 50 | + { |
| 51 | + matrice[i][j]=0; |
| 52 | + } |
| 53 | + } |
| 54 | + return; |
| 55 | +} |
| 56 | + |
| 57 | +void stampa(int matrice[9][9]) //visualizza sudoku |
| 58 | +{ |
| 59 | + int i,j; |
| 60 | + printf("\n\t Sudoku 3x3 generato:\n\n\tÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄ¿\n"); |
| 61 | + for(i=0;i<9;i++) |
| 62 | + { |
| 63 | + for(j=0;j<9;j++) |
| 64 | + { |
| 65 | + if(j==0) |
| 66 | + { |
| 67 | + printf("\t³ "); |
| 68 | + } |
| 69 | + printf("%d ",matrice[i][j]); |
| 70 | + if((j+1)%3==0) |
| 71 | + { |
| 72 | + printf("³ "); |
| 73 | + } |
| 74 | + } |
| 75 | + if((i+1)%3==0 && i!=8) |
| 76 | + { |
| 77 | + printf("\n\tÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄ´\n"); |
| 78 | + continue; |
| 79 | + } |
| 80 | + printf("\n"); |
| 81 | + } |
| 82 | + printf("\tÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÙ\n"); |
| 83 | + return; |
| 84 | +} |
| 85 | + |
| 86 | +int antiriga(int i, int j, int matrice[9][9]) //CONTROLLO ANTI-RIPETIZIONE RIGA |
| 87 | +{ |
| 88 | + int s; |
| 89 | + for(s=0; s<j; s++) |
| 90 | + { |
| 91 | + if(matrice[i][j] == matrice[i][s]) |
| 92 | + { |
| 93 | + return 0; |
| 94 | + } |
| 95 | + } |
| 96 | + return 1; |
| 97 | +} |
| 98 | + |
| 99 | +int anticolonna(int i, int j, int matrice[9][9]) //CONTROLLO ANTI-RIPETIZIONE COLONNA |
| 100 | +{ |
| 101 | + int s; |
| 102 | + for(s=0; s<i; s++) |
| 103 | + { |
| 104 | + if(matrice[i][j] == matrice[s][j]) |
| 105 | + { |
| 106 | + return 0; |
| 107 | + } |
| 108 | + } |
| 109 | + return 1; |
| 110 | +} |
| 111 | + |
| 112 | +int antiquadrato(int i, int j, int matrice[9][9]) //CONTROLLO ANTI-QUADRATO |
| 113 | +{ |
| 114 | + int s,c; |
| 115 | + if(i>=i-i%3 && i<3+i-i%3) |
| 116 | + { |
| 117 | + if(j>=j-j%3 && j<3+j-j%3) |
| 118 | + { |
| 119 | + for(s=i-i%3 ; s<3+i-i%3 ; s++) |
| 120 | + { |
| 121 | + for(c=j-j%3 ; c<3+j-j%3 ; c++) |
| 122 | + { |
| 123 | + if(matrice[i][j]==matrice[s][c] && (s!=i && c!=j)) |
| 124 | + { |
| 125 | + return 0; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + return 1; |
| 132 | +} |
| 133 | + |
| 134 | +void genera(int matrice[9][9]) //generazione del sudoku |
| 135 | +{ |
| 136 | + int i,j,c; |
| 137 | + int ripetizione=0; |
| 138 | + |
| 139 | + srand(time(NULL)); |
| 140 | + for(i=0; i<9; i++) |
| 141 | + { |
| 142 | + for(j=0; j<9; j++) |
| 143 | + { |
| 144 | + if(ripetizione>35) //CANCELLA RIGA DOPO TOT RIPETIZIONI |
| 145 | + { |
| 146 | + for(c=0;c<9;c++) |
| 147 | + { |
| 148 | + matrice[i][c]=0; |
| 149 | + } |
| 150 | + i--; |
| 151 | + ripetizione=0; |
| 152 | + } |
| 153 | + matrice[i][j]=(rand() % 9)+1; //GENERAZIONE VALORE |
| 154 | + |
| 155 | + if(antiriga(i,j,matrice)==0 || anticolonna(i,j,matrice)==0 || antiquadrato(i,j,matrice)==0) //CONTROLLO ANTI-RIPETIZIONE RIGA E COLONNA |
| 156 | + { |
| 157 | + ripetizione++; |
| 158 | + j--; |
| 159 | + continue; |
| 160 | + } |
| 161 | + ripetizione=0; |
| 162 | + } |
| 163 | + } |
| 164 | + return; |
| 165 | +} |
0 commit comments