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");
}