You are expected to complete this assignment individually. If you need help, you are invited to come to office hours and/or ask questions on piazza. Clarification questions about the assignments may be asked publicly. Once you have specific bugs related to your code, make the posts private.
This homework has several exercises. We are also providing you with some resources on printf and error handling for this assignment.
I have provided you the following starting files with compiling skeletons in them:
Step 1: Makefile
Because you are adding a second set of files to your directory, you need
to add a second target to your Makefile. In your makefile, add another
two lines (with a space between these and the ones already there).
mastermind: hw2_main.c mastermind.c mastermind.h clang -Wall -o hw2 hw2_main.c mastermind.c
Step 2: Copy files
Copy and paste each file into a file on your computer.
The code I have provided compiles - it also must
compile as shown when you turn in the code.
There are severe penalties for turning in code that does not compile.
There are two feedback colors - black and white. You receive a black feedback peg for each guessing peg that is the right color and location. You receive a white feedback peg for each guessing peg that is the right color and incorrect location. You receive no feedback pegs for any other guessing pegs. The feedback pegs are given to you in random order - you do not know which feedback peg goes with which guessing peg within a single round - you need to look at past guessing / feedback rounds to deduce more information.
As a result, the two pieces of information that are necessary:
The trick is to ensure that you are not double-counting something. How do you know if something is the right color but the wrong location? We're going to answer these two questions very differently. We'll count how many pegs are of each color in the guess and the solution and compare the two - that tells us how many are the right color. Then we will separately look at each peg and see if it matches both color and location. We'll use those two separate pieces of information to determine how many match both color and location and how many match color and not location.
You will implement this assignment in two stages. First, you are going to implement the individual functions that will be put together to implement mastermind. You will use a combination of conditionals, loops, and function calls to do so. There is no need for recursion - all of the loops are small with a known length, so loops are significantly more efficient than recursion.
Next, you are going to complete the game play. Right now, there are several things missing - it only accepts one guess, it doesn't provide proper feedback, and it doesn't determine whether the guess was correct or not. You'll need to fix it so that it allows up to 12 guesses, depending on whether the user has guessed correctly, provides proper feedback, and gives updates as to the number of guesses as they go along. There are comments in the code to help you.
For this exercise, we will write a series of helper functions to break down the problem.
What value is in specified digit of pattern? Digit 0 is the right-most digit, digit 1 is to the left of it, etc. You may use the pow function in the math library. Don't forget to use -lm on the compile line and #include <math.h>unsigned int extract_digit(unsigned int pattern, unsigned int digit);
How many instances of color are there in pattern?unsigned int num_of_color(unsigned int pattern, unsigned int color);
Count how many digits in guess match both in color and position as compared to the solution.unsigned int count_exact_matches(unsigned int guess, unsigned int solution);
Count how many digits in guess match in color, regardless of position, as compared to the solution.unsigned int count_color_matches(unsigned int guess, unsigned int solution);
unsigned int get_guess_feedback(unsigned int guess, unsigned int solution);
Testing:
To test your code, you need a test function for each individual function. Think of how many tests are
necessary to test all of the functionality of each function, and put those tests into test_mastermind.
In this part, you will complete the play loop for mastermind. You are responsible for things like error checking the user input (and asking them again until they input a valid response), repeating until the game is over, etc. You can see the comments in the code for details on what you are expected to complete.
$ svn add hw2_main.c mastermind.h mastermind.c
$ svn commit -m "hw2 complete"