1

我正在编写一个多人游戏,其中每个玩家必须与他组中的每个玩家只玩一次。即,如果您有 3 名玩家:Joe、Mary 和 Peter,这些将是组合:Joe & Mary、Joe & Peter 和 Mary & Peter。

计算轮数的代码非常简单。由于轮数等于n!/r!* (n - r)!其中 n 等于玩家人数,r 等于 2(因为游戏每轮有 2 名玩家进行)。

 public int factorial(int n)
 {
      if (n == 0)
          return 1;
      return n * factorial(n - 1);
 }

 public int calcNoOfRounds()
 {
      return factorial(noOfPlayers) / (factorial(2) * factorial(noOfPlayers -2));
 }

但是,我坚持要产生一种有效的方式来返回实际的玩家组合。我尝试了以下代码。它可以工作,但是它太手动了,而且有些事情我想改进。在这段代码中,我将 p1 vs p2、p2 vs p3、p3 vs p4 ... p(n-1) vs p(n) 配对。然后我从第 3 名球员开始,将这些球员与上述所有球员进行匹配,除了他们之前的球员,即 p3 vs p1、p4 vs p1、p4 vs p2、p5 vs p1、p5 vs p2、p5 vs p3 等。 . 你觉得我能做得更好吗?

 public void calcPlayerCombinations()
 {
     List<string> playerNames = new List<string>();

     for (int i = 0; i < noOfPlayers; i++)
     {
          playerNames.Add(players[i].PlayerName);
     }

     for (int i = 0; i < noOfPlayers - 1; i++)
     {
          playerCombinations.Add(playerNames[i] + " " + playerNames[i + 1]);
     }

     for (int j = 3; j <= noOfPlayers; j++)
     {
          int counter = 1;

          do
          {
             playerCombinations.Add(playerNames[j -1] + " " + playerNames[counter -1]);
             counter++;

          } while (counter != (j - 1));
     }
 }

我不喜欢这样,因为如果游戏真的在玩,你会希望同一个玩家连续玩 6 场比赛吗?我可以随机选择一个组合进行一轮是的,但我仍然想知道一个更好的方法以供将来参考。

谢谢你的帮助!

4

2 回答 2

2

Why would you not just pair each player (as "first" in a pairing) with each player later than them (as "second")? For example:

public static IEnumerable<string> PairPlayers(List<string> players)
{
    for (int i = 0; i < players.Count - 1; i++)
    {
        for (int j = i + 1; j < players.Count; j++)
        {
            yield return players[i] + " " + players[j];
        }
    }
}

(Obviously you can do this eagerly too, should you wish.)

It's possible that I've misinterpreted the requirements though.

于 2013-01-06T10:01:05.210 回答
1

此示例显示如何将玩家列表用作队列。当一名球员上场后,他们会被排在后面,并且最不可能再次被选中。它还展示了如何做 Jon Skeet 所做的但渴望(没有yield)。

using System;
using System.Collections.Generic;
using System.Linq;

namespace SOPlayersOrder
{
    class Program
    {
        /// <summary>
        /// Represents a match up between two players.
        /// It is tempting to use strings for everything, but don't do it,
        /// you'll only end up having to split those strings and you will
        /// not benefit from type safety.
        /// </summary>
        public class MatchUp
        {
            public string Player1 { get; set; }
            public string Player2 { get; set; }

            public override string ToString()
            {
                return string.Format("{0} vs {1}", Player1, Player2);
            }
        }

        public static IEnumerable<MatchUp> PairPlayers(List<string> players)
        {
            var results = new List<MatchUp>();
            for (int i = 0; i < players.Count - 1; i++)
            {
                for (int j = i + 1; j < players.Count; j++)
                {
                    var matchup = new MatchUp { Player1 = players[i], Player2 = players[j] };
                    //yield return matchup; //this is how Jon Skeet suggested, I am showing you "eager" evaluation
                    results.Add(matchup);
                }
            }
            return results;
        }

        public static IEnumerable<MatchUp> OrganiseGames(IEnumerable<string> players, IEnumerable<MatchUp> games)
        {
            var results = new List<MatchUp>();
            //a list that we will treat as a queue - most recently played at the back of the queue
            var playerStack = new List<string>(players);
            //a list that we can modify
            var gamesList = new List<MatchUp>(games);
            while (gamesList.Count > 0)
            {
                //find a game for the top player on the stack
                var player1 = playerStack.First();
                var player2 = playerStack.Skip(1).First();
                //the players are in the order of least recently played first
                MatchUp matchUp = FindFirstAvailableGame(playerStack, gamesList);
                //drop the players that just played to the back of the list
                playerStack.Remove(matchUp.Player1);
                playerStack.Remove(matchUp.Player2);
                playerStack.Add(matchUp.Player1);
                playerStack.Add(matchUp.Player2);
                //remove that pairing
                gamesList.Remove(matchUp);
                //yield return matchUp; //optional way of doing this
                results.Add(matchUp);
            }
            return results;
        }

        private static MatchUp FindFirstAvailableGame(List<string> players, List<MatchUp> gamesList)
        {            
            for (int i = 0; i < players.Count - 1; i++)
            {
                for (int j = i + 1; j < players.Count; j++)
                {
                    var game = gamesList.FirstOrDefault(g => g.Player1 == players[i] && g.Player2 == players[j] ||
                                                             g.Player2 == players[i] && g.Player1 == players[j]);
                    if (game != null) return game;
                }
            }
            throw new Exception("Didn't find a game");
        }

        static void Main(string[] args)
        {
            var players = new List<string>(new []{"A","B","C","D","E"});
            var allGames = new List<MatchUp>(PairPlayers(players));

            Console.WriteLine("Unorganised");

            foreach (var game in allGames)
            {
                Console.WriteLine(game);
            }

            Console.WriteLine("Organised");

            foreach (var game in OrganiseGames(players, allGames))
            {
                Console.WriteLine(game);
            }

            Console.ReadLine();
        }
    }
}
于 2013-01-06T12:31:20.940 回答