Thread: Creating a mastermind game

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    11

    Creating a mastermind game

    I have an assignment to create a mastermind game where the player has 10 attempts to find the secret code. After each input, the game indicates to the player the number of well placed pieces and the number of misplaced pieces.
    Pieces will be '0' '1' '2' '3' '4' '5' '6' '7' '8'.
    If the player finds the code, he wins, and the game stops. A misplaced piece is a piece that is present in the secret code but that is not in a good position.

    You must read the player's input from the standard input.
    You can use:
    • printf(3)
    • write(2)
    • read(2)
    • rand() (/ srand())
    • time()
    • atoi()

    You can NOT use:

    • Any functions/syscalls which does not appear in the previous list
    • Yes, it includes exit

    Your mastermind needs to handle the sequence Ctrl + d. It's End Of File. It's consider as a normal execution.

    and here is my code

    Copied my my IDE.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<stddef.h>
    #include<string.h>
    #include<unistd.h>
    #include<time.h>
    #include<stdbool.h>
    
    #define DEFAULT_ATTEMPS 10// constant variable
    
    _BoolisCorect(intans[])
    {
    int i = 0;
    while (i < 4)
        {
    if (ans[i] > 9 || ans[i] < 0)  // after changing char array to int array
            {
    printf("Wrong input!\n");
    return0;
            }
            i++;
        }
    return1;  // 1
    };
    
    /*Prototypes */
    voidintRandomCode(intcodeArray[]);
    voidgetGuess(intguessArray[]);
    intcompare(intcodeArray[], intguessArray[]);
    voidshowresults(intprintAnswer[]);
    voidoutputarray(intoutArray[]);
    
    /*main program input */
    
    intmain(intac, char **args)
    {
    intcodeArray[4], guessArray[4];
    int tries = DEFAULT_ATTEMPS;
    int win = 0;
    
    // printf("length: %d \n", ac);  // length of the passed arguments
    // printf("%s \n", args[0]);
    // printf("%s \n", args[1]);
    // printf("%s \n", args[2]);
    
    for (int i = 1; i < ac - 1; i++)
        {
    if (args[i][1] == 't')
                tries = atoi(
    args[i + 1]);  // like that we can get arguments which is passed
    // after -t argument that is the attampts value
    // and you need to get guess number like that if
    // there has -cpassed on arguments
        }
    
    printf("Welcome to MASTERMIND\n");
    printf("Will you find the secret code?\n");
    
    intRandomCode(codeArray);
    outputarray(codeArray);
    
    for (int i = 0; i <= tries;)  // i variable removed here because round only
    // increases if we have correct input
        {
    printf("---\nRound %d\n", i);
    printf(">\n");
    getGuess(guessArray);
    if (!isCorect(guessArray))
            {
    printf("Please enter a valid guess \n");
    continue;
            }
            i++;
    
            win = compare(codeArray, guessArray);
    if (win == 4)
            {
    printf("Congratz! You did it!\n");
    break;
            }
    // if (tries == i && win != 4)
    // {
    //     printf(
    //         "\nThat was TEN guesses, and you were unable to solve the "
    //         "puzzle.\n");
    // }
        }
    return0;
    }
    
    voidintRandomCode(intcodeArray[])
    {
    int i = 0;
    srand(time(NULL));
    for (i = 0; i < 4; i++)
        {
    codeArray[i] = rand() % 9;
        }
    }
    
    voidgetGuess(intguessArray[])
    {
    char *ans = (char *)malloc(5 * sizeof(char));
    read(0, ans, 5);
    ans[4] = '\0';
    for (int i = 0; i < 4; i++)
        {
    guessArray[i] = ans[i] - '0';
    // printf("%d", guessArray[i]);
        }
    }
    
    intcompare(intcodeArray[], intguessArray[])
    {
    intprintAnswer[4] = {0, 0, 0, 0};
    int i = 0, outer = 0;
    int right = 0, almost = 0;
    for (i = 0; i < 4; i++)
        {
    if (codeArray[i] == guessArray[i])
            {
    printAnswer[i] = 1;
                right++;
            }
        }
    for (outer = 0; outer < 4; outer++)
        {
    for (i = 0; i < 4; i++)
            {
    if (codeArray[outer] == guessArray[i] && printAnswer[outer] == 0)
                {
    printAnswer[i] = 1;
                    almost++;
                }
            }
        }
    if (right < 4)
    printf("Well placed pieces: %d , Misplaced pieces: %d\n", right,
                   almost);
    return (right);
    }
    
    
    voidoutputarray(intoutArray[])
    {
    int i = 0;
    for (i = 0; i < 4; i++)
        {
    printf("%d", outArray[i]);
        }
    printf("\n");
    }
    
    

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    395
    Are you able to tidy up the indentation and coloring of that code?

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    395
    Tidied up the formatting of the code, in case anybody wants to help:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    #include <string.h>
    #include <unistd.h>
    #include <time.h>
    #include <stdbool.h>
    
    
    #define DEFAULT_ATTEMPS 10// constant variable
    
    
    _Bool isCorect(int ans[])
    {
      int i = 0;
      while (i < 4)
      {
        if (ans[i] > 9 || ans[i] < 0)  // after changing char array to int array
        {
          printf("Wrong input!\n");
          return 0;
        }
        i++;
      }
      return 1;  // 1
    };
    
    
    /*Prototypes */
    void intRandomCode(int codeArray[]);
    void getGuess(int guessArray[]);
    int compare(int codeArray[], int guessArray[]);
    void showresults(int printAnswer[]);
    void outputarray(int outArray[]);
    
    
    /*main program input */
    
    
    int main(int ac, char **args)
    {
      int codeArray[4], guessArray[4];
      int tries = DEFAULT_ATTEMPS;
      int win = 0;
    
    
      // printf("length: %d \n", ac);  // length of the passed arguments
      // printf("%s \n", args[0]);
      // printf("%s \n", args[1]);
      // printf("%s \n", args[2]);
    
    
      for (int i = 1; i < ac - 1; i++)
      {
        if (args[i][1] == 't')
          tries = atoi(args[i + 1]);  // like that we can get arguments which is passed
          // after -t argument that is the attampts value
          // and you need to get guess number like that if
          // there has -cpassed on arguments
        }
    
    
        printf("Welcome to MASTERMIND\n");
        printf("Will you find the secret code?\n");
    
    
        intRandomCode(codeArray);
        outputarray(codeArray);
    
    
        for (int i = 0; i <= tries;) // i variable removed here because round only
                                     // increases if we have correct input
        {
          printf("---\nRound %d\n", i);
          printf(">\n");
          getGuess(guessArray);
          if (!isCorect(guessArray))
          {
            printf("Please enter a valid guess \n");
            continue;
          }
          i++;
    
    
          win = compare(codeArray, guessArray);
          if (win == 4)
          {
            printf("Congratz! You did it!\n");
            break;
          }
          // if (tries == i && win != 4)
          // {
          //     printf(
          //         "\nThat was TEN guesses, and you were unable to solve the "
          //         "puzzle.\n");
          // }
      }
      return 0;
    }
    
    
    void intRandomCode(int codeArray[])
    {
        int i = 0;
        srand(time(NULL));
        for (i = 0; i < 4; i++)
        {
           codeArray[i] = rand() % 9;
        }
    }
    
    
    void getGuess(int guessArray[])
    {
       char *ans = (char *)malloc(5 * sizeof(char));
       read(0, ans, 5);
       ans[4] = '\0';
       for (int i = 0; i < 4; i++)
       {
          guessArray[i] = ans[i] - '0';
    // printf("%d", guessArray[i]);
       }
    }
    
    
    int compare(int codeArray[], int guessArray[])
    {
       int printAnswer[4] = {0, 0, 0, 0};
       int i = 0, outer = 0;
       int right = 0, almost = 0;
       for (i = 0; i < 4; i++)
       {
          if (codeArray[i] == guessArray[i])
          {
             printAnswer[i] = 1;
             right++;
          }
       }
       for (outer = 0; outer < 4; outer++)
       {
          for (i = 0; i < 4; i++)
          {
             if (codeArray[outer] == guessArray[i] && printAnswer[outer] == 0)
             {
                printAnswer[i] = 1;
                almost++;
             }
          }
       }
       if (right < 4)
          printf("Well placed pieces: %d , Misplaced pieces: %d\n", right, almost);
       return (right);
    }
    
    
    
    
    void outputarray(int outArray[])
    {
       int i = 0;
       for (i = 0; i < 4; i++)
       {
          printf("%d", outArray[i]);
       }
       printf("\n");
    }

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    395
    First bit of feedback...

    Code:
    void getGuess(int guessArray[]);
    This function should return "bool" or "int", indicating if the data has been read from the user.

  5. #5
    Registered User
    Join Date
    Oct 2022
    Posts
    11
    It returns an int with the data gotten from the user, However the codes works perfectly, the only issue i'm having with this code is that i only want the randomly generated code to be displayed after getting the ten guesses wrong but it deplays in this

    Code:
    printf("Welcome to MASTERMIND\n");    
    printf("Will you find the secret code?\n");
     
     
        intRandomCode(codeArray);
        outputarray(codeArray);
    Creating a mastermind game-screen-shot-2022-11-09-07-28-12-png
    Last edited by dailysflash; 11-09-2022 at 12:29 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,433
    So just move your call to outputarray(codeArray); to somewhere else in the code.

    Like where you print the congrats message.

    Though better after the "You've had %d guesses, the code was...." kind of message.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2022
    Posts
    11
    Quote Originally Posted by Salem View Post
    So just move your call to outputarray(codeArray); to somewhere else in the code.

    Like where you print the congrats message.

    Though better after the "You've had %d guesses, the code was...." kind of message.
    Do you mind giving me a format

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,433
    Quote Originally Posted by dailysflash View Post
    Do you mind giving me a format
    Format for what?
    You've plenty of other printf calls in your code to show you what to do.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a mastermind game
    By dailysflash in forum C Programming
    Replies: 16
    Last Post: 11-07-2022, 10:05 PM
  2. c program for the mastermind game
    By College94 in forum C Programming
    Replies: 3
    Last Post: 04-14-2015, 12:09 PM
  3. Again, Mastermind Game Assistant Question
    By Jesterira in forum C Programming
    Replies: 8
    Last Post: 12-07-2012, 06:45 PM
  4. Problem with simple Mastermind game(beginner)
    By newbie2012 in forum Game Programming
    Replies: 1
    Last Post: 11-12-2012, 03:51 AM
  5. Finishing up Mastermind game program
    By Charak in forum C Programming
    Replies: 5
    Last Post: 02-17-2011, 02:49 AM

Tags for this Thread