Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2011
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.38 KB | None | 0 0
  1. //
  2. //  main.c
  3. //  MasterMind
  4. //
  5. //  Created by Robin Theys on 1/12/11.
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <term.h>
  12. #include <string.h>
  13. #include <time.h>
  14.  
  15. // Constantes
  16. #define COLORSMAX 8         // COULEURS DE BASE MAX
  17.  
  18. // On peut modifier cette constante si on veut pimenter le jeu
  19. // et augmenter le nombre de couleurs à trouver
  20. // MCOLORS peut aller de 1 à 7 max
  21. #define MCOLORS 4           // NOMBRE DE COULEURS MAXIMUM A TROUVER
  22.  
  23. // On peut modifier cette constante si on veut diminuer le
  24. // nombre d'essais min 1 max 10
  25. #define ESSAIS 10           // NOMBRE DE TENTATIVES MAXIMALES
  26.  
  27.  
  28. void gameTxt (char store[ESSAIS+1][MCOLORS], int menu, int essais);
  29. void printArray (char store[ESSAIS+1][MCOLORS], char rStore[ESSAIS+1][MCOLORS]);
  30. void colorGen (char store[ESSAIS+1][MCOLORS]);
  31. void resetArray (char store[ESSAIS+1][MCOLORS], char rStore[ESSAIS+1][MCOLORS]);
  32. void storeInput (char store[ESSAIS+1][MCOLORS], int essais);
  33. int checkStore (char store[ESSAIS+1][MCOLORS], char rStore[ESSAIS+1][MCOLORS], int essais, int mode);
  34. void mixPro (char rStore[ESSAIS+1][MCOLORS], int essais);
  35.  
  36.  
  37.  
  38.  
  39. int main () {
  40.    
  41.     // Variables Locales
  42.     int choose, nfois, code, i;
  43.     char store[ESSAIS+1][MCOLORS];
  44.     char rStore[ESSAIS+1][MCOLORS];
  45.    
  46.     // Pour générer un nombre aléatoire
  47.     srand(time(NULL));
  48.  
  49.        
  50.     do {
  51.        
  52.         nfois = ESSAIS;
  53.         resetArray(store, rStore);
  54.         colorGen(store);
  55.         gameTxt(store,0,nfois);
  56.         scanf("%i", &choose);
  57.         fpurge(stdin);
  58.        
  59.         // Si le joueur a choisit le mode débutant ou pro
  60.         if ((choose != 0) && (choose < 3)) {
  61.            
  62.             do {
  63.                
  64.                 gameTxt(store,choose,nfois);
  65.                 printArray(store, rStore);
  66.                 gameTxt(store,8,nfois);
  67.                 gameTxt(store,5,nfois);
  68.                 storeInput(store, nfois);
  69.                 code = checkStore(store, rStore, nfois, choose);
  70.                 nfois--;
  71.            
  72.             // On boucle tant qu'il nous reste des essais ou que
  73.             // la combinaison a été trouvée  
  74.             } while ((code != 1) && (nfois > 0));
  75.            
  76.             // Si la bonne combinaison a été trouvée
  77.             if (code == 1) {
  78.                
  79.                 gameTxt(store,choose,nfois);
  80.                 printArray(store, rStore);
  81.                 gameTxt(store,8,nfois);
  82.                 gameTxt(store,5,nfois);
  83.                 gameTxt(store,3,nfois);
  84.            
  85.                
  86.             } else {
  87.                
  88.                 gameTxt(store,choose,nfois);
  89.                 printArray(store, rStore);
  90.                 gameTxt(store,8,nfois);
  91.                 gameTxt(store,5,nfois);
  92.                 gameTxt(store,4,nfois);
  93.                
  94.             }
  95.             // On affiche la solution
  96.             printf("\n  S: ");
  97.            
  98.             for (i=0; i<MCOLORS; i++) {
  99.                
  100.                 printf(" %c", store[0][i]);
  101.             }
  102.            
  103.             printf("\n");
  104.             getc(stdin);
  105.        
  106.         } else {
  107.            
  108.             if (choose != 0) {
  109.                
  110.                 gameTxt(store,6,nfois);
  111.            
  112.             } else {
  113.                
  114.                 choose = 0;
  115.             }
  116.         }
  117.            
  118.     // On boucle tant qu'on demande pas de quitter    
  119.     } while (choose != 0);
  120.    
  121.    gameTxt(store,9,nfois);
  122.      
  123.     return 0;
  124. }
  125.  
  126. /* Fonction qui affiche les différents textes utilisés */
  127. void gameTxt (char store[ESSAIS+1][MCOLORS], int menu, int essais) {
  128.    
  129.     // Variables locales
  130.     char * color[COLORSMAX] = {"Rouge", "Jaune", "Vert", "Bleu", "Orange", "Blanc", "Violet", "Fuchsia"};
  131.    
  132.     switch (menu) {
  133.            
  134.         case 0:
  135.             system("clear");
  136.             printf("\t****************\n");
  137.             printf("\t*              *\n");
  138.             printf("\t*  MasterMind  *\n");
  139.             printf("\t*              *\n");
  140.             printf("\t****************\n");
  141.             printf("\n");
  142.             printf("\t1. Mode Débutant\n");
  143.             printf("\t2. Mode Pro     \n");
  144.             printf("\n");
  145.             printf("\t0. Quitter      \n");
  146.             printf("\n");
  147.             printf("\n");
  148.             printf("\t Votre sélection : ");
  149.             break;
  150.            
  151.         case 1:
  152.             system("clear");
  153.             printf("\n  Mode Débutant\n\n");
  154.             break;
  155.            
  156.         case 2:
  157.             system("clear");
  158.             printf("\n  Mode Pro\n\n");
  159.             break;
  160.        
  161.         case 3:
  162.             printf("\nVous avez gagné(e) en %d essai(s)\n", 10-essais);
  163.             break;
  164.            
  165.         case 4:
  166.             printf("Vous avez perdu(e)\n");
  167.             break;
  168.            
  169.         case 5:
  170.             printf("\nN=Une bonne couleur bien placée");
  171.             printf("\nB=Une bonne couleur mal placée");
  172.             printf("\n\nLes indices ne sont pas dans l'ordre en mode pro\n");
  173.             break;
  174.            
  175.         case 6:
  176.             printf("\t Choix invalide !\n");
  177.             getc(stdin);
  178.             break;
  179.            
  180.         case 8:
  181.             printf("\nLegende :\n");
  182.             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]);
  183.             break;
  184.            
  185.         case 9:
  186.             printf("\n Au Revoir.\n");
  187.             break;
  188.            
  189.         default:
  190.             printf("Option Invalide");
  191.             break;
  192.     }
  193. }
  194.  
  195. /* Procédure qui affiche le contenu du tableau */
  196. void printArray (char store[ESSAIS+1][MCOLORS], char rStore[ESSAIS+1][MCOLORS]) {
  197.    
  198.     // Variables locales
  199.     int i, j, nbLines=ESSAIS;
  200.    
  201.     //printf("  S:  %c  %c  %c  %c\n", store[0][0], store[0][1], store[0][2], store[0][3]);
  202.     printf("  S:");
  203.    
  204.     for (i=0; i<MCOLORS; i++) {
  205.        
  206.         printf("  *");
  207.     }
  208.    
  209.     printf("\n");
  210.    
  211.     for (i=1; i<ESSAIS+1; i++) {
  212.        
  213.         printf("%3d. ", nbLines);
  214.        
  215.         for (j=0; j<MCOLORS; j++) {
  216.            
  217.             printf(" %c ", store[i][j]);
  218.                    
  219.         }
  220.        
  221.         for (j=0; j<MCOLORS; j++) {
  222.            
  223.             printf("%c", rStore[i][j]);
  224.         }
  225.         printf("\n");
  226.        
  227.         nbLines--;
  228.     }
  229. }
  230.  
  231. /* Procédure qui génère une combinaison
  232.    à trouver sans doublon */
  233. void colorGen (char store[ESSAIS+1][MCOLORS]) {
  234.    
  235.     // Variables locales
  236.     int i, number, max=COLORSMAX;
  237.     char codeColor[COLORSMAX] = { '0', '1', '2', '3', '4', '5', '6', '7' };
  238.    
  239.     // On crée une combinaison sans créer de doublons
  240.     for (i=0; i<MCOLORS; i++) {
  241.        
  242.         number = rand()%max;
  243.         store[0][i] = codeColor[number];
  244.         codeColor[number] = codeColor[max-1];
  245.         codeColor[max-1] = store[0][i];
  246.                
  247.         max--;
  248.     }  
  249. }
  250. /* Procédure qui vide le tableau à 0 */
  251. void resetArray (char store[ESSAIS+1][MCOLORS], char rStore[ESSAIS+1][MCOLORS]) {
  252.    
  253.     // Variables locales
  254.     int i, j;
  255.    
  256.     for (i=0; i<ESSAIS+1; i++) {
  257.        
  258.         for (j=0; j<MCOLORS; j++) {
  259.            
  260.             store[i][j] = '-';
  261.             rStore[i][j] = '-';
  262.         }
  263.     }
  264. }
  265.  
  266. /* Procédure qui demande une combinaison à l'utilisateur
  267.    et l'enregistre */
  268. void storeInput (char store[ESSAIS+1][MCOLORS], int essais) {
  269.    
  270.     // Variables locales
  271.     int i;
  272.    
  273.     for (i=0; i<MCOLORS; i++) {
  274.        
  275.         printf("Donnez votre couleur n° %d : ", i+1);
  276.         scanf("%c", &store[essais][i]);
  277.         fpurge(stdin);
  278.     }
  279. }
  280.  
  281. /* Fonction qui vérifie si la combinaison de
  282.    l'utilisateur est identique à celle à trouver */
  283. int checkStore (char store[ESSAIS+1][MCOLORS], char rStore[ESSAIS+1][MCOLORS], int essais, int mode) {
  284.    
  285.     // Variables locales
  286.     int i, j, n=0, code;
  287.  
  288.     /* Nombre de pions noirs */
  289.     for (i=0; i<MCOLORS; i++) {
  290.        
  291.         if (store[0][i] == store[essais][i]) {
  292.            
  293.             rStore[essais][i] = 'N';
  294.             n++;
  295.        
  296.         } else {
  297.            /* Nombre de pions blancs */
  298.             for (j=0; j<MCOLORS; j++) {
  299.                
  300.                 if (store[0][j] == store[essais][i]) {
  301.                    
  302.                     rStore[essais][i] = 'B';
  303.                 }
  304.             }
  305.         }
  306.     }
  307.    
  308.     // Si on est en mode pro on mélange les pions blancs et noirs
  309.     if (mode == 2) {
  310.        
  311.         mixPro(rStore, essais);
  312.     }
  313.    
  314.     // Si on a 4 pions noirs on a trouvé la solution
  315.     // et on renvoie le code retour 1
  316.     if (n == MCOLORS) {
  317.        
  318.         code = 1;
  319.     }
  320.    
  321.     return code;
  322. }
  323.  
  324. /* Procédure qui mélange les pions blancs et noirs
  325.    en mode pro */
  326. void mixPro (char rStore[ESSAIS+1][MCOLORS], int essais) {
  327.    
  328.     // Variables locales
  329.     int i, indice, max=MCOLORS;
  330.     char temp;
  331.     int num[COLORSMAX] = { 0, 1, 2, 3, 4, 5, 6, 7 };
  332.    
  333.     for (i=0; i<MCOLORS-1; i++) {
  334.        
  335.         indice = rand()%max;
  336.         temp = rStore[essais][num[indice]];
  337.         rStore[essais][num[indice]] = rStore[essais][num[max-1]];
  338.         rStore[essais][num[max-1]] = temp;
  339.                
  340.         max--;
  341.     }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement