Thread: Rookie looking for help!

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    21

    Rookie looking for help!

    I couldn't think of a good title for the thread, hence the really bad one, sorry...

    I'm currently working on a "tiny" game to learn some of the basics in C. In short, the computer makes an array of four randomly generated numbers between 0 and 9, then you try to guess what those four numbers are by making your own array of 4 numbers between 0 and 9.

    What im doing right now is creating a sort of hint system, which tells you which of your four numbers are wrong, which ones are right but in the wrong position and which ones are completely wrong.

    If the number is right, you get an 'R', if it is right but in the wrong position you get an 'S', and if its completely wrong you get an '_'...

    Here's what i've done so far, im not copying all of the code, just the part i need help with, but could copy it all if its needed...

    Code:
    for(i=0; i<ARR_SIZE; i++)
        {
            if (guess[i] == secretNumber[i] && help[i] != 'S')
                help[i] = 'R';
    
            for(j=0; j<=i-1; j++)
            {
                if(guess[i] == secretNumber[j])
                {
                    help[i] = 'S';
                }
            }
    
            if (help[i] != 'R' && help[i] != 'S')
                help[i] = '_';
        }
    
        printf("help: ", help[i]);
        for(i=0; i<ARR_SIZE; i++)
        {
            printf("%c ", help[i]);
        }
    I'm guessing this is a rather simple task, but im having problems with the logic...

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Well, don't keep us in suspense. What's the problem, exactly? You couldn't even come up with a description for it - how are we to know?

    And please copy in more of the code. As far as we can see , the code snippet you show has undeclared variables, unknown #defines, etc.

    It will be worth YOUR effort to be able to publish STREAMLINED code (but code that COMPILES with NO WARNINGS) in which you've isolated the specific issue. This not only makes it easier for others to help you quickly, but more importantly, if you can reduce your code to the minimum in which your issue/problem exists, you will likely learn what is broken WITHOUT anyone's help.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I would suggest you break the problem into two steps.

    First, which numbers are right, and in the right position.

    Next, for whichever numbers you haven't used so far, whether they exist in the secret array.
    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.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Alright, sorry if it was confusing, the problem is that it doesnt give the correct hints. For example, it sometimes gives an '_' when you should get an 'S'...

    I'll paste the whole code on pastebin, so you can see it...

    The code

    its line 79 to 107 i need help with

  5. #5
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Use modular approach.
    1. Make a function that will find the right number at the right place and will return anthing that you want....
    2. Make a function that will find the right number at the wrong place and will return anything that you want....
    3. Make a function that will find the wrong number and will return anything that you want.....

    As you didn't describe your problem, so we can't see, what's the real problem. Anyways, check, if this helps you out....

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Quote Originally Posted by Mr.777 View Post
    Use modular approach.
    1. Make a function that will find the right number at the right place and will return anthing that you want....
    2. Make a function that will find the right number at the wrong place and will return anything that you want....
    3. Make a function that will find the wrong number and will return anything that you want.....

    As you didn't describe your problem, so we can't see, what's the real problem. Anyways, check, if this helps you out....
    Yes, i've tried this, but its the number 2 in your approach that i'm having problems with... :S

  7. #7
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    for(i=0; i<ARR_SIZE; i++)
        {
            if (guess[i] == secretNumber[i] && help[i] != 'S') // Your help array has garbage in it. So better try to handle it.... I hope after this, it will work fine...
                help[i] = 'R';
     
            for(j=0; j<=i-1; j++)
            {
                if(guess[i] == secretNumber[j])
                {
                    help[i] = 'S';
                }
            }
     
            if (help[i] != 'R' && help[i] != 'S')
                help[i] = '_';
        }
     
        printf("help: ", help[i]);
        for(i=0; i<ARR_SIZE; i++)
        {
            printf("%c ", help[i]);
        }
    }

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    if by handling it you mean for example doing this instead:

    Code:
    char help[ARR_SIZE] = {'_','_','_','_'};
    then no, it didnt help...

  9. #9
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Why aren't you using string comparisons for this case....
    Read about strcmp(string1, string2)....

    Code:
    if(help[i]!=null){
                             if (guess[i] == secretNumber[i] && help[i] != 'S') 
                             help[i] = 'R';
                             }
    Try this.....

    Code:
    for(j=0; j<=i; j++)
            {
                if(guess[i] == secretNumber[j])
                {
                    help[i] = 'S';
                }
            }
    Finally, i think, this was the problem, you were comparing one less each time... While you should compare till the inner index of guess to the outer index of secretNumber...
    I hope now it will work fine....
    Last edited by Mr.777; 03-10-2011 at 08:20 AM.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    tried changing it to j<=i, but its still not right...

    for example, if the secretNumber array is
    6, 3, 9, 2
    and the guess array is
    1, 2, 3, 4,
    it returns
    _ _ S _

    when it should return _ S S _

    So, it seems to be comparing forward, but not backwards, since it gives guess[2] the correct value, but not guess[1]...

  11. #11
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    int k=0;
    for(i=0; i<ARR_SIZE; i++)
        {
          while(k<ARR_SIZE){
                                          if (guess[i] == secretNumber[k] && help[i] != 'S'){
                                           help[i] = 'R';
                                           break;
                                            }
                                           k++;
                                          }
                                            k=0;
     
            for(j=0; j<=i-1; j++)
            {
                if(guess[i] == secretNumber[j])
                {
                    help[i] = 'S';
                }
            }
     
            if (help[i] != 'R' && help[i] != 'S')
                help[i] = '_';
        }
     
        printf("help: ", help[i]);
        for(i=0; i<ARR_SIZE; i++)
        {
            printf("%c ", help[i]);
        }
    }
    Now, it will run 100%..... :-)

    It will do _ RR_

    And i don't know why it should produce _SS_
    as you are replace R in case of right number but wrong place......

    Okay, so i think you want something like......
    Code:
    int k=0;
    for(i=0; i<ARR_SIZE; i++)
        {
                                                if (guess[i] == secretNumber[i] && help[i] != 'S'){
                                           help[i] = 'R';
                                           //break;
                                            }
                                           
            while(k<ARR_SIZE){
    
                if(guess[i] == secretNumber[k])
                {
                    help[i] = 'S';
                    break;
                }
           k++;
          }
       k=0;
     
            if (help[i] != 'R' && help[i] != 'S')
                help[i] = '_';
        }
     
        printf("help: ", help[i]);
        for(i=0; i<ARR_SIZE; i++)
        {
            printf("%c ", help[i]);
        }
    }
    Last edited by Mr.777; 03-10-2011 at 08:55 AM.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    No, that doesnt work either....

    I tried the code you wrote and if the guess array and the secretNumber array were exactly the same, it produced S S S S, when it should produce R R R R, since all the numbers are correct...

    Again, if one position in the guess array has the same value as the same position in the secretNumber array, it should produce an R, if in the same position the values are different, it should produce an _ UNLESS the value of the position in the guess array exists in any of the other positions in the secretNumber array, where as it should return an S instead of an _ ...

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have to record one of three states, for each number. Your logic should be something like this.

    Code:
    for each number[i]
      if guess[i] equals number[i] state[i]='R'
      for each [j] from 0 to MAX-1, and j != i
         if guess[j] == number[i] state[i]='S'
      end for   
      if state[i] != 'R' && state[i] != 'S' then state[i]=='_'
    end for
    Around the above, you'll probably want a while loop to play the game (it's called Mastermind, I believe):
    Code:
    while(numRight < MAX) { 
    
      print initial state array
      for i from 0 to MAX-1, get the user's guesses
      
      then add all the above code
    
      
    end while
    Last edited by Adak; 03-10-2011 at 02:04 PM.

  14. #14
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    int k=0;
    for(i=0; i<ARR_SIZE; i++)
        {
                                                if (guess[i] == secretNumber[i] && help[i] != 'S'){
                                           help[i] = 'R';
                                           //break;
                                            }
                                           
            while(k<ARR_SIZE){
    
                if(guess[i] == secretNumber[k])
                {
                    help[i] = 'S';
                    break;
                }
           k++;
          }
       k=0;
     
            if (help[i] != 'R' && help[i] != 'S')
                help[i] = '_';
        }
     
        printf("help: ", help[i]);
        for(i=0; i<ARR_SIZE; i++)
        {
            printf("%c ", help[i]);
        }
    }
    That's why i put this code after..... Did you really try this one????

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'm dismayed by the continuing dog's breakfast of poor code indentation in this thread...
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable/pointer scope (rookie Qs)
    By twinbee in forum C Programming
    Replies: 3
    Last Post: 05-20-2010, 03:33 PM
  2. an uber-simple array/function question for a c rookie!
    By variable83 in forum C Programming
    Replies: 13
    Last Post: 12-06-2009, 01:24 PM
  3. Help a rookie
    By elrookie in forum C Programming
    Replies: 14
    Last Post: 06-14-2007, 10:28 AM
  4. Rookie pointer problem: 2 dimensions, malloc
    By GatesAntichrist in forum C Programming
    Replies: 11
    Last Post: 12-20-2005, 12:35 PM
  5. my code.. remember i am a rookie dont laugh
    By aubrey in forum C Programming
    Replies: 2
    Last Post: 11-24-2002, 10:08 PM