Advertisement
Guest User

Untitled

a guest
Mar 14th, 2011
1,904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <locale.h>
  4. #define ARR_SIZE 4
  5. #define MAX 10
  6.  
  7. typedef int Secret[ARR_SIZE];
  8. typedef struct
  9. {
  10.     int points;
  11.     char name[20];
  12. }Player;
  13.  
  14. void addToScoreboard(int numberOfGuesses);
  15.  
  16. int showMenu()
  17. {
  18.     int choice;
  19.  
  20.     printf("\n\n-*-*-*-* MasterMind *-*-*-*-\n\n");
  21.     printf("1. Play\n\n2. Show scoreboard\n\n3. Exit\n\n");
  22.  
  23.     printf("Make your choice choice(1-3):\n\n");
  24.  
  25.     scanf("%d", &choice);
  26.  
  27.     return choice;
  28. }
  29.  
  30. void fillArray(Secret secretNumber)
  31. {
  32.     int i;
  33.     int j;
  34.     int hasDuplicate;
  35.  
  36.  
  37.     for(i=0;i<ARR_SIZE;i++)
  38.     {
  39.         secretNumber[i] = rand() % MAX;
  40.  
  41.         printf("array[%d] = %d\n\n", i, secretNumber[i]);
  42.         hasDuplicate = 0;
  43.  
  44.         for(j=0; j<=(i-1); j++)
  45.             {
  46.                 if (secretNumber[i] == secretNumber[j])
  47.                 {
  48.                     hasDuplicate = 1;
  49.                     i--;
  50.                 }
  51.  
  52.             }
  53.     }
  54. }
  55.  
  56. void giveGuess(Secret Guess)
  57. {
  58.     int temp;
  59.  
  60.     printf("\n\nGuess 4 numbers: ");
  61.  
  62.  
  63.     scanf("%d", &temp);
  64.  
  65.  
  66.     Guess[0] = (temp%10000) / 1000;
  67.     Guess[1] = (temp%1000) / 100 ;
  68.     Guess[2] = (temp%100) / 10 ;
  69.     Guess[3] = (temp%10) / 1 ;
  70. }
  71.  
  72. void control(Secret secretNumber, Secret Guess, char help[])
  73. {
  74.     int i,j;
  75.     int numRight=0;
  76.     int flag=0;
  77.     int numberOfGuesses=0;
  78.  
  79.     printf("\n\n*-*-*-*-*-*-\n\nYour Guess was:\n\n");
  80.  
  81.     while(numRight < ARR_SIZE)
  82.     {
  83.         giveGuess(Guess);
  84.         numberOfGuesses++;
  85.  
  86.         for(i=0; i<ARR_SIZE; i++)
  87.         {
  88.             printf("%d ", Guess[i]);
  89.         }
  90.         printf("\n\n\n");
  91.  
  92.         numRight=0;
  93.  
  94.         for(i=0; i<ARR_SIZE; i++)
  95.         {
  96.             if( Guess[i] == secretNumber[i] )
  97.             {
  98.                 help[i] = 'R';
  99.                 numRight++;
  100.                 flag++;
  101.             }
  102.  
  103.             for (j=0; j < ARR_SIZE ; j++)
  104.             {
  105.  
  106.                 if (Guess[i] == secretNumber[j] && j != i && flag != 1)
  107.                 {
  108.                     help[i] = 'S';
  109.                     break;
  110.                 }
  111.             }
  112.  
  113.             flag = 0;
  114.  
  115.             if (help[i] != 'R' && help[i] != 'S')
  116.             {
  117.                 help[i] = '_';
  118.             }
  119.         }
  120.  
  121.         printf("help: ", help[i]);
  122.         for(i=0; i<ARR_SIZE; i++)
  123.         {
  124.             printf("%c ", help[i]);
  125.         }
  126.         printf("\n\nnumber of guesses done: %d", numberOfGuesses);
  127.  
  128.         for(i=0; i < ARR_SIZE; i++)
  129.         {
  130.             help[i] ='_';
  131.         }
  132.     }
  133.     addToScoreboard(numberOfGuesses);
  134.     numRight=0;
  135.     for(i=0; i < ARR_SIZE; i++)
  136.     {
  137.         help[i] ='_';
  138.     }
  139.  
  140. }
  141.  
  142. void addToScoreboard(int numberOfGuesses)
  143. {
  144.     FILE *scoreboardFile = fopen("scoreboard.txt", "a");
  145.     char name[20];
  146.  
  147.     printf("\n\ngive name: ");
  148.     scanf("%s", name);
  149.  
  150.     fprintf(scoreboardFile, "name: %s \npoints: %d\n\n\n", name, numberOfGuesses);
  151.     fclose(scoreboardFile);
  152.  
  153. }
  154.  
  155.  
  156. int main()
  157. {
  158.     int menuChoice=0;
  159.     Secret secretNumber;
  160.     Secret Guess;
  161.     char help[ARR_SIZE] = {'_', '_', '_', '_'};
  162.     Player player[10];
  163.  
  164.     srand(time(NULL));
  165.  
  166.     setlocale (LC_ALL, "swedish");
  167.  
  168.     do
  169.     {
  170.         menuChoice = showMenu();
  171.         switch (menuChoice)
  172.         {
  173.             case 1:
  174.                 fillArray(secretNumber);
  175.                 control(secretNumber, Guess, help);
  176.                 break;
  177.  
  178.             /*case 2:
  179.                 scoreboard("scoreboard.txt", player);
  180.                 break;*/
  181.         }
  182.  
  183.     }while (menuChoice != 3);
  184.  
  185.     return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement