Démineur pour débutant (borland c++)

Description

Il s'agit du jeu du démineur (sans aucun graphisme)

Source / Exemple :


#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

#define L 11
#define C 11
#define M 10

int nb_mine=0;

struct type_case
{int val;
 int visible;
int zero;
int mine;
int utile;};

void initialise_jeu(struct type_case tab[L][C])
{
int i,j;
for (i=0;i<L;i++)
for (j=0;j<C;j++)
{	tab[i][j].visible=0;
	tab[i][j].val=0;
	tab[i][j].zero=0;
	tab[i][j].mine=0;
	tab[i][j].utile=0;}
}

void creation_jeu (int N,struct type_case tab[L][C])
{
int j; int i; int compteur=0;
randomize();
while ( compteur<N)
	{
	i=random(L-2);
	j=random(C-2);
	if (tab[i+1][j+1].val ==0)
		{
		tab[i+1][j+1].val=-1;
		compteur++;
		}
	else ;
	}
}

void rempli_case(struct type_case tab[L][C])
{
int i,j, compteur=0;
for (i=1;i<L-1;i++)
	for (j=1;j<C-1;j++)
	{	
	if (tab[i-1][j].val==-1) compteur++;
	if (tab[i-1][j+1].val==-1) compteur++;
	if (tab[i][j+1].val==-1) compteur++;
	if (tab[i+1][j+1].val==-1) compteur++;
	if (tab[i+1][j].val==-1) compteur++;
	if (tab[i+1][j-1].val==-1) compteur++;
	if (tab[i][j-1].val==-1) compteur++;
	if (tab[i-1][j-1].val==-1) compteur++;		
	if (tab[i][j].val==0) tab[i][j].val=compteur;
	compteur=0;
	}

for (i=0;i<L;i++)
	{tab[i][0].val=9;
	tab[i][C-1].val=9;}			
for (j=0;j<C;j++)
	{tab[0][j].val=9;
	tab[L-1][j].val=9;}
}

void affichage(struct type_case tab[L][C])
{
int i,j;	
for (i=1;i<L-1;i++)
for (j=1;j<C-1;j++)
	{ gotoxy(3*i,j); 
	if (tab[i][j].visible==1) 
		{ if ((tab[i][j].mine==0) && (tab[i][j].val!=-1)) {textcolor(11);cprintf("%d",tab[i][j].val);}
		  else {textcolor(10);cprintf("X");};
		}
	else printf("+");
	}
	   
printf("\n");
}

void affiche_zero(struct type_case tab[L][C],int xx,int yy)
{
tab[xx-1][yy].visible=1;
tab[xx-1][yy+1].visible=1;
tab[xx][yy+1].visible=1;
tab[xx+1][yy+1].visible=1;
tab[xx+1][yy].visible=1;
tab[xx+1][yy-1].visible=1;
tab[xx][yy-1].visible=1;
tab[xx-1][yy-1].visible=1;

tab[xx][yy].zero=1;
tab[xx][yy].visible=1;

if ((tab[xx-1][yy].val==0) && (tab[xx-1][yy].zero==0)) affiche_zero(tab,xx-1,yy);
if ((tab[xx-1][yy+1].val==0) && (tab[xx-1][yy+1].zero==0)) affiche_zero(tab,xx-1,yy+1);
if ((tab[xx][yy+1].val==0) && (tab[xx][yy+1].zero==0)) affiche_zero(tab,xx,yy+1);
if ((tab[xx+1][yy+1].val==0) && (tab[xx+1][yy+1].zero==0)) affiche_zero(tab,xx+1,yy+1);
if ((tab[xx+1][yy].val==0) && (tab[xx+1][yy].zero==0)) affiche_zero(tab,xx+1,yy);
if ((tab[xx+1][yy-1].val==0) && (tab[xx+1][yy-1].zero==0)) affiche_zero(tab,xx+1,yy-1);
if ((tab[xx][yy-1].val==0) && (tab[xx][yy-1].zero==0)) affiche_zero(tab,xx,yy-1);
if ((tab[xx-1][yy-1].val==0) && (tab[xx-1][yy-1].zero==0)) affiche_zero(tab,xx-1,yy-1);
}

void montre_ttes_mines(struct type_case tableau[L][C])
{
int i,j;
for (i=1;i<L-1;i++)
for (j=1;j<L-1;j++)
	{if (tableau[i][j].val==-1) 
		{textcolor(4);gotoxy(3*i,j);cprintf("#");}
	else;};
}

void verif_nb_mine( struct type_case tableau[L][C])
{
int a=0;
int i,j;
for (i=1;i<L-1;i++)
for (j=1;j<L-1;j++)
	if ((tableau[i][j].visible==1) && (tableau[i][j].val!=-1))
		{a++;}
	else;
	if (a==((L-2)*(C-2)-M))
		{ gotoxy(12,12); printf("GAGNE\n");exit(0);}
	else;
}

void demande(struct type_case tab[L][C])			
{
int x=0,y=0,choix=0,r;

printf("\n\t1 : Desamorcer une mine");
printf("\n\t2 : Localiser une mine");
printf("\nVotre choix :");
scanf("%d",&choix);
switch (choix) {

	case 1:
	{
	printf("\n\nCoordonnees de la case à desamorcer(x,y):");
	scanf("%d,%d",&y, &x);
	if (tab[x][y].utile==0)
		{tab[x][y].visible=1;
		tab[x][y].utile=1;
		tab[x][y].mine=0;
		clrscr();
		if (tab[x][y].val==0) 	
			affiche_zero(tab,x,y);
		else;
		affichage(tab);
		verif_nb_mine(tab);
		if (tab[x][y].val!=-1)
			 {demande(tab);}
		else 	
			{  montre_ttes_mines(tab);  gotoxy(12,12);printf("Perdu !");};}
	else {         clrscr();
		affichage(tab);
		demande(tab);};
	}
	break;

	case 2:{
	printf("\n\nCoordonnees de la case à isoler(x,y):");
	scanf("%d,%d",&y, &x);
	if (tab[x][y].visible!=1)
		{tab[x][y].visible=1;
		tab[x][y].mine=1;
		clrscr();
		affichage(tab);
		demande(tab);}
	else {
		clrscr();
		affichage(tab);
		demande(tab);};
	        };
	break;
	}
}

void main()
{
struct type_case jeu[L][C];
clrscr();
initialise_jeu(jeu);
creation_jeu(M,jeu);
rempli_case(jeu);
affichage(jeu);
demande(jeu);
}

Codes Sources

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.