begin process at 2012 05 27 16:34:51
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Jeux

 > [C]GENERATEUR DE SUDOKU [TERMINAL]

[C]GENERATEUR DE SUDOKU [TERMINAL]


 Information sur la source

Note :
9,25 / 10 - par 4 personnes
9,25 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Jeux Classé sous :sudoku, jeux, terminal, couleur, c Niveau :Débutant Date de création :29/06/2006 Vu :6 292

Auteur : blackdead08

Ecrire un message privé
Commentaire sur cette source (11)
Ajouter un commentaire et/ou une note

 Description

Cliquez pour voir la capture en taille normale
Il s'agit d'un générateur de grille de sudoku!
Programmé de manière inédite et beaucoup plus facile que celles que j'ai déjà vu...
Simple affichage des nombres pour assurer la probabilité du code.
Ou affichage complexifier pour système Unix avec couleurs...  (enlevez les /**/)
Un mode jeux est en cours mais limitera le caractère portable de ce code  (mode raw)

Source

  • /* Generateur de grille de SudoKu
  • by Van der Elst, Olivier*/
  • #include <stdio.h>
  • #include <stdlib.h>
  • #include <time.h>
  • #define NbrHole 25
  • static void init(void);
  • static void aff(void);
  • static int GSDK[9][9][3];
  • int main(void)
  • {
  • srand((unsigned)time(NULL));
  • init();
  • aff();
  • return 0;
  • }
  • void init(void)
  • {
  • int i, j, k, l;
  • int region[9];
  • int cond;
  • for(j=0;j<=8;j++)
  • {
  • k=(rand()%9)+1;
  • cond=1;
  • while(cond)
  • {
  • cond=0;
  • for(i=0;i<=8;i++) if(region[i]==k) cond=1;
  • if(cond)
  • {
  • k++;
  • if(k==10) k=1;
  • }
  • }
  • region[j]=k;
  • }
  • j=0;
  • l=0;
  • while(j!=9)
  • {
  • k=j;
  • for(i=0;i<=8;i++)
  • {
  • GSDK[i][l][0]=region[k];
  • k++;
  • if(k>8) k=k-9;
  • }
  • l++;
  • switch(j)
  • {
  • case 0 : j=3; break;
  • case 3 : j=6; break;
  • case 6 : j=7; break;
  • case 7 : j=1; break;
  • case 1 : j=4; break;
  • case 4 : j=5; break;
  • case 5 : j=8; break;
  • case 8 : j=2; break;
  • case 2 : j=9; break;
  • }
  • }
  • i=0; //mix col dans reg
  • j=(rand()%2)+1;
  • for(k=0;k<=8;k++)
  • {
  • if(k==3||k==6) j=rand()%3;
  • 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;
  • }
  • j++;
  • i++;
  • if(j==3) j=0;
  • }
  • int temp[9][9];
  • for(i=0;i<=8;i++)
  • {
  • for(j=0;j<=8;j++)
  • {
  • k=region[i];
  • temp[i][j]=GSDK[k][j][0];
  • }
  • }
  • k=6;
  • i=0;
  • while(k!=9)
  • {
  • for(j=0;j<=8;j++)
  • {
  • GSDK[i][j][1]=temp[k][j];
  • GSDK[i+1][j][1]=temp[k+1][j];
  • GSDK[i+2][j][1]=temp[k+2][j];
  • }
  • switch(k)
  • {
  • case 6 : k=0; i=3; break;
  • case 0 : k=3; i=6; break;
  • case 3 : k=9; break;
  • }
  • }
  • int masking[9][9];
  • for(i=0;i<=8;i++)
  • {
  • for(j=0;j<=8;j++)
  • {
  • masking[i][j]=0;
  • }
  • }
  • for(i=0;i<NbrHole;i++)
  • {
  • do
  • {
  • k=rand()%9;
  • l=rand()%9;
  • }
  • while(masking[k][l]);
  • masking[k][l]=1;
  • }
  • for(i=0;i<=8;i++)
  • for(j=0;j<=8;j++)
  • {
  • if(!masking[i][j]) GSDK[i][j][2]=GSDK[i][j][1];
  • else GSDK[i][j][2]=0;
  • }
  • }
  • void aff(void)
  • {
  • int i, j;
  • for(i=0;i<=8;i++)
  • {
  • for(j=0;j<=8;j++)
  • {
  • if(GSDK[i][j][2]!=0) printf("%2d", GSDK[i][j][2]);
  • else printf(" ");
  • }
  • printf("\n");
  • }
  • }
  • /*void aff(void)
  • {
  • #define Lo 9
  • #define La 9
  • #define RaZ "\e[2J\e[;H"
  • #define ClearCol "\e[00m"
  • #define Gg "\e[00;1;33m"
  • #define Gb "\e[00;31m"
  • #define Jg "\e[00;32m"
  • #define Jb "\e[00;34m"
  • #define GoToYX "\e[%u;%uH"
  • int i, j;
  • printf(RaZ);
  • for (j=1;j<=Lo;j++)
  • {
  • if(j==4||j==7) printf(Gg);
  • else printf(Gb);
  • printf("+");
  • for(i=1;i<=La;i++)
  • {
  • printf("---");
  • if(i==3||i==6||j==4||j==7) printf(Gg"+");
  • else printf(Gb"+");
  • if(j==4||j==7) printf(Gg);
  • else printf(Gb);
  • }
  • printf("\n");
  • printf(Gb);
  • printf("|");
  • for(i=1;i<=La;i++)
  • {
  • printf(" ");
  • if(i==3||i==6) printf(Gg"|"Gb);
  • else printf(Gb"|");
  • printf(Gb);
  • }
  • printf("\n");
  • }
  • printf("+");
  • for(i=1;i<=La;i++)
  • {
  • printf("---");
  • if(i==3||i==6) printf(Gg"+");
  • else printf(Gb"+");
  • printf(Gb);
  • }
  • for(j=0;j<=8;j++)
  • for(i=0;i<=8;i++)
  • {
  • printf(GoToYX,(j+1)*2,((i+1)*4)-1);
  • switch(i)
  • {
  • case 0 :
  • case 1 :
  • case 2 : switch(j)
  • {
  • case 0 :
  • case 1 :
  • case 2 : printf(Jg); break;
  • case 3 :
  • case 4 :
  • case 5 : printf(Jb); break;
  • case 6 :
  • case 7 :
  • case 8 : printf(Jg); break;
  • }
  • break;
  • case 3 :
  • case 4 :
  • case 5 : switch(j)
  • {
  • case 0 :
  • case 1 :
  • case 2 : printf(Jb); break;
  • case 3 :
  • case 4 :
  • case 5 : printf(Jg); break;
  • case 6 :
  • case 7 :
  • case 8 : printf(Jb); break;
  • }
  • break;
  • case 6 :
  • case 7 :
  • case 8 : switch(j)
  • {
  • case 0 :
  • case 1 :
  • case 2 : printf(Jg); break;
  • case 3 :
  • case 4 :
  • case 5 : printf(Jb); break;
  • case 6 :
  • case 7 :
  • case 8 : printf(Jg); break;
  • }
  • break;
  • }
  • if(GSDK[i][j][2]!=0) printf("%d", GSDK[i][j][2]);
  • }
  • printf(GoToYX,(j+1)*2,1);
  • printf(ClearCol);
  • }*/
