[C]GENERATEUR DE SUDOKU [TERMINAL]

blackdead08 Messages postés 18 Date d'inscription vendredi 23 décembre 2005 Statut Membre Dernière intervention 12 septembre 2006 - 29 juin 2006 à 00:50
Elsick Messages postés 2 Date d'inscription mardi 16 novembre 2010 Statut Membre Dernière intervention 17 décembre 2010 - 17 déc. 2010 à 08:07
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/38358-c-generateur-de-sudoku-terminal

Elsick Messages postés 2 Date d'inscription mardi 16 novembre 2010 Statut Membre Dernière intervention 17 décembre 2010
17 déc. 2010 à 08:07
Bonjour, voilà débutant en c, j'ai compris une bonne partie du programme mais quelques parties reste entièrement mystérieuse pour moi par exemple :
switch(k)
{
case 0 :
case 1 :
case 2 : region[i]=j; break;
case 3 :
case 4 :
case 5 : region[i]=j+3; break;
case 6 :
case 7 :
case 8 : region[i]=j+6; break;
}
pourquoi y a-t-il des cases non remplies ?

Enfin je ne comprends pas beaucoup de choses du module d'initialisation (void init)
De même que la description de l'auteur =S
BigRider Messages postés 5 Date d'inscription dimanche 25 septembre 2005 Statut Membre Dernière intervention 22 octobre 2010
22 oct. 2010 à 04:39
DSL mais ton algo est faux car ton initialisation de region peut dépasser 9 il te faut faire ceci ( écrie en C++ à adapté en C pour les boolean)

bool continu;
int region[9],k;
for(int i=0;i<9;i++)
{
__________continu=true;
__________while(continu)
__________{
____________________k=(rand()%9)+1;
____________________for(int j=0;j<i;j++)
____________________{
______________________________if(region[j] == k) continu = false;
____________________]
____________________if(continu == false) continu=true;
____________________else continu=false;
__________}
__________region[i]=k;
}
cs_natha31 Messages postés 1 Date d'inscription mercredi 15 novembre 2000 Statut Membre Dernière intervention 19 septembre 2009
19 sept. 2009 à 01:04
je viens de tester ce prog.
Les grilles sont assez facile mais je n'aurais jamais cru qu'un prog générateur de sudoku puisse être aussi compact.
Je suis débutant en C et je suis encore en train de me pencher sur la technique utilisée :D
par ex: calcules-tu les masks ou les fait tu au hasard?

De ce que j'ai compris du code, c'est assez ingénieux :) ...beau programme. ;)

Encore bravo!
Natha
le_papy Messages postés 15 Date d'inscription mercredi 29 mars 2006 Statut Membre Dernière intervention 6 juillet 2009
29 juin 2009 à 12:28
franchement lucaniznizar tu pourrais créer ton topic, ton code est assez difficilement abordable comme ça sans couleur, sans aérations et .... avec aucun commentaires.

