mirror of
https://github.com/magefree/mage.git
synced 2025-12-23 20:11:59 -08:00
Use approach with minimal weight matching to determine Swiss tournament pairings
This commit is contained in:
parent
9b074876db
commit
05a789cd8b
7 changed files with 2230 additions and 68 deletions
|
|
@ -0,0 +1,271 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package org.mage.test.serverside.tournament;
|
||||
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import mage.game.tournament.*;
|
||||
import mage.game.tournament.pairing.RoundPairings;
|
||||
import mage.game.tournament.pairing.SwissPairingMinimalWeightMatching;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.mage.test.stub.PlayerStub;
|
||||
import org.mage.test.stub.TournamentStub;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Quercitron
|
||||
*/
|
||||
public class SwissPairingMinimalWeightMatchingTest {
|
||||
|
||||
@Test
|
||||
public void FourPlayersSecondRoundTest() {
|
||||
// 1 > 3
|
||||
// 2 > 4
|
||||
|
||||
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);
|
||||
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);
|
||||
RoundPairings roundPairings = swissPairing.getRoundPairings();
|
||||
|
||||
Assert.assertEquals(2, roundPairings.getPairings().size());
|
||||
Assert.assertEquals(0, roundPairings.getPlayerByes().size());
|
||||
|
||||
CheckPair(roundPairings.getPairings(), player1, player2);
|
||||
CheckPair(roundPairings.getPairings(), player3, player4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void FourPlayersSecondThirdTest() {
|
||||
// 1 > 3
|
||||
// 2 > 4
|
||||
//
|
||||
// 1 > 2
|
||||
// 3 > 4
|
||||
|
||||
TournamentPlayer player3 = new TournamentPlayer(new PlayerStub(), null);
|
||||
TournamentPlayer player2 = new TournamentPlayer(new PlayerStub(), null);
|
||||
TournamentPlayer player4 = new TournamentPlayer(new PlayerStub(), null);
|
||||
TournamentPlayer player1 = new TournamentPlayer(new PlayerStub(), null);
|
||||
List<TournamentPlayer> players = new ArrayList<>();
|
||||
players.add(player4);
|
||||
players.add(player2);
|
||||
players.add(player3);
|
||||
players.add(player1);
|
||||
|
||||
player1.setPoints(6);
|
||||
player2.setPoints(3);
|
||||
player3.setPoints(3);
|
||||
player4.setPoints(0);
|
||||
|
||||
List<Round> rounds = new ArrayList<>();
|
||||
// round 1
|
||||
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);
|
||||
// round 2
|
||||
round = new Round(2, new TournamentStub());
|
||||
pair1 = new TournamentPairing(player2, player1);
|
||||
round.addPairing(pair1);
|
||||
pair2 = new TournamentPairing(player4, player3);
|
||||
round.addPairing(pair2);
|
||||
rounds.add(round);
|
||||
|
||||
SwissPairingMinimalWeightMatching swissPairing = new SwissPairingMinimalWeightMatching(players, rounds);
|
||||
RoundPairings roundPairings = swissPairing.getRoundPairings();
|
||||
|
||||
Assert.assertEquals(2, roundPairings.getPairings().size());
|
||||
Assert.assertEquals(0, roundPairings.getPlayerByes().size());
|
||||
|
||||
CheckPair(roundPairings.getPairings(), player1, player4);
|
||||
CheckPair(roundPairings.getPairings(), player2, player3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void FivePlayersThirdRoundTest() {
|
||||
// 1 > 2
|
||||
// 3 > 4
|
||||
// 5
|
||||
//
|
||||
// 1 > 5
|
||||
// 2 > 3
|
||||
// 4
|
||||
|
||||
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(player4);
|
||||
players.add(player2);
|
||||
players.add(player5);
|
||||
players.add(player3);
|
||||
players.add(player1);
|
||||
|
||||
player1.setPoints(6);
|
||||
player2.setPoints(3);
|
||||
player3.setPoints(3);
|
||||
player4.setPoints(3);
|
||||
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);
|
||||
// second round
|
||||
round = new Round(1, new TournamentStub());
|
||||
pair1 = new TournamentPairing(player1, player5);
|
||||
round.addPairing(pair1);
|
||||
pair2 = new TournamentPairing(player2, player3);
|
||||
round.addPairing(pair2);
|
||||
round.getPlayerByes().add(player4);
|
||||
rounds.add(round);
|
||||
|
||||
SwissPairingMinimalWeightMatching swissPairing = new SwissPairingMinimalWeightMatching(players, rounds);
|
||||
RoundPairings roundPairings = swissPairing.getRoundPairings();
|
||||
|
||||
Assert.assertEquals(2, roundPairings.getPairings().size());
|
||||
Assert.assertEquals(1, roundPairings.getPlayerByes().size());
|
||||
|
||||
CheckPair(roundPairings.getPairings(), player1, player4);
|
||||
CheckPair(roundPairings.getPairings(), player2, player5);
|
||||
Assert.assertTrue(roundPairings.getPlayerByes().contains(player3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void SimulateDifferentTournaments() {
|
||||
int playersCount = 12;
|
||||
for (int i = 0; i <= playersCount; i++) {
|
||||
int roundsCount = ((i + 1) / 2) * 2 - 1;
|
||||
for (int j = 1; j <= roundsCount; j++) {
|
||||
SimulateTournament(i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SimulateTournament(int playersCount, int roundsCount) {
|
||||
Random rnd = new Random();
|
||||
|
||||
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);
|
||||
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 (rnd.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckPair(List<TournamentPairing> pairs, TournamentPlayer player1, TournamentPlayer player2) {
|
||||
if (!ContainsPair(pairs, player1, player2)) {
|
||||
throw new AssertionError("Pairing doesn't contain expected pair of players.");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean ContainsPair(List<TournamentPairing> pairs, TournamentPlayer player1, TournamentPlayer player2) {
|
||||
for (TournamentPairing pair : pairs) {
|
||||
if (pair.getPlayer1().equals(player1) && pair.getPlayer2().equals(player2)) {
|
||||
return true;
|
||||
}
|
||||
if (pair.getPlayer1().equals(player2) && pair.getPlayer2().equals(player1)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
1222
Mage.Tests/src/test/java/org/mage/test/stub/PlayerStub.java
Normal file
1222
Mage.Tests/src/test/java/org/mage/test/stub/PlayerStub.java
Normal file
File diff suppressed because it is too large
Load diff
237
Mage.Tests/src/test/java/org/mage/test/stub/TournamentStub.java
Normal file
237
Mage.Tests/src/test/java/org/mage/test/stub/TournamentStub.java
Normal file
|
|
@ -0,0 +1,237 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package org.mage.test.stub;
|
||||
|
||||
import mage.cards.ExpansionSet;
|
||||
import mage.cards.decks.Deck;
|
||||
import mage.game.draft.Draft;
|
||||
import mage.game.events.Listener;
|
||||
import mage.game.events.PlayerQueryEvent;
|
||||
import mage.game.events.TableEvent;
|
||||
import mage.game.tournament.*;
|
||||
import mage.players.Player;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Quercitron
|
||||
*/
|
||||
public class TournamentStub implements Tournament {
|
||||
|
||||
private final UUID id = UUID.randomUUID();
|
||||
|
||||
@Override
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPlayer(Player player, String playerType) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removePlayer(UUID playerId) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public TournamentPlayer getPlayer(UUID playerId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<TournamentPlayer> getPlayers() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Round> getRounds() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ExpansionSet> getSets() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateResults() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBoosterInfo(String setInfo) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBoosterInfo() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void submitDeck(UUID playerId, Deck deck) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDeck(UUID playerId, Deck deck) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void autoSubmit(UUID playerId, Deck deck) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allJoined() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDoneConstructing() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void quit(UUID playerId) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void leave(UUID playerId) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nextStep() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addTableEventListener(Listener<TableEvent> listener) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPlayerQueryEventListener(Listener<PlayerQueryEvent> listener) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fireConstructEvent(UUID playerId) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public TournamentOptions getOptions() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStartTime() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getStartTime() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getEndTime() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getStepStartTime() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStepStartTime(Date date) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public TournamentType getTournamentType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTournamentType(TournamentType tournamentType) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTournamentState() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTournamentState(String tournamentState) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNumberRounds() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanUpOnTournamentEnd() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAbort() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAbort(boolean abort) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearDraft() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Draft getDraft() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue