Thread: My first major program- Bulls and cows Game

  1. #1
    Registered User Suhasa010's Avatar
    Join Date
    Sep 2013
    Posts
    10

    My first major program- Bulls and cows Game

    Hi,
    I'm a Computer Science student(Engg), who completed just 1 year in engineering.
    I tried to simulate a game called 'bulls and cows' which maybe a minor program to you masters, but its an important one for me, since it is my first so-called major program.
    Please correct me if i have done any mistake.
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<time.h>
    int numgen1(int a)
    {
      int b;
    do{
      b=rand()%10;
     }while(b==a);
      return b;
    }
    int numgen2(int a,int b)
    {
      int c;
    do{
      c=rand()%10;
     }while(c==a||c==b);
      return c;
    }
    int numgen3(int a,int b,int c)
    {
      int d;
    do{
      d=rand()%10;
     }while(d==a||d==b||d==c);
      return d;
    }
    int numcheck(int w,int x,int y,int z,int a,int b,int c,int d,char plrname[])
    {
      int bulls=0,cows=0;
      if(w==x||w==y||w==z)
      {
        printf("invalid entry!! Closing game...");
    	exit(0);
      }
      if(w==b||w==c||w==d)
        cows=cows+1;
      else if (w==a)
        bulls=bulls+1;
      if(x==a||x==c||x==d)
        cows=cows+1;
      else if (x==b)
        bulls=bulls+1;
      if(y==a||y==b||y==d)
        cows=cows+1;
      else if (y==c)
        bulls=bulls+1;
      if(z==a||z==b||z==c)
        cows=cows+1;
      else if (z==d)
        bulls=bulls+1;
      if(bulls==4)
      {
         printf("Correct answer!!!! You won!! Congratulations %s",plrname);
         return 1;
      }
      else
         printf("Bulls=%d\nCows=%d\nTry Again!!\n",bulls,cows);
    }
    int main()
    {
      int w,x,y,z,a,b,c,d,try=0,f;
      char plrname[20],ch1,ch;
    srand(time(0));  
    clrscr();
      printf("Welcome to Bulls and Cows game\nEnter your name to continue\n");
      gets(plrname);
      printf("\nMAIN MENU\nPress P for Play Game\nX for EXIT\n");
      scanf("%c",&ch); 
      a=rand()%10; 
      b=numgen1(a); 
      c=numgen2(a,b); 
      d=numgen3(a,b,c);
      while(1)
      {
       ++try;
        switch(ch)
    	{
    	  case 'p': printf("TRY NO. %d\n",try);
    		  printf("Enter 4 Digits\n");
    			  scanf("%d%d%d%d",&w,&x,&y,&z);
    			  f=numcheck(w,x,y,z,a,b,c,d,plrname);
    			  if(f==1)
    			  {
    			   getch();
    			   exit(0);
    			  }
    			  if(try==5||try==10||try==15)
    			   {
    			     printf("Press Y for correct answer\n");
    			     scanf("%c",&ch1);
    			     if(ch1=='Y'||ch1=='y')
    				   {
    				     printf("The Correct answer was %d%d%d%d",a,b,c,d);
    					 getch();
    					 exit(0);
    				   }
    			    }
    				else if(ch1=='N'||ch1=='n')
    				  break;
    				else
    				  continue;
    	  case 'c': break;
    	  case 'x': exit(0);
    	}
      }
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You should use more meaningful names than "w, x, y, z"...
    Try inserting comments so that others know what's you're doing.
    Improve your indentation.

    Other than those, what's your problem?
    Devoted my life to programming...

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    A description of "Bulls and Cows" would be nice.
    As Your variable names are useless.
    And it looks like you might be better off using an array.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You really should describe the game instead of assuming we know what you are talking about. I had to look it up.

    Bulls and cows is a simple guessing game played with a number. The number must not have repeating digits, and it is typically played with a four digit number. Players make a guess what the number is, and the feedback consists of how many bulls and cows there are. Bulls are digits that are correct in place value, and cows are digits that are correct, but not in the correct place value. You win if you find the number in X number of turns.

    Here's a suggestion for improvement. Number generation could be a lot simpler with an array. Set up an array with all of the digits, and when you select one randomly, replace it with an illegal value. Then, when you want to generate the number for the game, this code will check for repeats:

    Code:
    if (digits[ (num = randomdigit()) ]  == -1 ) {
       /* reroll num */
    } else {
       /* make num part of the secret */
       digit[num] = -1; /* no repeats */
    }
    You can loop that, and generate the secret number for the game.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Also this -> Mastermind (board game) - Wikipedia, the free encyclopedia
    I'm surprised how few moves are needed for someone who knows what they're doing.
    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.

  6. #6
    Registered User Suhasa010's Avatar
    Join Date
    Sep 2013
    Posts
    10

    Thanks to all for your feedback

    Lots of thanks to all those who pointed out my mistakes. I will take it seriously and i will try to improve my programming skill

  7. #7
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Code:
        if(try==5||try==10||try==15)
        {
            printf("Press Y for correct answer\n");
            scanf("%c",&ch1);
            if(ch1=='Y'||ch1=='y')
            {
                printf("The Correct answer was %d%d%d%d",a,b,c,d);
                getch();
                exit(0);
            }
        }
        else if(ch1=='N'||ch1=='n')
          break;
        else
          continue;
    Is this really doing what you want it to do?

    Edit: Also, is ch1 guaranteed to be initialized when you check its value?

    Edit2: You should probably also check the value returned by scanf()
    Last edited by SirPrattlepod; 09-05-2013 at 09:47 PM.

  8. #8
    Registered User Suhasa010's Avatar
    Join Date
    Sep 2013
    Posts
    10

    @sirPrattlepod

    Yes, that if(ch1=='N') condition wasn't required.. I see that during the execution of the program, it did not require that condition..
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed in cows and bulls
    By Nikolas in forum C Programming
    Replies: 14
    Last Post: 01-13-2012, 07:06 PM
  2. Looking for major help on hacking a online game
    By bergerman123 in forum C++ Programming
    Replies: 5
    Last Post: 12-09-2008, 10:27 PM
  3. Major game issues
    By VOX in forum Game Programming
    Replies: 0
    Last Post: 01-18-2005, 08:40 AM
  4. Major Windows Software - Are there any Major Players Left?
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-12-2004, 05:34 PM
  5. i need an idea for a first major game
    By revelation437 in forum Game Programming
    Replies: 10
    Last Post: 09-26-2003, 02:32 PM

Tags for this Thread