Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
Hi I'm learning Objective C (still). I create my own little projects for me so I can understand Obj C better. I created a little Battle Ship guessing game which is the computer playing against it's self guessing 2 random numbers. If they match the game ends.

I would like to slow down the the command line response to 1 guess every second so I can see the game slowly play out but I don't know how yet. Could one of you direct me to that information or show me how to add it. here is my code so far.

Code:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

	int comA, comB, count;
	
	count = 1;
	
	while(comA != comB)
	{
		NSLog(@"--------");
		comA = arc4random() % 20;
		comB = arc4random() % 20;
		NSLog(@"ComA guessed %i and comB gueesed %i", comA, comB);
		if (comA == comB){
			NSLog(@"Com_A sank your ship in %i moves", count);
			break;
		}
		NSLog(@"--------");
		comB = arc4random() % 20;
		comA = arc4random() % 20;
		NSLog(@"ComB guessed %i and ComA says it's %i", comB, comA);
		if (comB == comA){
			NSLog(@"Com_B sank your ship in %i movies", count);
			break;
		}
		
		count++;
	}
	NSLog(@"End of game_ it took %i rounds to kill you", count);
	
    [pool drain];
    return 0;

Thanks in advance!

-Lars
}
 
Hummm.... I did first assign 2 integers to the variables giving comA = 1; and comB = 2;. After the program was running I wondered if I could delete them and if the program would still work and it did.

Is it good Practice to always assign a variable after I declare them?

Also thanks for the link. I'll read up on it now!

-Lars
 
Hummm.... I did first assign 2 integers to the variables giving comA = 1; and comB = 2;. After the program was running I wondered if I could delete them and if the program would still work and it did.
Just because something works doesn't mean it's correct.
Lots of things will work for the wrong reasons, or for unpredictable reasons.


Is it good Practice to always assign a variable after I declare them?
It is good practice to assign a value to an automatic variable before attempting to read its value. If you neglect this, then the variable's value may be indeterminate, depending on whether the variable is an automatic variable, an instance variable, or a static variable. "Indeterminate" means it could be anything, including an invalid value for the variable's type.

Your code used automatic variables, which are indeterminate when defined. Instance variables are zeroed upon return from alloc. Uninitialized static variables are zeroed.
 
sleep() is the easiest, NSTimer class is more robust and since you're learning Objective-C would probably be better to learn about.
 
sleep() is the easiest, NSTimer class is more robust and since you're learning Objective-C would probably be better to learn about.

NSTimer will require a run-loop, which he doesn't have, and probably wouldn't want in this simple case. Making it work with NSTimer is over-complex for this.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.