Need help in creating a memory game
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:
---------------------------------------------------------------------
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 =)
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 =)
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().
And how exactly do you propose reducing a random number to an given range without using modulo?
(type)((random / randmax) * range + start)
?
?
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...
(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.
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?
oh. thx! now its workable =) thanks alot=)
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?
how do I unhighlighted it when the value is being generated again consecutively?
I'm not sure if I'm reading your question right, but maybe you want button.highlighted = !button.highlighted?
(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.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Need help in the memory game | soulesstps | 7 | 4,964 |
Jan 6, 2011 09:10 PM Last Post: soulesstps |
|
| User input for memory game | soulesstps | 0 | 2,376 |
Jan 2, 2011 09:37 PM Last Post: soulesstps |
|
| Need help in creating a memory game(2) | soulesstps | 9 | 5,530 |
Dec 13, 2010 07:44 PM Last Post: soulesstps |
|
| creating a game background problems | chrism | 6 | 3,466 |
Jun 6, 2009 06:58 PM Last Post: chrism |
|
| Creating an Age-Of-Empires-2-Esque Game for iPhone | Simie | 5 | 4,712 |
Aug 19, 2008 04:35 PM Last Post: SethWillits |
|

