-
help with mastermind
I just finished my mastermind yesterday and it was perfectly working.
i tried it again earlier and I found out that the no. of black and white chips does not match the random generated numbers. someone pls help me improve my code.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
void initRandom()
{ time_t random_seed;
srand(time(NULL));
}
int getRandomNum(int nLow, int nHigh)
{ int nRandomValue;
nRandomValue = nLow + rand() % (nHigh - nLow + 1);
return nRandomValue;
}
int main()
{ char col1, col2, col3, col4, R, B, G, W, O, Y, A, C, D, cheat;
int chip1, chip2, chip3, chip4, ctr, play;
int white=0, black=0;
play=1;
printf("=================================**************=================================");
printf("Instructions:\n");
printf("Enter four letters that will correspond to the color it represents to guess the random set of colors. ");
printf("Remember to press the space bar after each input. ");
printf("You onlyhave a maximum of six tries to guess the colors. ");
printf("GOOD LUCK!\n");
printf("=================================**************=================================\n");
initRandom();
while(play==1)
{
do
{
chip1=getRandomNum(1,6);
chip2=getRandomNum(1,6);
chip3=getRandomNum(1,6);
chip4=getRandomNum(1,6);
}
while(chip1==chip2||chip1==chip3||chip1==chip4||chip2==chip3||chip3==chip4||chip2==chip4);
/*The function getRandomNum is called to generate random numbers that will be stored in chip1, chip2, chip3, and chip4.
A do while statement is used to make sure that the generated values will unique.*/
printf("\tColors:\tR = Red\t\tB = Blue\tG = Green\n\t\tW = Brown\tO = Orange\tY = Yellow\n");
getch();
/*Cheat mode*/
printf("\n\tEnter cheat mode? Y/N: ");
scanf("%c", &cheat);
getchar();
if(cheat=='Y')
{
printf("\t\t\tR = 1\tB = 2\tG = 3\n\t\t\tW = 4\tO = 5\tY = 6\n");
printf("\t\t\t\t%d %d %d %d\n", chip1, chip2, chip3, chip4);
}
printf("\t\t\tR = 1\tB = 2\tG = 3\n\t\t\tW = 4\tO = 5\tY = 6\n");
printf("\t\t\t\t%d %d %d %d\n", chip1, chip2, chip3, chip4);
for(ctr=1;ctr<=6;ctr++)
/*This for loop will be used to limit the number of times that the user will be able to guess to only six times.*/
{
/*Ask the user to enter the guesses.*/
printf("\tEnter 4 colors:\n\t");
scanf("%c\n",&col1);
scanf("%c\n",&col2);
scanf("%c\n",&col3);
scanf("%c",&col4);
getchar();
switch(col1)
{
case 'R': A=1;break;
case 'B': A=2;break;
case 'G': A=3;break;
case 'W': A=4;break;
case 'O': A=5;break;
case 'Y': A=6;break;
}
switch(col2)
{
case 'R': B=1;break;
case 'B': B=2;break;
case 'G': B=3;break;
case 'W': B=4;break;
case 'O': B=5;break;
case 'Y': B=6;break;
}
switch(col3)
{
case 'R': C=1;break;
case 'B': C=2;break;
case 'G': C=3;break;
case 'W': C=4;break;
case 'O': C=5;break;
case 'Y': C=6;break;
}
switch(col4)
{
case 'R': D=1;break;
case 'B': D=2;break;
case 'G': D=3;break;
case 'W': D=4;break;
case 'O': D=5;break;
case 'Y': D=6;break;
}
black=0;
white=0;
if(A==chip1)
black++;
if(B==chip2)
black++;
if(C==chip3)
black++;
if(D==chip4)
black++;
if(A==chip2||A==chip3||A==chip4)
white++;
if(B==chip1||B==chip3||B==chip4)
white++;
if(C==chip1||C==chip2||C==chip4)
white++;
if(D==chip1||D==chip2||D==chip3)
white++;
printf("\tBlack: %d", black);
printf("\tWhite: %d\n", white);
if(black==4)
{
ctr=6;
printf("\n\tYou got all correct!!!\n");
}
}
if(black<4)
printf("\tYou Lose!\n");
printf("\tThe correct set of colors are %d %d %d %d\n", chip1, chip2, chip3, chip4);
printf("\n\tDo you want to play again? 1=Yes/0=No: ");
scanf("%d", &play);
getchar();
system("cls");
if(play==0)
printf("\n\n\t\t\tThank you for playing!\n");
}
getch();
return(0);
}
EDIT: i just forgot to put the braces.^_^
can someone still help me improve my code thanks
-
I wrote one and you can study it if interested and compare it to yours. Although its in C#.
SourceForge.net Repository - [mastermindkc] Index of /MasterMind
-
thanks.
i'll check it out.
-
how can I make the switch part a function??
-
How about like this?
Code:
char colToNumber(char col) {
char result;
switch(col)
{
case 'R': result=1;break;
case 'B': result=2;break;
case 'G': result=3;break;
case 'W': result=4;break;
case 'O': result=5;break;
case 'Y': result=6;break;
}
return result;
}
// ...
A = colToNumber(col1);
-
@dwks
It works. thanks a lot
I have another question.
How can I enable the user to be able to exit the game whenever he wishes?thanks
-
You could have the user enter something in particular at this point:
Code:
for(ctr=1;ctr<=6;ctr++)
/*This for loop will be used to limit the number of times that the user will be able to guess to only six times.*/
{
/*Ask the user to enter the guesses.*/
printf("\tEnter 4 colors:\n\t");
scanf("%c\n",&col1);
scanf("%c\n",&col2);
scanf("%c\n",&col3);
scanf("%c",&col4);
getchar();
Then if the user enters the quit command at this point (maybe 'q' for any of the col's, for example), exit the for loop and then the outer loop. (With your current setup you could do this by saying:
)
If you wanted to, you could check the return values of your scanf() calls (and maybe combine all the scanf()'s into one call); if the return value does not equal the number of format specifiers (e.g. %c's), then the user entered invalid input. Then you could just instruct the user to enter EOF (achieved by typing CTRL-Z on Windows) to quit the program.