CS1101C Lab 4 (Even Week)

Mastermind

The deadline for this lab question is Friday 07 April 2006, 23:59:59 hours.

The name of your C program file must be called mastermind.c, files with any other name will not be marked.

Background

The object of this game is to guess the sequence of four numbers that the computer has selected at random from the digits 1 to 8 inclusive. That means, the computer will select a four-digit number where each digit can be any digit from 1 to 8 inclusive. Repeated digits are allowed.

Your objective is to guess the number and the computer will respond by giving you one black for each digit that is guessed correctly and is in the correct place, and one white for each digit that is guessed correct but is in the wrong place. You will not be informed about which digits are correct. That part you will have to figure out for yourself.

You are to code this game in C, and perform the task of both the Player as well as the Programmer. You can assume that the user's guess will always be a valid four-digit number.

Suppose the answer is 3835. Then, the following guesses will yield the results as shown:

Two sample runs are given below (user's input is in bold):

Enter guess: 1234
Blacks: 1, whites: 0
Enter guess: 5678
Blacks: 1, whites: 1
Enter guess: 1655
Blacks: 1, whites: 0
Enter guess: 1788
Blacks: 0, whites: 1
Enter guess: 2275
Blacks: 3, whites: 0
Enter guess: 7275
Blacks: 4, whites: 0
Congratulations, you win!

Enter guess: 1234
Blacks: 0, whites: 2
Enter guess: 5678
Blacks: 0, whites: 2
Enter guess: 2121
Blacks: 2, whites: 0
Enter guess: 6565
Blacks: 0, whites: 0
Enter guess: 8787
Blacks: 2, whites: 0
Enter guess: 0
The answer is: 8721

The Algorithm

  1. Generate a random four-digit number following the specifications given above. Call this number the answer.
  2. Keep asking the user to enter his guess (a four-digit number) until one of the following happens:
    1. The user gives up and enters "0", upon which the program prints out the answer and quits.
    2. The user guesses the answer correctly, upon which the program prints out a congratulatory message and quits.
  3. The four-digit number that is entered by the user will be stored as a single int. You must extract the individual digits and store them in a one-dimensional array of int, and work with this one-dimensional array.

Random Numbers

We wish to generate random numbers without the user having to enter a seed value. Use the following skeleton code, which is also found in mastermind.c.

/**********************************************************************/
/* Matric Number: U051234A                                            */
/* Userid: u0501234                                                   */
/* Lab: 4                                                             */
/* Lab Group Number: 99                                               */
/* Lab TA's Name: Who needs a lab TA when you're Bill Gates?          */
/* Lab Session Date: 05 April 2006                                    */
/* Lab Session Time: 0800 - 0945                                      */
/* Title: I'm a moron if I forget to change this.                     */
/* Purpose: I'm a moron if I forget to change this.                   */
/**********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*  Declare function prototypes.  */
/*  ...  */

int main(void)
{
    /*  Declare and initialize variables.  */
    /*  ...  */

    /*  Sets the seed to a random value based on the system time.  */
    /*  This is done only once, at the beginning of the program.   */
    srand((unsigned int)time(NULL));

    /*  Your code goes here...  */

    return 0;
}

Take Note


This document, index.html, has been accessed 15 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 7 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 7 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.