Search in sources :

Example 1 with TournamentPairing

use of mage.game.tournament.TournamentPairing in project mage by magefree.

the class SwissPairingMinimalWeightMatchingTest method PlayerWithByeLeftTournament.

@Test
public void PlayerWithByeLeftTournament() {
    // 1 > 2
    // 3 > 4
    // 5
    // 5 left the tournament
    TournamentPlayer player1 = new TournamentPlayer(new PlayerStub(), null);
    TournamentPlayer player2 = new TournamentPlayer(new PlayerStub(), null);
    TournamentPlayer player3 = new TournamentPlayer(new PlayerStub(), null);
    TournamentPlayer player4 = new TournamentPlayer(new PlayerStub(), null);
    TournamentPlayer player5 = new TournamentPlayer(new PlayerStub(), null);
    List<TournamentPlayer> players = new ArrayList<>();
    // players.add(player5); -- player 5 is not active
    players.add(player4);
    players.add(player2);
    players.add(player3);
    players.add(player1);
    player1.setPoints(3);
    player2.setPoints(0);
    player3.setPoints(3);
    player4.setPoints(0);
    player5.setPoints(3);
    List<Round> rounds = new ArrayList<>();
    // first round
    Round round = new Round(1, new TournamentStub());
    TournamentPairing pair1 = new TournamentPairing(player1, player2);
    round.addPairing(pair1);
    TournamentPairing pair2 = new TournamentPairing(player3, player4);
    round.addPairing(pair2);
    round.getPlayerByes().add(player5);
    rounds.add(round);
    SwissPairingMinimalWeightMatching swissPairing = new SwissPairingMinimalWeightMatching(players, rounds, false);
    RoundPairings roundPairings = swissPairing.getRoundPairings();
    Assert.assertEquals(2, roundPairings.getPairings().size());
    Assert.assertEquals(0, roundPairings.getPlayerByes().size());
    CheckPair(roundPairings.getPairings(), player1, player3);
    CheckPair(roundPairings.getPairings(), player2, player4);
    Assert.assertEquals(0, roundPairings.getPlayerByes().size());
}
Also used : PlayerStub(org.mage.test.stub.PlayerStub) RoundPairings(mage.game.tournament.pairing.RoundPairings) TournamentPlayer(mage.game.tournament.TournamentPlayer) ArrayList(java.util.ArrayList) TournamentStub(org.mage.test.stub.TournamentStub) Round(mage.game.tournament.Round) TournamentPairing(mage.game.tournament.TournamentPairing) SwissPairingMinimalWeightMatching(mage.game.tournament.pairing.SwissPairingMinimalWeightMatching) Test(org.junit.Test)

Example 2 with TournamentPairing

use of mage.game.tournament.TournamentPairing in project mage by magefree.

the class SwissPairingSimple method getPairingsSimple.

private RoundPairings getPairingsSimple(List<TournamentPlayer> players, List<Round> rounds) {
    List<TournamentPairing> pairings = new ArrayList<>();
    List<TournamentPlayer> playerByes = new ArrayList<>();
    // sort players by tournament points
    players.sort((p1, p2) -> p2.getPoints() - p1.getPoints());
    // create pairings
    while (!players.isEmpty()) {
        TournamentPlayer player1 = players.get(0);
        players.remove(0);
        TournamentPlayer playerForPossibleSecondPairing = null;
        for (TournamentPlayer player2 : players) {
            if (alreadyPaired(rounds, player1, player2)) {
                // if already paired but equal points -> remember if second pairing is needed
                if (playerForPossibleSecondPairing == null) {
                    playerForPossibleSecondPairing = player2;
                }
            } else {
                if (player2.getPoints() < player1.getPoints() && playerForPossibleSecondPairing != null) {
                    // pair again with a player
                    pairings.add(new TournamentPairing(player1, playerForPossibleSecondPairing));
                    players.remove(playerForPossibleSecondPairing);
                    player1 = null;
                    break;
                } else {
                    // pair agains the next not paired before
                    pairings.add(new TournamentPairing(player1, player2));
                    players.remove(player2);
                    player1 = null;
                    break;
                }
            }
        }
        if (player1 != null) {
            // no pairing done yet
            if (playerForPossibleSecondPairing != null) {
                // pair again with a player
                pairings.add(new TournamentPairing(player1, playerForPossibleSecondPairing));
                players.remove(playerForPossibleSecondPairing);
            } else {
                // player free round - add to bye players of this round
                playerByes.add(player1);
            }
        }
    }
    return new RoundPairings(pairings, playerByes);
}
Also used : TournamentPlayer(mage.game.tournament.TournamentPlayer) ArrayList(java.util.ArrayList) TournamentPairing(mage.game.tournament.TournamentPairing)

