help with mastermind

This is a discussion on help with mastermind within the C Programming forums, part of the General Programming Boards category; I just finished my mastermind yesterday and it was perfectly working. i tried it again earlier and I found out ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    14

    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
    Last edited by jjexpress; 08-14-2010 at 09:59 PM. Reason: error

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    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
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    14
    thanks.
    i'll check it out.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    14
    how can I make the switch part a function??

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    14
    @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

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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:
    Code:
    play = 0;
    break;
    )

    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.
    Last edited by dwks; 08-16-2010 at 10:27 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mastermind program problem with iterator
    By Truckomobil in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2009, 02:16 PM
  2. Can anyone teach me how to write mastermind in GUI??
    By prefectticky in forum C++ Programming
    Replies: 18
    Last Post: 04-17-2009, 09:57 AM
  3. Algorithmic Mastermind Program
    By pr0n in forum C Programming
    Replies: 1
    Last Post: 05-13-2008, 12:02 AM
  4. Replies: 37
    Last Post: 12-13-2007, 03:40 PM
  5. coding mastermind in C, only got 2 probs..
    By lepricaun in forum C Programming
    Replies: 17
    Last Post: 07-24-2004, 10:15 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21