Generateur de grille sudoku
nadia hareb
Messages postés
122
Statut
Membre
-
toxic-design Messages postés 9 Statut Membre -
toxic-design Messages postés 9 Statut Membre -
Bonjour,
je ne peux pas savoir ce quoi le prob avec ce code en c; please help!!!!!!!!
comme vous allez remarquer le prob seras dans la fonction srand.........
#include<stdio.h>
#include <conio.h>
#include<stdlib.h>
const int size = 9;
long long Num = 0;
int Grille[size][size];
int GrilleE[size][size] = // Grille relative au niveau Facil (Easy)
{
5,0,0,0,0,2,9,1,0,
0,0,0,0,9,0,0,2,3,
0,9,1,0,3,4,8,0,0,
6,0,0,0,0,1,0,0,0,
4,3,0,7,6,9,0,8,5,
0,0,0,2,0,0,0,0,7,
0,0,5,9,2,0,6,3,0,
3,2,0,0,5,0,0,0,0,
0,7,9,6,0,0,0,0,2
};
int GrilleM[size][size] = // Grille relative au niveau Moyen (Medium)
{
3,0,0,0,0,5,4,8,1,
1,0,5,0,3,0,0,0,0,
0,0,0,0,0,4,0,0,6,
9,0,2,5,8,0,0,0,0,
0,7,0,0,4,0,0,6,0,
0,0,0,0,6,9,7,0,5,
8,0,0,4,0,0,0,0,0,
0,0,0,0,1,0,5,0,4,
2,3,4,9,0,0,0,0,7
};
int GrilleH[size][size] = // Grille relative niveau Difficil (Hard)
{
0,0,0,4,0,0,3,0,0,
7,1,0,2,0,0,8,0,0,
0,0,0,0,7,0,0,0,6,
9,0,0,1,0,0,0,5,0,
6,0,1,0,0,0,4,0,8,
0,3,0,0,0,4,0,0,7,
8,0,0,0,9,0,0,1,0,
0,0,7,0,0,2,0,6,5,
0,0,9,0,0,6,0,0,0
};
int GrilleG[size][size] = // Niveau impossible... ^^
{
0,0,0,0,0,5,0,6,0,
0,0,0,0,0,2,0,0,1,
0,2,7,0,0,0,8,0,0,
0,9,0,0,0,0,0,0,4,
0,6,0,0,1,0,0,9,5,
1,0,0,3,0,0,0,7,0,
0,0,3,0,0,0,6,1,0,
4,0,0,9,0,0,0,0,0,
0,8,0,7,0,0,0,0,0
};
bool Lin[size][size];
bool Col[size][size];
bool Sq[size][size];
FILE *fout;
int pos = 0;
int save[size][size];
int NB[size+1];
void InvCol(int col1, int col2) // Inverse 2 colonnes
{
int Col;
for (int i=0 ; i<size ; i++)
{
Col = Grille[col1-1][i];
Grille[col1-1][i] = Grille[col2-1][i];
Grille[col2-1][i] = Col;
}
}
void InvLin(int lin1, int lin2) // Inverse 2 Lignes
{
int Col;
for (int i=0 ; i<size ; i++)
{
Col = Grille[i][lin1-1];
Grille[i][lin1-1] = Grille[i][lin2-1];
Grille[i][lin2-1] = Col;
}
}
void InvColC(int col1, int col2) // Inverse des carré (sur les colonnes)
{
int Col[3][size];
for (int i=0 ; i<size ; i++)
for (int j=0 ; j<3 ; j++)
Col[j][i] = Grille[(col1-1)*3+j][i];
for (int i=0 ; i<size ; i++)
for (int j=0 ; j<3 ; j++)
Grille[(col1-1)*3+j][i] = Grille[(col2-1)*3+j][i];
for (int i=0 ; i<size ; i++)
for (int j=0 ; j<3 ; j++)
Grille[(col2-1)*3+j][i] = Col[j][i] ;
}
void InvLinC(int col1, int col2) // Inverse les carrés (sur les lignes)
{
int Col[size][3];
for (int i=0 ; i<size ; i++)
for (int j=0 ; j<3 ; j++)
Col[i][j] = Grille[i][(col1-1)*3+j];
for (int i=0 ; i<size ; i++)
for (int j=0 ; j<3 ; j++)
Grille[i][(col1-1)*3+j] = Grille[i][(col2-1)*3+j];
for (int i=0 ; i<size ; i++)
for (int j=0 ; j<3 ; j++)
Grille[i][(col2-1)*3+j] = Col[i][j] ;
}
void Make(int G[9][9]) // Crée une source
{
bool Nb[size+1] = {false,false,false,false,false,false,false,false,false,false};
// Tableau qui enregistre le nombre d'un nombre ^^
// 1 -> Nb[1] à chaque 1 mettre Nb[1];
// 2 -> Nb[2] à chaque 2 mettre Nb[2];
// 3 -> Nb[3] à chaque 3 mettre Nb[3];
for (int i = 1 ; i <= size ; i++)
{
int nbr = rand()%9 +1; // Nbr aléatoire
while (Nb[nbr]) nbr = rand()%9 +1;// On prend un nombre aléatoire pas encore pris
NB[i] = nbr; // On le met dans Nb
Nb[nbr] = true; // On indique qu'il a été pris
}
for (int lin = 0 ; lin < size ; lin++)
for (int col = 0 ; col < size ; col++)
Grille[col][lin] = G[lin][col]; // On crée la grille via la grille sélectionnée en fonctiop du niv
int nb = rand()%6; Num = Num*6 + nb; // On tire des nombres aléatoires et on fait les permutations en foncion
if (nb == 1) // A C B
{
InvCol(2,3);
}
if (nb == 2) // C B A
{
InvCol(1,3);
}
if (nb == 3) // B A C
{
InvCol(2,1);
}
if (nb == 4) // C A B
{
InvCol(1,3);
InvCol(2,3);
}
if (nb == 5) // B C A
{
InvCol(2,1);
InvCol(2,3);
}
// et on recommence
nb = rand()%6; Num = Num*6 + nb;
if (nb == 1)
{
InvCol(5,6);
}
if (nb == 2)
{
InvCol(4,6);
}
if (nb == 3)
{
InvCol(5,4);
}
if (nb == 4)
{
InvCol(4,6);
InvCol(5,6);
}
if (nb == 5)
{
InvCol(5,4);
InvCol(5,6);
}
nb = rand()%6; Num = Num*6 + nb;
if (nb == 1)
{
InvCol(8,9);
}
if (nb == 2)
{
InvCol(7,9);
}
if (nb == 3)
{
InvCol(8,7);
}
if (nb == 4)
{
InvCol(7,9);
InvCol(8,9);
}
if (nb == 5)
{
InvCol(8,7);
InvCol(8,9);
}
///////////////////// Cube
nb = rand()%6; Num = Num*6 + nb;
if (nb == 1)
{
InvLinC(2,3);
}
if (nb == 2)
{
InvLinC(1,3);
}
if (nb == 3)
{
InvLinC(2,1);
}
if (nb == 4)
{
InvLinC(1,3);
InvLinC(2,3);
}
if (nb == 5)
{
InvLinC(2,1);
InvLinC(2,3);
}
nb = rand()%6; Num = Num*6 + nb;
if (nb == 1)
{
InvColC(2,3);
}
if (nb == 2)
{
InvColC(1,3);
}
if (nb == 3)
{
InvColC(2,1);
}
if (nb == 4)
{
InvColC(1,3);
InvColC(2,3);
}
if (nb == 5)
{
InvColC(2,1);
InvColC(2,3);
}
///////////////////// Lin
nb = rand()%6; Num = Num*6 + nb;
if (nb == 1)
{
InvLin(2,3);
}
if (nb == 2)
{
InvLin(1,3);
}
if (nb == 3)
{
InvLin(2,1);
}
if (nb == 4)
{
InvLin(1,3);
InvLin(2,3);
}
if (nb == 5)
{
InvLin(2,1);
InvLin(2,3);
}
nb = rand()%6; Num = Num*6 + nb;
if (nb == 1)
{
InvLin(5,6);
}
if (nb == 2)
{
InvLin(4,6);
}
if (nb == 3)
{
InvLin(5,4);
}
if (nb == 4)
{
InvLin(4,6);
InvLin(5,6);
}
if (nb == 5)
{
InvLin(5,4);
InvLin(5,6);
}
nb = rand()%6; Num = Num*6 + nb;
if (nb == 1)
{
InvLin(8,9);
}
if (nb == 2)
{
InvLin(7,9);
}
if (nb == 3)
{
InvLin(8,7);
}
if (nb == 4)
{
InvLin(7,9);
InvLin(8,9);
}
if (nb == 5)
{
InvLin(8,7);
InvLin(8,9);
}
/* et voilà on a nouveau cube tout neuf*/
}
int main()
{
srand(GetTickCount());
textcolor(11);
textbackground(2);
printf("Cr%cation du Sudoku par Sullyper\t\t\t\t\t\t\n",130);
fout = fopen("sudoku.in","w");
textcolor(7);
printf("\n\n\nChoisissez un niveau entre :\n");
printf("\t\t 1 - Easy -> Facil (entre 5 %c 10 min)\n",133);
printf("\t\t 2 - Medium -> Moyen (entre 10 %c 15 min)\n",133);
printf("\t\t 3 - Hard -> Difficil (entre 15 %c 20 min)\n",133);
printf("\t\t 4 - God -> Expert (plus de 20 min)\n");
char niv = 0;
while (niv < '1' || niv > '4') niv = getch();
if (niv == '1')
Make(GrilleE);
if (niv == '2')
Make(GrilleM);
if (niv == '3')
Make(GrilleH);
if (niv == '4')
Make(GrilleG);
clrscr();
textcolor(11);
textbackground(2);
printf("Cr%cation du Sudoku par Sullyper\t\t\t\t\t\t\n",130);
textcolor(7);
printf("\n\n Num%cro de grille : %d", 130,Num);
char c[25] = {201,205,205,205,205,205,203,205,205,205,205,205,203,205,205,205,205,205,187};
char e[25] = {186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186};
char d[25] = {186,196,197,196,197,196,186,196,197,196,197,196,186,196,197,196,197,196,186};
textcolor(3);
printf("\n");
printf("\t\t\t %s \n",c);
printf("\t\t\t %s \n",e);
printf("\t\t\t %s \n",d);
printf("\t\t\t %s \n",e);
printf("\t\t\t %s \n",d);
printf("\t\t\t %s \n",e);
c[0] = 204;
c[6] = 206;
c[18] = 185;
c[12] = 206;
printf("\t\t\t %s \n",c);
printf("\t\t\t %s \n",e);
printf("\t\t\t %s \n",d);
printf("\t\t\t %s \n",e);
printf("\t\t\t %s \n",d);
printf("\t\t\t %s \n",e);
printf("\t\t\t %s \n",c);
c[0] = 200;
c[6] = 202;
c[18] = 188;
c[12] = 202;
printf("\t\t\t %s \n",e);
printf("\t\t\t %s \n",d);
printf("\t\t\t %s \n",e);
printf("\t\t\t %s \n",d);
printf("\t\t\t %s \n",e);
printf("\t\t\t %s \n",c);
for (int lin = 0 ; lin < size ; lin++)
{
for (int col = 0 ; col < size ; col++)
{
if (Grille[col][lin])
{
textcolor(15);
gotoxy(col*2+27, lin*2+6);
printf("%d",NB[Grille[col][lin]]);
fprintf(fout,"%d ",NB[Grille[col][lin]]);
}
else
fprintf(fout,"%d ",0);
}
fprintf(fout,"\n");
}
fclose(fout);
gotoxy(1,24);
system("PAUSE");
return 0;
}
je ne peux pas savoir ce quoi le prob avec ce code en c; please help!!!!!!!!
comme vous allez remarquer le prob seras dans la fonction srand.........
#include<stdio.h>
#include <conio.h>
#include<stdlib.h>
const int size = 9;
long long Num = 0;
int Grille[size][size];
int GrilleE[size][size] = // Grille relative au niveau Facil (Easy)
{
5,0,0,0,0,2,9,1,0,
0,0,0,0,9,0,0,2,3,
0,9,1,0,3,4,8,0,0,
6,0,0,0,0,1,0,0,0,
4,3,0,7,6,9,0,8,5,
0,0,0,2,0,0,0,0,7,
0,0,5,9,2,0,6,3,0,
3,2,0,0,5,0,0,0,0,
0,7,9,6,0,0,0,0,2
};
int GrilleM[size][size] = // Grille relative au niveau Moyen (Medium)
{
3,0,0,0,0,5,4,8,1,
1,0,5,0,3,0,0,0,0,
0,0,0,0,0,4,0,0,6,
9,0,2,5,8,0,0,0,0,
0,7,0,0,4,0,0,6,0,
0,0,0,0,6,9,7,0,5,
8,0,0,4,0,0,0,0,0,
0,0,0,0,1,0,5,0,4,
2,3,4,9,0,0,0,0,7
};
int GrilleH[size][size] = // Grille relative niveau Difficil (Hard)
{
0,0,0,4,0,0,3,0,0,
7,1,0,2,0,0,8,0,0,
0,0,0,0,7,0,0,0,6,
9,0,0,1,0,0,0,5,0,
6,0,1,0,0,0,4,0,8,
0,3,0,0,0,4,0,0,7,
8,0,0,0,9,0,0,1,0,
0,0,7,0,0,2,0,6,5,
0,0,9,0,0,6,0,0,0
};
int GrilleG[size][size] = // Niveau impossible... ^^
{
0,0,0,0,0,5,0,6,0,
0,0,0,0,0,2,0,0,1,
0,2,7,0,0,0,8,0,0,
0,9,0,0,0,0,0,0,4,
0,6,0,0,1,0,0,9,5,
1,0,0,3,0,0,0,7,0,
0,0,3,0,0,0,6,1,0,
4,0,0,9,0,0,0,0,0,
0,8,0,7,0,0,0,0,0
};
bool Lin[size][size];
bool Col[size][size];
bool Sq[size][size];
FILE *fout;
int pos = 0;
int save[size][size];
int NB[size+1];
void InvCol(int col1, int col2) // Inverse 2 colonnes
{
int Col;
for (int i=0 ; i<size ; i++)
{
Col = Grille[col1-1][i];
Grille[col1-1][i] = Grille[col2-1][i];
Grille[col2-1][i] = Col;
}
}
void InvLin(int lin1, int lin2) // Inverse 2 Lignes
{
int Col;
for (int i=0 ; i<size ; i++)
{
Col = Grille[i][lin1-1];
Grille[i][lin1-1] = Grille[i][lin2-1];
Grille[i][lin2-1] = Col;
}
}
void InvColC(int col1, int col2) // Inverse des carré (sur les colonnes)
{
int Col[3][size];
for (int i=0 ; i<size ; i++)
for (int j=0 ; j<3 ; j++)
Col[j][i] = Grille[(col1-1)*3+j][i];
for (int i=0 ; i<size ; i++)
for (int j=0 ; j<3 ; j++)
Grille[(col1-1)*3+j][i] = Grille[(col2-1)*3+j][i];
for (int i=0 ; i<size ; i++)
for (int j=0 ; j<3 ; j++)
Grille[(col2-1)*3+j][i] = Col[j][i] ;
}
void InvLinC(int col1, int col2) // Inverse les carrés (sur les lignes)
{
int Col[size][3];
for (int i=0 ; i<size ; i++)
for (int j=0 ; j<3 ; j++)
Col[i][j] = Grille[i][(col1-1)*3+j];
for (int i=0 ; i<size ; i++)
for (int j=0 ; j<3 ; j++)
Grille[i][(col1-1)*3+j] = Grille[i][(col2-1)*3+j];
for (int i=0 ; i<size ; i++)
for (int j=0 ; j<3 ; j++)
Grille[i][(col2-1)*3+j] = Col[i][j] ;
}
void Make(int G[9][9]) // Crée une source
{
bool Nb[size+1] = {false,false,false,false,false,false,false,false,false,false};
// Tableau qui enregistre le nombre d'un nombre ^^
// 1 -> Nb[1] à chaque 1 mettre Nb[1];
// 2 -> Nb[2] à chaque 2 mettre Nb[2];
// 3 -> Nb[3] à chaque 3 mettre Nb[3];
for (int i = 1 ; i <= size ; i++)
{
int nbr = rand()%9 +1; // Nbr aléatoire
while (Nb[nbr]) nbr = rand()%9 +1;// On prend un nombre aléatoire pas encore pris
NB[i] = nbr; // On le met dans Nb
Nb[nbr] = true; // On indique qu'il a été pris
}
for (int lin = 0 ; lin < size ; lin++)
for (int col = 0 ; col < size ; col++)
Grille[col][lin] = G[lin][col]; // On crée la grille via la grille sélectionnée en fonctiop du niv
int nb = rand()%6; Num = Num*6 + nb; // On tire des nombres aléatoires et on fait les permutations en foncion
if (nb == 1) // A C B
{
InvCol(2,3);
}
if (nb == 2) // C B A
{
InvCol(1,3);
}
if (nb == 3) // B A C
{
InvCol(2,1);
}
if (nb == 4) // C A B
{
InvCol(1,3);
InvCol(2,3);
}
if (nb == 5) // B C A
{
InvCol(2,1);
InvCol(2,3);
}
// et on recommence
nb = rand()%6; Num = Num*6 + nb;
if (nb == 1)
{
InvCol(5,6);
}
if (nb == 2)
{
InvCol(4,6);
}
if (nb == 3)
{
InvCol(5,4);
}
if (nb == 4)
{
InvCol(4,6);
InvCol(5,6);
}
if (nb == 5)
{
InvCol(5,4);
InvCol(5,6);
}
nb = rand()%6; Num = Num*6 + nb;
if (nb == 1)
{
InvCol(8,9);
}
if (nb == 2)
{
InvCol(7,9);
}
if (nb == 3)
{
InvCol(8,7);
}
if (nb == 4)
{
InvCol(7,9);
InvCol(8,9);
}
if (nb == 5)
{
InvCol(8,7);
InvCol(8,9);
}
///////////////////// Cube
nb = rand()%6; Num = Num*6 + nb;
if (nb == 1)
{
InvLinC(2,3);
}
if (nb == 2)
{
InvLinC(1,3);
}
if (nb == 3)
{
InvLinC(2,1);
}
if (nb == 4)
{
InvLinC(1,3);
InvLinC(2,3);
}
if (nb == 5)
{
InvLinC(2,1);
InvLinC(2,3);
}
nb = rand()%6; Num = Num*6 + nb;
if (nb == 1)
{
InvColC(2,3);
}
if (nb == 2)
{
InvColC(1,3);
}
if (nb == 3)
{
InvColC(2,1);
}
if (nb == 4)
{
InvColC(1,3);
InvColC(2,3);
}
if (nb == 5)
{
InvColC(2,1);
InvColC(2,3);
}
///////////////////// Lin
nb = rand()%6; Num = Num*6 + nb;
if (nb == 1)
{
InvLin(2,3);
}
if (nb == 2)
{
InvLin(1,3);
}
if (nb == 3)
{
InvLin(2,1);
}
if (nb == 4)
{
InvLin(1,3);
InvLin(2,3);
}
if (nb == 5)
{
InvLin(2,1);
InvLin(2,3);
}
nb = rand()%6; Num = Num*6 + nb;
if (nb == 1)
{
InvLin(5,6);
}
if (nb == 2)
{
InvLin(4,6);
}
if (nb == 3)
{
InvLin(5,4);
}
if (nb == 4)
{
InvLin(4,6);
InvLin(5,6);
}
if (nb == 5)
{
InvLin(5,4);
InvLin(5,6);
}
nb = rand()%6; Num = Num*6 + nb;
if (nb == 1)
{
InvLin(8,9);
}
if (nb == 2)
{
InvLin(7,9);
}
if (nb == 3)
{
InvLin(8,7);
}
if (nb == 4)
{
InvLin(7,9);
InvLin(8,9);
}
if (nb == 5)
{
InvLin(8,7);
InvLin(8,9);
}
/* et voilà on a nouveau cube tout neuf*/
}
int main()
{
srand(GetTickCount());
textcolor(11);
textbackground(2);
printf("Cr%cation du Sudoku par Sullyper\t\t\t\t\t\t\n",130);
fout = fopen("sudoku.in","w");
textcolor(7);
printf("\n\n\nChoisissez un niveau entre :\n");
printf("\t\t 1 - Easy -> Facil (entre 5 %c 10 min)\n",133);
printf("\t\t 2 - Medium -> Moyen (entre 10 %c 15 min)\n",133);
printf("\t\t 3 - Hard -> Difficil (entre 15 %c 20 min)\n",133);
printf("\t\t 4 - God -> Expert (plus de 20 min)\n");
char niv = 0;
while (niv < '1' || niv > '4') niv = getch();
if (niv == '1')
Make(GrilleE);
if (niv == '2')
Make(GrilleM);
if (niv == '3')
Make(GrilleH);
if (niv == '4')
Make(GrilleG);
clrscr();
textcolor(11);
textbackground(2);
printf("Cr%cation du Sudoku par Sullyper\t\t\t\t\t\t\n",130);
textcolor(7);
printf("\n\n Num%cro de grille : %d", 130,Num);
char c[25] = {201,205,205,205,205,205,203,205,205,205,205,205,203,205,205,205,205,205,187};
char e[25] = {186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186};
char d[25] = {186,196,197,196,197,196,186,196,197,196,197,196,186,196,197,196,197,196,186};
textcolor(3);
printf("\n");
printf("\t\t\t %s \n",c);
printf("\t\t\t %s \n",e);
printf("\t\t\t %s \n",d);
printf("\t\t\t %s \n",e);
printf("\t\t\t %s \n",d);
printf("\t\t\t %s \n",e);
c[0] = 204;
c[6] = 206;
c[18] = 185;
c[12] = 206;
printf("\t\t\t %s \n",c);
printf("\t\t\t %s \n",e);
printf("\t\t\t %s \n",d);
printf("\t\t\t %s \n",e);
printf("\t\t\t %s \n",d);
printf("\t\t\t %s \n",e);
printf("\t\t\t %s \n",c);
c[0] = 200;
c[6] = 202;
c[18] = 188;
c[12] = 202;
printf("\t\t\t %s \n",e);
printf("\t\t\t %s \n",d);
printf("\t\t\t %s \n",e);
printf("\t\t\t %s \n",d);
printf("\t\t\t %s \n",e);
printf("\t\t\t %s \n",c);
for (int lin = 0 ; lin < size ; lin++)
{
for (int col = 0 ; col < size ; col++)
{
if (Grille[col][lin])
{
textcolor(15);
gotoxy(col*2+27, lin*2+6);
printf("%d",NB[Grille[col][lin]]);
fprintf(fout,"%d ",NB[Grille[col][lin]]);
}
else
fprintf(fout,"%d ",0);
}
fprintf(fout,"\n");
}
fclose(fout);
gotoxy(1,24);
system("PAUSE");
return 0;
}
A voir également:
- Generateur de grille sudoku
- Generateur mot de passe - Télécharger - Sécurité
- Generateur d image - Guide
- Generateur de cle windows 10 - Guide
- Générateur de clé d'activation - Accueil - Windows
- Grille tombola vierge à imprimer 50 cases - Télécharger - Création musicale