dmilicev

mastermind_game_v1.c

Oct 23rd, 2020
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.10 KB | None | 0 0
  1. /*
  2.     mastermind_game_v1.c
  3.  
  4.     mastermind game
  5.     https://en.wikipedia.org/wiki/Mastermind_(board_game)
  6.  
  7.     In this console game MasterMind
  8.     Computer will be the codemaker and player will be the codebreaker.
  9.     The computer picks a sequence of 4 numbers from 1 to 5.
  10.  
  11.     The object of the game is to guess the exact positions of the colors
  12.     in the computer's sequence in as few guesses as possible.
  13.     After each guess, the computer gives you a score of exact and partial matches.
  14.  
  15.     Here colors will be replaced by numbers 1 to 5.
  16.     Colors (numbers) may be repeated in the code.
  17.  
  18.     In the score (results of guess)
  19.     Correct color & position    X
  20.     Correct color               O
  21.     None                        -
  22.  
  23.     Rules:
  24.     The sequence can contain colors (numbers) 1,2,3,4,5.
  25.     A color (number) can be used any number of times in the sequence.
  26.     All four colors of the secret sequence will contain a color,
  27.     no blanks/empties are allowed.
  28.     Each guess must consist of 4 colors, no blanks.
  29.     The player has 6 guesses to find the secret sequence.
  30.  
  31.     In settings we can: ( simple change default values in main() )
  32.     Choose the color code length (4 - 9), default 4.
  33.     Choose the number of used colors in the game (1 - 9), default 5.
  34.     Choose the maximum number of guesses the player has (4 - 9), default 6.
  35.  
  36.  
  37.     Another mastermind game that is played using a mouse,
  38.     exe file size 56 KB (language Serbian or Croatian or Bosnian)
  39.     download from:
  40.     https://mega.nz/file/64ZnXAKA#Sg8lML0S8RiwsY6n1GLYwLIx-2ScjRiFjfO1XibOwT8
  41.  
  42.  
  43.     You can find all my C programs at Dragan Milicev's pastebin:
  44.  
  45.     https://pastebin.com/u/dmilicev
  46.  
  47. */
  48.  
  49. #include <stdio.h>
  50. #include <string.h>         // for strcpy()
  51. #include <stdlib.h>         // for function rand()
  52. #include <time.h>           // for time_t
  53.  
  54. // the computer imagines a secret code that the user will guess
  55. void makeSecretCode( char secretCode[10], int color_code_length, int number_of_used_colors )
  56. {
  57.     int i;
  58.  
  59.     for(i=0; i<color_code_length; i++)                      // 48 is ASSCII code for '0'
  60.         secretCode[i] = rand() % number_of_used_colors + 1 + 48;
  61.  
  62.     secretCode[i] = '\0';   // finish the string
  63. }
  64.  
  65.  
  66. // returns 1 if all colors are in their position, otherwise returns 0
  67. int getScore( char guess[10], char secretCode[10],int color_code_length, int number_of_used_colors )
  68. {
  69.     int i, j, position=0, color=0;              // iterators and counters
  70.     char tempGuess[10], tempSecretCode[10];
  71.  
  72.     strcpy(tempGuess, guess);                   // temporary, because we will change it
  73.     strcpy(tempSecretCode, secretCode);
  74.  
  75.     for(i=0; i<color_code_length; i++)          // count score correct color & position (X)
  76.         if( tempGuess[i] == tempSecretCode[i] )
  77.         {
  78.             position++;                         // count correct positon
  79.             tempGuess[i] = tempSecretCode[i] = 'X'; // mark these positions with X
  80.         }
  81.  
  82.     for(i=0; i<color_code_length; i++)          // count score correct color (O)
  83.         for(j=0; j<color_code_length; j++)
  84.             if( tempGuess[i] == tempSecretCode[j] && tempGuess[i] != 'X' )
  85.             {
  86.                 color++;                        // count score color
  87.                 tempGuess[i] = tempSecretCode[j] = 'X'; // mark these positions with X
  88.             }
  89.  
  90.     printf("\t\t\t   ");                        // spacing
  91.  
  92.     for(i=0; i<position; i++)                   // print score:
  93.         printf("%c", 'X');                      // correct color & position
  94.  
  95.     for(i=0; i<color; i++)
  96.         printf("%c", 'O');                      // correct color
  97.  
  98.     for(i=0; i<color_code_length-(position+color); i++)
  99.         printf("%c", '-');                      // none
  100.  
  101.     printf("\n");
  102.  
  103.     if( position == color_code_length )
  104.         return 1;                       // return 1 if all colors are in their position
  105.  
  106.     return 0;                           // otherwise return 0
  107. } // getScore()
  108.  
  109.  
  110. int main(void)
  111. {                                       // counter and default values
  112.     int attempt=0, color_code_length=4, number_of_used_colors=5, maximum_number_of_guesses=6;
  113.     char secretCode[10], guess[10];     // strings to store variables
  114.     time_t t;   // for random number generator, don't forget to #include <time.h>
  115.     int i;                              // iterator
  116.  
  117.     srand((unsigned) time(&t)); // Intializes random number generator, should only be called once!
  118.  
  119.     // Title
  120.     printf("\n MasterMind game \t\t author Dragan Milicev \n\n Guess %d numbers of ", color_code_length );
  121.     for(i=0; i<number_of_used_colors; i++)
  122.         printf(" %d", i+1 );
  123.     printf("  in maximum %d attempts \n\n", maximum_number_of_guesses);
  124.  
  125.     // the computer imagines a secret code that the user will guess
  126.     makeSecretCode( secretCode, color_code_length, number_of_used_colors );
  127.  
  128. //  printf(" SecretCode %s \n\n", secretCode);  // print the secret code imagined by computer
  129.  
  130.     while( attempt < maximum_number_of_guesses )
  131.     {
  132.         printf(" Enter your %d. guess: ", attempt+1 );
  133.         scanf("%s", guess );                    // get guess from user
  134.  
  135.         attempt++;                              // count the attempts
  136.  
  137.         // getScore() returns 1 if all colors are in their position, otherwise returns 0
  138.         if ( getScore( guess, secretCode, color_code_length, number_of_used_colors ) )
  139.         {
  140.             printf("\n You are the WINNER after %d attempts ! \n", attempt );
  141.             break;                              // exit while() loop
  142.         }
  143.         else if( attempt == maximum_number_of_guesses ) // all attempts were used, print secret code
  144.             printf("\nSorry, nothing hit from maximum %d attempts \n%s   was secretCode \n", attempt, secretCode);
  145.     }
  146.  
  147.     return 0;
  148.  
  149. } // main()
  150.  
Add Comment
Please, Sign In to add comment