Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Mac OS X Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-27-2011, 04:58 PM   #1 (permalink)
Jez
Registered Member
 
Join Date: Aug 2011
Posts: 10
Jez is on a distinguished road
Default Where is the logic leak?

Hi guys,

i am working right now on a little game. To figure out who won at the end I am using a couple of loops to find out who got the highest score. The problem is, if i got 2 or more winners.

Here is the code:
Code:
int maxValue = 0;
                for (NSNumber *number in scores)
                {
                    int currentValue = [number intValue];
                
                    if (currentValue > maxValue)
                    maxValue = currentValue;
                }
                NSLog(@"%d", maxValue);
                //END
            
            NSMutableArray *winners = [[NSMutableArray alloc] init];
            int playerNum;
            NSUInteger winnerIndex = 0;
            for (int x = 0; x < 4; x++) {
                if ([scores containsObject:[NSNumber numberWithInt:maxValue ]]) {
                    winnerIndex = [scores indexOfObject:[NSNumber numberWithInt:maxValue]];
                    [scores removeObjectAtIndex:winnerIndex];
                    playerNum = winnerIndex + 1;
                    NSLog(@"%d", playerNum);
                    [winners addObject:[NSString stringWithFormat:@"Player %d", playerNum]];
                }
                
            }
            for (NSString *winWin in winners){
                NSLog(@"%@", winWin);
            }
            
            [winners release];
If just 1 person wins, it works just fine, but if its a draw I get player 1 twice in the output. It has to be obvious but I can't see it...
Jez is offline   Reply With Quote
Old 12-27-2011, 06:10 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 158
cribasoft is on a distinguished road
Default

Quote:
Originally Posted by Jez View Post
Hi guys,

i am working right now on a little game. To figure out who won at the end I am using a couple of loops to find out who got the highest score. The problem is, if i got 2 or more winners.

Here is the code:
Code:
int maxValue = 0;
                for (NSNumber *number in scores)
                {
                    int currentValue = [number intValue];
                
                    if (currentValue > maxValue)
                    maxValue = currentValue;
                }
                NSLog(@"%d", maxValue);
                //END
            
            NSMutableArray *winners = [[NSMutableArray alloc] init];
            int playerNum;
            NSUInteger winnerIndex = 0;
            for (int x = 0; x < 4; x++) {
                if ([scores containsObject:[NSNumber numberWithInt:maxValue ]]) {
                    winnerIndex = [scores indexOfObject:[NSNumber numberWithInt:maxValue]];
                    [scores removeObjectAtIndex:winnerIndex];
                    playerNum = winnerIndex + 1;
                    NSLog(@"%d", playerNum);
                    [winners addObject:[NSString stringWithFormat:@"Player %d", playerNum]];
                }
                
            }
            for (NSString *winWin in winners){
                NSLog(@"%@", winWin);
            }
            
            [winners release];
If just 1 person wins, it works just fine, but if its a draw I get player 1 twice in the output. It has to be obvious but I can't see it...
That's a seriously complicated way to do something really simple. So, wow, right there

Anyway, your problem is that you are changing your "scores" array and at the same time as you try to use the indexes into it to determine the player number. For example, say you have two scores, both "6."

So, maxValue = 6. index 0 of scores is equal to 6. So, you put "player 1" into your winners array. BUT, you also remove index 0. Now "scores" has only one score in it, 6, because you removed the first 6. Index 0 of scores is equal to 6. So, you put "player 1" into your winners array again, and now "scores" is empty.

I don't know why you are looping four times using "x" and not using "x" in the loop body, or why you are removing from "scores" while looking in it, but I'm pretty sure that is the cause of your unexpected output.

I would just loop scores and record each player index that has the same value as maxValue. Use an indexed loop (i=0;i<[scores count];i++) and in the body just check [scores objectAtIndex:i] to see if it equals maxValue. If it does, add "Player i+1" to your winners list and move on. If you did that in my example above, when you're done Winners will have "Player 1" and "Player 2." If you need to clear "scores" just do [scores removeAllObjects]
cribasoft is offline   Reply With Quote
Old 12-28-2011, 04:54 AM   #3 (permalink)
Jez
Registered Member
 
Join Date: Aug 2011
Posts: 10
Jez is on a distinguished road
Default

Ahhh. Of Course, if I remove an object I as well remove the index and the other objects go 1 up.

And of course. This is much simpler:
Code:
for (int x = 0; x < [scores count]; x++) {
                if ([scores objectAtIndex:x]== [NSNumber numberWithInt:maxValue]) {
                   [winners addObject:[NSString stringWithFormat:@"Player %d ", x + 1]]; 
                }
                
            }
Thanks cribasoft!!!
Jez is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 376
10 members and 366 guests
Creativ, Emy, eski, husthlj, illogical, LegionMD, LunarMoon, padsoftware, stanny, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,677
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, husthlj
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:18 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0