/* Generateur de grille de SudoKu
     by Van der Elst, Olivier*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NbrHole 25
static void init(void);
static void aff(void);
static int GSDK[9][9][3];
int main(void)
{
	srand((unsigned)time(NULL));
	init();
	aff();
	return 0;
}
void init(void)
{
	int i, j, k, l;
	int region[9];
	
	int cond;
	for(j=0;j<=8;j++)
	{
		k=(rand()%9)+1;
		cond=1;
		while(cond)
		{
			cond=0;
			for(i=0;i<=8;i++) if(region[i]==k) cond=1;
			if(cond)
			{
				k++;
				if(k==10) k=1;
			}
		}
		region[j]=k;
	}
	
	j=0;
	l=0;
	while(j!=9)
	{
		k=j;
		for(i=0;i<=8;i++)
		{
			GSDK[i][l][0]=region[k];
			k++;
			if(k>8) k=k-9;
		}
		l++;
		switch(j)
		{
			case 0 : j=3; break;
			case 3 : j=6; break;
			case 6 : j=7; break;
			case 7 : j=1; break;
			case 1 : j=4; break;
			case 4 : j=5; break;
			case 5 : j=8; break;
			case 8 : j=2; break;
			case 2 : j=9; break;
		}
	}
	
	i=0;			//mix col dans reg
	j=(rand()%2)+1;
	for(k=0;k<=8;k++)
	{
		if(k==3||k==6) j=rand()%3;
		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;
		}
		j++;
		i++;
		if(j==3) j=0;
	}
	
	int temp[9][9];
	for(i=0;i<=8;i++)
	{
		for(j=0;j<=8;j++)
		{
			k=region[i];
			temp[i][j]=GSDK[k][j][0];
		}
	}
	
	k=6;
	i=0;
	while(k!=9)
	{
		for(j=0;j<=8;j++)
		{
			GSDK[i][j][1]=temp[k][j];
			GSDK[i+1][j][1]=temp[k+1][j];
			GSDK[i+2][j][1]=temp[k+2][j];
		}
		switch(k)
		{
			case 6 : k=0; i=3; break;
			case 0 : k=3; i=6; break;
			case 3 : k=9; break;
		}
	}
	
	int masking[9][9];
	for(i=0;i<=8;i++)
        {
                for(j=0;j<=8;j++)
                {
                        masking[i][j]=0;
                }
        }
	for(i=0;i<NbrHole;i++)
	{
		do
			{
				k=rand()%9;
				l=rand()%9;
			}
		while(masking[k][l]);
		masking[k][l]=1;
	}
	for(i=0;i<=8;i++)
		for(j=0;j<=8;j++) 
		{
			if(!masking[i][j]) GSDK[i][j][2]=GSDK[i][j][1];
			else GSDK[i][j][2]=0;
		}
}
void aff(void)
{
	int i, j;
	for(i=0;i<=8;i++)
	{
		for(j=0;j<=8;j++)
		{
			if(GSDK[i][j][2]!=0) printf("%2d", GSDK[i][j][2]);
			else printf("  ");
		}
		printf("\n");
	}
}
/*void aff(void)
{
	#define Lo 9
	#define La 9
	#define RaZ "\e[2J\e[;H"
	#define ClearCol "\e[00m"
	#define Gg "\e[00;1;33m"
	#define Gb "\e[00;31m"
	#define Jg "\e[00;32m"
	#define Jb "\e[00;34m"
	#define GoToYX "\e[%u;%uH"
	
	int i, j;
	printf(RaZ);
	for (j=1;j<=Lo;j++)
	{
		if(j==4||j==7) printf(Gg);
		else printf(Gb);
		printf("+");
		for(i=1;i<=La;i++) 
		{
			printf("---");
			if(i==3||i==6||j==4||j==7) printf(Gg"+");
			else printf(Gb"+");
			if(j==4||j==7) printf(Gg);
		        else printf(Gb);
		}
		printf("\n");
		printf(Gb);
		printf("|");
		for(i=1;i<=La;i++) 
		{
			printf("   ");
			if(i==3||i==6) printf(Gg"|"Gb);
			else printf(Gb"|");
			printf(Gb);
		}
		printf("\n");
	}
	printf("+");
	for(i=1;i<=La;i++) 
	{
		printf("---");
		if(i==3||i==6) printf(Gg"+");
		else printf(Gb"+");
		printf(Gb);
	}
	
	for(j=0;j<=8;j++)
		for(i=0;i<=8;i++)
		{
			printf(GoToYX,(j+1)*2,((i+1)*4)-1);
			switch(i)
			{
				case 0 :
				case 1 :
				case 2 : switch(j)
					{
						case 0 :
						case 1 :
						case 2 : printf(Jg); break;
						case 3 :
						case 4 :
						case 5 : printf(Jb); break;
						case 6 :
						case 7 :
						case 8 : printf(Jg); break;
					}
					break;
				case 3 :
				case 4 :
				case 5 : switch(j)
					{
						case 0 :
						case 1 :
						case 2 : printf(Jb); break;
						case 3 :
						case 4 :
						case 5 : printf(Jg); break;
						case 6 :
						case 7 :
						case 8 : printf(Jb); break;
					}
					break;
				case 6 :
				case 7 :
				case 8 : switch(j)
					{
						case 0 :
						case 1 :
						case 2 : printf(Jg); break;
						case 3 :
						case 4 :
						case 5 : printf(Jb); break;
						case 6 :
						case 7 :
						case 8 : printf(Jg); break;
					}
					break;
			}
			if(GSDK[i][j][2]!=0) printf("%d", GSDK[i][j][2]);
		}
	
	
	printf(GoToYX,(j+1)*2,1);
	printf(ClearCol);
}*/

 Conclusion

