randoming problem...

This is a discussion on randoming problem... within the C Programming forums, part of the General Programming Boards category; now i'm using the for loop things for my mastermind game,but.....it still got a randoming problem just like the first ...

  1. #1
    C Newbie
    Join Date
    Oct 2005
    Posts
    13

    Question randoming problem...

    now i'm using the for loop things for my mastermind game,but.....it still got a randoming problem just like the first time i use with if else..
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int guess(int);
    
    int main()
    {
    
    int digits;
    srand((unsigned)time(NULL));
    printf("\nWelcome To Mastermind!\n");
    printf("How Much Digits Do You Want To Play:\n");
    scanf("%d",&digits);
    guess(digits);
    return 0;
    
    }
    
    int guess(int code)
    {
    int get_random,get_guess,checking,checking2,right,place;
    int guess_code[code-1];
    int secret_code[code-1];
    
    right = 0;
    place = 0;
    
    for(get_random=0;get_random<code;get_random++)
            {
                    secret_code[get_random] = rand()%10;
                    printf("%d",secret_code[get_random]);
            }
    printf("\n I Have Made %d Digit For You To Guess..\n",code);
    
    for(get_guess=0;get_guess<code;get_guess++)
    {       printf("Guess #%d:",get_guess+1);
            scanf("%d",&guess_code[get_guess]);
    }
    
    for(checking=0;checking<code;checking++)
    {
            if(secret_code[checking]==guess_code[checking])
            {
                    right = right + 1;
                    secret_code[checking] = -1;
            }
            for(checking2=0;checking2<code;checking2++)
            {
                    if(secret_code[checking] == guess_code[checking2])
                    {
                            place = place + 1;
                            secret_code[checking] = -1;
                    }
            }
    }
    
    printf("\nYou have placing %d code at a right place and %d code at a wrong place..\n",right,place);
    
    if(right==code)
    {
            printf("\nCongratulations,You have won the game!!\n");
            return 0;
    }
    else
    {
            guess(code);
    }
    }
    this is what i do,anyone can help me??

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    There are several syntax errors -- your compiler should have complained
    Code:
    int guess_code[code-1];
    int secret_code[code-1];
    arrays allocated like the above must use a const int value -- code is not a const int. When you need variable size of array, then use malloc() to allocate them, like this:
    Code:
    int* guess_code = malloc((code-1) * sizeof(int));
    int* secret_code = malloc((code-1) * sizeof(int))

  3. #3
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by Ancient Dragon
    There are several syntax errors -- your compiler should have complained
    Code:
    int guess_code[code-1];
    int secret_code[code-1];
    arrays allocated like the above must use a const int value -- code is not a const int.
    Well according to new standard....these statements are valid...you can have non-const value like that

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    The standards may allow it, but does your compiler? VC++ 6.0 does not because it is older than the current standard by quite a few years. Dev-C++ probably will allow it.

  5. #5
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Yes my compiler does...i am using dev c++ 4.9.9.2.....it conforms to most of the new standards....i have stopped using VC++6.0 compiler.....but the latest version of VC must be supporting it(though i haven't used them)

  6. #6
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote Originally Posted by sunnypalsingh
    Yes my compiler does...i am using dev c++ 4.9.9.2.....it conforms to most of the new standards....i have stopped using VC++6.0 compiler.....but the latest version of VC must be supporting it(though i haven't used them)
    Your compiler is supporting because it is based on c99 where variable length arrays are allowed.Every compiler will not do that.You can use this if Code portability is not an issue for you.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I just tried it with VC++ .net 2003 -- not supprised that that M$ compiler doesn't like it either
    Last edited by Ancient Dragon; 10-31-2005 at 12:07 PM.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I got it to compile using malloc() as I previously posted, and ran it with 5 digits. What exactly is the program doing that you think is wrong? The function guess() generates randon numbers between 0 and 9 just as you coded it. rand() does NOT guarantee UNIQUE numbers, just random ones. If you don't want duplicates, then you will have to add code to filter them out.

  9. #9
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    A good Mastermind game should allow duplicates. Otherwise the game is much easier.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  10. #10
    C Newbie
    Join Date
    Oct 2005
    Posts
    13
    frankly,my compiler is supporting the code,but when i get the wrong place number and looping to function guess,the random generator will create a new random number again..what can i do if i want the random generator just create one random number...

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    delete these lines
    Code:
    else
    {
            guess(code);
    }

  12. #12
    C Newbie
    Join Date
    Oct 2005
    Posts
    13
    i need to back to the guess function because if it has a wrong numbers,we must retype the guess number..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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