# CS 107 - Summer 2015

### Programming Project 2 - Code Breaker

Due: Thursday, July 9, 2015 at 11:59pm

For this project, you will write a program in "C+" that will have the user guess a code that was randomly created by the program. This game is available in boardgame form under the name Mastermind.

For this program, we will uses the digits from 1 to 6 as our code items (instead of the traditional collored pegs). The code will consist of 4 digits. This will allow 1296 different code combinations.

At the start of the program, a randomly generated code is created by the program. The user is then to attempt to figure out this code by making various guesses as to what the code could be.

After the user makes each guess, the program will give feedback on the number of correct digits in their correct locations and the number of correct digits in a wrong location. From this feedback, the user is to narrow down the potential code choices. Note: there is a way that can find the solution in 5 guesses, but most users take about 10-12 guesses.

When the user has properly guessed the code, the program should report that the code has been guessed and tell the user how many guesses were used to determine the code. At this point, the program can stop running or FOR 5 POINTS EXTRA CREDIT, ask the user if he/she wants to play again. If the users responds with a no answer, quit the program. If the user responds with a yes answer, generate a new code and prompt the user for the first guess with this new code.

### Giving Guess Feedback

This part of the program is extremely important. Identifying when the user has a correct digit in the correct location is rather straight-forward. However, identifying when the user has a correct digit in the wrong location is much more complicated. On the surface it seems pretty easy.

Consider the code of: 4 2 1 5

The guess of: 6 3 5 2 will generate the feeback that zero digits are in the correct location and two digits are in the wrong position.

The guess of: 4 3 5 2 will generate the feeback that one digit is in the correct location and two digits are in the wrong position.

The guess of: 2 1 4 5 will generate the feedback that one digit is in the correct location and three digits are in the wrong position.

The guess of: 2 6 1 5 will generate the feedback that two digits are in the correct location and one digit is in the wrong position.

However, when the same digits is used multiple time, things get more complicated. Note the following statement from the Wikipedia page on Mastermind:

If there are duplicate colours in the guess, they cannot all be awarded a key peg unless they correspond to the same number of duplicate colours in the hidden code.
When matching digits in the guess to digits in the code, each digit can only be used once. Each digit in the guess is only used once and each digit in the code is only used once. Also, when a digit appears multiple times matching a digit to the correct location takes precedence over matching a digit to the wrong location.

Consider the code of: 2 4 3 4

The guess of: 1 5 4 6 will only generate that ONE digit in the guess is in the wrong location.

The guess of: 1 4 5 6 will only generate that ONE digit in the guess is in the correct location. Reporting a digit in the correct location is given precedence over reporting a digit in the wrong position.

The guess of: 3 3 5 6 will only generate that ONE digit in the guess is in the wrong location (not two). Once a digit in the code is matched to a digit in the guess, it cannot be matched to another digit in the guess.

The guess of: 3 5 3 6 will only generate that ONE digit in the guess is in the correct location. Again, once a digit in the code is matched to a digit in the guess, it cannot be matched to another digit in the guess.

You will need to keep track when you match a digit in the guess with a digit in the code.

### User Input

You will need to prompt the user for each guess. It is assumed that each guess will be given as 4 integer values given on a single line of input. You should verify that each integer given is in the range from 1 to 6. If the user enters a value outside of this range: disgard the entire guess, print an error message explaining the proper form of the input, and prompt the user to re-enter the entire guess.

At the start of your program, you should give the user some instructions on how the play the game. The exct verbiage is left up to you, but some points will be given for the appropriateness of this message.

At the end of the program, you should thank the user for playing and give some information on the "development team" (i.e. your name, that this program was developed for CS 107, etc).

### Random Numbers

While there really is no such thing as a real random number, the sequences of numbers generated are complicated enough that to just about everyone, they seem random. A nice discussion of random numbers is given on the cplusplus.com web page.

The primary library function for random numbers in C/C++ is rand() in the C library stdlib.h or in the C++ library cstdlib.

The rand() library function returns an integer value from 0 to the largest integer usable on the machine. To get this value into a desired range the modulus operator % is normally used. The modulus operator will get the random value to be in the range from 0 to some number. Since we want to have our random number to be in the range from 1 to 6, we will want to add 1 to the random number after the modulus operation is performed.

Note that rand() when used by itself will generate the same random numbers everytime a program runs. While this is useful for testing, if makes playing a game like codebreaker a bit boring. Thus we will also want to use the library function of srand() to make things become truly random. The most common way to use srand() is to use the library function of time(). All of the cplusplus.com web pages that discuss rand() show how to use srand() and time() to create truly random numbers.