Button Looping and me!

Joined
Aug 30, 2011
Messages
4
Reaction score
0
Points
1
Hey guys how is everyone today? I just had a couple questions and wondering if anyone could help.

I been trying to write a Xcode 4 cocoa app that is a guessing game. I was able to complete the game in the console, but now that I am trying to link it to a UI I'm finding it not so easy.

This is the basic Idea, i know its off. I had it working at one point but every time i hit the button it generated a new random number and i can't figure out how to do it outside the button, and how to add 5 tries before calling game over.

Code:
@implementation LoopingAppDelegate

-(void)setStuff:(id)sender{
    NSInteger g = [input];
    NSInteger y;
    NSInteger x = rand() % 100;
    
   
    
}

- (IBAction)run:(id)sender
{
     [y setIntValue:[x intValue]];
    if([g] intValue] == x){
        [results setStringValue:@"YOU ARE CORRECT SIR!!!"];
        return;}
    else if(guess.intValue < y.intValue){
        [results setStringValue:@"Higher \nGuess again!"];}
    else if(guess.intValue > y.intValue){
        [results setStringValue:@"Lower \nGuess again!"];}
    else {
        [results setStringValue:@"What the **** did you type??!?!"];
        }
}
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
Hi, welcome to the forums.

First, you may want to use code tags to make your code easier to read (maintains indenting and doesn't implement smileys)

Second - you have the line: [self release]; - I can't see ANY reason why you would want to do this unless you want to create a crash. If you need to show a different view (or return to a previous view), there are specific ways of handling that.

Third - I'm not sure what you mean by having it generate the random number outside of the button - at what time would you want the random number to be generated? In terms of guesses, you need to keep a counter to see how many guesses were entered and when that counter reached your max guess threshold that it would throw a you lost message.

Fourth - I'm not sure if this program is ultimately meant for OSX or an iOS device. You mentioned Cocoa, which would imply you intend this for OSX (iOS is Cocoa Touch) - if this is for OSX, you may want to ask that this thread get moved to the OSX development thread :)
 
OP
A
Joined
Aug 30, 2011
Messages
4
Reaction score
0
Points
1
1. sorry about the tags thing

2. I thought the release meant it would kick me out of those brackets.

3. When i had the random number generated in the button, it would create a new number every time i clicked the button. So I'm not sure where to put it.

4. I would like this to be for the IOS, I felt that making it in OSX was a little easier and quicker to test.

Im still trying to learn everything and understand objective c, Any help would be appreciated. I figured if someone understood my goal maybe they can show me the right way to be doing it.
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
No worries on the tag thing - a lot of people don't realize their importance at first (that includes me ;) ) - just makes the code sooo much easier to read - at least on this forum - there's another one I go to where it changes the color of the text making it really hard to read :(.

On the release issue,

[blablabla release];

will signify that you want to release the memory and reduce the retain count, if the retain count on an object reaches 0, it will be dealloc'd at which time it will really be gone and anything referencing it will be referencing bad memory and thro a bad exc error or something similar.

When would you like the random number generated? On first entry and after 5 attempts?
 
OP
A
Joined
Aug 30, 2011
Messages
4
Reaction score
0
Points
1
Thanks a ton for the help, and working with me.

For the randomly generated number I would like to generate 1 number per 5 attempts and generate a new number if someone chooses to try again.

I have been taking a lot of tutorials and reading a couple Xcode books, the hardest part to learning is that most of the information was built for older versions of Xcode. I think thats why I was using the release. I noticed in Xcode 4 I can enable garbage clean up, which i think is what you explained.

But now as I learn more I realize I need a lot more code to get this right.

Ill be working on this for a couple days, I would love any input or ideas, or ways to do it easier. Im starting from the beginning so, this may take a while.
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
Well, in terms of garbage collection, it's not really an XCode thing - and it's been available for a while, but it's ONLY for OSX apps, there is no garbage collection on iOS. Plus, what garbage collection is meant for is basically so you can be lazy about making sure you release your objects. Once the system determines that there is nothing holding onto a chunk of memory it gets released - to learn more about garbage collection, see: Loading&#133;

Just remember, it's not available on iOS. The closest thing to garbage collection won't be available until the next iOS release.

I don't have time tonight, but I'll see if I can throw together some sample code about what you are trying to do. it won't be pretty and depending on my time available tomorrow it may be for iOS rather then OSX, but hopefully it may get you in the right direction.
 
OP
A
Joined
Aug 30, 2011
Messages
4
Reaction score
0
Points
1
I appreciate everything you have done to help me understand this stuff. I been working in IOS and learning a lot more about how it all links together. I took an Objective C class and I guess it was based in OSX. I plan on doing all IOS apps in the future. I also noticed that in this version of Xcode it auto adds my releases to viewDidLoad and Dealloc. Thanks a lot! your the best.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top