par contre blackdead08 je vais regarder de prés ton code de génération une fois que j'aurais fini de mettre mon algo de génération au point!
lucaniznizar Messages postés 1 Date d'inscription lundi 23 mars 2009 Statut Membre Dernière intervention 3 avril 2009
3 avril 2009 à 14:23
we je vois que votre programme comporte bcp des astuces genials,bn j'ai aussi un programme de sudoku a vous proposer le voila
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<dos.h>
#include<math.h>
void traitement_cursur(int [9][9],int [9][9],int ,int );
void initialiser_mat(int [9][9],int ,int );
void initialiser_mat1(int [9][9],int ,int );
void grille();
int controle_ligne_colone_region(int [9][9],int ,int ,char );
int controle_traitement(int [9][9],int ,int ,char );
int traitement(int [9][9],int [9][9],int ,int ,char *);
void afficher_mat(int [9][9]);
void connexion(void);
void deconnexion(void);
char repetition; //cette variable a le but d'arreter le defilement des solution
/******************************************************************
* main *
*******************************************************************/
void main()
{
clrscr();
int i,j;
int mat[9][9],mat1[9][9];
char repetition=1;
repetition=1;
gotoxy(30,18);textcolor(LIGHTRED);
cprintf("REALISER PAR:");
gotoxy(30,22);textcolor(LIGHTBLUE);
cprintf("NIZAR ELASRI");
gotoxy(30,24);textcolor(LIGHTGREEN);
cprintf("SOUFIAN CHENNANE");
textbackground(6);
textcolor(BLUE);gotoxy(15,15);cprintf("ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
textcolor(BLUE);gotoxy(15,16);cprintf("º º");
textcolor(BLUE);gotoxy(15,17);cprintf("º º");
textcolor(BLUE);gotoxy(15,18);cprintf("º REALISER PAR: º");
textcolor(BLUE);gotoxy(15,19);cprintf("º**********************************º");
textcolor(BLUE);gotoxy(15,20);cprintf("º º");
textcolor(BLUE);gotoxy(15,20);cprintf("º º");
textcolor(BLUE);gotoxy(15,21);cprintf("º º");
textcolor(BLUE);gotoxy(15,22);cprintf("º NIZAR SOUFIANE º");
textcolor(BLUE);gotoxy(15,23);cprintf("º ELASRI CHENNANE º");
textcolor(BLUE);gotoxy(15,24);cprintf("º º");
textcolor(BLUE);gotoxy(15,25);cprintf("º**********************************º");
textcolor(BLUE);gotoxy(15,26);cprintf("º º");
textcolor(BLUE);gotoxy(15,27);cprintf("º EMSI º");
textcolor(BLUE);gotoxy(15,28);cprintf("º º");
textcolor(BLUE);gotoxy(15,29);cprintf("º**********************************º");
textcolor(BLUE);gotoxy(15,30);cprintf("º º");
textcolor(BLUE);gotoxy(15,31);cprintf("º 2008/2009 º");
textcolor(BLUE);gotoxy(15,32);cprintf("º º");
textcolor(BLUE);gotoxy(15,33);cprintf("ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍŒ");
textbackground(BLACK);
getch();
clrscr();
connexion();
getch();
clrscr();
grille();
initialiser_mat(mat,i,j);
initialiser_mat1(mat1,i,j);
traitement_cursur(mat,mat1,i,j);
traitement(mat,mat1,0,0,&repetition);
clrscr();
deconnexion();
clrscr();
// getch();
}
/********************************************************************
* connexion *
*********************************************************************/
void connexion(void)
{ int i;
clrscr();
gotoxy(30,17);
textcolor(CYAN);
cprintf("************************");
gotoxy(30,18);
cprintf("* SUDOKU *");
gotoxy(30,19);
cprintf("************************");
gotoxy(35,22);
printf("ÚÄÄÄÄÄÄÄÄÄÄÄÄ¿");
gotoxy(35,23);
printf("³°°°°°°°°°°°°³");
gotoxy(35,24);
printf("ÀÄÄÄÄÄÄÄÄÄÄÄÄÙ");
gotoxy(37,25);
textcolor(RED);
cprintf("chargement");
for(i=36;i<48;i++)
{
gotoxy(i,23);
textcolor(RED);
cprintf("²");

if(i!=36)
{gotoxy(i-1,23);
textcolor(GREEN);
cprintf("²");
}
delay(200);
}
gotoxy(47,23);
textcolor(GREEN);
cprintf("²");
gotoxy(37,25);
cprintf("chargement");
delay(500);
textcolor(LIGHTGRAY);
}
/***********************************************************************
* la grille *
************************************************************************/
void grille()
{
gotoxy(25,12);textcolor(GREEN);cprintf("SODOKU");textbackground(BLUE);
gotoxy(15,15);cprintf("ÉÍÍÑÍÍÑÍÍËÍÍÑÍÍÑÍÍËÍÍÑÍÍÑÍÍ»");
gotoxy(15,16);cprintf("º ³ ³ º ³ ³ º ³ ³ º");
gotoxy(15,17);cprintf("ºÄÄÅÄÄÅÄÄ×ÄÄÅÄÄÅÄÄ×ÄÄÅÄÄÅÄÄ×");
gotoxy(15,18);cprintf("º ³ ³ º ³ ³ º ³ ³ º");
gotoxy(15,19);cprintf("ºÄÄÅÄÄÅÄÄ×ÄÄÅÄÄÅÄÄ×ÄÄÅÄÄÅÄÄ×");
gotoxy(15,20);cprintf("º ³ ³ º ³ ³ º ³ ³ º");
gotoxy(15,21);cprintf("ÌÍÍØÍÍØÍÍÎÍÍØÍÍØÍÍÎÍÍØÍÍØÍÍÎ");
gotoxy(15,22);cprintf("º ³ ³ º ³ ³ º ³ ³ º");
gotoxy(15,23);cprintf("ºÄÄÅÄÄÅÄÄ×ÄÄÅÄÄÅÄÄ×ÄÄÅÄÄÅÄÄ×");
gotoxy(15,24);cprintf("º ³ ³ º ³ ³ º ³ ³ º");
gotoxy(15,25);cprintf("ºÄÄÅÄÄÅÄÄ×ÄÄÅÄÄÅÄÄ×ÄÄÅÄÄÅÄÄ×");
gotoxy(15,26);cprintf("º ³ ³ º ³ ³ º ³ ³ º");
gotoxy(15,27);cprintf("ÌÍÍØÍÍØÍÍÎÍÍØÍÍØÍÍÎÍÍØÍÍØÍÍÎ");
gotoxy(15,28);cprintf("º ³ ³ º ³ ³ º ³ ³ º");
gotoxy(15,29);cprintf("ºÄÄÅÄÄÅÄÄ×ÄÄÅÄÄÅÄÄ×ÄÄÅÄÄÅÄÄ×");
gotoxy(15,30);cprintf("º ³ ³ º ³ ³ º ³ ³ º");
gotoxy(15,31);cprintf("ºÄÄÅÄÄÅÄÄ×ÄÄÅÄÄÅÄÄ×ÄÄÅÄÄÅÄÄ×");
gotoxy(15,32);cprintf("º ³ ³ º ³ ³ º ³ ³ º");
gotoxy(15,33);cprintf("ÈÍÍÏÍÍÏÍÍÊÍÍÏÍÍÏÍÍÊÍÍÏÍÍÏÍÍŒ");
textbackground(BLACK);

gotoxy(15,40);
printf("ÚÄ[INDICATIONS]ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿");
gotoxy(15,41);
printf("³ ³");
gotoxy(15,42);
printf("³ Remplir les cases avec les ³");
gotoxy(15,43);
printf("³ chiffres de 1 … 9 de telle ³");
gotoxy(15,44);
printf("³ sorte qu'ils n'apparaissent ³");
gotoxy(15,45);
printf("³ qu'une seule fois par ligne,³");
gotoxy(15,46);
printf("³ par colonne et par r‚gion. ³");
gotoxy(15,47);
printf("³ ³");
gotoxy(15,48);
printf("ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ");
gotoxy(16,16);

}
/***********************************************************************
* fonction d'initialisation des deux matrices *
************************************************************************/
void initialiser_mat(int mat[9][9],int i,int j)
{
for(i=0;i<9;i++)
for(j=0;j<9;j++)
{
mat[i][j]=0;
}
}
void initialiser_mat1(int mat1[9][9],int i,int j)
{
for(i=0;i<9;i++)
for(j=0;j<9;j++)
{
mat1[i][j]=0;
}
}


/************************************************************************
* traitement de cursur *
*************************************************************************/
void traitement_cursur(int mat[9][9],int mat1[9][9],int i,int j)
{
char c;
int x,y,p;
i=0,j=0;
x=16;y=16;
gotoxy(x,y);
do
{
c=getch();
if(c==0)
c=getch();
switch(c)
{
case 72:if(y==16){y=32;i=8;gotoxy(x,y);}
else
{y=y-2;i--;gotoxy(x,y);}break;
case 80:if(y==32){y=16;i=0;gotoxy(x,y);}
else
{y=y+2;i++;gotoxy(x,y);}break;
case 77:if(x==40){x=16;j=0;gotoxy(x,y);}
else
{x=x+3;j++;gotoxy(x,y);}break;
case 75:if(x==16){x=40;j=8;gotoxy(x,y);}
else
{x=x-3;j--;gotoxy(x,y);}break;}
if((isdigit(c)) && (c!='0'))
{ if(controle_ligne_colone_region(mat,i,j,c))
{
mat[i][j]=c-48;
mat1[i][j]=1;
textcolor(LIGHTRED);
cprintf("%c",c);
if(x==40)
{x=13;j=0;
i++; y=y+2;}
x=x+3;
j++;
gotoxy(x,y);
/*if(c==8)
{ if(mat[i][j]!=0) //pour supprimer le contenu de la case
{cprintf(" ");
gotoxy(x-3,y);// pour retourner curseur dans la case precedant
mat[i][j]=0;
}
}*/
}}
}while(c!=13);
}


/***********************************************************************
* controle de traitement de cursur *
************************************************************************/
int controle_ligne_colone_region(int mat[9][9],int i,int j,char c)
{
int x,y,a,b,m,n;
for(x=0;x<9;x++)
if((mat[x][j])==c-48) /**********colone***************/
return 0;
for(y=0;y<9;y++)
if((mat[i][y])==c-48) /**********ligne***************/
return 0;
a=i/3;
b=j/3;
for(m=a*3;m<=(a*3)+2;m++)
for(n=b*3;n<=(b*3)+2;n++)
if((mat[m][n])==c-48) /************region**********/
{
return 0;}
return 1;
}
/***********************************************************************
* fonction de backtrackin *
************************************************************************/
int traitement(int mat[9][9],int mat1[9][9],int i,int j,char *repetition)
{
char c,rep;
if(i<=8&&i>=0)

{

if(mat1[i][j])
{
if(*repetition)
{
if(j<8){traitement(mat,mat1,i,j+1,repetition);}
else {if(j==8){traitement(mat,mat1,i+1,0,repetition);}}
}
}
else
{ if (!mat[i][j]||(mat[i][j] && !mat1[i][j]))
{
for(c=1;c<=9; )
{
if(controle_ligne_colone_region(mat,i,j,c+48))
{
mat[i][j]=c;
if(*repetition)
{
if(j<8){traitement(mat,mat1,i,j+1,repetition);}
else{ if(j==8){traitement(mat,mat1,i+1,0,repetition);}}
}
}
else {
c=c+1;
if (c>9)
{
mat[i][j]=0;
return 0;
}

}
}
}


}

}
else
{
if(i<0 ) {printf("pas de solution");getch();return 0;}
else
{
if(i>8)
{
afficher_mat(mat);
gotoxy(50,50);
fflush (stdin);
textcolor(BLUE+BLINK);cprintf("Pour quitter,tapez Q");delay(200);
rep=getch();
if(toupper(rep)=='Q') (*repetition)=0;
}
}
}
return 0;
}




/****************************************************************************
* affichage *
*****************************************************************************/

void afficher_mat(int mat[9][9])
{
int i,j,n,m;
for(i=0,m=16;i<9;i++,m+=2)
for(j=0,n=16;j<9;j++,n+=3)
{gotoxy(n,m);
printf("%d",mat[i][j]);}
}


/*void backtrackin(int mat[9][9],int *il , int *ic )
{

int c, inc ;

mat[*il][*ic]=0 ;
recule(mat,il,ic);

if (mat[*il][*ic] > -9 ) // Compris entre -1 et -8
{ inc = 1 ;
do{
c= mat[*il][*ic]-inc;
inc++;
if(controle_ligne_colone_region(mat,*il,*ic,-c))
{
mat[*il][*ic]=c;
return; //----------> return a sudo
}
}while(c > -9);

if( c <= -9)
backtrackin(mat,il,ic);

}
else if (mat[*il][*ic] == (-9) )
{

backtrackin(mat,il,ic);


}


}


/*void recule(int mat[9][9],int *il,int *ic)
{
if((*ic)==0 &&(*il)>0){(*ic)=8;(*il)--;}
if((*ic)>0 && (*il)>=0){(*ic)--;}
for((*il);(*il)>=0;(*il)--)
{
while((*ic)>=0)
{
if((mat[*il][*ic])<0)
return ;
(*ic)--;
}
if((*ic)<0 &&(*il)>0) (*ic)=8;
}
if((*il)<0 && *ic>=0) (*il)=0;
if((*il)<0 && (*ic)<0)
{
textcolor(180);
gotoxy(30,25);
cprintf("pas de solution !");
getch();getch();
_exit(0);
}
}
/*void sudo(int mat[9][9])
{
int il,ic;
int c;
for(il=0;il<9;il++)
for(ic=0;ic<9;ic++)
if(mat[il][ic]==0)
{
for(c=1;c<=9;c++)
{
if(controle_ligne_colone_region(mat,il,ic,c))
{
mat[il][ic]=-c;
break;
}
}
if(c>9)
backtrackin(mat,&il,&ic);
}
enfin(mat);
}




/********************************************************************
* deconnexion
*********************************************************************/
void deconnexion(void)
{clrscr();
//curseur(0);//masquer curseur
int i;
gotoxy(36,13);
printf("ÚÄÄÄÄÄÄÄÄÄÄÄÄ¿");
textcolor(GREEN);
gotoxy(36,14);
printf("³");cprintf("°°°°°°°°°°°°");printf("³");
textcolor(LIGHTGRAY);
gotoxy(36,15);
printf("ÀÄÄÄÄÄÄÄÄÄÄÄÄÙ");
gotoxy(38,16);
textcolor(GREEN);
cprintf("DECONNEXION");
for(i=37;i<49;i++)
{
gotoxy(i,14);
textcolor(GREEN);
cprintf("²");

if(i!=37)
{gotoxy(i-1,14);
textcolor(RED);
cprintf("²");
}
delay(200);
}
gotoxy(48,14);
textcolor(RED);
cprintf("²");
gotoxy(38,16);
cprintf("DECONNEXION");
delay(500);

textcolor(LIGHTGRAY);
}
il faut utilisr le turbo c ou le borland c pour le bon fonctionnemment
realiser par moi meme (nizar elasri)
thesheepbiker Messages postés 1 Date d'inscription mardi 26 février 2008 Statut Membre Dernière intervention 8 avril 2008
8 avril 2008 à 15:34
merci pour ta source la seule du site que je comprend
vecchio56 Messages postés 6535 Date d'inscription lundi 16 décembre 2002 Statut Membre Dernière intervention 22 août 2010 14
29 juin 2006 à 13:59
CPPFrance::nbSudoku++;
Ca va bientot déborder
blackdead08 Messages postés 18 Date d'inscription vendredi 23 décembre 2005 Statut Membre Dernière intervention 12 septembre 2006
29 juin 2006 à 10:49
L'unicité est inversement proportionnel au nombre de case(s) masquée(s)
Si 80 cases sont masquées forcément il y aura plus d'une possibilité....
Si vous avez une suggestion pour améliorer celle-ci, je suis preneur :-)

Ils existent beaucoup de sources mais je crois qu'aucune n'est aussi simple.
(compilation séparée, programmation de contrainte,... )
Elle n'intéressera peut-être que des débutants mais elle peut être utile...
(Du moins je l'espère)
rrk275 Messages postés 540 Date d'inscription vendredi 25 juin 2004 Statut Membre Dernière intervention 1 octobre 2007 2
29 juin 2006 à 01:43
t'assure tu qu'avec ton masquage tu offre une unicité de solutions (obligatoire en theorie..) et les sources sur le sudoku ne sont elles pas deja trop nombreuses?
blackdead08 Messages postés 18 Date d'inscription vendredi 23 décembre 2005 Statut Membre Dernière intervention 12 septembre 2006
29 juin 2006 à 00:54
pas d'éditeur, grosse erreure:

Des UpDate's viendront bientôt...

(avec les commentaires et le mode raw)

Qu'en pensez-vous?
blackdead08 Messages postés 18 Date d'inscription vendredi 23 décembre 2005 Statut Membre Dernière intervention 12 septembre 2006
29 juin 2006 à 00:50
Des UpDate viendront bien tôt

(avec les commantaires et le mode raw)

qu'en pensez-vous?
Rejoignez-nous