java - Logically sorted tournament fixtures -
i have upcoming fifa tournament , i've written program prints out possible matchup's. problem is not logically sorted, meaning players have play 5-6 sequential games while other have wait 6 games. following result:
player 1 - player 2 player 3 - player 4 player 5 - player 6 player 1 - player 3 player 2 - player 4
and on. have @ moment:
public class fifa { public static void main(string[] args) { string[] players= {"jens", "dane", "keppens", "roel", "john", "onslo", "jonasdb", "bellon", "sander"}; string[] players2 = {"jens", "dane", "keppens", "roel", "john", "onslo", "jonasdb", "bellon", "sander"}; multimap<string, string> fixtures = linkedhashmultimap.create(); for(int = 0; < players.length; i++){ (int j = 0; j < players.length; j++){ if(!players[i].equals(players2[j])) { if(!fixtures.containskey(players2[j])) fixtures.put(players[i], players2[j]); } } } for(map.entry map : fixtures.entries()){ string key = map.getkey().tostring(); object value = map.getvalue(); system.out.println(key + " - " + value); }
but here prints out:
jens - dane jens - keppens jens - roel jens - john jens - onslo jens - jonasdb jens - bellon jens - sander dane - keppens dane - roel dane - john dane - onslo dane - jonasdb dane - bellon dane - sander keppens - roel keppens - john keppens - onslo keppens - jonasdb keppens - bellon keppens - sander roel - john roel - onslo roel - jonasdb roel - bellon roel - sander john - onslo john - jonasdb john - bellon john - sander onslo - jonasdb onslo - bellon onslo - sander jonasdb - bellon jonasdb - sander bellon - sander
i used multimap because needed multiple keys same value.
a simple approach loop on distances, first output match-ups of distance 1, 2, 3, etc.
the basic version of this:
for(int dist = 1; dist < players.length; dist++) for(int = 0; + dist < players.length; i++) system.out.println(players[i] + " - " + players[i+dist]);
this give match-ups in following order: (grouped distance brevity)
0 - 1, 1 - 2, 2 - 3, 3 - 4, 4 - 5, 5 - 6, 0 - 2, 1 - 3, 2 - 4, 3 - 5, 4 - 6, 0 - 3, 1 - 4, 2 - 5, 3 - 6, 0 - 4, 1 - 5, 2 - 6, 0 - 5, 1 - 6, 0 - 6,
if want avoid scenario in first line plays 2 games in row, can separate out , split odd , numbers:
for(int = 0; < players.length-1; i+=2) system.out.println(players[i] + " - " + players[i+1]); for(int = 1; < players.length-1; i+=2) system.out.println(players[i] + " - " + players[i+1]); for(int dist = 2; dist < players.length; dist++) for(int = 0; + dist < players.length; i++) system.out.println(players[i] + " - " + players[i+dist]);
that gives match-ups in order:
0 - 1, 2 - 3, 4 - 5, 1 - 2, 3 - 4, 5 - 6, 0 - 2, 1 - 3, 2 - 4, 3 - 5, 4 - 6, 0 - 3, 1 - 4, 2 - 5, 3 - 6, 0 - 4, 1 - 5, 2 - 6, 0 - 5, 1 - 6, 0 - 6,
a variation on wrapping around , looping on half distance (with special case avoid distance = length/2
line duplicating match-ups even-sized arrays).
for(int = 0; < players.length; i+=2) system.out.println(players[i] + " - " + players[(i+1)%players.length]); for(int = 1; < players.length; i+=2) system.out.println(players[i] + " - " + players[(i+1)%players.length]); for(int dist = 2; dist < (players.length+1)/2; dist++) for(int = 0; < players.length; i++) system.out.println(players[i] + " - " + players[(i+dist)%players.length]); if (players.length % 2 == 0) for(int = 0; < players.length/2; i++) system.out.println(players[i] + " - " + players[i+players.length/2]);
the match-ups this:
0 - 1, 2 - 3, 4 - 5, 6 - 0, 1 - 2, 3 - 4, 5 - 6, 0 - 2, 1 - 3, 2 - 4, 3 - 5, 4 - 6, 5 - 0, 6 - 1, 0 - 3, 1 - 4, 2 - 5, 3 - 6, 4 - 0, 5 - 1, 6 - 2,
Comments
Post a Comment