Cocoa Number Guesser Help

Discussion in 'Mac Programming' started by bguy, May 30, 2009.

  1. bguy
    Expand Collapse
    macrumors newbie

    Joined:
    May 30, 2009
    #1
    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?

    Code:
    #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
    
     
  2. SRossi
    Expand Collapse
    macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #2
    I'm no expert at objective-c but with the compare method shouldn't it actually be ...

    Code:
    [guesser Compare:aString]
    I am sure someone will disagree but im sure you need something to compare it with.

    Stephen
     
  3. bguy
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    May 30, 2009
    #3
    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.
     
  4. SRossi
    Expand Collapse
    macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #4
    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
     
  5. bguy
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    May 30, 2009
    #5
    How do you do that?
     
  6. SRossi
    Expand Collapse
    macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #6
    Code:
    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
     
  7. bguy
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    May 30, 2009
    #7
    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.
     
  8. SRossi
    Expand Collapse
    macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #8
    Right to create a random number you do:

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

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

    Code:
    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
     
  9. bguy
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    May 30, 2009
    #9
    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
     
  10. SRossi
    Expand Collapse
    macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #10
    To set the string of a text field you need to do

    Code:
    [textField setStringValue:@"Hello"];
    Notice setStringValue and the @ symbol before the text brackets

    Stephen
     
  11. lee1210
    Expand Collapse
    macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #11
    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
     
  12. bguy
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    May 30, 2009
    #12
    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:
    Code:
    
    #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
    
    
     
  13. SRossi
    Expand Collapse
    macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #13
    I take it that Secret and Guess are functions that return the random number and the number guessed?

    Overall the code looks fine.

    Stephen
     
  14. bguy
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    May 30, 2009
    #14
    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.
     
  15. SRossi
    Expand Collapse
    macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #15
    I would also do @property(readwrite, assign);

    Try that.

    Stephen
     
  16. bguy
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    May 30, 2009
  17. SRossi
    Expand Collapse
    macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #17
    Do you need to use @property or could you just use:

    Code:
    - (NSString)foo
    - (void)setFoo:(NSString)aString
    Because thats all that @propert and @synthesize is doing is taking away those methods
     
  18. bguy
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    May 30, 2009
    #18
    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.
     
  19. lee1210
    Expand Collapse
    macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #19
    What is the type of Guess and Secret (and why is the leading letter capitalized?). Print their values in Compare (again... capitalized... camelCase of the lower variety is "the way"), and see what they are. Print the values of your conditionals and see what they are:
    Code:
    NSLog(@"Is secret less? %d\tIs guess less? %d",[self secret] < [self guess],[self guess] < [self secret]);
    -Lee
     
  20. bguy
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    May 30, 2009
    #20
    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?
     
  21. SRossi
    Expand Collapse
    macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #21
    Why not do:

    Code:
    int compareNumber = [self Compare];
    Then use the if statement on the compareNumber.

    Stephen
     
  22. JoshDC
    Expand Collapse
    macrumors regular

    Joined:
    Apr 8, 2009
    #22
    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: )
     
  23. GorillaPaws
    Expand Collapse
    macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
  24. bguy
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    May 30, 2009
    #24
    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?
     
  25. JoshDC
    Expand Collapse
    macrumors regular

    Joined:
    Apr 8, 2009
    #25
    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.
     

Share This Page