HOW to develop a program that plays the game of Mastermind by C programming?

The Game of Mastermind
Mastermind is a simple code-breaking board game for two players, i.e., a codemaker and a
codebreaker, invented in 1970 by Mordecai Meirowitz.
The codemaker secretly creates a code consisting of four colors selected from the set
{red, white, green, blue, black, yellow}, with duplicates allowed. The codebreaker tries to
guess the colors as well as the positions of the colors in the secret code. For each guess that
the codebreaker makes, the codemaker provides feedback in the following form:
1. The number of “black hits”, indicating the number of colors that are correct and in the
correct position.
2. The number of “white hits”, indicating the number of colors that are correct but in the
wrong position.
For instance, suppose we use the digits 0, 1, 2, 3, 4 and 5 to form the code. If the secret code
created by the codemaker is 0123 and the guess by the codebreaker is 0011, the codemaker
should provide a feedback of one black hit (B = 1) and one white hit (W = 1). Once feedback
is received, another guess is made by the codebreaker. The players continue to alternate turns
until the codebreaker guesses correctly.

Your Task
Develop a program that plays the game of Mastermind.
The user should be given the options to play the roles of codemaker and codebreaker. A game
is complete when the codebreaker succeeds in determining the secret code created by the
codemaker. Once a game has been completed, the user should be prompted for new games.
More games should be played until the user chooses to end the game.

Member Avatar for iamthwee

Post what you have done so far...

Only help No homeworks doing forum.

could U plz show me what have u done and how did u do this game for more experience??

waiting for ur reply

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

#define false 0
#define true 1


typedef struct{
  int colors[4];
  int black;
  int white;
} guess;

void valuation(int *col1, int *col2, int *wp, int *bp)
{
  int color[6][2];
  int c;
  int white;
  int black;

  for(c=0; c<6; c++)
    color[c][0]=color[c][1]=0;

  for(c=0; c<4; c++){
    color[col1[c]][0]++;
    color[col2[c]][1]++;
  }

  white=0;
  for(c=0; c<6; c++)
    white+=color[c][(color[c][0]>color[c][1] ? 1 : 0)];

  black=0;
  for(c=0; c<4; c++)
    black+=(col1[c]==col2[c] ? 1 : 0);
  white-=black;

  *wp=white;
  *bp=black;
}

int search(int *colors, int depth, guess *gp, int guesses)
{
  int guess;

  int white, black;
  int c;

  if(depth==4){
    for(guess=0; guess<guesses; guess++){
      valuation(colors, gp[guess].colors, &white, &black);
      if(!(white==gp[guess].white && black==gp[guess].black))
	break;
    }
    return (guess<guesses ? 0 : 1);
  }

  for(c=0; c<6; c++){
    colors[depth]=c;
    if(search(colors, depth+1, gp, guesses))
      return 1;
  }
  
  return 0;
}


int main(int argc, char **argv)
{
	srand( (unsigned)time( NULL ) );
	int break_or_make;
	int get_random;
	int max_tries;
	int get_answer;
	int correctplace=0;
	int run;
	int correctdigit=0;
	int secret_code[4];
	int answer[4];
	int check[4];
	int c =1;
	int again;


	printf("press 1 to break a code.\n");
	printf("press 2 to make a code.\n");
	printf("choice?");
	scanf("%d",&break_or_make);
	printf("---------------------\n");
	printf(" turn :  code  : B/W \n");
	printf("---------------------\n");

	

	if (break_or_make==2)
	{   /* start for the code makeer*/
		int toguess[4];
		int c;

		int done=0;
		
		guess *gp=NULL, *cur;
		int guesses=0;
		

		if(argc!=5)
		{
			fprintf(stderr, "usage: 2 num1 num2 num3 num4\n",
				argv[0]);
			exit(-1);
		}

  
		for(c=0; c<4; c++)
		{
			sscanf(argv[c+1], "%d", toguess+c);
			if(0>toguess[c] || toguess[c]>5){
				fprintf(stderr, "color no. %d (value %d) is out of range\n", 
					c+1, toguess[c]);
				exit(-1);
			}
		}
		while(!done){
			guesses++;
			gp=(guess *)realloc((void *)gp, guesses*sizeof(guess));
			cur=gp+(guesses-1);

			search(cur->colors, 0, gp, guesses-1);
			valuation(cur->colors, toguess, &(cur->white), &(cur->black));
			
			printf("#%d\t ", guesses);
			for(c=0; c<4; c++)
				printf(" %d", cur->colors[c]);
			printf("  %d %d\n", cur->white, cur->black);
			
			done=(cur->black==4 ? 1 : 0);
		}
	}
	if (break_or_make ==1)
	{
		
		
		for (get_random = 0; get_random <4; get_random++)
			secret_code[get_random] = rand()%10;
		for(max_tries=0;max_tries<=10;max_tries++)
		{
			printf("#%d\t",max_tries);
			for(get_answer=0;get_answer<=3;get_answer++)
			{
				scanf("%d",&answer[get_answer]);
			}

			check[get_answer] = false;

			for (get_answer = 0; get_answer < 4; get_answer++)
			{
   				if (answer[get_answer] == secret_code[get_answer])
   				{
      					correctplace++;
      					check[get_answer] = true;
   				}
			}
				
			for (get_answer = 0; get_answer < 4; get_answer++)
			{
			   for (run = 0; run < 4; run++)
			   {
			      if ((answer[get_answer] == secret_code[run]) && (check[run] == false))
			      {
			         correctdigit++;
			         check[run] = true;
			      }
			   }
			}
			if(correctplace ==4)
			{
				printf("\nCongratulations, you've won!\n");
				return 0;
			}
			for(;correctplace >=0;correctplace--)
				printf("\t\t*");
			for(;correctdigit >=0;correctdigit--)
				printf("0");
			printf("\n");
			if(max_tries==10)/*if maximum number of tries have passed, let them know*/
			{			/*they didn't made it and end the game*/
				printf("\nUnfortunately you did not make it.\n");
				
				return 0;
			}
		}
		
	}

	printf("Play again?\n");
	printf("1=yes,2=no: ");
	scanf("%d",&again);

	if (again ==1)
		return (-1);
	else 
		
		return (0);
}

use tags, please[code=c] tags, please

Thanks alot for ur reply,,,,,but when I tried to run this code a 38 errors have occured

use code tags, please

In which line should I write these code tags.

Thanks alot for ur reply,,,,,but when I tried to run this code a 38 errors have occured

so what it seems to me that you're saying, is that you don't understand anything about the code you posted because, i assume, you copied it from someone/somewhere else?

if i'm wrong, then accept my apologies, and continue by explaining

what errors are you getting
what have you already tried to fix said errors
what particular function are you having trouble with?

Thanks for ur reply,,,
really I understand the task but when I tried to check the performance for this code by using Microsoft visual c++, I descovered 38 errors, when I come to see and fix these errors, i found alot of them are undefined identifier like the word again in the last of the code, and when i checked the code it was already declared in the main function!!!!!

what does what mean?


.

see I read this code and got what the program is ok,,,,,then when I printed this code to the Microsoft visual c++ 6.0, I found 38 silly errors, like semicolon, and declaration variables where there are already declared, I tried to use another program to see the output of this code by using code blokcs (another program), I found that when I run it on that code builder I have the error in the "make code section.....

medoyem i just realized you are not the original poster. i made a mistake. sorry for the confusion.

oceanaut you cant just throw a few hundred lines of code out and hope Giapetto's elves come along and fix it for you.

what is your problem with the code? besides the 38 silly errors as mdoyem pointed out. perhaps you should start by fixing the obvious compile errors. then come back with specific questions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.