CS 211 - Lab 6

Fall 2012

Due: Monday, October 8th, 2012 at 11:59 pm

Code Breaker

Create a c program that will play a game of code breaker. This game was marketed as the board game Mastermind by the Hasbro Company. For this lab, the program will randomly generate the code, the user will attempt to guess/break to code, and the program will give responses on how correct (or incorrect) each guess is.

Each code will consist of 4 numbers of a value from 1 to 6. The code is to be stored in an integer array.

Your program will need use multiple functions. The first function will be to generate the code the user will attempt to guess. The second function will be to get the code guess from the user. The third function is to compare the user's guess against the code.

Your program is to first generate a code, then it will get a guess from the user. If the guess is not correct, provide the user some feedback on the guess and then allow the user to make another guess. If the user makes a correct guess, congratulate the user, tell them how many guesses it took to get the correct code, then quit the program.

The function to generate the code is to take an integer array as a parameter. The function is to randomly fill in the array with 4 integer values in the range from 1 to 6.

The function to get the user's guess is to take a different integer array as a parameter. The function should prompt the user for 4 integer values from the user in the range from 1 to 6. If the user does not provide the proper, give an errror message and ask the user for a different input. Once the user has entered valid input, fill in the guess into the parameter array then return from the function. Since your program will read in multiple integer values from the user, perhaps the use of the scanf() is the best to use here. Consider the following code to read in 4 integer values from standard input that are separated by white space characters:

int g1, g2, g3, g4;

scanf ("%d %d %d %d", &g1, &g2, &g3, &g4);

The function to compare the guess is to take both arrays as parameters. For each value in the guess, if it contains same value as the code in that same position that value in the guess is a "match". For any value in the guess that is not a match, if that value is used in the code a a different position that value in the guess is a "near-match". This function is to print out the total number of matches and the total number of near-matches. The function is to return a true value if the user's guess is 100% correct, but false in all other cases.

The following examples show the number of matches and near-matches for each guess for the code of:

1 2 4 2
The guess of 1 3 5 4 gives 1 Match and 1 Near-match

The guess of 3 1 5 4 gives 0 Matches and 2 Near-matches

The guess of 1 2 3 1 gives 2 Matches and 1 Near-match

The guess of 1 3 2 6 gives 1 Match and 1 Near-match

The guess of 1 5 4 4 gives 2 Matches and 1 Near-match

The guess of 1 2 2 4 gives 2 Matches and 2 Near-matches

Command Line Argument: Debug Mode

Your program is to be able to take one optional command line argument, the -d flag. When this flag is given, your program is to run in "debug" mode. When in this mode, your program is to at least display the code when it is generated. You may have it display other debugging information you feel is important. When the flag is not given, this debugging information should not be displayed. One simple way to set up a "debugging" mode is to use a boolean variable which is set to true when debugging mode is turned on but false otherwise. Then usig a simple if statement controls whether information should be output.
if ( debugMode == TRUE )

printf (" Debugging Information \n");

Use of #defined Values

As is shown below in the Suggested Program Template, you should use #define statements to determine the number of values in the code and the range of the valid values. THis way, if you need to change the program from a code of "4 values in the range from 1 to 6" to "5 values from the range of 1 to 8", this can easily be done. In fact, using the #defined values will be one of the grading criteria items for this lab.

Program Submission

Your are to submit the programs for this lab via the Assignments Page in Blackboard.

To help the TA, name your file with your net-id and the assignment name, like:

  • ptroy1LabX.c

Submit this file via the Assignment Link for the Lab in Blackboard.

  1. In the CS 211 Web Pages in Blackboard, go to the Assignments Page
  2. Click on the link for the correct lab. This will open a web page with the title: "Upload Assignment: Lab X", where X is the number of the lab
  3. Scroll down and click on the button "Browse for Local File"
  4. Select the file that you created that contains the program. Then click OK.
  5. Repeat steps 3 and 4 for your second program.
  6. Click the submit button on the "Upload Assignment: Lab X" page.
  7. You should see the Submission History page that shows what you submitted. Verify you actually submitted the correct information.

Suggested Program Template Layout

#define CODELENGTH 4

#define NUMSYMBOLS 6

void genCode (int code[], ...);

void getGuess (int code[], ...);

int checkGuess (int code[], int guess[], ...);

int main (int argc, char **argv)



void genCode (int code[], ...)



void getGuess (int guess[], ...)



int checkGuess (int code[], int guess[], ...)



-- Main.troy - 2012-10-03

Topic revision: r3 - 2012-10-03 - 17:31:41 - Main.troy
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
Helping Women Faculty Advance
Funded by NSF