diff --git a/Mage/src/mage/game/tournament/TournamentImpl.java b/Mage/src/mage/game/tournament/TournamentImpl.java index e326882b29..a578a68110 100644 --- a/Mage/src/mage/game/tournament/TournamentImpl.java +++ b/Mage/src/mage/game/tournament/TournamentImpl.java @@ -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 roundPlayers = getActivePlayers(); + + // search the player with a bye last round + List 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 roundPlayers, List 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 iterator = playerWithByes.iterator(); + nextPlayer = iterator.next(); + iterator.remove(); + roundPlayers.remove(nextPlayer); + } + return nextPlayer; + } + + private List getTournamentPlayersWithBye(List roundPlayers) { + List 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); diff --git a/Mage/src/mage/game/tournament/TournamentSwiss.java b/Mage/src/mage/game/tournament/TournamentSwiss.java index 9f6c970bd4..6855e57eb0 100644 --- a/Mage/src/mage/game/tournament/TournamentSwiss.java +++ b/Mage/src/mage/game/tournament/TournamentSwiss.java @@ -46,8 +46,8 @@ public abstract class TournamentSwiss extends TournamentImpl { } @Override - protected void runTournament() { - for (Map.Entry entry: players.entrySet()) { + protected void runTournament() { + for (Map.Entry entry : players.entrySet()) { if (entry.getValue().getPlayer().autoLoseGame()) { entry.getValue().setEliminated(); entry.getValue().setResults("Auto Eliminated"); @@ -57,7 +57,7 @@ public abstract class TournamentSwiss extends TournamentImpl { while (this.getActivePlayers().size() > 1 && this.getNumberRounds() > this.getRounds().size()) { // check if some player got killed / disconnected meanwhile and update their state tableEventSource.fireTableEvent(TableEvent.EventType.CHECK_STATE_PLAYERS); - // Swiss pairing + // Swiss pairing Round round = createRoundSwiss(); playRound(round); } @@ -70,8 +70,9 @@ public abstract class TournamentSwiss extends TournamentImpl { List roundPlayers = getActivePlayers(); // sort players by tournament points Collections.sort(roundPlayers, new Comparator() { - @Override public int compare(TournamentPlayer p1, TournamentPlayer p2) { - return p2.getPoints() - p1.getPoints(); + @Override + public int compare(TournamentPlayer p1, TournamentPlayer p2) { + return p2.getPoints() - p1.getPoints(); } }); @@ -80,9 +81,9 @@ public abstract class TournamentSwiss extends TournamentImpl { TournamentPlayer player1 = roundPlayers.get(0); roundPlayers.remove(0); TournamentPlayer playerForPossibleSecondPairing = null; - for (TournamentPlayer player2: roundPlayers) { + 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; } @@ -126,7 +127,7 @@ public abstract class TournamentSwiss extends TournamentImpl { protected boolean alreadyPaired(TournamentPlayer player1, TournamentPlayer player2) { for (Round round : rounds) { - for (TournamentPairing pairing: round.getPairs()) { + for (TournamentPairing pairing : round.getPairs()) { if (pairing.getPlayer1().equals(player1) || pairing.getPlayer2().equals(player1)) { if (pairing.getPlayer1().equals(player2) || pairing.getPlayer2().equals(player2)) { return true;