Any idea to write this code more smarter and shorter? I also expect your advice about general organisation of the code.
Object of the Game
In your version of Mastermind, the computer will be the codemaker and one player will be the codebreaker. The computer picks a sequence of 4 pegs, each one being one of any of size colors.
The object of the game is to guess the exact positions of the colors in the computer's sequence in as few guesses as possible. After each guess, the computer gives you a score of exact and partial matches.
Rules
- The sequence can contain pegs of colors: red, yellow, green, blue, white, black.
- A color can be used any number of times in the sequence.
- All four pegs of the secret sequence will contain a color - no blanks/empties are allowed.
- Each guess must consist of 4 peg colors - no blanks.
- The player has 12 guesses to find the secret sequence.
Scoring
For each of the pegs in your guess that is the correct color and in the correct position, the computer will give you one small black peg to the right of that move. If you score 4 small black pegs on a guess, you have guessed the secret sequence.
For each of the pegs in your guess that is a correct color in an incorrect position, the computer will give you one small white peg to the right of that move. Together, there will be no more than four small black and white pegs for each move.
If none of the pegs in your guess is of a correct color, you will see no small pegs to the right of that move.
Sample scoring:
Requirements:
- The players should be able to enter four colours as their guess. When they enter their guess, then your program should display their guess and next to the guess it should display the score. Make sure that you clearly visualise the guess and also the score next to it. You do not need to use graphics as long as you display the colours properly that will be enough.
Different visualisation approach can be used, for example in your program when you do the computation you can assign an integer number to each colour, but then you can visualise it as follows which is based on Figure 1:
Your guess: | Your score: --------------------------------------------------- Blue Yellow Green Red | Black Black White White --------------------------------------------------- Black Yellow White Red | Black White --------------------------------------------------- Yellow Blue White Yellow |
After the player completes playing the came once (after either they win or they had 12 guesses), your program should ask the user if they would like to continue if they do then your program should generate a new code.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
void makeCode(char secretCode[4][10])
{
int i, randColor;
for(i=0; i<4; i++)
{
randColor = 1 + rand() % 6; //creates a number
switch(randColor) //converts number created to a string
{
case 1: strcpy(secretCode[i], "red"); break;
case 2: strcpy(secretCode[i], "yellow"); break;
case 3: strcpy(secretCode[i], "green"); break;
case 4: strcpy(secretCode[i], "blue"); break;
case 5: strcpy(secretCode[i], "white"); break;
case 6: strcpy(secretCode[i], "black"); break;
}
}
}
void guess(char guessCode[4][10])
{
printf("\nEnter your guess:\n");
for(int i=0; i<4; i++)
scanf("%s", guessCode[i]);
}
void codeCheck(char secretCode[4][10], char guessCode[4][10], int *blackPeg, int *whitePeg)
{
int i, j, checkSecret[4] = {1,1,1,1}, checkGuess[4] = {1,1,1,1};
*blackPeg = *whitePeg = 0;
for(i=0; i<4; i++) //if secret and guess's position and color are same, blackpeg increases and mark "check"
if(strcmp(guessCode[i], secretCode[i]) == 0)
{
++*blackPeg;
checkSecret[i] = checkGuess[i] = 0;
}
for(i=0; i<4; i++)
for(j=0; j<4; j++)
if(strcmp(secretCode[i],guessCode[j]) == 0 && checkGuess[i] && checkSecret[j] && i != j)
{// determines crushes and eliminates extra whitePegs
++*whitePeg;
checkSecret[j] = checkGuess[i] = 0;
}
}
void displayGuess(char guessCode[4][10], int blackPeg, int whitePeg)
{
int i;
printf("\nYour Guess\t\t\t\tYour Score\n");
for(i=0; i<4; i++)
printf("%s ", guessCode[i]);
printf("\t\t");
for(i=0; i<blackPeg; i++)
printf("black ");
for(i=0; i<whitePeg; i++)
printf("white ");
printf("\n\n");
}
int main()
{
srand(time(NULL));
int i, option=1, blackPeg, whitePeg, wrongGuess;
char secretCode[4][10], guessCode[4][10];
while(1)
{
printf("MASTER MIND! \nPress 1 to start game \nPress any number to exit\n\n");
scanf("%d", &option);
if(option == 1)
{
makeCode(secretCode);
for(wrongGuess=1; wrongGuess<=12; wrongGuess++) //gives 12 rights to guess
{
guess(guessCode);
codeCheck(secretCode, guessCode, &blackPeg, &whitePeg);
displayGuess(guessCode, blackPeg, whitePeg);
if(blackPeg == 4) //if player guess correct all, than the game finishes
{
printf("You Win!\n\n\n\n"); break;
}
}
if(wrongGuess == 13) //if player cannot guess correct colors in 12 rounds, he losts
printf("\nYou Lost!\nSecret Code: %s %s %s %s\n\n\n\n\n", secretCode[0], secretCode[1], secretCode[2], secretCode[3]);
}
else
exit(1);
}
}