Simple Guessing Game

This is a discussion on Simple Guessing Game within the C Programming forums, part of the General Programming Boards category; This is in no way any assignment. I am new and i'm attempting to learn some C. I was just ...

  1. #1
    Elk
    Elk is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Simple Guessing Game

    This is in no way any assignment. I am new and i'm attempting to learn some C.

    I was just wondering on some sort of basic idea to make a guessing game. Say you have to pick a number between 1 and 1000 and the number is randomly chosen by the computer (The part I can NOT get). You then input the number into the program and it tells you higher, lower, or if you have won. After winning, the program exits. But untill you have won, the game loops. Just somethin i tried doin after learning some things but i couldn't get the computer to pick a random number and have you guess it. Just wonderin if someone could gimmie a little help there.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Start with

    int target = 50;
    Then implement all your code to prompt the user for values, and loop until they get the guess right.
    This proves the logic, and gets all the user interaction stuff working.


    Then read this
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    and then you can do
    int target = genrand(1,1000);
    and the jobs a good'un.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Elk
    Elk is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    where do i place target = genrand(1, 1000); into the code? Do I use a for loop?
    idk it looks like crap but this is all i've got hmmm...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    main()
    {
    int close;
    int repete = 1;
    int target = 1;
    int input;
    
    while (repete = 1)
    {
    
    		target = genrand(1, 1000);
    
    	printf("Pick a number between 1 and 1000\n");
    	scanf("%d", &input);
    
    	if (input < target)
    	{ printf("Higher than %d\n"), input; }
    
    	else if (input > target)
    	{ printf("Lower than %d\n"), input; }
    	
    	if (input == target)
    	{ printf("Correct! The correct number was %d!\n"), input;
    	printf("Press a key then hit enter to exit the program\n");
    	scanf("%d", &close);
    	return 0; }
    }
    }
    Last edited by Elk; 09-02-2006 at 01:33 AM.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    3
    Its really easy program.hare is its code


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    void main()
    {
         int x,g;
         randomize();
         x=random(1000);
         while(1)
          {
              printf("\n GUESS MY NO::");
              scanf("%d",&g);
              if(x==g)
              {
                       printf("YOU WON");
                        break;
               }
              if(g<x)
               {
                       printf("TOO LOW");
                }
              else
               {
                     printf("\n too high");
                }
      }
    }
    Last edited by Ken Fitlike; 09-02-2006 at 05:57 AM. Reason: code tags added

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > while (repete = 1)
    Use == for comparisons, = is for assignment.

    > target = genrand(1, 1000);
    1. You need to copy the genrand() function from the FAQ
    2. You probably want this outside the loop, otherwise the number you're supposed to guess keeps changing.

    > printf("Higher than %d\n"), input;
    Put the parameter inside the function call, like so
    printf("Higher than %d\n", input );

    > scanf("%d", &close);
    How to pause the program before exiting is also in the FAQ.

    @radhika
    The whole point of the board is to help people, not just post alternative answers which don't teach anyone anything. Besides, when people don't use code tags and use void main and a bunch of non-standard headers and functions, it really is not helping at all.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by Salem
    @radhika
    The whole point of the board is to help people, not just post alternative answers which don't teach anyone anything. Besides, when people don't use code tags and use void main and a bunch of non-standard headers and functions, it really is not helping at all.
    Amen.

    radhika:

    I added the code tags for you since you're relatively new here but please make sure you use them if you post code in future.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Elk
    Elk is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Can't seem to find the genrand() function on the FAQ. And also, is it also possible to set the RAND_MAX to a number and just use srand and rand() function?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They call it 'GetRand' in the FAQ link they've provided. RAND_MAX is a standard C macro. You don't set it to anything, it's already a specified value. You can just take the return value from rand and apply some basic math to get a range you like. Search the forum for rand or random and you'll get plenty on the topic.


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

  9. #9
    Elk
    Elk is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Ok ty. finally got it working. is there any way to imporve it?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int GetRand(int min, int max);
    
    int main(void)
    
    {
    int i, r, x;
    
    for (i = 0; i < 1001; i++)
    {
    r = GetRand(1, 1000);
    }
    
    while (1)
    {
    	char last;
    
    	printf("Pick a number between 1 and 1000\n");
    	scanf("%d", &x);
    
    	if (x < r)
    	{ printf("Higher than %d\n", x); }
    
    	if (x > r)
    	{ printf("Lower than %d\n", x); }
    	
    	if (x == r)
    	{ printf("Correct! The correct number was %d!\n", r);
    	scanf("%c", &last);
    	return 0; }
    }
    }
    
    int GetRand(int min, int max)
    {
      static int Init = 0;
      int rc;
      
      if (Init == 0)
      {
        srand(time(NULL));
        Init = 1;
      }
     
      rc = (rand() % (max - min + 1) + min);
      
      return (rc);
    }

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > for (i = 0; i < 1001; i++)
    This loop serves no purpose, just doing

    r = GetRand(1, 1000);
    once is sufficient.

    Your indentation and brace positioning style could be better.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    123
    Maybe it's a personal thing, but I really hate breaking out of infinite loops. Rather, I like to see a controlled exit using something like this (haven't re-entered the GetRand function):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int GetRand(int min, int max);
    
    int main(void)
    {
    	int i, r, x;
    	char last;
    
    	r = GetRand(1, 1000);
    
    	printf("Pick a number between 1 and 1000\n");
    	scanf("%d", &x);
    
    	while (x != r)
    	{
    		if (x < r)
    		{
    			printf("Higher than %d\n", x);
    		}
    		else
    		{
    			printf("Lower than %d\n", x);
    		}
    
    		printf("Pick a number between 1 and 1000\n");
    		scanf("%d", &x);
    	}
    	
    	printf("Correct! The correct number was %d!\n", r);
    	scanf("%c", &last);
    	return 0;
    }
    However, as you can see it involved two calls to get the input number. This could very easily be put into a function, though.

    Also, I wouldn't declare a variable within a loop, so I moved the declaration of 'last' outside of it.
    I think you can put a signature here.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Angoid
    However, as you can see it involved two calls to get the input number. This could very easily be put into a function, though.
    Code:
    do
    {
        printf( "Pick a number: " );
        fflush( stdout );
        if( scanf( "%d", &number ) != 1 )
            printf( "Try again.\n" );
        else
        if( number < r )
            printf( "Higher.\n" );
        else
        if( number > r )
            printf( "Lower.\n" );
    } while( number != r );
    One prompt. One read.


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

  13. #13
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    This may not be applicable, but if it was coded in C++ it would be a little eaier to code.
    You could seed the random number using srand() and it may take fewer lines to complete.
    Just an idea, but the same thing can be acomplished in C

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    That doesn't make sence. It would be easier to code in C++ because you can add an srand() call, which you can do in C anyway?
    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. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  2. help with a simple game..
    By ninne in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2006, 04:48 AM
  3. Help with a simple game
    By jjj93421 in forum Game Programming
    Replies: 1
    Last Post: 03-28-2004, 02:52 PM
  4. number guessing game, no idea where to start
    By karin in forum C Programming
    Replies: 9
    Last Post: 03-18-2004, 08:49 PM
  5. Replies: 1
    Last Post: 11-06-2001, 01:15 PM

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