Example 3 with TournamentPairing

use of mage.game.tournament.TournamentPairing in project mage by magefree.

the class SwissPairingMinimalWeightMatching method getRoundPairings.

public RoundPairings getRoundPairings() {
    // return round pairings with minimal weight
    List<TournamentPairing> pairings = new ArrayList<>();
    List<TournamentPlayer> playerByes = new ArrayList<>();
    Map<Integer, TournamentPlayer> map = new HashMap<>();
    for (PlayerInfo player : swissPlayers) {
        map.put(player.getId(), player.getTournamentPlayer());
    }
    if (n > playersCount) {
        // last vertex -- bye
        playerByes.add(map.get(result[n - 1]));
        result[result[n - 1]] = -1;
        result[n - 1] = -1;
    }
    for (int i = 0; i < playersCount; i++) {
        if (result[i] >= 0) {
            pairings.add(new TournamentPairing(map.get(i), map.get(result[i])));
            result[result[i]] = -1;
            result[i] = -1;
        }
    }
    return new RoundPairings(pairings, playerByes);
}
Also used : TournamentPlayer(mage.game.tournament.TournamentPlayer) TournamentPairing(mage.game.tournament.TournamentPairing)

Example 4 with TournamentPairing

use of mage.game.tournament.TournamentPairing in project mage by magefree.

the class SwissPairingMinimalWeightMatchingTest method SimulateTournament.

private void SimulateTournament(int playersCount, int roundsCount) {
    List<TournamentPlayer> players = new ArrayList<>();
    for (int i = 0; i < playersCount; i++) {
        players.add(new TournamentPlayer(new PlayerStub(), null));
    }
    List<TournamentPairing> playedPairs = new ArrayList<>();
    Set<TournamentPlayer> playersByes = new HashSet<>();
    List<Round> rounds = new ArrayList<>();
    for (int i = 0; i < roundsCount; i++) {
        SwissPairingMinimalWeightMatching swissPairing = new SwissPairingMinimalWeightMatching(new ArrayList<>(players), rounds, i + 1 == roundsCount);
        RoundPairings roundPairings = swissPairing.getRoundPairings();
        Assert.assertEquals(playersCount / 2, roundPairings.getPairings().size());
        Assert.assertEquals(playersCount % 2, roundPairings.getPlayerByes().size());
        Round round = new Round(1, new TournamentStub());
        rounds.add(round);
        for (TournamentPairing pairing : roundPairings.getPairings()) {
            if (ContainsPair(playedPairs, pairing.getPlayer1(), pairing.getPlayer2())) {
                if (i < (playersCount + 1) / 2) {
                    throw new AssertionError("Match between players has been played already.");
                }
            }
            playedPairs.add(pairing);
            round.addPairing(pairing);
            if (RandomUtil.nextBoolean()) {
                pairing.getPlayer1().setPoints(pairing.getPlayer1().getPoints() + 3);
            } else {
                pairing.getPlayer2().setPoints(pairing.getPlayer2().getPoints() + 3);
            }
        }
        for (TournamentPlayer playerBye : roundPairings.getPlayerByes()) {
            if (playersByes.contains(playerBye)) {
                throw new AssertionError("Player already had bye.");
            }
            playersByes.add(playerBye);
            round.getPlayerByes().add(playerBye);
            playerBye.setPoints(playerBye.getPoints() + 3);
        }
    }
}
Also used : TournamentPlayer(mage.game.tournament.TournamentPlayer) ArrayList(java.util.ArrayList) PlayerStub(org.mage.test.stub.PlayerStub) RoundPairings(mage.game.tournament.pairing.RoundPairings) TournamentStub(org.mage.test.stub.TournamentStub) Round(mage.game.tournament.Round) TournamentPairing(mage.game.tournament.TournamentPairing) SwissPairingMinimalWeightMatching(mage.game.tournament.pairing.SwissPairingMinimalWeightMatching) HashSet(java.util.HashSet)

Example 5 with TournamentPairing

use of mage.game.tournament.TournamentPairing in project mage by magefree.

the class SwissPairingMinimalWeightMatchingTest method PlayerLeftTournamentAfterFirstRound.

@Test
public void PlayerLeftTournamentAfterFirstRound() {
    // 1 > 3
    // 2 > 4
    // 4 left the tournament
    TournamentPlayer player1 = new TournamentPlayer(new PlayerStub(), null);
    TournamentPlayer player2 = new TournamentPlayer(new PlayerStub(), null);
    TournamentPlayer player3 = new TournamentPlayer(new PlayerStub(), null);
    TournamentPlayer player4 = new TournamentPlayer(new PlayerStub(), null);
    List<TournamentPlayer> players = new ArrayList<>();
    // players.add(player4); -- player 4 is not active
    players.add(player2);
    players.add(player3);
    players.add(player1);
    player1.setPoints(3);
    player2.setPoints(3);
    player3.setPoints(0);
    player4.setPoints(0);
    List<Round> rounds = new ArrayList<>();
    Round round = new Round(1, new TournamentStub());
    TournamentPairing pair1 = new TournamentPairing(player1, player3);
    round.addPairing(pair1);
    TournamentPairing pair2 = new TournamentPairing(player4, player2);
    round.addPairing(pair2);
    rounds.add(round);
    SwissPairingMinimalWeightMatching swissPairing = new SwissPairingMinimalWeightMatching(players, rounds, false);
    RoundPairings roundPairings = swissPairing.getRoundPairings();
    Assert.assertEquals(1, roundPairings.getPairings().size());
    Assert.assertEquals(1, roundPairings.getPlayerByes().size());
    CheckPair(roundPairings.getPairings(), player1, player2);
    Assert.assertTrue(roundPairings.getPlayerByes().contains(player3));
}
Also used : PlayerStub(org.mage.test.stub.PlayerStub) RoundPairings(mage.game.tournament.pairing.RoundPairings) TournamentPlayer(mage.game.tournament.TournamentPlayer) ArrayList(java.util.ArrayList) TournamentStub(org.mage.test.stub.TournamentStub) Round(mage.game.tournament.Round) TournamentPairing(mage.game.tournament.TournamentPairing) SwissPairingMinimalWeightMatching(mage.game.tournament.pairing.SwissPairingMinimalWeightMatching) Test(org.junit.Test)

Aggregations

TournamentPairing (mage.game.tournament.TournamentPairing)8 TournamentPlayer (mage.game.tournament.TournamentPlayer)8 ArrayList (java.util.ArrayList)7 Round (mage.game.tournament.Round)6 RoundPairings (mage.game.tournament.pairing.RoundPairings)6 SwissPairingMinimalWeightMatching (mage.game.tournament.pairing.SwissPairingMinimalWeightMatching)6 PlayerStub (org.mage.test.stub.PlayerStub)6 TournamentStub (org.mage.test.stub.TournamentStub)6 Test (org.junit.Test)5 HashSet (java.util.HashSet)1