Mastermind en langage c basique

Contenu du snippet

Entièrement écrit en langage C pour mode console sans interface graphique mais avec un menu.

Source / Exemple :


/* JEU : MASTERMIND */
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <ctype.h>

#define max 14
#define bleu 9
#define rouge 12
#define blanc 15
#define blink 128

int f1 (void);
int f2 (void);
int f3 (void);
int f4 (void);
int f5 (void);
int f6 (void);
int f7 (void);
void page_garde (char *, int);
void continuation (void);
long duree (int, long, long, char *);
void ouverture (char *, int);
void fermeture (char *, int);
void raz (char *);
void tri (int);
void score (int);
void initialisation (int *, int);
void saisir (int *, long, int);
int comparebon (int *, int *, int);
int comparemal (int *, int *, int);
void affiche (int, int);
void sauvegarde (int, int);
void restitution (int, int);

struct {
		 int elt;
		 int (*f)();
		 }
		 tableau[]={
						{1, f1},
						{2, f2},
						{3, f3},
						{4, f4},
						{5, f5},
						{6, f6},
						{7, f7}
					  };
struct result {
				  int pi;
				  int temp;
				  char nom[max];
				  char num[max];
				  };
struct result resultat[2][11];
FILE * fichier;
int tabo[11], tabj[11];
int k, kprim, parite, premier[2];
time_t temps, tps1, tps2;
long tps[4];

void main()
{
int ind=0;
char chy;

page_garde ("LOGICIEL DU JEU DU MASTERMIND", 1999);
ouverture ("mind_sc0.txt", 0);
ouverture ("mind_sc1.txt", 1);
while (ind<7)
	{
	clrscr();
	printf("Menu g?n?ral:");
	printf("\n<A> propos du mastermind.");
	printf("\n<J>oueur contre l'ordinateur.");
	printf("\n<O>rdinateur contre le joueur.");
	printf("\n<V>oir les meilleurs scores contre l'ordinateur.");
	printf("\nVoir les meilleurs <S>cores de l'ordinateur.");
	printf("\n<Q>uitter.\nQuel est votre choix? : ");
	fflush(stdin);
	scanf("%c", &chy);
	if (chy=='q' || chy=='Q')
		ind=7;
	else
		if (chy=='j' || chy=='J')
			ind=1;
		else
			if (chy=='o' || chy=='O')
				ind=2;
			else
				if (chy=='a' || chy=='A')
					ind=4;
				else
					if (chy=='v' || chy=='V')
						ind=5;
					else
						if (chy=='s' || chy=='S')
							ind=6;
						else
							ind=3;
	ind=(*tableau[ind-1].f)();
	}
fermeture ("mind_sc0.txt", 0);
fermeture ("mind_sc1.txt", 1);
clrscr();
}

int f1 (void)        /* jeu joueur contre l'ordinateur */
{
char chx='o', name[max];
int position, local, poub, test, erreur, i, x, y;
int nbchiffre, cpt, nmax;
long t0, tfin, comb;

clrscr();
printf("Veuillez entrez votre nom puis valider : ");
fflush(stdin);
gets(name);
for (i=0; i<strlen(name); i++)
	name[i]=toupper(name[i]);
do
	{
	erreur=0;
	x=wherex();
	y=wherey();
	printf("\nCombien de chiffres voulez-vous qu'il y ait dans le code secret? : ");
	fflush(stdin);
	scanf("%d", &nbchiffre);
	if (nbchiffre<0 || nbchiffre>10)
		{
		erreur=f3();
		gotoxy(1, y+1);
		delline();
		gotoxy(x, y);
		}
	}
while (erreur==3);
while (chx=='o' || chx=='O')
	{
	clrscr();
	cpt=0;
	initialisation (tabo, nbchiffre);
	nmax=0;
	for (i=0; i<nbchiffre; i++)
		nmax+=nbchiffre*((int)(pow10(i)));
	srand ((unsigned)time (&temps));
	t0=time(0);
	do
		{
		cpt++;
		do
			{
			erreur=0;
			x=wherex();
			y=wherey();
			printf("%d", nmax);
			printf("Entrez la combinaison %s: ", name);
			fflush(stdin);
			scanf("%ld", &comb);
			printf("%ld", comb);
			if (comb<0 || comb>nmax);
				{
				erreur=f3();
				gotoxy(x, y);
				delline();
				gotoxy(x, y);
				}
			}
		while (erreur==3);
		saisir (tabj, comb, nbchiffre);
		affiche (comparebon (tabo, tabj, nbchiffre), comparemal (tabo, tabj, nbchiffre));
		}
	while ((comparebon (tabo, tabj, nbchiffre))!=nbchiffre);

	do
		{
		test=0;
		x=wherex();
		y=wherey();
		printf("\n\nVoulez-vous rejouer? <O>ui, <N>on : ");
		fflush(stdin);
		scanf("%c", &chx);
		if (chx!='o' && chx!='O' && chx!='n' && chx!='N')
			{
			test=f3();
			gotoxy(1, y+2);
			delline();
			gotoxy(x, y);
			}
		}
	while (test==3);
	}
return(1);
}