fonctionnement :
1ère  dimension : créer une grille jouable de sudoku à partir d'une ligne dont chaque nombre tiré au "hasard" est unique (10>x>0)
2ème dimension : mélange des différentes lignes (et/ou) des régions par tirage au sort
3ème dimension : masquage toujours en utilisant un tableau tiré dont les éléments sont 1 (mask) 0 (nomask)

J'èpère pouvoir aider avec cette façon simple de creation de grille...

Je remercie mon grand-père pour m'avoir donné cette idée!

je suis débutant c'est mon premier programme en C

N'hésitez pas, je suis ouvert aux critiques constructives et je ne demande qu'a apprendre...


 Sources du même auteur

Source avec Zip Source avec une capture CCRT
Source avec Zip Source avec une capture OTHELLOC

 Sources de la même categorie

Source avec Zip Source avec une capture JEU DES CARTES par eapaceinfo
PROGRAMME DE JEU DE MPT par KerizGarmm
Source avec Zip Source avec une capture JEUX SERPENT par antho974
Source avec Zip Source avec une capture PENDU EN SDL par Damsou91
Source avec Zip STATE MACHINE MODIFICATION MATH BUCKHAM par billybones79

 Sources en rapport avec celle ci

MASTER MIND CONSOLE par bast63
Source avec Zip JEU DES PETIT CHEVAUX par flamt
Source avec Zip SUDOKU AVEC BACKTRACKING ET DANCING LINK par pabbati
Source avec Zip CODE DE HUFFMAN par Ricky_MacElroy
Source avec Zip JEU DES BOITES par ghribi

