多人游戏的配对算法

问题描述 投票:0回答:0

我想在锦标赛中将玩家分组,例如 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;
    }

它以几乎手动的方式排列,但无法制作与目标大小不同的多个表格。

算法是:

  • 如果玩家数量可以被目标 pod 数量整除,则将他们平分
  • 如果余数足以将最后一张桌子分成 2 份,则将第一批玩家分成 pod,直到最后两个 pod 足够为止
  • 如果剩余的足够制作一个 pod,将第一个玩家分成 pod,直到最后一个 pod 足够为止
  • 如果剩下的可以加到最后一个pod,平分第一个玩家,把剩下的玩家加到最后一个pod
algorithm sorting grouping cluster-analysis game-development
© www.soinside.com 2019 - 2024. All rights reserved.