Need help in creating a memory game(2)

Apprentice
Posts: 16
Joined: 2010.12
Post: #1
Hi all, for the memory game that I've previously asked for help, I am having some other problems in getting the buttons to be randomize in the correct way.
Code:
-(IBAction) btnClick{
        if(gamestate == gamestateRunning){
            NSUInteger count = [pattern count];
            for(NSUInteger i=0; i<count; i++){
            //int n = count-i;
            
                int d = (arc4random()% count) +i;
                if(d==1){
                    [self performSelector:@selector(delayEventRED) withObject:nil afterDelay:2.0f];
                    [self performSelector:@selector(releaseRed) withObject:nil afterDelay:0.01f];
                }
                else if(d==2){
                    [self performSelector:@selector(delayEventBLUE) withObject:nil afterDelay:2.0f];
                    [self performSelector:@selector(releaseBlue) withObject:nil afterDelay:0.01f];
                }
                else if(d==3){
                    [self performSelector:@selector(delayEventGREEN) withObject:nil afterDelay:2.0f];
                    [self performSelector:@selector(releaseGreen) withObject:nil afterDelay:0.01f];
                }  
                else if(d==4){
                    [self performSelector:@selector(delayEventYELLOW) withObject:nil afterDelay:2.0f];
                    [self performSelector:@selector(releaseYellow) withObject:nil afterDelay:0.01f];
                }
            }
        }
    
}
with this, the buttons are able to randomize and highlight and unhighlight by its own, but for now, it gave me randomize in a fast way. Can I know how to control the randomize speed, as well as to limit how many times it is to be randomize?

Moderator: Please use code tags where appropriate
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #2
1) just doing int d = (arc4random()% count) is sufficient for randomization, in doing so you would check 0-count.
2) code isn't very robust. it will lead to unpredictable results. If you truly only have 4 options, then: int d = (arc4random()% 4) and then use a switch, 0-3.

now all these are asides from your question: what do you mean by "randomize speed"?
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.12
Post: #3
(Dec 10, 2010 11:34 AM)skyhawk Wrote:  \.
2) code isn't very robust. it will lead to unpredictable results. If you truly only have 4 options, then: int d = (arc4random()% 4) and then use a switch, 0-3.

now all these are asides from your question: what do you mean by "randomize speed"?

Ya, I will need to have 4 options. But will need to have 4 options at the first stage and adding 1 more in subsequent stages.

Now its speed is like haywire as I cannot get the speed of the options right.
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #4
(Dec 10, 2010 12:12 PM)soulesstps Wrote:  Now its speed is like haywire as I cannot get the speed of the options right.

This really doesn't give us the information we'd need to help you. What effect are you seeing, and what effect are you trying to achieve?
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.12
Post: #5
(Dec 10, 2010 01:11 PM)ThemsAllTook Wrote:  This really doesn't give us the information we'd need to help you. What effect are you seeing, and what effect are you trying to achieve?

Erm. what I was trying to say is, the buttons that are being randomize did not highlight 1 by 1.
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #6
That's because you are telling them all to highlight after a delay of 2 seconds. To make them flash at different times you will need to change the delay. Try using i as the delay time.
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.12
Post: #7
(Dec 12, 2010 06:40 AM)backslash Wrote:  That's because you are telling them all to highlight after a delay of 2 seconds. To make them flash at different times you will need to change the delay. Try using i as the delay time.

it is still the same as i is still considered an integer.
i tried using NSTimer to slow down the random speed but it still went random highlighting of several buttons at a time after 10-13 "self-generated" highlights.
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #8
(Dec 13, 2010 12:00 AM)soulesstps Wrote:  it is still the same as i is still considered an integer.

OK, use (float)i.
My point is that if you give the same delay to all the buttons, they will all flash at the same time (after the same delay). You need to give a different delay time to each button. I suggested using i because it changes as you iterate through your buttons. performSelector:withObject:afterDelay doesn't block the thread, so you are sending these messages to all the buttons at almost exactly the same time.
I've also just noticed that, for the same reason, you are unhighlighting all your buttons 1.99 seconds before you highlight them, which will presumably make them all light up and stay lit. The delay for the "turn back off" function needs to be "delay until it lights up"+"time it is to stay lit for".
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #9
Basically, change this:
Code:
[self performSelector:@selector(delayEventRED) withObject:nil afterDelay:2.0f];
[self performSelector:@selector(releaseRed) withObject:nil afterDelay:0.01f];

To this:
Code:
[self performSelector:@selector(delayEventRED) withObject:nil afterDelay:2.0f*i];
[self performSelector:@selector(releaseRed) withObject:nil afterDelay:2.0f+2*i];

And similar changes to the other lines.
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.12
Post: #10
(Dec 13, 2010 05:56 PM)PowerMacX Wrote:  Basically, change this:
Code:
[self performSelector:@selector(delayEventRED) withObject:nil afterDelay:2.0f];
[self performSelector:@selector(releaseRed) withObject:nil afterDelay:0.01f];

To this:
Code:
[self performSelector:@selector(delayEventRED) withObject:nil afterDelay:2.0f*i];
[self performSelector:@selector(releaseRed) withObject:nil afterDelay:2.0f+2*i];

And similar changes to the other lines.

hmm. the result shows is still the same. =((
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Need help in the memory game soulesstps 7 6,071 Jan 6, 2011 09:10 PM
Last Post: soulesstps
  User input for memory game soulesstps 0 2,902 Jan 2, 2011 09:37 PM
Last Post: soulesstps
  Need help in creating a memory game soulesstps 12 8,778 Dec 4, 2010 11:58 AM
Last Post: shazi
  creating a game background problems chrism 6 3,950 Jun 6, 2009 06:58 PM
Last Post: chrism
  Creating an Age-Of-Empires-2-Esque Game for iPhone Simie 5 5,525 Aug 19, 2008 04:35 PM
Last Post: SethWillits