/***************************************************************************** This is a snippet of sample code for implementing Mastermind. Please compile it with: gcc -o mastermind mastermind.c To execute the program, for example: ./mastermind YYGP, then type 'Enter'. For gathering a guessed code from user input, you might need to use standard C I/O functions (e.g., scanf()). ******************************************************************************/ #include #include #include // the 4-peg code chosen by the codemaker char hidden_code[4]; // Your other global variables here. Use global variables sparingly. /**** Function declarations ****/ /* Given: A pointer to a character array Effects: Sets the character array 'hidden_code' to the characters from array_ptr Returns: 0 if successful, -1 otherwise */ int SetHiddenCode (char *array_ptr); /* Given: A pointer to a character array Effects: None Returns: 0 if the character array 'array_ptr' contains a valid Mastermind 4-peg code. Otherwise returns -1. */ int ValidateCode (char *array_ptr); /* Given: A pointer to a character array containing a valid 4-peg code Effects: Print the code to the screen */ inline void PrintCode(char *array_ptr); // Your other function declarations here int main(int argc, char **argv) { int i; char *array_ptr; // Set the codemaker's hidden code using the first argument to the executable if (argc != 2) { printf ("%s: Invalid number of arguments %d\n", argv[0], argc); exit (1); } else { if (SetHiddenCode(argv[1]) < 0) { printf ("%s: SetHiddenCode failed\n", argv[0]); exit (1); } } // Sanity check PrintCode(hidden_code); return 0; } /**** Function definitions ****/ int SetHiddenCode (char *array_ptr) { if (ValidateCode (array_ptr) < 0) { printf ("SetHiddenCode: Invalid array_ptr\n"); return -1; } int i; for (i = 0; i < 4; i++) hidden_code[i] = array_ptr[i]; return 0; } int ValidateCode (char *array_ptr) { if (strlen (array_ptr) != 4) { printf ("Invalid number of characters in code.\n"); return -1; } // Further validation is necessary // Your code here return 0; } inline void PrintCode(char *array_ptr) { printf("%s\n", array_ptr); } // Your other function definitions here