# Rookie looking for help!

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-10-2011
Newklear
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...
• 03-10-2011
mike65535
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.
• 03-10-2011
Salem
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.
• 03-10-2011
Newklear
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
• 03-10-2011
Mr.777
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....
• 03-10-2011
Newklear
Quote:

Originally Posted by Mr.777
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
• 03-10-2011
Mr.777
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]);     } }```
• 03-10-2011
Newklear
if by handling it you mean for example doing this instead:

Code:

`char help[ARR_SIZE] = {'_','_','_','_'};`
then no, it didnt help...
• 03-10-2011
Mr.777
Why aren't you using string comparisons for this case....

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....
• 03-10-2011
Newklear
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]...
• 03-10-2011
Mr.777
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]);     } }```
• 03-10-2011
Newklear
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 _ ...
• 03-10-2011
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```
• 03-10-2011
Mr.777
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????
• 03-11-2011
Salem
I'm dismayed by the continuing dog's breakfast of poor code indentation in this thread...
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last