Goals for this homework

  • Practice the C development cycle
  • Practice using control, iteration, and recursion

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:

  • hw2_main.c - contains test loop (choosing between testing mastermind functions and playing game)
  • mastermind.h - contains prototype for mastermind game
  • mastermind.c - contains starting implementation for mastermind game

Set Up

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.

Mastermind Description

You are going to implement the game of Mastermind. In mastermind, the goal is to match the pattern of pegs that the master has set. There are 6 different colors of guessing pegs (which Wikipedia calls code pegs). Each guess, the master provides limited feedback using feedback pegs (which Wikipedia calls key pegs). There are up to 12 rounds of guessing / feedback to try to guess the pattern.

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:

  • How many pegs match the right color?
  • How many pegs match the exact color and location?

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.

Mastermind Functions

For this exercise, we will write a series of helper functions to break down the problem.

unsigned int extract_digit(unsigned int pattern, unsigned int digit);
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 num_of_color(unsigned int pattern, unsigned int color);
How many instances of color are there in pattern?
unsigned int count_exact_matches(unsigned int guess, unsigned int solution);
Count how many digits in guess match both in color and position as compared to the solution.
unsigned int count_color_matches(unsigned int guess, unsigned int solution);
Count how many digits in guess match in color, regardless of position, as compared to the solution.

These are all used to create the final feedback. All error checking will occur in this function to make sure that both the guess and solution adhere to the 4-digit, values 1-6 format.
unsigned int get_guess_feedback(unsigned int guess, unsigned int solution);


For guess: 3413, solution: 2315,
  • extract_digit(3413, 2) = 4
  • num_of_color(3413, 1) = 1
  • num_of_color(3413, 2) = 0
  • num_of_color(3413, 3) = 2
  • count_exact_matches(3413, 2315) = 1 (the 1 in digit 1)
  • count_color_matches(3413, 2315) = 2 (1 each of 1 and 3)
  • get_guess_feedback(3413, 2315) = 11 (1 exact match and 1 additional color match)

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.

Mastermind play loop

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.

Submit

At this point, you should have done the following:
  • Created three files and filled in the proper information: hw2_main.c, mastermind.h, mastermind.c inside your hw2 directory.
  • $ svn add hw2_main.c mastermind.h mastermind.c
    
  • Implemented all of your functions. If not, you at least need skeletons of your functions so that our automated tests will compile for the functions you did write.
  • Compiled your executable manually and with the Makefile
  • Implemented your test cases
  • Executed your code
  • Debugged your code
  • $ svn commit -m "hw2 complete"