int f2 (void)    /* jeu ordinateur contre joueur */
{
return(2);
}

int f3 (void)            /* traitement des erreurs */
{
int x, y, cl;

x=wherex();
y=wherey();
textcolor(rouge);
cprintf("\n\rChoix impossible!!!\a");
continuation();
gotoxy(1, y+1);
for (cl=0; cl<4; cl++)
	delline();
gotoxy(x, y);
return(3);
}

int f4 (void)             /* à propos du jeu */
{
char aide[800]="", choix;
int test, x, y;

clrscr();
if ((fichier=fopen("mind.txt", "r"))!=NULL)
	{
	fread(&aide, 1, 1, fichier);
	fclose(fichier);
	fflush(stdin);
	puts(aide);
	do
		{
		test=0;
		x=wherex();
		y=wherey();
		printf("\nPage <S>uivante, <Q>uitter : ");
		fflush(stdin);
		scanf("%c", &choix);
		if (choix!='s' && choix!='S' && choix!='q' && choix!='Q')
			{
			f3();
			gotoxy(1, y+1);
			delline();
			gotoxy(x, y);
			test=3;
			}
		}
	while (test==3);
	if (choix=='s' || choix=='S')
		{
		clrscr();
		fichier=fopen("mind.txt", "r");
		fseek(fichier, +683, 0);
		fread(&aide, 792, 1, fichier);
		fclose(fichier);
		fflush(stdin);
		puts(aide);
		do
			{
			test=0;
			x=wherex();
			y=wherey();
			printf("\n\nPage <P>récédente, <Q>uitter : ");
			fflush(stdin);
			scanf("%c", &choix);
			if (choix!='p' && choix!='P' && choix!='q' && choix!='Q')
				{
				f3();
				gotoxy(1, y+2);
				delline();
				gotoxy(x, y);
				test=3;
				}
			}
		while (test==3);
		if (choix=='p' || choix=='P')
			f4();
		}
	}
else
	{
	printf("Le fichier d'aide est absent.\n");
	f3();
	}
clrscr();
return(4);
}

int f5 (void)         /* affichage des scores joueur contre ordinateur */
{
score (0);
return(5);
}

int f6 (void)        /* affichage des scores joueurs contre joueurs */
{
score (1);
return(6);
}

int f7 (void)           /* quitter */
{
int dip=1, x, y;
char cht;

x=wherex();
y=wherey();
do
	{
	printf("\nVoulez-vous vraiment quitter? <O>ui, <N>on : ");
	fflush(stdin);
	scanf("%c", &cht);
	if (cht=='o' || cht=='O')
		dip=7;
	else
		if (cht=='n' || cht=='N')
			dip=1;
		else
			{
			dip=f3();
			gotoxy(1, y+1);
			delline();
			gotoxy(x, y);
			}
	}
while (dip==3);
return(dip);
}

void page_garde (char pgm[], int an)
{
char txt[60], txt0[2];

clrscr();
textcolor(blanc);
strcpy(txt, "***************************************************");
gotoxy(15, 8);
puts(txt);
strcpy(txt0, "*");
gotoxy(15, 9);
puts(txt0);
gotoxy(65, 9);
puts(txt0);
gotoxy(15, 10);
puts(txt0);
gotoxy((int)(41-(strlen(pgm))/2), 10);
puts(pgm);
gotoxy(65, 10);
puts(txt0);
gotoxy(15, 11);
puts(txt0);
gotoxy(65, 11);
puts(txt0);
gotoxy(15, 12);
puts(txt0);
gotoxy(65, 12);
puts(txt0);
strcpy(txt, "*          COPYRIGHT ALEXANDRE ROGER");
gotoxy(15, 13);
puts(txt);
gotoxy(52, 13);
printf("%d", an);
gotoxy(65, 13);
puts(txt0);
gotoxy(15, 14);
puts(txt0);
gotoxy(65, 14);
puts(txt0);
gotoxy(15, 15);
puts(txt0);
gotoxy(65, 15);
puts(txt0);
gotoxy(15, 16);
puts(txt0);
textcolor(blanc+blink);
gotoxy(20, 16);
cprintf("< APPUYEZ SUR UNE TOUCHE POUR CONTINUER >");
textcolor(blanc);
gotoxy(65, 16);
puts(txt0);
gotoxy(15, 17);
puts(txt0);
gotoxy(65, 17);
puts(txt0);
strcpy(txt, "***************************************************");
gotoxy(15, 18);
puts(txt);
gotoxy(80, 25);
getch();
}

