如何处理一副牌?

时间:2014-08-19 23:00:11

标签: c# arrays jagged-arrays

我正在尝试创建一个应用程序来处理一副牌。我已经制作了牌组并且还在牌组上进行了洗牌但是如果玩家的话,我很难将其交给x号码 这是我到目前为止所拥有的。

class Dealer
{
    private string[] suffeledDeck = new string[52];
    private static string[] playerOne = new string[26];
    private static string[] playerTwo = new string[26];
    private static string[] playerThree = new string[26];
    private static string[] playerFour = new string[26];
    private static string[] playerFive = new string[26];
    private static string[] playerSix = new string[26];
    private static string[][] allplayers = new string[][] { playerOne, playerTwo,playerThree,playerFour,playerFive,playerSix };

    private int counter = 0;
    private int playerCount = 6;

    public Dealer(string[] deck)
    { 
        suffeledDeck = deck;
    }

    private void deal()
    {
        for (int i = 0; i < suffeledDeck.Length; i++)
        {
            allplayers[counter][0] = suffeledDeck[i];
            counter++;
            if (counter == playerCount)
            {
                counter = 0;
            }
        }
    }
}

}

2 个答案:

答案 0 :(得分:1)

使用List<string[]>代替string[][]

答案 1 :(得分:1)

这是一种方法,但我可能会使用List<T>,正如Selman22所建议的那样。

var sourceIndex = 0;
var handCount = 0;
while (sourceIndex < shuffledDeck.Length)
{
    for (var playerIndex = 0; playerIndex < allplayers.Length; playerIndex++)
    {
        allplayers[playerIndex][handCount] = shuffledDeck[sourceIndex++];
        if (sourceIndex == shuffledDeck.Length)
            break;
    }
    handCount++;
}

列表示例:

class Dealer
{
    private List<string> shuffledDeck= new List<string>();
    private static List<string> playerOne = new List<string>();
    private static List<string> playerTwo = new List<string>();
    private static List<string> playerThree = new List<string>();
    private static List<string> playerFour = new List<string>();
    private static List<string> playerFive = new List<string>();
    private static List<string> playerSix = new List<string>();
    private static List<List<string>> allplayers = new List<List<string>> { playerOne, playerTwo,playerThree,playerFour,playerFive,playerSix };

    private int counter = 0;
    private int playerCount = allplayers.Count;
    public Dealer(string[] deck)
    { 
        shuffledDeck= deck;
    }
    private void deal()
    {
        while (shuffledDeck.Count > 0)
        {
            foreach (var player in allplayers)
            {
                player.Add(shuffledDeck[shuffledDeck.Length - 1];
                shuffledDeck.RemoveAt(shuffledDeck.Length - 1];
                if (shuffledDeck.Length == 0)
                    break;
            }
        }
    }
}

这种方法的优点是可以移除从牌组中发出的牌。从最后处理,因为RemoveAt(0)复制了所有剩余的卡片以填补空白。

更好的初始化,除非您确实需要playerOne ... playerSix变量:

private static int playerCount = 6;
private static List<List<string>> allplayers =
    Enumerable.Range(0, playerCount).Select(i => new List<string());