home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 344_01 / mind.c < prev    next >
Text File  |  1989-06-08  |  7KB  |  238 lines

  1.  
  2. /* HEADER:       (cat #);
  3.    TITLE:        Mastermind - number guessing game with skill levels;
  4.    DATE:         06/08/1989;
  5.    DESCRIPTION:  "Try to guess a random number of a specified number of digits.
  6.                  The computer tells you how many digits you have correct, each
  7.                  time, and how many of them are in the correct position.";
  8.    KEYWORDS:     mastermind, game.;
  9.    SYSTEM:       MS-DOS;
  10.    FILENAME:     MIND.C;
  11.    AUTHOR:       Eric Horner;
  12.    COMPILERS:    Turbo C 2.0;
  13. */
  14.  
  15.  
  16.  
  17. #include    <stdio.h>
  18. #include    <stdlib.h>
  19. #include    <conio.h>
  20. #include    <time.h>
  21.  
  22. #define        BSpace    '\x8'
  23.  
  24. static  char    rnd_num[8],
  25.     rnd_flag[] = {'N','N','N','N','N','N','N','N'},
  26.     in_flag[]  = {'N','N','N','N','N','N','N','N'},
  27.     in_num[8], char_in;
  28. int     loops, result_1, result_2, inner,
  29.     num_digs, attempts;
  30. char    reply, max_dig;
  31.  
  32. void menu()
  33. {
  34.     clrscr();
  35.     printf("                 ╔═══════════════════════════════════════╗\n");
  36.     printf("                 ║              PC MASTERMIND            ║\n");
  37.     printf("                 ╟───────────────────────────────────────╢\n");
  38.     printf("                 ║ Try to guess the number. The computer ║\n");
  39.     printf("                 ║ will give you hints if you guess well ║\n");
  40.     printf("                 ║ Press Q to QUIT any time you need to! ║\n");
  41.     printf("                 ║ Written by Eric Horner       Apr 1989 ║\n");
  42.     printf("                 ╚═══════════════════════════════════════╝\n\n");
  43.  
  44.     printf("                  1: JERK   (3 Figure number - 0 to 5)\n\n");
  45.  
  46.     printf("                  2: NOVICE (3 Figure number - 0 to 9)\n\n");
  47.  
  48.     printf("                  3: TRYER  (4 Figure number - 0 to 5)\n\n");
  49.  
  50.     printf("                  4: NORMAL (4 Figure number - 0 to 9)\n\n");
  51.  
  52.     printf("                  5: BRAINY (5 Figure number - 0 to 9)\n\n");
  53.  
  54.     printf("                  S: SADIST (Set someone a number)\n\n");
  55.  
  56.     printf("                  Q: QUITTER\n\n");
  57.  
  58.     printf("                  Enter your SKILL LEVEL (1 to 5, S or Q)\n");
  59.  
  60.     do          /* until valid character (1 to 7) */
  61.     {
  62.         reply = getch();
  63.         if (((reply <= '0') || (reply > '7'))
  64.             && (reply != 'Q') && (reply != 'q')
  65.             && (reply != 'S') && (reply != 's')) printf("\07");
  66.  
  67.     } while (((reply <= '0') || (reply > '7'))
  68.             && (reply != 'Q') && (reply != 'q')
  69.             && (reply != 'S') && (reply != 's'));
  70.  
  71.     switch (reply)
  72.     {
  73.     case '1': {
  74.             num_digs = 3;
  75.             max_dig  = '5';
  76.                     break;
  77.                   }
  78.     case '2': {
  79.             num_digs = 3;
  80.             max_dig  = '9';
  81.                     break;
  82.                   }
  83.     case '3': {
  84.             num_digs = 4;
  85.             max_dig  = '5';
  86.                     break;
  87.                   }
  88.     case '4': {
  89.             num_digs = 4;
  90.             max_dig  = '9';
  91.                     break;
  92.                   }
  93.     case '5': {
  94.             num_digs = 5;
  95.             max_dig  = '9';
  96.                     break;
  97.                   }
  98.         case 'Q':
  99.         case 'q': {
  100.                     exit(0);            /* Quit program */
  101.                     break;
  102.                   }
  103.         case 'S':
  104.     case 's': {
  105.             num_digs = 0;    /* This means user selected */
  106.             max_dig  = '9';
  107.                     break;
  108.                   }
  109.         default : {
  110.                     break;
  111.                   }
  112.     } /* End of switch */
  113.  
  114. } /* End of menu() */
  115.  
  116.  
  117. main()
  118. {
  119.     randomize();            /* set up rnd gen       */
  120.     do
  121.     {
  122.         clrscr();
  123.     menu();                             /* Get players skill level */
  124.     clrscr();
  125.  
  126.     if (num_digs == 0)
  127.     {
  128.         printf("%s\n\n",
  129.            "TELL YOUR OPPONENT NOT TO LOOK, THEN ENTER YOUR NUMBER");
  130.         /* get user selected number - up to 7 digits */
  131.         for (loops = 0; loops < 7 && num_digs == 0; ++loops)
  132.         {
  133.         do
  134.         {
  135.             char_in = getche();
  136.                     if ((char_in == 'Q') || (char_in == 'q')) exit(0);
  137.             if (((char_in < '0') || (char_in > '9')) &&
  138.              (char_in != BSpace) &&
  139.              (char_in != '\r'))
  140.             {
  141.              printf("\07");
  142.              putch(BSpace);
  143.             }
  144.  
  145.             if ((char_in == BSpace) && (loops > 0))
  146.             --loops;
  147.         } while (((char_in < '0') || (char_in > '9'))
  148.             && (char_in != '\r'));
  149.         if (char_in != '\r') rnd_num[loops] = char_in;
  150.         else num_digs = loops;
  151.         if (loops == 6) num_digs = loops + 1;
  152.         }
  153.     }
  154.     else
  155.     {
  156.         /* get random number (in ASCII) */
  157.         for (loops = 0; loops < num_digs; ++loops)
  158.         rnd_num[loops] = random(max_dig - 0x30 + 1) + 0x30;
  159.     }
  160.     clrscr();
  161.     printf("ENTER YOUR GUESS NOW (USE BACKSPACE TO CORRECT ERRORS)\n\n");
  162.     attempts = 0;
  163.  
  164.     do
  165.         {
  166.         ++attempts;            /* Keep score */
  167.         result_1 = 0;
  168.             result_2 = 0;
  169.  
  170.         for (loops = 0; loops < 8; ++loops)
  171.         {
  172.         in_flag[loops]  = 'N';
  173.         rnd_flag[loops] = 'N';
  174.         }
  175.  
  176.         /* Get next guess */
  177.         for (loops = 0; loops < num_digs; ++loops)
  178.             {
  179.                 do
  180.                 {
  181.                     char_in = getche();
  182.                     if ((char_in == 'Q') || (char_in == 'q')) exit(0);
  183.             if (((char_in < '0') || (char_in > max_dig)) &&
  184.              (char_in != BSpace))
  185.             {
  186.              printf("\07");
  187.              putch(BSpace);
  188.             }
  189.             if ((char_in == BSpace) && (loops > 0))
  190.             --loops;
  191.         } while ((char_in < '0') || (char_in > max_dig));
  192.                 in_num[loops] = char_in;
  193.             }
  194.  
  195.  
  196.  
  197.  
  198.         /* How many right and in the right place? */
  199.         for (loops = 0; loops < num_digs; ++loops)
  200.             {
  201.                 if (rnd_num[loops] == in_num[loops])
  202.                 {
  203.                     ++result_1;     /* right number, right place    */
  204.                     rnd_flag[loops] = 'Y';
  205.                     in_flag[loops]  = 'Y';
  206.                 }
  207.         }
  208.         /* How many right but in the wrong place? */
  209.         for (loops = 0; loops < num_digs; ++loops)
  210.             {
  211.         for (inner = 0; inner < num_digs; ++inner)
  212.                 {
  213.                     if ((rnd_num[loops] == in_num[inner])
  214.                         && (rnd_flag[loops] != 'Y')
  215.                         && (in_flag[inner]  != 'Y'))
  216.                     {
  217.                         rnd_flag[loops] = 'Y';
  218.                         in_flag[inner]  = 'Y';
  219.                         ++result_2;     /* right number, wrong place    */
  220.                     }
  221.                 }
  222.             }
  223.             printf("\t%s%d\t%s%d\n",
  224.                    "Right number, right place : ", result_1,
  225.                    "Right number, wrong place : ", result_2);
  226.     } while (result_1 < num_digs);
  227.  
  228.     if (attempts == 1)
  229.         printf("\07\nLUCKY GUESS!!!");
  230.     else printf("\07\n%s%d%s",
  231.             "WELL DONE! You got it all right in ",
  232.              attempts, " attempts!");
  233.     printf("\n\nPress <Q> to quit, any other key to play again!");
  234.         char_in = getche();
  235.  
  236.     } while ((char_in != 'Q') && (char_in != 'q'));
  237. }
  238.