Thread: Help with mastermind code

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    6

    Help with mastermind code

    Using microsoft visual studio 2010

    So im suppose to create a mastermind program that has 4 master color pegs and they are to be randomly generated.
    '#' is used to represent the covered-up master pegs. The colour will only be revealed to the user when he guess all of them correctly - User enters R, B, G, Y to represent RED, BLUE, GREEN, YELLOW. User can also press 'Q' to quit.
    '@' represents correct colour and position
    'O' represents correct colour but wrong position
    'X' represents incorrect colour and position
    If user enters any character besides G, R, Y, B, An 'X' will be displayed
    As long as user did not guess all the colours correctly, he will be prompted to enter them again. - He can exit the current game when : he tries a maximum of 10 times. A failed message will be displayed. he press 'Q' to quit the current game. - After which he will be prompted if he wants to start a new game. - If he press 'N', he will exit out of this application.

    My codes for now are:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int getRandomNum(int nLow, int nHigh)
    {
        int nRandomValue;
        nRandomValue = nLow + rand() % (nHigh - nLow + 1);
        return nRandomValue;
    }
    void main()
    {
        char cont, yes, no, color1, color2, color3, color4, answerA, answerB, answerC, answerD, R, B, Y, G, Az, Bz, Cz, Dz;
        int fixed1, fixed2, fixed3, fixed4, ctr;
        int play = 1, correct = 0;
        printf("*************************************************************************************************************************");
        printf("\t\t\t\t\t\tI AM THE MASTER OF YOUR MIND");
        printf("\t\t\t\t\t\t************************************************************************************************************************");
        printf("*\t\t\t\t\t\t#\t#\t#\t#\t#\t\t\t\t\t       *");
        printf("\n************************************************************************************************************************");
    
    
        while(play == 1)
        {
            do
            {
                fixed1 = getRandomNum(1,4);
                fixed2 = getRandomNum(1,4);
                fixed3 = getRandomNum(1,4);
                fixed4 = getRandomNum(1,4);
            }
            //This will generate a random number that will be stored in fixed1,2,3,4
            while(fixed1==fixed2||fixed1==fixed3||fixed1==fixed4||fixed2==fixed3||fixed3==fixed4||fixed2==fixed4);
    
    
            printf("Select the colour you want to enter or 'Q' to Quit:\n'R' for RED, 'B' for BLUE, 'G' for GREEN, 'Y' for YELLOW\n");
            getchar();
            //This for loop is to limit the number of tries to max 10
            for(ctr = 1; ctr <=10; ctr ++)
            {
                printf("Enter 4 colours\n");
    
    
                scanf("%c,\t%c,\t%c,\t%c,",&color1, &color2, &color3, &color4);
                getchar();
                Az = 0, Bz = 0, Cz = 0, Dz = 0;
                switch(color1)
                {
                case 'R' : Az = 1; break;
                case 'B' : Az = 2; break;
                case 'G' : Az = 3; break;
                case 'Y' : Az = 4; break;
                }
    
    
                switch(color2)
                {
                case 'R' : Bz = 1; break;
                case 'B' : Bz = 2; break;
                case 'G' : Bz = 3; break;
                case 'Y' : Bz = 4; break;
                }
    
    
                switch(color3)
                {
                case 'R' : Cz = 1; break;
                case 'B' : Cz = 2; break;
                case 'G' : Cz = 3; break;
                case 'Y' : Cz = 4; break;
                }
    
    
                switch(color4)
                {
                case 'R' : Dz = 1; break;
                case 'B' : Dz = 2; break;
                case 'G' : Dz = 3; break;
                case 'Y' : Dz = 4; break;
                }
    
    
                correct = 0;
                if(Az == fixed1)
                    answerA = '@';
                    correct++;
                if(Bz == fixed2)
                    answerB = '@';
                    correct++;
                if(Cz == fixed3)
                    answerC = '@';
                    correct++;
                if(Dz == fixed4)
                    answerD = '@';
                    correct++;
    
    
                if(Az==fixed2||Az==fixed3||Az==fixed4)
                    answerA = 'O';
                if(Bz==fixed1||Bz==fixed3||Bz==fixed4)
                    answerB = 'O';
                if(Cz==fixed1||Cz==fixed2||Cz==fixed4)
                    answerC = 'O';
                if(Dz==fixed1||Dz==fixed2||Dz==fixed3)
                    answerD = 'O';
    
    
                if(Az!=fixed2&&Az!=fixed3&&Az!=fixed4)
                    answerA = 'X';
                if(Bz!=fixed1&&Bz!=fixed3&&Bz!=fixed4)
                    answerB = 'X';
                if(Cz!=fixed1&&Cz!=fixed2&&Cz!=fixed4)
                    answerC = 'X';
                if(Dz!=fixed1&&Dz!=fixed2&&Dz!=fixed3)
                    answerD = 'X';
    
    
                printf("%c %c %c %c", answerA, answerB, answerC, answerC);
    
    
                if(correct == 4)
                {
                    ctr = 10;
    
    
                    printf("Congratulation, you have won !\n");
                    printf("Do you want to play a new game? <Y/N>");
                    scanf("%c", &cont);
    
    
                    if(cont == 'Y')
                        play == 1;
                    if(cont == 'N')
                        printf("\nThank you for playing! hehexd");
                }
    
    
                if(correct < 4)
                    printf("\nYou Lose!\n");
    
    
                    printf("Do you want to play a new game? <Y/N>");
                    scanf("%c", &cont);
    
    
                    if(cont = 'Y')
                        play == 1;
                    if(cont = 'N')
                        printf("\nThank you for playing!");
            }
            getchar();
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So do you have a particular question?

    void main is wrong.
    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

    > color1, color2, color3, color4, answerA, answerB, answerC, answerD
    When you start putting numeric suffixes on variables, and then copy/pasting the code, you should really be thinking about arrays and loops.
    Eg.
    Code:
    char color[4], answer[4];
    ...
    for ( i = 0 ; i < 3 ; i++ ) {
      if ( A[i] == fixed[i] ) {
        answer[i] = '@';
        correct++;
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    6
    I'm not really sure where is the error for my codes and what do you mean by void main is wrong? That's the only method my school taught me till now, so i'm pretty clueless about that.
    Must i change my color and answers to for loops? Didn't learnt much about for loops and not confident in using them.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I'm not really sure where is the error for my codes
    Well presumably, you have some test cases.
    Like say if the hidden pegs are RGBY, then your program works properly.
    But if you have duplicate pegs, say RGGY, then your program breaks.

    One thing I noticed in your code, you don't seem to take account that a "right colour, wrong place" is used only once.

    > and what do you mean by void main is wrong?
    Did you bother to read the link?

    > That's the only method my school taught me till now
    Well they're wrong - get used to it.
    Employ a little more scepticism in what they teach you, by verifying it through alternative channels.

    > Must i change my color and answers to for loops?
    No, you can leave the code as it is.
    Arrays and loops would make for easier code IMO, but if you're not comfortable with them, then carry on as you are.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2016
    Posts
    6
    >One thing I noticed in your code, you don't seem to take account that a "right colour, wrong place" is used only once.
    Not sure what u meant by this, could u elaborate a little bit more? Thanks

  6. #6
    Registered User
    Join Date
    Jul 2016
    Posts
    6
    Btw, my program doesn't work properly. It keeps showing me an '@' even if the answer is suppose to be wrong and likewise showing me 'O' when the answer is right.

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Code:
    if(Az == fixed1)
                    answerA = '@';
                    correct++;
                if(Bz == fixed2)
                    answerB = '@';
                    correct++;
                if(Cz == fixed3)
                    answerC = '@';
                    correct++;
                if(Dz == fixed4)
                    answerD = '@';
                    correct++;
    Any scope of an 'if' statement the exceeds a single line of code should
    have braces surrounding them. That is part of your problem overall.

    Also on the style front it might be easier to read and maintain to split the code
    up into more than just two functions. That is a lot of code in main.
    Double Helix STL

  8. #8
    Registered User
    Join Date
    Jul 2016
    Posts
    6
    I've made amendments to my 'if' statements, the only problem now is that is doesn't properly show my answers like 'O', '@' , 'X'.
    My latest codes
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int getRandomNum(int nLow, int nHigh)
    {
    	int nRandomValue;
    	nRandomValue = nLow + rand() % (nHigh - nLow + 1);
    	return nRandomValue;
    }
    int main(void)
    {
    	char cont, yes, no, color1, color2, color3, color4, answerA, answerB, answerC, answerD, R, B, Y, G, Az, Bz, Cz, Dz;
    	int fixed1, fixed2, fixed3, fixed4, ctr;
    	int play = 1, correct = 0;
    	Az = 0, Bz = 0, Cz = 0, Dz = 0;
    	printf("*************************************************************************************************************************");
    	printf("\t\t\t\t\t\tI AM THE MASTER OF YOUR MIND");
    	printf("\t\t\t\t\t\t************************************************************************************************************************");
        printf("*\t\t\t\t\t\t#\t#\t#\t#\t#\t\t\t\t\t       *");
    	printf("\n************************************************************************************************************************");
    
    
    	while(play == 1)
    	{
    		do
    		{
    			fixed1 = getRandomNum(1,4);
    			fixed2 = getRandomNum(1,4);
    			fixed3 = getRandomNum(1,4);
    			fixed4 = getRandomNum(1,4);
    		}
    		//This will generate a random number that will be stored in fixed1,2,3,4
    		while(fixed1==fixed2||fixed1==fixed3||fixed1==fixed4||fixed2==fixed3||fixed3==fixed4||fixed2==fixed4);
    
    
    		//This for loop is to limit the number of tries to max 10
    		for(ctr = 1; ctr <=10; ctr ++)
    		{
    			printf("Select the colour you want to enter or 'Q' to Quit:\n'R' for RED, 'B' for BLUE, 'G' for GREEN, 'Y' for YELLOW\n");
    			scanf("%c %c %c %c",&color1, &color2, &color3, &color4);
    			getchar();
    
    
    			switch(color1)
    			{
    			case 'R' : Az = 1; break;
    			case 'B' : Az = 2; break;
    			case 'G' : Az = 3; break;
    			case 'Y' : Az = 4; break;
    			}
    		
    			switch(color2)
    			{
    			case 'R' : Bz = 1; break;
    			case 'B' : Bz = 2; break;
    			case 'G' : Bz = 3; break;
    			case 'Y' : Bz = 4; break;
    			}
    
    
    			switch(color3)
    			{
    			case 'R' : Cz = 1; break;
    			case 'B' : Cz = 2; break;
    			case 'G' : Cz = 3; break;
    			case 'Y' : Cz = 4; break;
    			}
    
    
    			switch(color4)
    			{
    			case 'R' : Dz = 1; break;
    			case 'B' : Dz = 2; break;
    			case 'G' : Dz = 3; break;
    			case 'Y' : Dz = 4; break;
    			}
    
    
    			correct = 0;
    			if(Az == fixed1)
    			{
    				answerA = '@';
    				correct++;
    			}
    			if(Bz == fixed2)
    			{
    				answerB = '@';
    				correct++;
    			}
    			if(Cz == fixed3)
    			{
    				answerC = '@';
    				correct++;
    			}
    			if(Dz == fixed4)
    			{
    				answerD = '@';
    				correct++;
    			}
    
    
    			if(Az==fixed2||Az==fixed3||Az==fixed4)
    			{
    				answerA = 'O';
    			}
    			if(Bz==fixed1||Bz==fixed3||Bz==fixed4)
    			{
    				answerB = 'O';
    			}
    			if(Cz==fixed1||Cz==fixed2||Cz==fixed4)
    			{
    				answerC = 'O';
    			}
    			if(Dz==fixed1||Dz==fixed2||Dz==fixed3)
    			{
    				answerD = 'O';
    			}
    			if(Az!=fixed2&&Az!=fixed3&&Az!=fixed4)
    			{
    				answerA = 'X';
    			}
    			if(Bz!=fixed1&&Bz!=fixed3&&Bz!=fixed4)
    			{
    				answerB = 'X';
    			}
    			if(Cz!=fixed1&&Cz!=fixed2&&Cz!=fixed4)
    			{
    				answerC = 'X';
    			}
    			if(Dz!=fixed1&&Dz!=fixed2&&Dz!=fixed3)
    			{
    				answerD = 'X';
    			}
    			printf("%c %c %c %c", answerA, answerB, answerC, answerC);
    
    
    			if(correct == 4)
    			{
    				ctr = 10;
    
    
    				printf("Congratulation, you have won !\n");
    				printf("Do you want to play a new game? <Y/N>\n");
    				scanf("%c", &cont);
    				getchar();
    				
    
    
    				if(cont == 'Y')
    				{
    					play == 1;
    				}
    				if(cont == 'N')
    				{
    					printf("\nThank you for playing! hehexd\n");
    					exit(EXIT_SUCCESS);
    				}
    			}
    
    
    			if(correct < 4)
    			{
    				printf("\nYou Lose!\n");
    				printf("Do you want to play a new game? <Y/N>\n");
    				scanf("%c", &cont);
    				getchar();
    
    
    				if(cont = 'Y')
    				{
    					play == 1;
    				}
    				if(cont = 'N')
    				{
    					printf("\nThank you for playing! hehexd\n");
    					exit(EXIT_SUCCESS);
    				}
    			}
    		}
    		getchar();
    	}
    }

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    				if(cont = 'Y')
    				{
    					play == 1;
    				}
    				if(cont = 'N')
    Go through the code, and check where you should be using == and where you should be using =
    They're ALL wrong in the snippet.
    There are others in your code to fix.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Jul 2016
    Posts
    6
    Thanks for the help guys, my codes are working fine now.
    Just wanna know how do u keep the first or subsequent values i entered if it was in a loop?
    Code:
    for(ctr = 1; ctr <=10; ctr ++)
    		{
    			printf("Select the colour you want to enter or 'Q' to Quit:\n'R' for RED, 'B' for BLUE, 'G' for GREEN, 'Y' for YELLOW\n");
    			scanf("%c\t%c\t%c\t%c",&color1, &color2, &color3, &color4);
    			getchar();
    
    
                            printf("*\t\t\t\t\t\t%c\t%c\t%c\t%c\t\t",color1, color2, color3, color4);
                    }
    printf("*\t\t\t\t\t\t%c\t%c\t%c\t%c\t\t",color1, color2, color3, color4);
    How do i keep this these value and print with the new set of value if i was to have 2 or more tries?

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I'm not sure I understand. If you want a variable to retain it's value
    through x amount of increments then just re-assign the original value
    again after each iteration has completed. Thus:

    Code:
    for (i = 1; i <= 5; ++i)
    {
        // do stuff
        x = 6; // original variable modified to starting value
    }
    Again, this may not be want your asking.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mastermind
    By roknroll3r in forum C Programming
    Replies: 5
    Last Post: 12-17-2013, 05:47 PM
  2. help with mastermind
    By jjexpress in forum C Programming
    Replies: 6
    Last Post: 08-16-2010, 09:24 AM
  3. mastermind
    By roeyman in forum C Programming
    Replies: 11
    Last Post: 01-01-2009, 08:21 AM
  4. Mastermind
    By grade10 in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2002, 03:52 PM
  5. MasterMind
    By ski6ski in forum Game Programming
    Replies: 0
    Last Post: 12-05-2001, 09:25 AM

Tags for this Thread