void continuation (void)
{
textcolor(blink+blanc);
cprintf("\n\n\r< APPUYEZ SUR UNE TOUCHE POUR CONTINUER >");
textcolor(blanc);
gotoxy(80, 25);
getch();
}

long duree (int ordi, long tpa, long tpb, char nombis[])
{
long temps_partiel, mn, sec;

if ((parite%2)==1)
	{
	tps1+=tpb-tpa;
	temps_partiel=tps1;
	}
else
	{
	tps2+=tpb-tpa;
	temps_partiel=tps2;
	}
sec=temps_partiel%60;
mn=(temps_partiel-sec)/60;
if (ordi==1)
	{
	if (temps_partiel==0)
		printf("\nTemps utilisé par %s : %ld seconde", nombis, sec);
	else
		if (mn==0)
			printf("\nTemps utilisé par %s : %ld secondes", nombis, sec);
		else
			if (mn==1 && sec==0)
				printf("\nTemps utilisé par %s : %ld minute", nombis, mn);
			else
				if (mn==1 && sec==1)
					{
					printf("\nTemps utilisé par %s : %ld minute ", nombis, mn);
					printf("et %ld seconde", sec);
					}
				else
					if (mn==1 && sec>1)
						{
						printf("\nTemps utilisé par %s : %ld minute ", nombis, mn);
						printf("et %ld secondes", sec);
						}
					else
						if (mn>1 && sec==0)
							printf("\nTemps utilisé par %s : %ld minutes", nombis, mn);
						else
							if (mn>1 && sec==1)
								{
								printf("\nTemps utilisé par %s : ", nombis);
								printf("%ld minutes et %ld seconde", mn, sec);
								}
							else
								{
								printf("\nTemps utilisé par %s : ", nombis);
								printf("%ld minutes et %ld secondes", mn, sec);
								}
	}
return(temps_partiel);
}

void ouverture (char fic[], int h)
{
int i;

if ((fichier=fopen(fic, "r+"))!=NULL)
	{
	fread(&premier[h], 1, 1, fichier);
	if (premier[h]!=0)
		{
		for (i=1; i<=premier[h]; i++)
			{
			fseek(fichier, 2+3*i, 0);
			fflush(stdin);
			fread(&(resultat[h][i].pi), 2, 1, fichier);
			fseek(fichier, 35+5*i, 0);
			fflush(stdin);
			fread(&(resultat[h][i].temp), 4, 1, fichier);
			fseek(fichier, 80+20*i, 0);
			fflush(stdin);
			fread(&(resultat[h][i].nom), 14, 1, fichier);
			fflush(stdin);
			fseek(fichier, 280+20*i, 0);
			fread(&(resultat[h][i].num), 14, 1, fichier);
			}
		}
	fclose(fichier);
	}
else
	raz (fic);
}

void fermeture (char fic[], int h) /* réécriture dans le fichier */
{
int i;

fichier=fopen(fic, "w+");
fseek(fichier, 0, 0);
fflush(stdin);
fwrite(&premier[h], sizeof(premier[h]), 1, fichier);
if (premier[h]!=0)
	{
	for (i=1; i<=premier[h]; i++)
		{
		fseek(fichier, 2+3*i, 0);
		fflush(stdin);
		fwrite(&(resultat[h][i].pi), sizeof(resultat[h][i].pi), 1, fichier);
		fseek(fichier, 35+5*i, 0);
		fflush(stdin);
		fwrite(&(resultat[h][i].temp), sizeof(resultat[h][i].temp), 1, fichier);
		fseek(fichier, 80+20*i, 0);
		fflush(stdin);
		fwrite(&(resultat[h][i].nom), 1+strlen(resultat[h][i].nom), 1, fichier);
		fseek(fichier, 280+20*i, 0);
		fflush(stdin);
		fwrite(&(resultat[h][i].num), 1+strlen(resultat[h][i].num), 1, fichier);
		}
	}
fclose(fichier);
}

void raz (char fic[])
{
int premier=0;

fichier=fopen(fic, "w");
fseek(fichier, 0, 0);
fflush(stdin);
fwrite(&premier, sizeof(premier), 1, fichier);
fclose(fichier);
}

