Adding a delay to the command line responce

Discussion in 'Mac Programming' started by larswik, Aug 15, 2010.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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
    }
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    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
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Just because something works doesn't mean it's correct.
    Lots of things will work for the wrong reasons, or for unpredictable reasons.


    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.
     
  5. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #5
    sleep() is the easiest, NSTimer class is more robust and since you're learning Objective-C would probably be better to learn about.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    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.
     

Share This Page