COMP104: Programming

Lab 3: Mastermind


In Lab3, you will implement the game MasterMind.  Your task is to crack a code which has been randomly created by the computer. To crack the code, you make guesses and the computer responds with clues.

Making Guesses

To make a guess, simply fill in a row with 4 digits between 1 - 6 separated by a space

Using the Clues

A "O" symbol means "Right value, right position." That is, one of your digits has the right value in the right position.

A "#" symbol means "Right value, wrong position." That is, one of your digits has the right value, but is in the wrong position.

If the computer responds with no pegs, that means everything is wrong.

Note: The code may have repetition.

Example output should look like this:

        MasterMind
        Enter four digits (1 - 6) separated by a space
        -----------------------------------------------
        Round 1
        Enter Guess: 1 2 3 4
                     O #
        -----------------------------------------------
        Round 2
        Enter Guess: 6 5 3 1
                     O O
        ------------------------------------------------
        .........
        .........
        .........
        ------------------------------------------------
        Round 10
        Enter Guess: 6 3 3 2
                     O O O O
        ------------------------------------------------
        Congratulations! You win in 10 steps!

Programming

You will need to use a random number generator to generate the computer's digits. The following program skeleton initializes the random number generator, and uses it to pick the computer's digits.

//-----------------------------------------------------------------------------------------
#include <iostream>             // contains cout and cin
#include <cstdlib>              // contains the random number generator rand()
#include <ctime>                // contains time() to seed the random number
                                // generator to gives a different answer each time
using namespace std;

int main(){
        int c1, c2, c3, c4;            // computer's digits
        srand(time(0));                // initialize random number generator

        c1 = rand()%6 + 1;             // picks random value between 1 and 6
        c2 = rand()%6 + 1;             // picks random value between 1 and 6
        c3 = rand()%6 + 1;             // picks random value between 1 and 6
        c4 = rand()%6 + 1;             // picks random value between 1 and 6

        // rest of program
        return 0;
}
//-----------------------------------------------------------------------------------------



Demo your working programs for your TA. If for some reason you cannot finish before the end of the lab period, email your program to your TA by Sunday 2 October 5PM (please include your name and lab section as a comment on line 1 of your program).