CS 132 – Project 3: Mastermind

Due: Monday, November 6th by the start of class

 

 

Introduction

 

In this game, your opponent chooses 4 pegs each with one of 6 colors.  Your job is then to guess the colors that your opponent has chosen in the proper order. After each guess by you, the computer will give two numbers as feedback.  The first number is how many pegs are the proper color and in the proper position. The second number is how many pegs are the proper color, but not in the correct position. Once you get the pattern correct, you and your opponent switch tasks, with you providing the solution, and them guessing.

Whoever guesses the solution in the fewest guesses wins.

 

Project Description

 

This project is a friendly competition.  Your job is not to write the entire game of “Mastermind,”  as I’ve done that for you. Your job is to come up with the intelligence behind the players.  You will write two functions: one that creates a solution and one that takes a guess. I will take all of your submissions and combine them with my driver program, and pit you in a head-to-head battle against the code written by your classmates. The winner will receive a prize.

 

Design-by-contract

 

Since you will be combining your code with code you did not write, we must agree upon some common interface so that the parts will work when we put them together. We will use C’s facilities for multi-file development, as well as the preprocessor directives to achieve this.

 

This agreement on how to interface the components together is referred to as Design-by-contract in Software Engineering.

 

The contract for this particular project is as follows:

 


#ifdef CHOOSER

void create_solution(int solution[]){

      return jrmst106_solution(solution);

}

#endif

#ifdef PLAYER

void create_guess(int right, int almost, int guess[]) {

      return jrmst106_turn(right, almost, guess);

}

#endif

void jrmst106_solution(int solution[])

{

      solution[0] = RED; // a silly solution

solution[1] = RED;

solution[2] = RED;

solution[3] = RED;

}

 

void jrmst106_turn(int right, int almost, int guess[])

{

      guess[0] = rand()%6; //random opponent

      guess[1] = rand()%6;

      guess[2] = rand()%6;

      guess[3] = rand()%6;

}

 

You must make a file named your_username.c The file shown above is jrmst106.c (all lowercase please.)  Inside of it you must implement two functions: create_solution and create_guess. When you submit your program, each of these functions should simply call a function that does the actual work, but is prefixed with your username. In this example, my program simply picks random guess and creates a fixed solution.  You will play against your classmates 3 times as the chooser and 3 times as the guesser.

 

The driver program (which we will dissect in class) will then play the two opponents against each other until a winner is determined.

 

Development

 

In order for you to test your function, I am providing you with all of the driver code. Under your private/cs132 directory, make a directory called project3 and cd into it. There execute the following:

 

cp  ~jrmst106/public/cs132/mastermind.tar  .

tar  xvf  mastermind.tar

 

This will create 3 files: Makefile, mastermind.c, and jrmst106.c

 

Type  make and then run the mastermind program to see it in action.

 

You can then make a copy of jrmst106.c with your username as the filename. Make sure you update the Makefile to compile the new file.

 

Hints

 

 

Requirements

 

By the deadline you must submit a tarfile containing: