Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // MasterMind
- //
- // Created by Robin Theys on 1/12/11.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <term.h>
- #include <string.h>
- #include <time.h>
- // Constantes
- #define COLORSMAX 8 // COULEURS DE BASE MAX
- // On peut modifier cette constante si on veut pimenter le jeu
- // et augmenter le nombre de couleurs à trouver
- // MCOLORS peut aller de 1 à 7 max
- #define MCOLORS 4 // NOMBRE DE COULEURS MAXIMUM A TROUVER
- // On peut modifier cette constante si on veut diminuer le
- // nombre d'essais min 1 max 10
- #define ESSAIS 10 // NOMBRE DE TENTATIVES MAXIMALES
- void gameTxt (char store[ESSAIS+1][MCOLORS], int menu, int essais);
- void printArray (char store[ESSAIS+1][MCOLORS], char rStore[ESSAIS+1][MCOLORS]);
- void colorGen (char store[ESSAIS+1][MCOLORS]);
- void resetArray (char store[ESSAIS+1][MCOLORS], char rStore[ESSAIS+1][MCOLORS]);
- void storeInput (char store[ESSAIS+1][MCOLORS], int essais);
- int checkStore (char store[ESSAIS+1][MCOLORS], char rStore[ESSAIS+1][MCOLORS], int essais, int mode);
- void mixPro (char rStore[ESSAIS+1][MCOLORS], int essais);
- int main () {
- // Variables Locales
- int choose, nfois, code, i;
- char store[ESSAIS+1][MCOLORS];
- char rStore[ESSAIS+1][MCOLORS];
- // Pour générer un nombre aléatoire
- srand(time(NULL));
- do {
- nfois = ESSAIS;
- resetArray(store, rStore);
- colorGen(store);
- gameTxt(store,0,nfois);
- scanf("%i", &choose);
- fpurge(stdin);
- // Si le joueur a choisit le mode débutant ou pro
- if ((choose != 0) && (choose < 3)) {
- do {
- gameTxt(store,choose,nfois);
- printArray(store, rStore);
- gameTxt(store,8,nfois);
- gameTxt(store,5,nfois);
- storeInput(store, nfois);
- code = checkStore(store, rStore, nfois, choose);
- nfois--;
- // On boucle tant qu'il nous reste des essais ou que
- // la combinaison a été trouvée
- } while ((code != 1) && (nfois > 0));
- // Si la bonne combinaison a été trouvée
- if (code == 1) {
- gameTxt(store,choose,nfois);
- printArray(store, rStore);
- gameTxt(store,8,nfois);
- gameTxt(store,5,nfois);
- gameTxt(store,3,nfois);
- } else {
- gameTxt(store,choose,nfois);
- printArray(store, rStore);
- gameTxt(store,8,nfois);
- gameTxt(store,5,nfois);
- gameTxt(store,4,nfois);
- }
- // On affiche la solution
- printf("\n S: ");
- for (i=0; i<MCOLORS; i++) {
- printf(" %c", store[0][i]);
- }
- printf("\n");
- getc(stdin);
- } else {
- if (choose != 0) {
- gameTxt(store,6,nfois);
- } else {
- choose = 0;
- }
- }
- // On boucle tant qu'on demande pas de quitter
- } while (choose != 0);
- gameTxt(store,9,nfois);
- return 0;
- }
- /* Fonction qui affiche les différents textes utilisés */
- void gameTxt (char store[ESSAIS+1][MCOLORS], int menu, int essais) {
- // Variables locales
- char * color[COLORSMAX] = {"Rouge", "Jaune", "Vert", "Bleu", "Orange", "Blanc", "Violet", "Fuchsia"};
- switch (menu) {
- case 0:
- system("clear");
- printf("\t****************\n");
- printf("\t* *\n");
- printf("\t* MasterMind *\n");
- printf("\t* *\n");
- printf("\t****************\n");
- printf("\n");
- printf("\t1. Mode Débutant\n");
- printf("\t2. Mode Pro \n");
- printf("\n");
- printf("\t0. Quitter \n");
- printf("\n");
- printf("\n");
- printf("\t Votre sélection : ");
- break;
- case 1:
- system("clear");
- printf("\n Mode Débutant\n\n");
- break;
- case 2:
- system("clear");
- printf("\n Mode Pro\n\n");
- break;
- case 3:
- printf("\nVous avez gagné(e) en %d essai(s)\n", 10-essais);
- break;
- case 4:
- printf("Vous avez perdu(e)\n");
- break;
- case 5:
- printf("\nN=Une bonne couleur bien placée");
- printf("\nB=Une bonne couleur mal placée");
- printf("\n\nLes indices ne sont pas dans l'ordre en mode pro\n");
- break;
- case 6:
- printf("\t Choix invalide !\n");
- getc(stdin);
- break;
- case 8:
- printf("\nLegende :\n");
- printf("\n0->%s ,1->%s, 2->%s, 3->%s\n4->%s, 5->%s, 6->%s, 7->%s\n", color[0], color[1], color[2], color[3], color[4], color[5],color[6], color[7]);
- break;
- case 9:
- printf("\n Au Revoir.\n");
- break;
- default:
- printf("Option Invalide");
- break;
- }
- }
- /* Procédure qui affiche le contenu du tableau */
- void printArray (char store[ESSAIS+1][MCOLORS], char rStore[ESSAIS+1][MCOLORS]) {
- // Variables locales
- int i, j, nbLines=ESSAIS;
- //printf(" S: %c %c %c %c\n", store[0][0], store[0][1], store[0][2], store[0][3]);
- printf(" S:");
- for (i=0; i<MCOLORS; i++) {
- printf(" *");
- }
- printf("\n");
- for (i=1; i<ESSAIS+1; i++) {
- printf("%3d. ", nbLines);
- for (j=0; j<MCOLORS; j++) {
- printf(" %c ", store[i][j]);
- }
- for (j=0; j<MCOLORS; j++) {
- printf("%c", rStore[i][j]);
- }
- printf("\n");
- nbLines--;
- }
- }
- /* Procédure qui génère une combinaison
- à trouver sans doublon */
- void colorGen (char store[ESSAIS+1][MCOLORS]) {
- // Variables locales
- int i, number, max=COLORSMAX;
- char codeColor[COLORSMAX] = { '0', '1', '2', '3', '4', '5', '6', '7' };
- // On crée une combinaison sans créer de doublons
- for (i=0; i<MCOLORS; i++) {
- number = rand()%max;
- store[0][i] = codeColor[number];
- codeColor[number] = codeColor[max-1];
- codeColor[max-1] = store[0][i];
- max--;
- }
- }
- /* Procédure qui vide le tableau à 0 */
- void resetArray (char store[ESSAIS+1][MCOLORS], char rStore[ESSAIS+1][MCOLORS]) {
- // Variables locales
- int i, j;
- for (i=0; i<ESSAIS+1; i++) {
- for (j=0; j<MCOLORS; j++) {
- store[i][j] = '-';
- rStore[i][j] = '-';
- }
- }
- }
- /* Procédure qui demande une combinaison à l'utilisateur
- et l'enregistre */
- void storeInput (char store[ESSAIS+1][MCOLORS], int essais) {
- // Variables locales
- int i;
- for (i=0; i<MCOLORS; i++) {
- printf("Donnez votre couleur n° %d : ", i+1);
- scanf("%c", &store[essais][i]);
- fpurge(stdin);
- }
- }
- /* Fonction qui vérifie si la combinaison de
- l'utilisateur est identique à celle à trouver */
- int checkStore (char store[ESSAIS+1][MCOLORS], char rStore[ESSAIS+1][MCOLORS], int essais, int mode) {
- // Variables locales
- int i, j, n=0, code;
- /* Nombre de pions noirs */
- for (i=0; i<MCOLORS; i++) {
- if (store[0][i] == store[essais][i]) {
- rStore[essais][i] = 'N';
- n++;
- } else {
- /* Nombre de pions blancs */
- for (j=0; j<MCOLORS; j++) {
- if (store[0][j] == store[essais][i]) {
- rStore[essais][i] = 'B';
- }
- }
- }
- }
- // Si on est en mode pro on mélange les pions blancs et noirs
- if (mode == 2) {
- mixPro(rStore, essais);
- }
- // Si on a 4 pions noirs on a trouvé la solution
- // et on renvoie le code retour 1
- if (n == MCOLORS) {
- code = 1;
- }
- return code;
- }
- /* Procédure qui mélange les pions blancs et noirs
- en mode pro */
- void mixPro (char rStore[ESSAIS+1][MCOLORS], int essais) {
- // Variables locales
- int i, indice, max=MCOLORS;
- char temp;
- int num[COLORSMAX] = { 0, 1, 2, 3, 4, 5, 6, 7 };
- for (i=0; i<MCOLORS-1; i++) {
- indice = rand()%max;
- temp = rStore[essais][num[indice]];
- rStore[essais][num[indice]] = rStore[essais][num[max-1]];
- rStore[essais][num[max-1]] = temp;
- max--;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement