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:
- 1234: One black since the third digit 3 is in
the correct place. Note that since this third digit 3 has already
generated a black, it will NOT generate a white even though the first
digit in the answer is also a 3. Each digit can generate at most
only one colour.
- 5678: Two whites since the first digit 5 and
the fourth digit 8 are correct but they are in the wrong place.
- 1375: One black and one white since the second digit
3 is in the wrong place but the fourth digit 5 is in the
correct place.
- 5383: Four whites since all four digits are correct
but they are in the wrong place.
- 3835: Four blacks since all four digits are correct
and they are in the correct place. The player wins when he / she gets
four blacks.
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
- Generate a random four-digit number following the specifications
given above. Call this number the answer.
- Keep asking the user to enter his guess (a four-digit number)
until one of the following happens:
- The user gives up and enters "0", upon which the program prints
out the answer and quits.
- The user guesses the answer correctly, upon which the program
prints out a congratulatory message and quits.
- 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
- For simplicity, we assume that the inputs from the user are always
valid.
- Do not use any two-dimensional arrays or File I/O in your program.
One-dimensional arrays are fine; in fact, you are required to use
one-dimensional arrays.
- Be very careful with array index out-of-bounds errors. The compiler
will not notify you of such problems; you may get the run-time errors
"Segmentation Fault", "Bus Error", or other strange logic errors.
Understand exactly what your code is doing before compiling!
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.