* Fixed that players during elemination tournaments could get multiple byes in a row.

This commit is contained in:
LevelX2 2015-10-11 23:55:45 +02:00
parent 01a14a2665
commit a54843fd26
2 changed files with 48 additions and 14 deletions

View file

@ -32,6 +32,7 @@ import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
@ -187,15 +188,16 @@ public abstract class TournamentImpl implements Tournament {
Round round = new Round(rounds.size() + 1, this);
rounds.add(round);
List<TournamentPlayer> roundPlayers = getActivePlayers();
// search the player with a bye last round
List<TournamentPlayer> playerWithByes = getTournamentPlayersWithBye(roundPlayers);
while (roundPlayers.size() > 1) {
int i = rnd.nextInt(roundPlayers.size());
TournamentPlayer player1 = roundPlayers.get(i);
roundPlayers.remove(i);
i = rnd.nextInt(roundPlayers.size());
TournamentPlayer player2 = roundPlayers.get(i);
roundPlayers.remove(i);
TournamentPlayer player1 = getNextAvailablePlayer(roundPlayers, playerWithByes);
TournamentPlayer player2 = getNextAvailablePlayer(roundPlayers, playerWithByes);
round.addPairing(new TournamentPairing(player1, player2));
}
if (roundPlayers.size() > 0) {
// player free round - add to bye players of this round
TournamentPlayer player1 = roundPlayers.get(0);
@ -207,6 +209,37 @@ public abstract class TournamentImpl implements Tournament {
return round;
}
private TournamentPlayer getNextAvailablePlayer(List<TournamentPlayer> roundPlayers, List<TournamentPlayer> playerWithByes) {
TournamentPlayer nextPlayer;
if (playerWithByes.isEmpty()) {
int i = rnd.nextInt(roundPlayers.size());
nextPlayer = roundPlayers.get(i);
roundPlayers.remove(i);
} else { // prefer players with byes to pair
Iterator<TournamentPlayer> iterator = playerWithByes.iterator();
nextPlayer = iterator.next();
iterator.remove();
roundPlayers.remove(nextPlayer);
}
return nextPlayer;
}
private List<TournamentPlayer> getTournamentPlayersWithBye(List<TournamentPlayer> roundPlayers) {
List<TournamentPlayer> playersWithBye = new ArrayList<>();
if (rounds.size() > 1) {
for (int i = rounds.size() - 2; i >= 0; i--) {
Round oldRound = rounds.get(i);
if (oldRound != null && !oldRound.getPlayerByes().isEmpty()) {
TournamentPlayer tournamentPlayerWithBye = oldRound.getPlayerByes().iterator().next();
if (roundPlayers.contains(tournamentPlayerWithBye)) {
playersWithBye.add(tournamentPlayerWithBye);
}
}
}
}
return playersWithBye;
}
protected void playRound(Round round) {
for (TournamentPairing pair : round.getPairs()) {
playMatch(pair);

View file

@ -70,7 +70,8 @@ public abstract class TournamentSwiss extends TournamentImpl {
List<TournamentPlayer> roundPlayers = getActivePlayers();
// sort players by tournament points
Collections.sort(roundPlayers, new Comparator<TournamentPlayer>() {
@Override public int compare(TournamentPlayer p1, TournamentPlayer p2) {
@Override
public int compare(TournamentPlayer p1, TournamentPlayer p2) {
return p2.getPoints() - p1.getPoints();
}
@ -82,7 +83,7 @@ public abstract class TournamentSwiss extends TournamentImpl {
TournamentPlayer playerForPossibleSecondPairing = null;
for (TournamentPlayer player2 : roundPlayers) {
if (alreadyPaired(player1, player2)) {
// if laready paired but equal ponts -> remember if second pairing is needed
// if already paired but equal points -> remember if second pairing is needed
if (playerForPossibleSecondPairing == null) {
playerForPossibleSecondPairing = player2;
}