Link to home
Start Free TrialLog in
Avatar of Type25
Type25

asked on

Which variable is ranked higher

Here's my situation, i have four players, all four played are asked a series of questions and their score out of 3 is recorded as well as the time it took them to answer the questions.


int p1, p2, p3, p4;
int p1Time, p2Time, p3Time, p4Time;

// fill with example data
p1 = 2, p2 = 1, p3 = 2, p4 = 2;
p1Time = 4, p2Time = 10, p3Time = 6, p4Time = 4;

Basically i need to fill a new variable string with the name of the winner, for the moment just p1,p2,p3 or p4 will be fine.

The winner is the one who answered the most questions, if there is a tie then it should then work out who the winner is against how fast they answered (lower the better)

I started with the code below but then i got kind of stuck on the best way to work out the winner based on time taken.

Thanks!
if (p1 > p2 && p1 > p3 && p1 > p4)
	{
		winner = [NSString stringWithFormat:@"p1"];
	}
	else if (p2 > p1 && p2 > p3 && p2 > p4)
	{
		winner = [NSString stringWithFormat:@"p2"];
	}
	else if (p3 > p1 && p3 > p2 && p3 > p4)
	{
		winner = [NSString stringWithFormat:@"p3"];
	}
	else if (p4 > p1 && p4 > p2 && p4 > p3)
	{
		winner = [NSString stringWithFormat:@"p4"];
	}
	else
	{
		// there's a draw
		 
		
	}

Open in new window

Avatar of Type25
Type25

ASKER

They aren't that unrelated. Either language is fine as the principal will be the same.

Thanks
There would be tie in between P1 and P4 as their time is same too.
Your code looks very strange.
I think, the simplest way (at least to understand) is to make a table with your players. I the first column put the player's identifiers, and in the second - their ranks and sort the table (second column). The player who answers with a maximal number of answers is the winner.
If there few players with the same number, make another table (or use the the first one) and add the time information Sort the table.
Avatar of Type25

ASKER

Ok the sample data wasn't the best example, let's just presume the time taken will always be different.

I can't use a table or any type of sql (easily).

I could use an array of dictionaries i guess.
ok. so you can use other tricks to get the winner faster (if it makes sense). I do understand that the winner has more answers and less time.
For example you can replace the time values with an index (koefficient) - maximal value is 100, minimal is 1. And add these values to the player's answers. But you may get that the winner answered on 9 questions in 10 seconds but will have a player with 10 answers in 20 seconds. Who's the winner in this case?
 
Avatar of Type25

ASKER

The outright winner is always the person who has answered the most correct answers, so the player with 10 questions is the winner.

Time taken only comes into play if there is a tie.

My original logic works to find out the winner, but then i need to be more clever to work out the winner from the time taken as it, more than likely, won't include all four players, so there's no good running the same logic for that as player 3 & 4 for example might have answered less questions than 1 & 2 that have tied.

If that makes any sense.

So i'm going need some kind of big if statement i think.


Avatar of Type25

ASKER

See it get's quite tricky when the results are like this:

Player1 = 2
Player2 = 2

Player3 = 3
Player4 = 3

So the original logic i posted will work out it's a tie, the next bit of logic needs to workout who's tied and then the time taken between ONLY those highest tied scores (so in the above scenario, only player3 &4)

Avatar of evilrix
Why don't you just divide the score by time taken?

So if up1 scores 8 in 10 seconds and p2 scores 8 in 5 seconds...

p1: 8 / 10 = 0.8
p2: 8 / 5 = 1.6    *winner*

So p2 wins
So you need to sort your players - more answers in the top of the table. the subindex will be the time, so if few players have the same answers, the one with less time should be on a higher position.
it like sort by ANSWER (bigger number on the top) + TIME (smaller number on top) - whatever, I don't remember the SQL to recommend
Avatar of Type25

ASKER

i can't use a sql table
>> i can't use a sql table
You really don't need to make it this complex. You just need to work out who answered the most questions per second. :)
I think evilrix has answered.
I don't know why :) but I can explain my point in this way:

struct Player
{
   int questions;
   int time;
}
int Compare(Player* p1, Player* p2)
{
  if (p1->questions > p2.questions)
        return 1;
  if (p1->questions < p2.questions)
        return -1;
  if (p1->time < p2->time)
        return 1;
  if (p1->time > p2->time)
        return -1;
       
   return 0;
}
 
Avatar of Type25

ASKER

evilrix, i think the pennies dropped :)


Avatar of Type25

ASKER

evilrix, hang on, what about this:

p1: 2 questions in 15 seconds   (2/15=0.13)
p2: 3 questions in 60 seconds   (3/60=0.05)

Your logic says player 1 won when it was player 2 as they answered more questions
>> Your logic says player 1 won when it was player 2 as they answered more questions

You only apply the questions per second logic if 2 or more players scored the same. It only makes sense if they have the same score.
Avatar of Type25

ASKER

ok so how do i now which scores are highest tied?
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just to note, it doesn't have to be a stack -- any container that can easily add and remove user IDs will do... I'd just use a stack cos it's the most convenient I think :)
Avatar of Type25

ASKER

Really wasn't that hard was it :)

Thanks evilrix
	NSMutableArray *arr = [[NSMutableArray alloc] init];
	
	int highestScore = 0;
	for (int i=0; i<[players count];i++)
	{
	
		PlayerProfile *p = [players objectAtIndex:i];
		if ([p.currentRoundScore intValue] > highestScore)
		{
			[arr removeAllObjects];
			[arr addObject:[players objectAtIndex:i]];
			highestScore = [p.currentRoundScore intValue];
		}
		else if ([p.currentRoundScore intValue] == highestScore)
		{
			[arr addObject:[players objectAtIndex:i]];
		}
		
	}
	
	[arr release];

Open in new window

>> Thanks evilrix
Anytime