Need help in creating a memory game

Apprentice
Posts: 16
Joined: 2010.12
Post: #1
Hi, can someone help me with a memory game using objective C on an iPhone game development? The game is like the current Memory Block game available in the App store. It is for my school's project and it will not be published to web.

currently i have this to generate its values and randomize its sequence:


Code:
-(void) selfGenerate:(NSMutableArray *)btnArray{
    NSString *temp;
    if(self.gamestate == gamestateRunning){
        for(int i = 0; i<[btnArray count]; i++){
            int d = rand()%4 +1;
            temp =[btnArray objectAtIndex];
            [btnArray removeObjectAtIndex];
            [btnArray addObject:temp];
        }
---------------------------------------------------------------------
Can I know whether is it the correct way to write the codes this way? and to initialize the method, do I have to put it in:
-(void) viewDidLoad{}
Or do I have to put it somewhere else?
Thanks and appreciate someone could help =)
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
A few things:
  • Your compiler should tell you that NSMutableArray doesn't respond to -objectAtIndex and -removeObjectAtIndex. It does, however, respond to -objectAtIndex: and -removeObjectAtIndex:. Very important distinction.
  • You've added 1 to d. Though it's not actually used as an array index in the code you posted, that seems to be its intention. Arrays are indexed starting at 0, not 1.
  • The way I normally shuffle an array is to iterate through every index in order, pick another random index, and swap the two entries (in NSMutableArray's case, by using -exchangeObjectAtIndex:withObjectAtIndex:). Doing a quick analysis, the method you're using seems to be biased to randomize entries toward the end of the array more than ones at the beginning.
  • As I understand, rand() often has poor randomness in the low bits (which is what you get with %4). You may want to use a more entropic random number generator such as arc4random().
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #3
(Dec 1, 2010 07:44 AM)ThemsAllTook Wrote:  
  • As I understand, rand() often has poor randomness in the low bits (which is what you get with %4). You may want to use a more entropic random number generator such as arc4random().
This of course be fixed by just not using modulo
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
And how exactly do you propose reducing a random number to an given range without using modulo?
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #5
(type)((random / randmax) * range + start)

?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
Going via floating point division? Certainly works, and avoids much of the issue since the high bits are used instead of the low. Though as Seth can attest, even the high bits are not as good as a decent random number generator...
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.12
Post: #7
(Dec 1, 2010 07:44 AM)ThemsAllTook Wrote:  [*]The way I normally shuffle an array is to iterate through every index in order, pick another random index, and swap the two entries (in NSMutableArray's case, by using -exchangeObjectAtIndex:withObjectAtIndex:). Doing a quick analysis, the method you're using seems to be biased to randomize entries toward the end of the array more than ones at the beginning.
[/list]

Sorry but I still not very sure on how to use exchangeObjectAtIndex:withObjectAtIndex. i replaced [btnArray ObjectAtIndex:d] with [btnArray exchangeObjectAtIndex:i withObjectAtIndex:d];
it shows me Void value not ignored as it ought to be.
I understand that the fault comes from the btnArray but I've got no idea on how to resolve the problem.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #8
Void value not ignored as it ought to be is generated when you're trying to assign the return value of a void function to something. Is [btnArray exchangeObjectAtIndex:i withObjectAtIndex:d]; your entire line of code, or are you trying to assign it to something?
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.12
Post: #9
oh. thx! now its workable =) thanks alot=)
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.12
Post: #10
I have another issue now. as I uses buttons as the self generated values to randomize, when i set it as .highlighted = YES;

how do I unhighlighted it when the value is being generated again consecutively?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #11
I'm not sure if I'm reading your question right, but maybe you want button.highlighted = !button.highlighted?
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.12
Post: #12
(Dec 3, 2010 08:04 AM)ThemsAllTook Wrote:  I'm not sure if I'm reading your question right, but maybe you want button.highlighted = !button.highlighted?

hmmm. I mean that its like a blinking effect, if the randomized number is being generated again, it will at least do a blinking. =) sorry for the misleading.
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.08
Post: #13
(Dec 3, 2010 08:04 AM)ThemsAllTook Wrote:  I'm not sure if I'm reading your question right, but maybe you want button.highlighted = !button.highlighted?
me also having this issue , but said way is not working ? what about button.focused ?
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 5,516 Jan 6, 2011 09:10 PM
Last Post: soulesstps
  User input for memory game soulesstps 0 2,640 Jan 2, 2011 09:37 PM
Last Post: soulesstps
  Need help in creating a memory game(2) soulesstps 9 6,254 Dec 13, 2010 07:44 PM
Last Post: soulesstps
  creating a game background problems chrism 6 3,712 Jun 6, 2009 06:58 PM
Last Post: chrism
  Creating an Age-Of-Empires-2-Esque Game for iPhone Simie 5 5,175 Aug 19, 2008 04:35 PM
Last Post: SethWillits