Commentaires et avis

Commentaire de blackdead08 le 29/06/2006 00:50:23

Des UpDate viendront bien tôt

(avec les commantaires et le mode raw)

qu'en pensez-vous?

Commentaire de blackdead08 le 29/06/2006 00:54:13

pas d'éditeur, grosse erreure:

Des UpDate's viendront bientôt...

(avec les commentaires et le mode raw)

Qu'en pensez-vous?

Commentaire de rrk275 le 29/06/2006 01:43:29

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?

Commentaire de blackdead08 le 29/06/2006 10:49:54

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)

Commentaire de vecchio56 le 29/06/2006 13:59:42 administrateur CS

CPPFrance::nbSudoku++;
Ca va bientot déborder

Commentaire de thesheepbiker le 08/04/2008 15:34:17

merci pour ta source la seule du site que je comprend

Commentaire de lucaniznizar le 03/04/2009 14:23:25

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)

Commentaire de le_papy le 29/06/2009 12:28:06

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!

Commentaire de natha31 le 19/09/2009 01:04:07 10/10

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

Commentaire de BigRider le 22/10/2010 04:39:09

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;
}

Commentaire de Elsick le 17/12/2010 08:07:34 10/10

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

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Modification de l'affichage du terminal [ par nbbob ] Bonjour,Je crée un sudoku sous linux en langage C avec ubuntuJ'aimerais modifier l'affichage sur le terminal, c'est a dire:-Effacer le terminal-Change sudoku [ par nbbob ] Bonjour, Je crée un sudoku sous linux en langage C avec ubuntu connaissez vous des liens pour du code sur le jeu du sudoku en C? merci [Visual C++] Couleur d'un pixel [ par kesakoa ] Bonjour.Je veux automatiser des taches suivant ce que l'écran affiche : j'ai besoin de pouvoir tester la couleur de certains pixels de l'écran. (je ne Couleur d'un pixel / Hotkey fonctions [ par Derto ] Bonjours. Quelqu'un aurait t'il la solution pour appeler une fonction grace à une touche de raccourci ? Et j'aimerai aussi savoir si il existe une f Surchage opérateur de copie [ par assoul ] Bonjour,Débutant en C++ je me trouve face à un problème.Je dois réaliser un jeu de poker:   tableJoueur représente l'ensemble des Joueurs (type vector Recherche Codeur pour AntiHack Jeux Video [ par WooAf ] Salut, je cherche un Codeur C++ capable de créer un Anti Hack pour un jeux video Nommer: DarKGunZLe but n,est pas de faire de la pub, le site est dark Couleur d'un pixel api win [ par Derto ] <span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: -webkit-monospace; font-size: 13px; font-style: nor pobleme de fonction [ par yacine93000 ] slt a tous voila mon problème un ami a moi ma passer le code source d'un d'un jeux qu'il a programmer mais quand je le colle dans code bloks le jeux n Couleur dans un listView [ par pepsidrinker ] Bonjour tout le monde! Je recherche un lien ou un tutorial , peux importe, kkchose pour pouvoir alterner les couleur de background d'un item dans un


Nos sponsors


Sondage...

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,858 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales