Adding chances calculation for Swiss tournament.

This commit is contained in:
marcins78@gmail.com
2014-11-04 01:18:06 +00:00
parent f8924f882a
commit 1fce3cdce9

View File

@@ -5,9 +5,17 @@ import com.gempukku.lotro.competitive.PlayerStanding;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
public class SwissPairingMechanismTest {
// @Test
@@ -20,6 +28,99 @@ public class SwissPairingMechanismTest {
// }
// }
public void calculateSallyJackChances() {
// 10 Sallys and 250 Jacks play in a tournament with 9 rounds of Swiss and Top 8.
// Sally has 66% chance of winning a game against Jack and 50% against anotherSally, Jack has 50% of winning
// a game against another Jack
int repeatCount = 10000;
int playerCount = 260;
int roundCount = 9;
int betterPlayerWin =0;
for (int repeat = 0; repeat < repeatCount; repeat++) {
Set<String> players = new HashSet<String>();
for (int i = 0; i < playerCount; i++)
players.add(String.valueOf(i));
Set<String> droppedPlayers = new HashSet<String>();
Map<String, Integer> byes = new HashMap<String, Integer>();
Set<TournamentMatch> matches = new HashSet<TournamentMatch>();
Map<String, Set<String>> previouslyPaired = new HashMap<String, Set<String>>();
for (String player : players)
previouslyPaired.put(player, new HashSet<String>());
SwissPairingMechanism pairing = new SwissPairingMechanism("swiss");
for (int i = 1; i <= roundCount; i++) {
if (!pairing.isFinished(i - 1, players, droppedPlayers)) {
List<PlayerStanding> standings = BestOfOneStandingsProducer.produceStandings(players, matches, 3, 0, byes);
Map<String, String> newPairings = new LinkedHashMap<String, String>();
Set<String> newByes = new HashSet<String>();
pairing.pairPlayers(i, players, droppedPlayers, byes, standings, previouslyPaired, newPairings, newByes);
if (newByes.size() > 0) {
for (String newBye : newByes) {
byes.put(newBye, 1);
}
}
for (Map.Entry<String, String> newPairing : newPairings.entrySet()) {
String playerOne = newPairing.getKey();
String playerTwo = newPairing.getValue();
String winner = getWinner(playerOne, playerTwo);
previouslyPaired.get(playerOne).add(playerTwo);
previouslyPaired.get(playerTwo).add(playerOne);
matches.add(new TournamentMatch(playerOne, playerTwo, winner, i));
}
}
}
List<PlayerStanding> standings = BestOfOneStandingsProducer.produceStandings(players, matches, 1, 0, byes);
String firstSemi = getWinner(standings.get(0).getPlayerName(), standings.get(7).getPlayerName());
String secondSemi = getWinner(standings.get(1).getPlayerName(), standings.get(6).getPlayerName());
String thirdSemi = getWinner(standings.get(2).getPlayerName(), standings.get(5).getPlayerName());
String fourthSemi = getWinner(standings.get(3).getPlayerName(), standings.get(4).getPlayerName());
String firstFinalist = getWinner(firstSemi, fourthSemi);
String secondFinalist = getWinner(secondSemi, thirdSemi);
String winner = getWinner(firstFinalist, secondFinalist);
if (Integer.parseInt(winner)<10)
betterPlayerWin++;
}
System.out.println(betterPlayerWin);
}
private String getWinner(String playerOne, String playerTwo) {
int playerOneNo = Integer.parseInt(playerOne);
int playerTwoNo = Integer.parseInt(playerTwo);
if (playerOneNo < 10 && playerTwoNo >= 10) {
if (new Random().nextFloat() < 0.66f)
return playerOne;
else
return playerTwo;
} else if (playerTwoNo < 10 && playerOneNo >= 10) {
if (new Random().nextFloat() < 0.66f)
return playerTwo;
else
return playerOne;
} else {
if (new Random().nextBoolean())
return playerOne;
else
return playerTwo;
}
}
@Test
public void testPairingSmallTournament() {
int repeatCount = 10;
@@ -70,7 +171,7 @@ public class SwissPairingMechanismTest {
List<PlayerStanding> standings = BestOfOneStandingsProducer.produceStandings(players, matches, 1, 0, byes);
for (PlayerStanding standing : standings) {
String player = standing.getPlayerName();
log(player +" points - "+standing.getPoints() + " played against: "+ StringUtils.join(previouslyPaired.get(player), ","));
log(player + " points - " + standing.getPoints() + " played against: " + StringUtils.join(previouslyPaired.get(player), ","));
}
Map<String, String> newPairings = new LinkedHashMap<String, String>();
@@ -97,7 +198,7 @@ public class SwissPairingMechanismTest {
System.out.println("Paired " + playerOne + " against " + playerTwo + " points - " + getPlayerPoints(standings, playerOne) + " vs " + getPlayerPoints(standings, playerTwo));
String winner = new Random().nextBoolean() ? playerOne : playerTwo;
log("Winner - "+winner);
log("Winner - " + winner);
previouslyPaired.get(playerOne).add(playerTwo);
previouslyPaired.get(playerTwo).add(playerOne);