PDA

View Full Version : Cocoa Number Guesser Help




bguy
May 30, 2009, 09:35 AM
Hello, I am a new Cocoa programmer and I am having problems with a number guesser program. I have set up and linked all the actions to the interface, but when I enter a guess and press the button I set up, nothing happens. I assume that means something in the action's programming is messed up. I am also new to Objective-C so I wouldn't know if I messed up. I am supplying the GuesserController.m file, but if you need to check anything else, I can supply it. Could someone explain to me what is wrong?

#import "GusserController.h"


@implementation GusserController
- (IBAction)Guess:(id)sender
{
guesser = [[Guesser alloc] init];
[guesser setGuess:[GuessField intValue]];
if ([guesser Compare] == 0)
{
[ResponceField setStringValue:@"Congrats, you got it!"];
if ([guesser Guesses] < [guesser Highscore])
[HighscoreField setIntValue:[guesser Guesses]];
if ([guesser Guesses] > [guesser Lowscore])
[LowscoreField setIntValue:[guesser Guesses]];
[guesser setGuesses:1];
[self SetRand:(id)sender];
}
else if ([guesser Compare] == -1)
{
[ResponceField setStringValue:@"Guess Higher"];
[guesser setGuesses:([guesser Guesses] + 1)];
}
else if ([guesser Compare] == 1)
{
[ResponceField setStringValue:@"Guess Lower"];
[guesser setGuesses:([guesser Guesses] + 1)];
}
[GuessField selectText:self];
}
-(IBAction)SetRand:(id)sender
{
srand( time(NULL));
guesser = [[Guesser alloc] init];
int randomNumber;
randomNumber = rand() % 10 + 1;
[guesser setSecret:(randomNumber)];
NSLog(@"random number %d", randomNumber);
}
@end



SRossi
May 30, 2009, 09:51 AM
I'm no expert at objective-c but with the compare method shouldn't it actually be ...

[guesser Compare:aString]

I am sure someone will disagree but im sure you need something to compare it with.

Stephen

bguy
May 30, 2009, 09:59 AM
No, compare is a function I have in a different file that compares two inputs and tells if your guess is higher, lower or equal.

SRossi
May 30, 2009, 10:06 AM
Ah right sorry its just because u can use the compare method to find if they are equal. What I would do is set up NSLogs to make sure that numbers are being set up and that the application is taking in your guesses.

Stephen

bguy
May 30, 2009, 10:09 AM
What I would do is set up NSLogs to make sure that numbers are being set up and that the application is taking in your guesses.


How do you do that?

SRossi
May 30, 2009, 10:19 AM
NSLog(@"random number %@", urRandom);

That will post a message onto the console of the random number. Just change urRandom to the random number you have set up.

Stephen

bguy
May 30, 2009, 10:36 AM
I did as you suggested and it said that the random number is null. I can't figure out what is wrong.
I also changed the code a bit so it shouldn't reset the number every time you guess. I edited my first post to show my changes.

SRossi
May 30, 2009, 10:44 AM
Right to create a random number you do:

int randomNumber;
randomNumber = random() %10 + 1;


This will create a random number, implement this into your SetRand function and then do

NSLog(@"Random number %d", randomNumber);

Im just saying this on the basis of the code that you have given, and earlier when i said %@ in the NSLog i meant %d sorry. If you do the code above that will give you a random number.

Stephen

bguy
May 30, 2009, 10:55 AM
Changed it again.
The random number generator works, but the guessing field doesn't. When I input a number and press it, in the console it says:
2009-05-30 11:50:56.407 InterfaceNumberGuesser[492:10b] *** -[NSTextField setString:]: unrecognized selector sent to instance 0x1282f0
2009-05-30 11:50:56.408 InterfaceNumberGuesser[492:10b] *** -[NSTextField setString:]: unrecognized selector sent to instance 0x1282f0

SRossi
May 30, 2009, 10:59 AM
To set the string of a text field you need to do

[textField setStringValue:@"Hello"];

Notice setStringValue and the @ symbol before the text brackets

Stephen

lee1210
May 30, 2009, 11:03 AM
SRossi beat me to it, but setString didn't work because NSTextField does not respond to this. You should have gotten a compiler warning about this. You should take a look at the docs for NSTextField and NSControl (its parent) so you know what you have to work with.

-Lee

bguy
May 30, 2009, 12:06 PM
Thanks for all the help. I am still having problems, but I think I know what they are, just not how to fix them. When I run the application, no matter what I guess, it always shows up as the correct answer. I know this is not the case since the NSLog tells me what the number is. I assume this means there is a problem with the Compare method I wrote. Here it is:


#import "Guesser.h"


@implementation Guesser
@synthesize Secret, Guess, Highscore, Lowscore, Guesses;

- (int)Compare
{
NSLog(@"Is secret less? %d\tIs guess less? %d",[self Secret] < [self Guess],[self Guess] < [self Secret]);
if ([self Secret] > [self Guess])
return -1;
else if ([self Secret] < [self Guess])
return 1;
else
return 0;
}

@end

SRossi
May 30, 2009, 12:19 PM
I take it that Secret and Guess are functions that return the random number and the number guessed?

Overall the code looks fine.

Stephen

bguy
May 30, 2009, 12:21 PM
I used @property(readwrite) and synthesize like a tutorial told me to do for a different program. That might be the problem if I am getting the functions wrong.

SRossi
May 30, 2009, 12:26 PM
I would also do @property(readwrite, assign);

Try that.

Stephen

bguy
May 30, 2009, 12:48 PM
It is still not comparing correctly.

SRossi
May 30, 2009, 12:56 PM
Do you need to use @property or could you just use:

- (NSString)foo
- (void)setFoo:(NSString)aString

Because thats all that @propert and @synthesize is doing is taking away those methods

bguy
May 30, 2009, 01:07 PM
I don't think it is the problem, I believe I just wrote my code wrong, so I was wondering if people can check it. Besides the header files and the nib file it is all there.

lee1210
May 30, 2009, 03:40 PM
What is the type of Guess and Secret (and why is the leading letter capitalized?). Print their values in Compare (again... capitalized... camelCase (http://en.wikipedia.org/wiki/CamelCase) of the lower variety is "the way"), and see what they are. Print the values of your conditionals and see what they are:
NSLog(@"Is secret less? %d\tIs guess less? %d",[self secret] < [self guess],[self guess] < [self secret]);

-Lee

bguy
May 31, 2009, 07:54 AM
They are both integers. I added the code you suggested and i doesn't show up. I think that means Compare isn't being called. Why is that?

SRossi
May 31, 2009, 08:14 AM
Why not do:

int compareNumber = [self Compare];

Then use the if statement on the compareNumber.

Stephen

JoshDC
May 31, 2009, 08:15 AM
Have you initialized the guesser anywhere? Looks like you haven't...

You'll need to put: guesser = [[Guesser alloc] init] in the init: or awakeFromNib: method of the GusserController.

Just as a point of semantics, methods generally start lower case (e.g. Compare: should really be compare: )

GorillaPaws
May 31, 2009, 08:17 AM
Tripple-check your connections in IB.

bguy
May 31, 2009, 08:53 AM
I added the initialization, but now the compare is saying the secret number is always higher than the guess, no matter what the number is. Is there a fundamental flaw in the program?

JoshDC
May 31, 2009, 09:02 AM
Simply declaring it creates a pointer that points nowhere (0x0 or nil). The alloc method allocates memory for the new object, the init method sets everything to their default values. Both method returns an id (or pointer) that points to the created instance of the object. In Objective-C anything sent to a nil object will return nil (or 0). That's why your compare method was always returning 0.

bguy
May 31, 2009, 09:37 AM
Thanks for the help. I figured out the rest of the mistakes and now it works.