我想在锦标赛中将玩家分组,例如 MTG 或 Catan,在 3 到 4 名玩家的桌子上,尽量减少轮空玩家的数量(跳过一轮)。
我试过以下代码:
public static List<GroupMatch> createGroupMatches(List<Player> players, Integer targetPodSize, Integer minPodSize, Integer maxPodSize) {
if (targetPodSize < minPodSize || targetPodSize > maxPodSize) {
throw new IllegalArgumentException("targetPodSize must be between minPodSize and maxPodSize");
}
List<Player> rankedPlayers = getClassification(players);
Integer playerCount = rankedPlayers.size();
Integer remainder = (playerCount % targetPodSize);
List<GroupMatch> groupMatches = new ArrayList<>();
if (playerCount % targetPodSize == 0) {
int j = 0;
for (int i = 0; i < playerCount / targetPodSize; i++) {
groupMatches.add(new GroupMatch(rankedPlayers.subList(j, j + targetPodSize)));
j += targetPodSize;
}
} else if (((remainder + targetPodSize) % 2 == 0) && (remainder + targetPodSize > minPodSize)) {
int smallpod = (remainder + targetPodSize) / 2;
int j = 0;
for (int i = 0; i < (playerCount - 2 * smallpod) / targetPodSize; i++) {
groupMatches.add(new GroupMatch(rankedPlayers.subList(j, j + targetPodSize)));
j += targetPodSize;
}
groupMatches.add(new GroupMatch(rankedPlayers.subList(j, j + smallpod)));
j += smallpod;
groupMatches.add(new GroupMatch(rankedPlayers.subList(j, j + smallpod)));
} else if (remainder >= minPodSize) {
int smallpod = remainder;
int j = 0;
for (int i = 0; i < (playerCount - smallpod) / targetPodSize; i++) {
groupMatches.add(new GroupMatch(rankedPlayers.subList(j, j + targetPodSize)));
j += targetPodSize;
}
groupMatches.add(new GroupMatch(rankedPlayers.subList(j, j + smallpod)));
} else if ((remainder + targetPodSize) <= maxPodSize) {
int smallpod = remainder + targetPodSize;
int j = 0;
for (int i = 0; i < (playerCount - smallpod) / targetPodSize; i++) {
groupMatches.add(new GroupMatch(rankedPlayers.subList(j, j + targetPodSize)));
j += targetPodSize;
}
groupMatches.add(new GroupMatch(rankedPlayers.subList(j, j + smallpod)));
}
return groupMatches;
}
它以几乎手动的方式排列,但无法制作与目标大小不同的多个表格。
算法是: