A few Questions...

Discussion in 'iOS Programming' started by iphonedevelop18, Mar 30, 2009.

  1. iphonedevelop18
    Expand Collapse
    macrumors member

    Joined:
    Feb 26, 2009
    #1
    First of all I am making a tic tac toe game.

    1. how can a set an int to a random number, ie.
    Code:
    difficulty = 5;
    choice = (random() % difficulty) + 1;
    if (choice != 2){
    } 
    
    it will stay the same throughout as i tried to set difficulty to 100 and it would stay the same.

    2. How can I delay the reaction of the computer.

    Thanks!
     
  2. mccannmarc
    Expand Collapse
    macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #2
    I will answer your first question but I don't quite get what you mean by the second one

    The reason your random number will be the same is because you are not seeding the random number off of anything. if you put the following line before your code for generating the random number you will find it will start working. Try this:-

    Code:
    difficulty = 5;
    srandom(time(NULL));
    choice = (random() % difficulty) + 1;
    if (choice != 2){
    } 
    
     
  3. jnic
    Expand Collapse
    macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
  4. iphonedevelop18
    Expand Collapse
    thread starter macrumors member

    Joined:
    Feb 26, 2009
    #4
    mccannmarc

    Yea I already seeded it in the app delegate under didfinish launching or whatever. So I don't think I have to seed it again do I?
     
  5. CocoaPuffs
    Expand Collapse
    macrumors 68010

    Joined:
    Aug 23, 2008
    #5

    Code:
    - (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
     

Share This Page