Introduction

Mastermind is one of the famous games where a player has a number of attempts to guess a color code pattern.
Below, I describe an implementation of a Mastermind program developed in C programming language.

Rules

The rules for the game are, as follows:

  • a game is composed of 9 pieces / numbers: '0' '1' '2' '3' '4' '5' '6' '7' '8'
  • a secret code is composed of 4 distinct pieces
  • player has 10 attempts to find a secret code
  • players input is read from the standard input
  • if the player doesn't provide an input, the secret code is computer generated for 10 attempts
  • the game starts at Round 0, and prompts the player to enter a 4 digit code
  •               
                    Round 0
                    > 1234
                  
                
  • if the player entered a wrong input, e.g. entered less than 4 digits or entered letters instead of digits, then the player can enter the correct input again, in the same round.
  • if the player's input is correct, the program will diplay the number of well placed pieces and the number of misplaced pieces.
  • after each input, the game indicates to the player the number of well placed pieces and the number of misplaced pieces
  • a misplaced piece is a piece that is present in the code but it is not in a good position
  •               
                    Round 0
                    6507
                    Well placed pieces: 1
                    Misplaced pieces: 2
                  
                
  • if the player finds the code, he/she wins, and the game stops
  •               
                    Round 1
                    7608
                    Well placed pieces: 4
                    Misplaced pieces: 0
                    ------------------------------
                        Congratz! You did it!
                    ------------------------------
                  
                
  • If the game was lost, the following message will be displayed:
  •               
                    Round 1
                    5612
                    Well placed pieces: 0
                    Misplaced pieces: 2
                    ------------------------------
                      Oh no, you lost! Try again!
                    ------------------------------
                  
                

Program's Parameters

The program can receive the following parameters:

  • -c [CODE]: specifies the secret code. If no code is specified, a random code will be generated
  • -t [ATTEMPS]: specifies the number of attempts; by default the player has 10 attempts

Program's Structure

In order to implement the Mastermind game, it was broken down into the following sections:
# 1. Get the users input from the command line: This is implemented in main() function of the program by having a for loop to iterate over the command line arguments, checking for an input of either the code (-c) or the number of attemps (-t).

              
                int attempts = 10;

                for (int i = 1; i < ac; i++)
                {
                    if(strcmp(av[i], "-c") == 0)
                    {
                        strcpy(secret_code, av[i+1]);
                    }
                    else if (strcmp(av[i], "-t") == 0)
                    {
                        attempts = atoi(av[i+1]);
                    }
                }

                if (strlen(secret_code) == 0)
                {
                    secret_code = generate_secret_code(secret_code);
                }
              
            

# 2. Generate a secret code: If the secret code is not provided by the user, it is created in the generate_secret_code() function. Within this function, I declared an array of integers from 0 to 9 (not including). And, a rand() function is used to create a random number, then using that number to shuffle the numbers in the array. Then, the secret code is created from the first 4 digits of the array and returned to the main().

              
                char* generate_secret_code(char* code)
                {
                    srand(time(NULL));

                    int digit[DIGIT_ARRAY_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
                    int i, j, k, temp;

                    for (i = 0; i < DIGIT_ARRAY_SIZE; i++)
                    {
                        j = rand() % 9;
                        temp = digit[i];
                        digit[i] = digit[j];
                        digit[j] = temp;
                    }

                    code = (char*)malloc((CODE_SIZE+1)*sizeof(char));

                    for (k = 0; k < CODE_SIZE; k++)
                    {
                        code[k] = digit[k] + '0'; 
                    }
                    code[CODE_SIZE] = '\0'; 
                  
                    return code;
                }
              
            

# 3. Get a guess code from the user: This is perfomed through the function called start_message() which is called in main().

              
                void start_message()
                {
                    printf("=============================\n");
                    printf("        MASTERMIND\n");
                    printf("=============================\n");
                    printf("Will you find a secret code?\nPlease enter a valid guess.\n");
                }
              
            

# 4. Check the guess code: This is impletemented in the function called read_guess_code() which takes a guess code as a paramenter and checks that the input is valid (e.g., the digits entered are 0 to 9, not including; only 4 digits entered). The function returns a guess code back to main(), if it is valid input.

            
              char* read_guess_code(char* guess_code)
              {
                  guess_code = (char*)malloc((CODE_SIZE+1)*sizeof(char));

                  while(1)
                  {
                      int valid_input = 0; 

                      read(STDIN_FILENO, guess_code, BUFFER_SIZE);
                      guess_code[CODE_SIZE] = '\0'; 

                      for (int i = 0; i < CODE_SIZE; i++)
                      {
                          if (guess_code[i] < '0' || guess_code[i] > '8')
                          {
                              valid_input = 1; // Input is invalid
                              break; 
                          }
                      }
                      
                      if (strlen(guess_code) != CODE_SIZE || valid_input == 1)
                      {
                          printf("Wrong Input!\n");
                      }
                      else
                      {
                          break;
                      }
                  }
                  return guess_code;
              }
            
          

# 5. Compare the guess code with the secret code: This operation is performed in the guess_compare() function. It compares the codes, counts and prints the well-placed and misplaced pieces. It returns the number of well-placed pieces to main().

            
              int guess_compare(char* secret_code, char* guess_code)
              {
                  int well_placed_piece = 0;
                  int mis_placed_piece = 0;

                  for (int i = 0; i < CODE_SIZE; i++)
                  {
                      if (guess_code[i] == secret_code[i])
                      {
                          well_placed_piece = well_placed_piece + 1;
                      }
                      else   
                      {   
                          for (int j = 0; j < CODE_SIZE; j++)
                              {   
                                  if (guess_code[j] == secret_code[i])
                                  {
                                      mis_placed_piece = mis_placed_piece + 1;
                                  }
                              }
                      }
                  }

                  printf("Well placed pieces: %d\n", well_placed_piece);
                  printf("Misplaced pieces: %d\n", mis_placed_piece);

                  return well_placed_piece;
              }
            
          

# 6. Check the well-placed pieces: The number of well-placed pieces is returned from the guess_compare() function to main(). If 4 pieces of the guess code match with the secret code, then the player wins the game.

            
              int well_placed_pieces;
              int i = 0;
              while (i < attempts)
              {
                  printf("---\n");
                  printf("Round %d\n", i);
                  guess_code = read_guess_code(guess_code);
                  well_placed_pieces = guess_compare(secret_code, guess_code);
                  if (well_placed_pieces == 4)
                  {
                      printf("------------------------------\n");
                      printf("    Congratz! You did it!\n");
                      printf("------------------------------\n");
                      break;
                  }
                  i++;
              }

              if (i == attempts && well_placed_pieces != 4)
              {
                  printf("------------------------------\n");
                  printf("  Oh no, you lost! Try again!\n");
                  printf("------------------------------\n");
              }
            
          

Programming Concepts

In order to complete this project, I used such programming concepts as loops, arrays, string comparison, string copy, string length, reading from standard input, command line arguments, dynamic memory allocation and generating random numbers.

The Program

A full implementation of the Mastermind program and detailed documentation of the programming logic is available here.


Made at Qwasar SV -- Software Engineering School Qwasar SV -- Software Engineering School's Logo