|
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
|

05-18-2016
, 15:41
Re: Help vote
|
#5
|
By saving the current highest current value, you don't have to loop everything in two dimensions. Just compare every entry to the previously stored value.
Easy version:
Code:
new best = 0
for ( new i = 1 ; i < sizeof votes ; i++ ) {
if ( votes[i] > votes[best] ) {
best = i
}
}
// best will contain the player index that won. And you can use votes[best] for the number of votes.
The problem is the player index doesn't change until you rejoin the server and that may get unfair if two players always get the same amount of votes. So then you will have to determine if there are mutliple winners and perhaps randomly select one of them.
Complicated version:
Code:
#include <amxmodx>
public plugin_init() {
register_plugin("Test Plugin 1", "1.0", "[ --{-@ ]");
new votes[33];
new best;
new number;
for ( new i ; i < sizeof votes ; i++ ) {
votes[i] = random(10);
server_print("%d: %d", i, votes[i]);
}
for ( new i = 1 ; i < sizeof votes ; i++ ) {
switch ( clamp(votes[i] - votes[best], -1, 1) ) { // This compares how many more votes the person "i" got than person "best(so far)".
case 0 : number++; // "i" has the same number of votes as the current leader.
case 1 : { // "i" is the new leader.
best = i;
number = 1;
}
// default : { } Nobody cares about a loser.
}
}
server_print("Highest value is %d, which %d people got.", votes[best], number);
}
Another, probably better option is creating an array storing both the player index and the value, using bubble sort on them and looping from the top until you reach a lower value. That is of course if you want multiple results.
Code:
#include <amxmodx>
enum {
PlayerIndex,
Score
}
new ArrayWithPlayers[33][2];
public plugin_init() {
register_plugin("Test Plugin 1", "1.0", "[ --{-@ ]");
server_print("Before sorting:^n");
for ( new i = 1 ; i < sizeof ArrayWithPlayers ; i++ ) {
ArrayWithPlayers[i][PlayerIndex] = i;
ArrayWithPlayers[i][Score] = random(10);
server_print("%2d: PID: %2d, Score: %d", i, ArrayWithPlayers[i][PlayerIndex], ArrayWithPlayers[i][Score]);
}
SortCustom2D(ArrayWithPlayers, sizeof ArrayWithPlayers, "MySortFunc");
server_print("^nAfter sorting:^n");
for ( new i = 0 ; i < sizeof ArrayWithPlayers ; i++ ) {
server_print("%2d: PID: %2d, Score: %d", i, ArrayWithPlayers[i][PlayerIndex], ArrayWithPlayers[i][Score]);
}
}
public MySortFunc(const elem1[], const elem2[])
return clamp(elem2[Score] - elem1[Score], -1, 1); // Clamp just in case. It seems to work without it but I'm not taking that risk.
Code:
Before sorting:
1: PID: 1, Score: 9
2: PID: 2, Score: 5
3: PID: 3, Score: 3
4: PID: 4, Score: 7
5: PID: 5, Score: 4
6: PID: 6, Score: 1
7: PID: 7, Score: 8
8: PID: 8, Score: 6
9: PID: 9, Score: 4
10: PID: 10, Score: 0
11: PID: 11, Score: 4
12: PID: 12, Score: 0
13: PID: 13, Score: 9
14: PID: 14, Score: 2
15: PID: 15, Score: 6
16: PID: 16, Score: 1
17: PID: 17, Score: 4
18: PID: 18, Score: 1
19: PID: 19, Score: 1
20: PID: 20, Score: 2
21: PID: 21, Score: 4
22: PID: 22, Score: 1
23: PID: 23, Score: 0
24: PID: 24, Score: 0
25: PID: 25, Score: 1
26: PID: 26, Score: 1
27: PID: 27, Score: 3
28: PID: 28, Score: 9
29: PID: 29, Score: 5
30: PID: 30, Score: 2
31: PID: 31, Score: 6
32: PID: 32, Score: 0
After sorting:
0: PID: 1, Score: 9
1: PID: 28, Score: 9
2: PID: 13, Score: 9
3: PID: 7, Score: 8
4: PID: 4, Score: 7
5: PID: 15, Score: 6
6: PID: 31, Score: 6
7: PID: 8, Score: 6
8: PID: 2, Score: 5
9: PID: 29, Score: 5
10: PID: 11, Score: 4
11: PID: 9, Score: 4
12: PID: 5, Score: 4
13: PID: 21, Score: 4
14: PID: 17, Score: 4
15: PID: 3, Score: 3
16: PID: 27, Score: 3
17: PID: 14, Score: 2
18: PID: 20, Score: 2
19: PID: 30, Score: 2
20: PID: 19, Score: 1
21: PID: 22, Score: 1
22: PID: 6, Score: 1
23: PID: 18, Score: 1
24: PID: 16, Score: 1
25: PID: 25, Score: 1
26: PID: 26, Score: 1
27: PID: 23, Score: 0
28: PID: 12, Score: 0
29: PID: 10, Score: 0
30: PID: 0, Score: 0
31: PID: 24, Score: 0
32: PID: 32, Score: 0
__________________
Last edited by Black Rose; 05-18-2016 at 16:15.
|
|