Programme Mastermind ??

ribery_7_91 Messages postés 240 Statut Membre -  
ribery_7_91 Messages postés 240 Statut Membre -
Bonjour a tous

Voila pour les cours je dois faire un programme de Mastermind
j'ai commencé quleque chose mais la je bloque
pouvez vous m'aider en essayant de garder l'ossature au maximum merci

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define M 4
#define N 6
#define P 5

int CduPif (int);
int INIT (void);
int SAISIE (void);
int BIEN_PLACE(int, int);
int MAL_PLACE(int, int);

main ()
{
int difficulte, REF[M], joueur[M], i, j=0, nbrCoups, noir, blanc, y;

printf("***** Master Mind *****\n\n");
do
{
srand(time(NULL));
printf("Difficultés :\n");
printf("1_amateur\n");
printf("2_initier\n\n");
scanf("%d", &difficulte);
printf("\n\n");
}while (difficulte != 1 && difficulte != 2);

if( difficulte == 1)
{
return -1;
}
if (difficulte == 2)
{
*REF = INIT();
}
for (i=1 ; i<= P ; i++)
{
*joueur= SAISIE();
noir = BIEN_PLACE(*REF, *joueur);
for (y=1 ; y <= noir ; y++)
{
printf ("*");
}

blanc = MAL_PLACE(*REF, *joueur);
for (y=1 ; y <= blanc ; y++)
{
printf("°");
}

if (noir == M)
{
j = 1;
nbrCoups = i;
i = P;
}
}
if (j == 0)
{
printf("Vous avez perdu");
}
if (j == 1)
{
printf("vous avez gagner en %d coups", nbrCoups);
}
}

int INIT (void)
{
int suite[M], i;

for (i=0 ; i<M; i++)
{
suite[i] = CduPif(N);
}
return *suite;
}

int CduPif (int n)
{
int aleatoire;

aleatoire = rand() % n;

return aleatoire;
}

int SAISIE (void)
{
int tab[M], i, j, valeur;
for (i=0 ; i<M ; i++)
{
j = i + 1;
printf ("entrer la valeur numero %d : ",j);
scanf ("%d", &valeur);
tab[i] = valeur;
}
return *tab;
}

int BIEN_PLACE(int tab1[], int tab2[])
{
int i, compare = 0;
for (i=0 ; i<M ; i++)
{
if (tab1[i] != tab2[i])
{
compare++;
}
}
return compare;
}
A voir également:

6 réponses

ribery_7_91 Messages postés 240 Statut Membre 16
 
personne ??
s'il vous plait
0
Nico# Messages postés 338 Statut Membre 102
 
Salut, Tu bloque sur quoi exactement
0
ribery_7_91 Messages postés 240 Statut Membre 16
 
ba justement je ne sais pas
je veux juste faire un mastermind avec ce programme mais sa ne marche aps alors je voulais savoir ce qui n'allait pas
merci
0
Nico# Messages postés 338 Statut Membre 102
 
Voila un exemple complet

tu mets sa dans un fichier Fonction.h

#define DROITE 77
#define BAS 80

enum CURSOR
{
NORMALCURSOR = 0,
NOCURSOR = 100,
SOLIDCURSOR = 20,
};

enum COLORS
{
BLACK = 0,
BLUE = FOREGROUND_BLUE,
GREEN = FOREGROUND_GREEN,
CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE,
RED = FOREGROUND_RED,
MAGENTA = FOREGROUND_RED | FOREGROUND_BLUE,
BROWN = FOREGROUND_RED | FOREGROUND_GREEN,
LIGHTGRAY = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
DARKGRAY = FOREGROUND_INTENSITY,
LIGHTBLUE = FOREGROUND_BLUE | FOREGROUND_INTENSITY,
LIGHTGREEN = FOREGROUND_GREEN | FOREGROUND_INTENSITY,
LIGHTCYAN = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
LIGHTRED = FOREGROUND_RED | FOREGROUND_INTENSITY,
LIGHTMAGENTA = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
YELLOW = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
};

typedef unsigned char U8;

void gotoxy( U8 x, U8 y );
void SetCursorType( U8 cur );
void textcolor( U8 text, U8 background );

void gotoxy(U8 x, U8 y)
{
COORD c;
c.X = x - 1;
c.Y = y - 1;
SetConsoleCursorPosition (STDOUT, c);
}

void SetCursorType( U8 cur )
{
BOOL visible = cur != NOCURSOR;
CONSOLE_CURSOR_INFO CursorInfo = { cur, visible };
SetConsoleCursorInfo (STDOUT, &CursorInfo);
}

void textcolor( U8 text, U8 background )
{
if (text > 15)
text = 7;
if (background >15)
background = 0;
SetConsoleTextAttribute (STDOUT, 16 * background + text);
}

puis sa dans un fichier Mastermind.c

#include "Fonction.h"
#include "Mastermind.h"

int main()
{
initialisation();
menu();
do {
switch(action)
{
case MENU : menu();
break;

case AIDE : aide();
break;

case PARTIE : reinitialisation();
demarrer_une_partie();
demande_rejouer();
break;
}
}while(rejouer == OUI || partie != DEMARREE);
}

void initialisation (void)
{
srand(time(NULL));
SetCursorType(NOCURSOR);
}

void menu (void)
{
U8 ligne = 8, touche;

system ("CLS");
gotoxy(21,3); printf ("BIENVENUE DANS LE JEU DU MASTERMIND");

gotoxy(27, 6); printf ("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",201,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,187);
gotoxy(27, 7); printf ("%c %c",186,186);
gotoxy(27, 8); printf ("%c D%cmarrer une partie %c",186,130,186);
gotoxy(27, 9); printf ("%c %c",186,186);
gotoxy(27,10); printf ("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",200,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,188);

gotoxy(27,13); printf ("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",201,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,187);
gotoxy(27,14); printf ("%c %c",186,186);
gotoxy(27,15); printf ("%c Aide %c",186,186);
gotoxy(27,16); printf ("%c %c",186,186);
gotoxy(27,17); printf ("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",200,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,188);

gotoxy(27,20); printf ("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",201,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,187);
gotoxy(27,21); printf ("%c %c",186,186);
gotoxy(27,22); printf ("%c Quitter le jeu %c",186,186);
gotoxy(27,23); printf ("%c %c",186,186);
gotoxy(27,24); printf ("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",200,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,188);

do {
gotoxy(22,ligne); printf ("==%c",62);
touche = getch();
gotoxy(22,ligne); printf (" ");
if (touche == HAUT)
{
if (ligne != 8) ligne-= 7;
else ligne = 22;
}
if (touche == BAS)
{
if (ligne != 22) ligne+= 7;
else ligne = 8;
}
}while (touche != ENTREE);

switch (ligne)
{
case 8 : action = PARTIE;
break;

case 15 : action = AIDE;
break;

case 22 : exit(0);
break;
}
}

void aide (void)
{
system("CLS");
printf("4 numeros sont tires au hasard et vous devez les retrouver\n\n");
printf("Si un numero que vous avez propose est correct, un I apparaitra en dessous\n\n");
printf("Si un numero que vous avez propose est parmi les quatres tires mais pas a la bonne place, un");
textcolor(LIGHTRED,BLACK); printf(" I "); textcolor(LIGHTGRAY,BLACK); printf("apparaitra en dessous.");
getch();
action = MENU;
}

void reinitialisation (void)
{
U8 i;

for (i=0;i<4;i++)
nb_choisi[i] = rand() % 10;
nb_proposition = 0;
combi_trouve = 0;
}

void demarrer_une_partie (void)
{
partie = DEMARREE;
affichage_depart();
do {
saisie_des_numeros();
test_des_numeros();
affichage();
}while(combi_trouve != TROUVE && nb_proposition < NB_PROPOSITION_MAX);
}

void affichage_depart (void)
{
system ("CLS");
printf ("Trouver les 4 bons num%cros : X X X X\n\t\t\t\t",130);
}

void saisie_des_numeros (void)
{
U8 i;

for (i=0;i<4;i++)
{
do {
nb_propose[i] = getch();
}while(nb_propose[i] < '0' || nb_propose[i] > '9');
nb_propose[i]&=0x0F;
printf ("%d ",nb_propose[i]);
}
nb_proposition++;
}

void test_des_numeros (void)
{
U8 i;

for (i=0;i<4;i++)
{
if (nb_propose[i] == nb_choisi[i])
nb_valide[i] = BP;
else
{
nb_valide[i] = FF;
if (i == 0)
{
if (nb_propose[i] == nb_choisi[1] || nb_propose[i] == nb_choisi[2] || nb_propose[i] == nb_choisi[3])
nb_valide[i] = MP;
}
if (i == 1)
{
if (nb_propose[i] == nb_choisi[0] || nb_propose[i] == nb_choisi[2] || nb_propose[i] == nb_choisi[3])
nb_valide[i] = MP;
}
if (i == 2)
{
if (nb_propose[i] == nb_choisi[0] || nb_propose[i] == nb_choisi[1] || nb_propose[i] == nb_choisi[3])
nb_valide[i] = MP;
}
if (i == 3)
{
if (nb_propose[i] == nb_choisi[0] || nb_propose[i] == nb_choisi[1] || nb_propose[i] == nb_choisi[2])
nb_valide[i] = MP;
}
}
}
if (nb_valide[0] == BP && nb_valide[1] == BP && nb_valide[2] == BP && nb_valide[3] == BP)
combi_trouve = TROUVE;
}

void affichage (void)
{
U8 i;

printf ("\n\t\t\t\t");
for (i=0;i<4;i++)
{
if (nb_valide[i] == BP) { printf ("I "); }
if (nb_valide[i] == FF) { printf (" "); }
if (nb_valide[i] == MP) { textcolor(LIGHTRED,BLACK); printf ("I "); }
textcolor(LIGHTGRAY,BLACK);
}

if (combi_trouve == TROUVE)
{
gotoxy(1,22);
printf ("Bravo, vous avez trouv%c la combinaison.",130);
}
else
{
if (nb_proposition == NB_PROPOSITION_MAX)
{
gotoxy(1,22);
printf ("Dommage. Vous n'avez pas trouv%c la combinaison qui %ctait : %d %d %d %d",130,130,nb_choisi[0],nb_choisi[1],nb_choisi[2],nb_choisi[3]);
}
else
printf ("\n\n\t\t\t\tX X X X\n\t\t\t\t");
}

}

void demande_rejouer (void)
{
U8 touche;

printf ("\n\nVoulez-vous rejouer : ");
do {
touche = getch();
}while(touche != 'o' && touche != 'O' && touche != 'n' && touche != 'N');

if (touche == 'o' || touche == 'O')
rejouer = OUI;
else
rejouer = NON;
}

puis sa dans Mastermind.h

/*Sert pour connaître l'action en cours*/
#define MENU 0
#define AIDE 1
#define PARTIE 2

/*Sert pour savoir si la partie est démarrée*/
#define DEMARREE 3

/*Sert pour savoir si les chiffres sont à la bonne place*/
#define BP 0 // bonne place
#define MP 1 // mauvaise place
#define FF 2 // nombre faux

/*Sert pour savoir si la combinaison à été trouvée*/
#define TROUVE 1

/*Sert pour savoir si le nombre de proposition est inférieur à 5*/
#define NB_PROPOSITION_MAX 5

/*Sert pour savoir si le joueur veut rejouer*/
#define OUI 0
#define NON 1

U8 action, partie, rejouer;
U8 nb_choisi[4], nb_propose[4], nb_valide[4];
U8 nb_proposition, combi_trouve;

void initialisation (void);
void menu (void);
void aide (void);
void reinitialisation (void);
void demarrer_une_partie (void);
void affichage_depart (void);
void saisie_des_numeros (void);
void test_des_numeros (void);
void affichage (void);
void demande_rejouer (void);
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
mype Messages postés 2449 Statut Membre 437
 
va voir ici ça pourrait t'aider...
0
ribery_7_91 Messages postés 240 Statut Membre 16
 
alors voila plutot que d'utiliser celui que je vous ai donné je vous en donne un nouveau qui consiste a faire un mastermind

la consigne est la suivante :
il faut faire des sous programme :
- le sous programme CduPif et init pour générer les nombres aléatoires du jeu
- le sous programme saisie pour que l'utilisateur saisisse les chiffres qu'il veut
-le sous programme mal_place qui dit le nombre de chiffres mal placé
- le sous programme bien_placé qui dit le nombre de chiffres bien placés

Il me manque les fonctions mal_placé et bien_placé et peut étre quelques retouches a faire sur ce qui est deja fait mais la structure ne doit pas changé

#include <stdio.h>
#include <stdlib.h>
#define taille 4
int CduPif (int);
int init (int []);
int saisie (void);
main ()
{
int tab[taille], i;
printf ("***********************\n");
printf ("* MASTERMIND *\n");
printf ("***********************\n\n");
printf ("Bienvenue sur le jeu de Mastermind\n");
init (tab);
for (i=0; i < taille; i++)
{
printf (" %d | ",tab[i]);
}
printf ("\t");
printf (" Les chiffres ont été générés \n\n");
printf (" Début du jeu : \n\n");
saisie ();

}

int init(int tab[])
{

int i,n ;

randomize();
printf (" Jusqu'à quel valeur voulez vous initialiser le jeu??\n");
scanf ("%d",&n);
for(i=0; i<4; i++)
{
tab[i] = CduPif(n);
}
}

int CduPif(int n)
{
int a;
a = rand() % n ;
return a;
}

int saisie ()
{
int i;
int ch;
for (i=0; i < taille; i++)
{
printf ("Veuillez saisir le chiffre %d : ",i+1);
scanf ("%d",&ch);
}
}


merci
0