Thread: Help needed in cows and bulls

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    3

    Help needed in cows and bulls

    I'm relatively new to c programming and I'm in the learning process.
    Using only simple functions, I have written code for the
    Cows & Bulls game, otherwise known as mastermind. The program however throws up random values for bulls and cows.Can you please spot the problem and tell me?Forgive me if it's a very basic mistake.

    Thanks in advance!
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    main()
    {
    int cows,bulls,i;
    char a,b,c,d,A,B,C,D;
    clrscr();
    printf("Welcome to Mastermind \n\n Player 1,enter the letters of you word.");
    a=getchar();b=getchar();c=getchar();d=getchar();
    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nPlayer2, please get ready");
    for(i=1;i<=10;i++)
        {
         bulls=0;cows=0;
         printf("\nGuess %d\n",i);
         A=getchar();
         B=getchar();
         C=getchar();
         D=getchar();
         if(a==A)bulls=bulls+1;if((a==B)||(a==C)||(a==D))cows+=1;
         if(b==B)bulls=bulls+1;if((b==A)||(b==C)||(b==D))cows+=1;
         if(c==C)bulls=bulls+1;if((c==A)||(c==B)||(c==D))cows+=1;
         if(d==D)bulls=bulls+1;if((d==A)||(d==B)||(d==C))cows+=1;
         if(bulls==4) printf("Congratulations player 2, you have cracked the Mastermind");
         else printf("Bulls = %d Cows = %d",bulls,cows);
         }
    getch();
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    This problem gets pretty much everyone.

    In short, when asking for input characters are not read straight into your program they instead go into an input buffer and your program reads from that buffer. The newline character ('\n') is stored in this buffer as well.

    You need an extra call to getchar to retrieve this newline character.

    Haven't actually checked your logic, I just noticed this about the input.

    So many people have this problem, thinking that C books don't cover input buffers very well because it's beyond their scope or something?

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    There's a couple things I think you should do, and it'll help you debug what the actual problem is:

    - Lines 20-24 are impossible to read, as you've crammed so much in. At the very least, put each "if" on its own line.
    - The statement "bulls = bulls+1" could be replaced with "bulls++" for clarity.
    - You're collecting each character individually, instead of doing a fgets() into a char array, and picking out the individual letters.
    - Not sure what the extra getch() at the end of the for loop is doing..
    - It's bad practice to do main(). The standard is (at the very least) int main().

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    First of all thanks memcpy & Deadplanet, even if the issue is not resolved yet.
    I have done as you suggested.
    -I used scanf for accepting variables.
    -getch() allows you to compile the program and view the output screen. Otherwise you'll need to press control+F9
    & then alt+F5.

    Also, I'm using Turbo c if that will help.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    int main()
    {
    int cows,bulls,i;
    char a,b,c,d,A,B,C,D;
    clrscr();
    printf("Welcome to Mastermind \n\n Player 1,enter the letters of you word.");
    scanf("%c%c%c%c",&a,&b,&c,&d);
    printf("Player2, please get ready");
    for(i=1;i<=10;i++)
        {
         bulls=0;cows=0;
         printf("\nGuess %d\n",i);
         scanf("%c%c%c%c",&A,&B,&C,&D);
         if(a==A)bulls++;
         if((a==B)||(a==C)||(a==D))cows++;
         if(b==B)bulls++;
                       if((b==A)||(b==C)||(b==D))cows++;
         if(c==C)bulls++;
                       if((c==A)||(c==B)||(c==D))cows++;
         if(d==D)bulls++;
                       if((d==A)||(d==B)||(d==C))cows++;
         if(bulls==4) printf("Congratulations player 2, you have cracked the Mastermind");
         else printf("Bulls = %d Cows = %d",bulls,cows);
         }
    getch();
    }
    
    

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You aren't taking care of the enter key that player one is hitting after they type in their word.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    Hi guys,problem still not resolved.@DeadPlanet, could you explain the part about input buffers and what needs to be done?
    Here's my take with arrays.Still same result though.
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #include<string.h>
    int main()
    {
    int cows,bulls,i,s,c,d;
    char guess[4],word[4];
    clrscr();
    printf("Welcome to Mastermind \n\n Player 1,enter the letters of your word.");
    for(i=1;i<=4;i++)
        {
        scanf("%c",&word[i]);
        }
    clrscr();
    printf("Player2, please get ready");
    for(i=1;i<=10;i++)
    {
        bulls=0;cows=0;
        printf("\nEnter Guess %d:",i);
        for(c=1;c<=4;c++)
            {
            scanf("%c",&guess[c]);
            }
        for(d=1;d<=4;d++)
            {
            if(guess[d]==word[d]) bulls++;
            else {
                for(s=1;s<=4;s++)
                {
                if(guess[d]==word[s]) cows++;
                }
                 }
            }
    if(bulls==4) printf("You are Victorious");
    printf("Bulls = %d, Cows = %d",bulls,cows);
    }
    getch();
    }
    Last edited by Nikolas; 01-13-2012 at 10:37 AM.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to make sure that the newline has been read after the 4 letters of the word. A cheap (non-robust) solution would be to add another getchar (which you should be using instead of scanf) after reading the word, which of course only works if the newline is directly after the 4 letters (and there's no spaces before the letters).

    A better solution would be to write a function to read the word, skipping initial spaces, and consuming any extra characters including the newline after the 4 characters that you want.

    Also, your for loop with index d should go from 0 to 3, not 1 to 4, since array indices start at 0.

    Even with these fixes it looks like your algorithm still won't work correctly, but that's another story.

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    17
    I don't know if you terribly attached to scanf() but if I were you I'd probably be using fgets();

    Like this:
    Code:
    //at top
    char guess[5];
    
    //to get input
    fgets(guess, sizeof(guess), stdin);
    It's totally up to you though, I just dislike scanf() alot.

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    17
    I looked up the game and it seemed interesting so I just changed yours all around to make it work. I don't mind answering questions if you don't understand something.

    Two things to note:
    1. I used the system() function to clear the screen so you might have to change that if it's not portable to your computer.
    2. I made the GUESS and WORD variables 10 bytes long just to protect from accidental overflow. Just being safe =)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(){
        char guess[10], word[10];
        int turns, a, b, bulls, cows;
        system("cls");
        printf("Welcome to Mastermind \n\nPlayer 1, enter the letters of your word: ");
        fgets(word,sizeof(word),stdin);
        system("cls");
        printf("\nPlayer2, please get ready.");
        
        for(turns=1;turns<=10;turns++){
            bulls=0;cows=0;
            printf("\nEnter Guess %i: ",turns);
            fgets(guess,sizeof(guess),stdin);
            
            for(a=0;a<4;a++){
                if(guess[a]==word[a]){
                    bulls++;
                }else{
                    for(b=0;b<4;b++){
                        if(guess[a]==word[b]){
                            cows++;
                        }
                    }
                }
            }
            
            if(bulls==4){
                printf("\nYou are Victorious!\n");
                system("pause");
                break;
            }else{
                printf("\nBulls = %i, Cows = %i\n",bulls,cows);
            }
        }
        
        return(0);
    }
    Last edited by OhPoo; 01-13-2012 at 05:56 PM.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    @Nikolas, as you can see, someone has attempted to do your homework for you. If I was you, I would complain ... the more indignant, the better.

    At any rate, the proffered code is incorrect.

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    17
    @oogabooga Explain

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    @OhPoo:

    I used your code and the number of Cows was 3; when, I think 1 was correct.

    Tim S.

    Code:
    Player2, please get ready.
    Enter Guess 1: 4321
    
    Bulls = 0, Cows = 4
    
    Enter Guess 2: 1111
    
    Bulls = 1, Cows = 3
    
    Enter Guess 3: 1234
    
    You are Victorious!
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  13. #13
    Registered User
    Join Date
    Jan 2012
    Posts
    17
    @stahta01: I didn't want to over complicate the program with duplicate checking since the game rules state that all the numbers have to be unique. "1111" is not a legal guess.

    "On a sheet of paper, the players each write a 4-digit secret number. The digits must be all different."
    from:
    http://en.wikipedia.org/wiki/Bulls_and_cows
    Last edited by OhPoo; 01-13-2012 at 06:54 PM.

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    @OhPoo:
    Then why is your title mastermind? You look to be correct for Bulls and Cows.
    Mastermind (board game) - Wikipedia, the free encyclopedia

    It this output right for Bulls and Cows it is wrong for Master mind I lean growing up; but, the Wiki page says it is right. The mastermind I played as a kid gave 4 4 as a perfect guess.

    Player2, please get ready.
    Enter Guess 1: 5678

    Bulls = 0, Cows = 0

    Enter Guess 2: 1245

    Bulls = 2, Cows = 1

    Enter Guess 3:
    Tim S.
    Last edited by stahta01; 01-13-2012 at 07:09 PM. Reason: Added results
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  15. #15
    Registered User
    Join Date
    Jan 2012
    Posts
    17
    @stahta01:
    Because I used the strings provided in the original code. I didn't know there was any difference between mastermind and Cows & bulls as I've never played either. I'm just trying to demonstrate an alternative method of user input; I thought that was clear. The thread author can change the logic of the game if they please.
    Last edited by OhPoo; 01-13-2012 at 07:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed please.
    By TaktaK in forum C++ Programming
    Replies: 7
    Last Post: 10-04-2005, 09:32 PM
  2. Help Needed !!
    By vodlum in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2005, 02:33 PM
  3. Help needed.
    By hyrule in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2005, 09:51 AM
  4. Web Bot Needed ( will pay ) Plz Look
    By tribalflames69 in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 05-04-2005, 11:37 AM
  5. Help Needed Please!!!!!
    By jon_morrow in forum C++ Programming
    Replies: 7
    Last Post: 03-22-2002, 11:55 AM