|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <time.h> |
| 4 | + |
| 5 | +void azzera(int matrice[9][9]); |
| 6 | +int antiriga(int i, int j, int matrice[9][9]); |
| 7 | +int anticolonna(int i, int j, int matrice[9][9]); |
| 8 | +int antiquadrato(int i, int j, int matrice[9][9]); |
| 9 | +void genera(int matrice[9][9]); |
| 10 | +void crea_puzzle(int q, int stampo_[9][9], int sudoku_[9][9]); |
| 11 | +void schema(int sudoku[9][9]); |
| 12 | +void salva(int sudoku[9][9]); |
| 13 | +void cotrolla(int sudoku[9][9]); |
| 14 | +void gioca(int q, int sudoku_[9][9], int def[9][9]); |
| 15 | +int carica(int *q, int sudoku_[9][9]); |
| 16 | +void riprendi(int q, int sudoku_[9][9]); |
| 17 | + |
| 18 | +main() |
| 19 | +{ |
| 20 | + int sudoku[9][9], stampo[9][9]; |
| 21 | + int sc; |
| 22 | + |
| 23 | + while(1){ |
| 24 | + int quantita=0; |
| 25 | + system("cls"); |
| 26 | + printf("Sudoku 3x3:\n1. Nuova partita\n2. Carica partita salvata\n3. Esci dal gioco\n\n-¯ "); |
| 27 | + scanf("%d",&sc); |
| 28 | + system("cls"); |
| 29 | + switch(sc){ |
| 30 | + case 1: |
| 31 | + azzera(stampo); //azzera(stampo,sudoku); |
| 32 | + azzera(sudoku); |
| 33 | + genera(stampo); |
| 34 | + do{ |
| 35 | + system("cls"); |
| 36 | + printf("Inserisci la quantit… di valori da avere:\n(min:16 / max:32)-¯ "); |
| 37 | + scanf("%d",&quantita); |
| 38 | + }while(quantita<16 || quantita>32); |
| 39 | + crea_puzzle(quantita,stampo,sudoku); |
| 40 | + gioca(quantita,sudoku,sudoku); |
| 41 | + break; |
| 42 | + |
| 43 | + case 2: |
| 44 | + if(carica(&quantita,sudoku)) |
| 45 | + { |
| 46 | + riprendi(quantita,sudoku); |
| 47 | + } |
| 48 | + break; |
| 49 | + |
| 50 | + case 3: |
| 51 | + exit(0); |
| 52 | + break; |
| 53 | + |
| 54 | + default: |
| 55 | + printf("Scelta non valida.\n\n"); |
| 56 | + system("pause"); |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +void azzera(int matrice[9][9]) //crea un 9x9 di zeri |
| 63 | +{ |
| 64 | + int i,j; |
| 65 | + for(i=0;i<9;i++) |
| 66 | + { |
| 67 | + for(j=0;j<9;j++) |
| 68 | + { |
| 69 | + matrice[i][j]=0; |
| 70 | + } |
| 71 | + } |
| 72 | + return; |
| 73 | +} |
| 74 | + |
| 75 | +int antiriga(int i, int j, int matrice[9][9]) //CONTROLLO ANTI-RIPETIZIONE RIGA |
| 76 | +{ |
| 77 | + int s; |
| 78 | + for(s=0; s<j; s++) |
| 79 | + { |
| 80 | + if(matrice[i][j] == matrice[i][s]) |
| 81 | + { |
| 82 | + return 0; |
| 83 | + } |
| 84 | + } |
| 85 | + return 1; |
| 86 | +} |
| 87 | + |
| 88 | +int anticolonna(int i, int j, int matrice[9][9]) //CONTROLLO ANTI-RIPETIZIONE COLONNA |
| 89 | +{ |
| 90 | + int s; |
| 91 | + for(s=0; s<i; s++) |
| 92 | + { |
| 93 | + if(matrice[i][j] == matrice[s][j]) |
| 94 | + { |
| 95 | + return 0; |
| 96 | + } |
| 97 | + } |
| 98 | + return 1; |
| 99 | +} |
| 100 | + |
| 101 | +int antiquadrato(int i, int j, int matrice[9][9]) //CONTROLLO ANTI-QUADRATO |
| 102 | +{ |
| 103 | + int s,c; |
| 104 | + if(i>=i-i%3 && i<3+i-i%3) |
| 105 | + { |
| 106 | + if(j>=j-j%3 && j<3+j-j%3) |
| 107 | + { |
| 108 | + for(s=i-i%3 ; s<3+i-i%3 ; s++) |
| 109 | + { |
| 110 | + for(c=j-j%3 ; c<3+j-j%3 ; c++) |
| 111 | + { |
| 112 | + if(matrice[i][j]==matrice[s][c] && (s!=i && c!=j)) |
| 113 | + { |
| 114 | + return 0; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + return 1; |
| 121 | +} |
| 122 | + |
| 123 | +void genera(int matrice[9][9]) //Genera sudoku |
| 124 | +{ |
| 125 | + int i,j,c; |
| 126 | + int ripetizione=0; |
| 127 | + |
| 128 | + srand(time(NULL)); |
| 129 | + for(i=0; i<9; i++) |
| 130 | + { |
| 131 | + for(j=0; j<9; j++) |
| 132 | + { |
| 133 | + if(ripetizione>35) //CANCELLA RIGA DOPO TOT RIPETIZIONI |
| 134 | + { |
| 135 | + for(c=0;c<9;c++) |
| 136 | + { |
| 137 | + matrice[i][c]=0; |
| 138 | + } |
| 139 | + i--; |
| 140 | + ripetizione=0; |
| 141 | + } |
| 142 | + matrice[i][j]=(rand() % 9)+1; //GENERAZIONE VALORE |
| 143 | + |
| 144 | + if(antiriga(i,j,matrice)==0 || anticolonna(i,j,matrice)==0 || antiquadrato(i,j,matrice)==0) //CONTROLLO ANTI-RIPETIZIONE RIGA E COLONNA E ANTI-QUADRATO |
| 145 | + { |
| 146 | + ripetizione++; |
| 147 | + j--; |
| 148 | + continue; |
| 149 | + } |
| 150 | + ripetizione=0; |
| 151 | + } |
| 152 | + } |
| 153 | + return; |
| 154 | +} |
| 155 | + |
| 156 | +void crea_puzzle(int q, int stampo_[9][9], int sudoku_[9][9]) //CREA PUZZLE |
| 157 | +{ |
| 158 | + int i,x,y; |
| 159 | + |
| 160 | + srand(time(NULL)); |
| 161 | + for(i=0; i<q; i++) |
| 162 | + { |
| 163 | + x=(rand() % 9)+1; //coordinata X |
| 164 | + y=(rand() % 9)+1; //coordinata Y |
| 165 | + if(sudoku_[x][y]!=0) |
| 166 | + { |
| 167 | + i--; |
| 168 | + continue; |
| 169 | + } |
| 170 | + sudoku_[x][y]=stampo_[x][y]; |
| 171 | + } |
| 172 | + return; |
| 173 | +} |
| 174 | + |
| 175 | +void schema(int sudoku[9][9]) //formazione del gioco |
| 176 | +{ |
| 177 | + int i,j; |
| 178 | + printf("\t 0 1 2 3 4 5 6 7 8\n"); |
| 179 | + printf("\t ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄ¿\n"); |
| 180 | + for(i=0;i<9;i++) |
| 181 | + { |
| 182 | + printf("\t%d ",i); |
| 183 | + for(j=0;j<9;j++) |
| 184 | + { |
| 185 | + if(j==0) |
| 186 | + { |
| 187 | + printf("³ "); |
| 188 | + } |
| 189 | + printf("%d ",sudoku[i][j]); |
| 190 | + if((j+1)%3==0) |
| 191 | + { |
| 192 | + printf("³ "); |
| 193 | + } |
| 194 | + } |
| 195 | + if((i+1)%3==0 && i!=8) |
| 196 | + { |
| 197 | + printf("\n\t ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄ´\n"); |
| 198 | + continue; |
| 199 | + } |
| 200 | + printf("\n"); |
| 201 | + } |
| 202 | + printf("\t ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÙ\n"); |
| 203 | + return; |
| 204 | +} |
| 205 | + |
| 206 | +void salva(int sudoku[9][9]) //salva partita |
| 207 | +{ |
| 208 | + FILE *save; |
| 209 | + int i,j; |
| 210 | + save=fopen("salvataggio.txt","w"); |
| 211 | + for(i=0; i<9; i++) |
| 212 | + { |
| 213 | + for(j=0; j<9; j++) |
| 214 | + { |
| 215 | + fprintf(save,"%d\n",sudoku[i][j]); |
| 216 | + } |
| 217 | + } |
| 218 | + fclose(save); |
| 219 | + return; |
| 220 | +} |
| 221 | + |
| 222 | +void controlla(int sudoku[9][9]) |
| 223 | +{ |
| 224 | + int i,j; |
| 225 | + system("cls"); |
| 226 | + printf("Controllo del sudoku in corso..."); |
| 227 | + for(i=0; i<9; i++) |
| 228 | + { |
| 229 | + for(j=0; j<9; j++) |
| 230 | + { |
| 231 | + if(antiriga(i,j,sudoku)==0 || anticolonna(i,j,sudoku)==0 || antiquadrato(i,j,sudoku)==0) //CONTROLLO ANTI-RIPETIZIONE RIGA E COLONNA |
| 232 | + { |
| 233 | + printf("\nSudoku non valido:\n\n"); |
| 234 | + schema(sudoku); |
| 235 | + printf("\n\n"); |
| 236 | + system("pause"); |
| 237 | + return; |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | + printf("\nComplimenti, è corretto!\n\n"); |
| 242 | + system("pause"); |
| 243 | + return; |
| 244 | +} |
| 245 | + |
| 246 | +void gioca(int q, int sudoku_[9][9], int def[9][9]) //gioca |
| 247 | +{ |
| 248 | + int i,x,y; |
| 249 | + |
| 250 | + for(i=0; i+q<81; i++) |
| 251 | + { |
| 252 | + system("cls"); |
| 253 | + printf("Per tornare al men— metti 9-9, la partita si salver… in automatico.\nSudoku 3x3 - completamento %d/81 ~ %.2f%%:\nX:riga - Y:colonna\n\n",(i+q),(float)(i+q)*100/81); |
| 254 | + schema(sudoku_); |
| 255 | + printf("\n\n"); |
| 256 | + do{ |
| 257 | + printf("Posizione(X-Y)-¯ "); |
| 258 | + scanf("%d-%d",&x,&y); |
| 259 | + }while((x<0 || x>9) || (y<0 || y>9)); |
| 260 | + |
| 261 | + if(x==9 && y==9) //torna al menù |
| 262 | + { |
| 263 | + salva(sudoku_); |
| 264 | + printf("\nPartita salvata su 'salvataggio.txt'.\n"); |
| 265 | + system("pause"); |
| 266 | + return; |
| 267 | + } |
| 268 | + else if(x==9 || y==9) //scelta non valida |
| 269 | + { |
| 270 | + printf("\nSe vuoi tornare indietro metti 9-9, non uno solo.\n"); |
| 271 | + i--; |
| 272 | + system("pause"); |
| 273 | + } |
| 274 | + else |
| 275 | + { |
| 276 | + if(def[y][x]!=0) |
| 277 | + { |
| 278 | + printf("\nNon puoi rimpiazzare un valore dato dal gioco!\n"); |
| 279 | + system("pause"); |
| 280 | + i--; |
| 281 | + continue; |
| 282 | + } |
| 283 | + do{ |
| 284 | + printf("Valore-¯ "); |
| 285 | + scanf("%d",&sudoku_[y][x]); |
| 286 | + }while(sudoku_[y][x]<1 || sudoku_[y][x]>9); |
| 287 | + printf("\nValore aggiunto correttamente in: %d-%d\n",x,y); |
| 288 | + system("pause"); |
| 289 | + } |
| 290 | + } |
| 291 | + controlla(sudoku_); |
| 292 | + return; |
| 293 | +} |
| 294 | + |
| 295 | +int carica(int *q, int sudoku_[9][9]) //carica partita |
| 296 | +{ |
| 297 | + FILE *load; |
| 298 | + int i,j,x; |
| 299 | + |
| 300 | + load=fopen("salvataggio.txt","r"); |
| 301 | + if(load==NULL) |
| 302 | + { |
| 303 | + printf("\nNon ci sono partite salvate... Devi iniziarne una nuova.\n\n"); |
| 304 | + system("pause"); |
| 305 | + return 0; |
| 306 | + } |
| 307 | + else |
| 308 | + { |
| 309 | + for(i=0, j=0, x=0; !feof(load); j++) |
| 310 | + { |
| 311 | + if(j==9) |
| 312 | + { |
| 313 | + i++; |
| 314 | + j=0; |
| 315 | + } |
| 316 | + fscanf(load,"%d",&sudoku_[i][j]); |
| 317 | + if(sudoku_[i][j]!=0) |
| 318 | + { |
| 319 | + x++; |
| 320 | + } |
| 321 | + } |
| 322 | + *q=x; |
| 323 | + printf("\nParita caricata con successo!\n\n"); |
| 324 | + system("pause"); |
| 325 | + } |
| 326 | + fclose(load); |
| 327 | + return 1; |
| 328 | +} |
| 329 | + |
| 330 | +void riprendi(int q, int sudoku_[9][9]) //gioca la partita vecchia |
| 331 | +{ |
| 332 | + int i,x,y; |
| 333 | + for(i=0; i+q<81; i++) |
| 334 | + { |
| 335 | + system("cls"); |
| 336 | + printf("Per tornare al men— metti 9-9, la partita si salver… in automatico.\nSudoku 3x3 - completamento %d/81 ~ %.2f%%:\nX:riga - Y:colonna\n\n",(i+q),(float)(i+q)*100/81); |
| 337 | + schema(sudoku_); |
| 338 | + printf("\n\n"); |
| 339 | + do{ |
| 340 | + printf("Posizione(X-Y)-¯ "); |
| 341 | + scanf("%d-%d",&x,&y); |
| 342 | + }while((x<0 || x>9) || (y<0 || y>9)); |
| 343 | + |
| 344 | + if(x==9 && y==9) //torna al menù |
| 345 | + { |
| 346 | + salva(sudoku_); |
| 347 | + printf("\nPartita salvata su 'salvataggio.txt'.\n"); |
| 348 | + system("pause"); |
| 349 | + return; |
| 350 | + } |
| 351 | + else if(x==9 || y==9) //scelta non valida |
| 352 | + { |
| 353 | + printf("\nSe vuoi tornare indietro metti 9-9, non uno solo.\n"); |
| 354 | + system("pause"); |
| 355 | + i--; |
| 356 | + } |
| 357 | + else |
| 358 | + { |
| 359 | + do{ |
| 360 | + printf("Valore-¯ "); |
| 361 | + scanf("%d",&sudoku_[y][x]); |
| 362 | + }while(sudoku_[y][x]<1 || sudoku_[y][x]>9); |
| 363 | + printf("\nValore aggiunto correttamente in: %d-%d\n",x,y); |
| 364 | + system("pause"); |
| 365 | + } |
| 366 | + } |
| 367 | + controlla(sudoku_); |
| 368 | + return; |
| 369 | +} |
0 commit comments