Simplified Mastermind Game

I'm trying to mimic the Mastermind Game. This code generates 3 letters A-G and the objective is for you to guess the correct letters. This game gives hints:
X the letter is not int the set.
* letter is in the correct place.
- letter is in the set but wrong place.

1) Main is supposed to be correct and all the functions are correct. Im a little iffy about wannaContinue function.
2) How can I make main compatible with my functions?

// simplified mastermind game
// Written by:
// Date:


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


void greeting(void);



int main(void)
{
int tries, done;

srand(time(NULL));
char x, y, z, a, b, c, cont;

greeting();
do
{
tries = 0;
generateLetter(&a, &b, &c);
do
{
readguess(&x, &y, &z);
done = hint (a, b, c, x, y, z);
}while(!done);
printf("Congrats you did it in %d tries", tries);
cont = wannaContinue(&cont);


}while (cont == 'c' || cont == 'C');



// 1. call greeting
// 2. repeat this block until user chooses quit
// 1. call a function to generate 3 uppercase letters (A-G)
// 2. call a function to read in the guess (3 letters) from the user
// 3. call a function to check the user's guess, print hints, and return good or bad guess
// 4. if the guess is good (user guesses everything correctly)
// 1. then print a congrats message and print how many tries it took***
// 2. call a function to ask the user to play again or quit, the function returns the answer
// 3. print a goodbye message

// *** somehwere in main, figure out how to count up the number of tries

return 0;
}




void generateLetter(char* a, char* b, char* c)
{
char x, y, z;

x = rand()%6;
x += 65;

y = rand()%6;
y += 65;

while (y == x)
{
y = rand()%6;
y += 65;

}

z = rand()%6;
z += 65;

while (z == x || z == y)
{
z = rand()%6;
z += 65;
}

printf("%c %c %c\n",x ,y , z);
*a = x;
*b = y;
*c = z;
return;
}



// this function generates 3 different, uppercase letters within the A-G range
// input:
// return:

// 1. call a library function to generate first letter
// 2. call a library function to generate second letter
// 3. check that 2nd letter is not the same as 1st letter, otherwise repeat step 2
// 4. call a library function to generate third letter
// 5. check that 3rd letter is not the same as 1st or 2nd letter, otherwise repeat step 4
// 6. send back all 3 letters


void readGuess(char* x, char* y, char* z)
{
printf("Enter 3 letters between (A-G): \n");
scanf(" %c %c %c", x, y, z);
return;

}
// this function prompts and reads in the 3 letters that the user guesses
// input:
// return:

// 1. ask the user for 3 letters
// 2. read in 3 letters
// 3. send back 3 letters


void hint(char a, char b, char c, char x, char y, char z)
{

int asterisk = 42;
int ex = 120;
int dash = 45;

if (x == a)
{
printf("%c ", asterisk);
}
else if (x == b || x == c)
{
printf("%c ", dash);
}
else if (x != a && x != b && x != c)
{
printf("%c ", ex);
}
else
{
printf("Not valid letters.\n");
}

if (y == b)
{
printf("%c ", asterisk);
}
else if (y == a || y == c)
{
printf("%c ", dash);
}
else if (y != b && y != a && y != c)
{
printf("%c ", ex);
}
else
{
printf("Not valid letters.\n");
}

if (z == c)
{
printf("%c ", asterisk);
}
else if (z == a || z == b)
{
printf("%c ", dash);
}
else if (z != c && z != b && z != a)
{
printf("%c ", ex);
}
else
{
printf("Not valid letters.\n");
}
printf("\n");
if (x == a && y == b && z == c)
{
printf("You Guessed the correct letters in the correct place!\n");
}
return;


}

// this function checks the user's guess against the sequence and prints the
// hints as symbols
// input:
// return:

// 1. check first letter guess against the 3 letters in the sequence
// 2. print hint for first guess
// 3. check second letter guess against the 3 letters in the sequence
// 4. print hint for second guess
// 5. check third letter guess against the 3 letters in the sequence
// 6. print hint for third guess
// 7. *** figure out how to determine that all 3 letters of guess match
// 8. return a value to say all match or not

void wannaContinue(char* cont)
{

char x;
printf("Do you wish to continue or quit? [c/q]\n");
scanf("%c", &x);
*cont = x;
while ( x == 'c' || x == 'C')
{
return;
}
while ( x == 'q' || x == 'Q')
{
return;
}




}

// this function asks the user if they want to continue or quit
// and keeps prompting until it gets a valid character (c, C, q, Q)
// input:
// return:

// 1. ask the user if they want to continue or quit
// 2. read in answer
// 3. if answer is valid, return the answer
// 4. otherwise repeat again from step 1



// greeting: prints welcome message and explains the rules of the game
// runs only at the beginning of the program
// input: none
// return: none
void greeting(void)
{
printf ("Welcome to Mastermind, the 15AG edition.\n\n");
printf ("I will randomly place 3 different letters in a sequence.\n");
printf ("Your goal is to guess the sequence in as few tries as possible.\n\n");
printf ("After every guess, I will give you hints.\n");
printf ("For each of the 3 letters you guessed, in its location:\n");
printf (" * means the correct letter is in the correct place\n");
printf (" - means the letter is in the sequence but is in the wrong place\n");
printf (" X means the letter is not in the sequence\n");
printf ("Let's play.\n\n");
}
Last edited on
Try putting the checks for the same number into while () loops:

1
2
3
4
5
6

while(y == x)
{
y = rand()%6;
Y+=65;
}


and the same thing for z.

Right now it only goes through once, and if the number is the same then it will just continue. Putting this block into a loop ensures that it's different.

You also do not need to include an else when using an if statement.
Last edited on
Thank you, works perfectly. I had a few more questions.

1) In step 2. how can I repeat the block of code until the user quits?
2) In my function "hint", how can i repeat it so that it runs infinite guesses until you get it right. My third guess results in the code ending.
3) How can I add up the number of tries it took and print it in main?

If anyone can help, much appreciated.
use a variable to check wether to quit or go on.
read the users input and set that variable inside a while or do loop.
the loop has to stop if the variable gets set to quit.

for an infinite loop use while(1) {...}

for counting tries use a variable and increment it in each step of your loop
this variable has to get defined outside the loop otherwise you just get a new one in each step
Last edited on
Topic archived. No new replies allowed.