void tri (int h)
{
int i=0, j;

while ((i<=premier[h]) && (resultat[h][0].pi<=resultat[h][i].pi))
	{
	if (resultat[h][0].pi==resultat[h][i].pi)
		if (resultat[h][0].temp<resultat[h][i].temp)
			break;
	i++;
	if (premier[h]==0)
		break;
	}
if (i<10)
	{
	for (j=0; j<=premier[h]-i; j++)
		{
		resultat[h][premier[h]+1-j].pi=resultat[h][premier[h]-j].pi;
		resultat[h][premier[h]+1-j].temp=resultat[h][premier[h]-j].temp;
		strcpy(resultat[h][premier[h]+1-j].nom, resultat[h][premier[h]-j].nom);
		strcpy(resultat[h][premier[h]+1-j].num, resultat[h][premier[h]-j].num);
		}
	}
if (i<11)
	{
	resultat[h][i].pi=resultat[h][0].pi;
	resultat[h][i].temp=resultat[h][0].temp;
	strcpy(resultat[h][i].nom, resultat[h][0].nom);
	strcpy(resultat[h][i].num, resultat[h][0].num);
	}
}

void score (int h)         /* affichage des scores */
{
int i, j, ind_erreur, x, y, param1, param2;
char variable[22], chw, chq;

clrscr();
if (h==0)
	{
	strcpy(variable, "CONTRE L'ORDINATEUR");
	gotoxy(15, 1);
	}
else
	{
	strcpy(variable, "");
	gotoxy(25, 1);
	}
printf("CLASSEMENT DES MEILLEURS JOUEURS %s\n\n", variable);
printf("     pions  temps          nom       contre       nom");
for (i=1; i<=premier[h]; i++)
	{
	printf("\n%2d : ", i);
	printf(" %2d", resultat[h][i].pi);
	param1=(resultat[h][i].temp)%60;
	param2=(resultat[h][i].temp-param1)/60;
	if (param2>60)
		param2=60;
	printf("    %2d.%02d     ", param2, param1);
	j=0;
	do
		{
		putchar((resultat[h][i].nom)[j]);
		j++;
		}
	while((isalpha((resultat[h][i].nom)[j])) || (((resultat[h][i].nom)[j])=='-'));
	gotoxy(40, 3+i);
	printf("vs    ");
	j=0;
	do
		{
		putchar((resultat[h][i].num)[j]);
		j++;
		}
	while((isalpha((resultat[h][i].num)[j]))||(((resultat[h][i].num)[j])=='-'));
	}
gotoxy(1, 15);
do
	{
	ind_erreur=1;
	printf("<Q>uitter, <E>ffacer les scores : ");
	fflush(stdin);
	scanf("%c", &chw);
	if (chw!='q' && chw!='Q' && chw!='e' && chw!='E')
		{
		ind_erreur=f3();
		gotoxy(1, 15);
		delline();
		gotoxy(1, 15);
		}
	}
while (ind_erreur==3);
if (chw=='e' || chw=='E')
	{
	do
		{
		x=wherex();
		y=wherey();
		ind_erreur=1;
		printf("\nVoulez-vous vraiment effacer les scores? <O>ui, <N>on : ");
		fflush(stdin);
		scanf("%c", &chq);
		if (chq=='n' || chq=='N')
			score (h);
		else
			if ((chq=='o' || chq=='O') && premier[h]!=0)
				{
				premier[h]=0;
				if (h==0)
					raz ("sig_sc0.txt");
				else
					raz ("sig_sc1.txt");
				break;
				}
			else
				{
				ind_erreur=f3();
				gotoxy(1, y+1);
				delline();
				gotoxy(x, y);
				}
		}
	while (ind_erreur==3);
	}
}

void initialisation (int ta[], int variable)        /* initialisation du jeu */
{
int i, j, alpha, poub, u, v;
time_t temps;

for (i=0; i<variable; i++)
	ta[i]=i;
srand ((unsigned)time (&temps));
alpha=rand()%100;
srand ((unsigned)time (&temps));
for (i=0; i<1+rand()%alpha; i++)
	{
	u=rand()%variable;
	v=u+1;
	if (v>=variable)
		v=0;
	poub=ta[u];
	ta[u]=ta[v];
	ta[v]=poub;
	}
}

void saisir (int tb[], long a, int b)
{
int i;

for (i=0; i<=b; i++)
	{
	tb[b-i]=a%10;
	a-=tb[b-i];
	a/=10;
	}
}

int comparebon (int co[], int cj[], int c)
{
int bon=0, i=0;

while (i<c)
	{
	if (co[i]==cj[i])
		bon++;
	i++;
	}
return (bon);
}

int comparemal (int fo[], int fj[], int f)
{
int mal=0, i, j;

for (i=0; i<f; i++)
	for (j=0; j<f; j++)
		if (fj[i]==fo[j] && i!=j)
			{
			mal++;
			break;
			}
return (mal);
}

void affiche (int bo, int ma)
{
printf("%d pions biens placés et %d pions mal placés", bo, ma);
}

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.