需要帮助简化我的扑克评估代码

时间:2016-10-06 07:00:51

标签: c#

我目前正在创建一个扑克评估员,比较来自多个玩家的牌并决定胜者是谁。然而,实际比较和打破平局的方法比它应该的工作要多得多。有什么方法可以简化我的代码吗?由于我没有发布所有源代码,因此我将讨论在其中调用的一些方法的功能:

int HandEval(Card [] hand)获取一系列牌并返回一个符号表示手牌得分的int(即三种牌的得分高于一对牌)

int GetHighCard(Card [] hand)获取一系列牌并返回用于打破平局的高牌的等级。(即,在三种情况下,这三种中的一种的等级将被退回)

 public void FindWinner()
    {
        int maxScore = 0;
        int maxRank = 0;
        List<String> potentialWinners = new List<string>();
        List<Card[]> candHand = new List<Card[]>();

        Dictionary<string, Card[]> deckTable = new Dictionary<string, Card[]>();
        Dictionary<string, int> scoreTable = new Dictionary<string, int>();
        Dictionary<string, int> highTable = new Dictionary<string, int>();

        //place each player to deckTable which holds the player name and their hand
        //place each player to scoreTable which holds the player name and the score of their hand
        for (int i = 0; i < players.Count; i++)
        {
            deckTable.Add(players[i].GetName(), players[i].GetSorted());
            scoreTable.Add(players[i].GetName(), HandEval(players[i].GetSorted()));

        }

        //display the player name and the score of their hand
        foreach (KeyValuePair<string, int> pair in scoreTable)
        {
            Console.WriteLine("{0}, {1}", pair.Key, +pair.Value);
        }

        //find the max score of the current game
        foreach (var kvp in scoreTable)
        {
            if (kvp.Value > maxScore)
            {
                maxScore = kvp.Value;
            }
        }

        //display the max score
        Console.WriteLine("The maximum score is " + maxScore);

        //for all players with the max score, add them to the potential winners list
        foreach (var kvp in scoreTable)
        {
            if (kvp.Value == maxScore)
            {
                potentialWinners.Add(kvp.Key);
            }
        }

        //if there are more than one potential winner, run the tie-break checks
        if (potentialWinners.Count > 1)
        {
            Console.WriteLine("Potential winners include: ");
            for (int i = 0; i < potentialWinners.Count(); i++)
            {
                Console.WriteLine("" + potentialWinners[i] + "");
            }

            //add the name of the potential winners and the rank of the high card as a key value pair to highTable
            for (int i = 0; i < potentialWinners.Count(); i++)
            {
                if (deckTable.ContainsKey(potentialWinners[i]))
                {
                    Card[] cHand = deckTable[potentialWinners[i]];
                    highTable.Add(potentialWinners[i], GetHighCard(cHand));
                }
            }

            Console.WriteLine("Displaying potential winners with their high card rank.");
            foreach (KeyValuePair<string, int> pair in highTable)
            {
                Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
            }

            //find the max rank of high card from all potential winners
            foreach (var kvp in highTable)
            {
                if (kvp.Value > maxRank)
                {
                    maxRank = kvp.Value;
                }
            }

            Console.WriteLine("The final winner after tie-breaking is");

            //display the winner(s) with the highest rank of card
            foreach (var kvp in highTable)
            {
                if (kvp.Value == maxRank)
                {
                    Console.WriteLine("" + kvp.Key + "");
                }
            }
        }
        //if there is only one potential winner, display the name
        else
        {
            Console.WriteLine("The final winner is");
            Console.WriteLine(potentialWinners[0]);
        }
    }
}

非常感谢任何帮助或提示!

2 个答案:

答案 0 :(得分:2)

你最大的错误就是忽略你正在使用的语言。你的风格势在必行,而不是面向对象。

你想要做的是创建处理这些东西的类,比如cardHand或scoreTable。然后你给他们像getBestHand这样的方法。 打印出来的代码部分应该看起来像这样的伪代码:

print“获胜者是”+ table.getBestHand.getPlayer.name

即,代码的那部分中没有任何循环等。您想要分离逻辑和输出。通常,当单个函数或方法的大小不超过屏幕的一半时,这是一个好兆头(当然会发生异常)。把你能用一个单词描述的所有东西都拿出来,但是有五行以上的代码并用它来制作一个方法。

对于这个孤立的例子,这当然会产生更多的代码,但我想即使在扑克游戏中,拥有适当的课程也会付出代价。如果不是代码量,那么至少在可读性和可维护性方面。

答案 1 :(得分:0)

for (int i = 0; i < potentialWinners.Count(); i++)
{
    Console.WriteLine("" + potentialWinners[i] + "");
}

//add the name of the potential winners and the rank of the high card as a key value pair to highTable


for (int i = 0; i < potentialWinners.Count(); i++)
{
    if (deckTable.ContainsKey(potentialWinners[i]))
    {
        Card[] cHand = deckTable[potentialWinners[i]];
        highTable.Add(potentialWinners[i], GetHighCard(cHand));
    }
}

你基本上在循环中调用相同的2次,你可以带

Console.WriteLine("" + potentialWinners[i] + "");

在它之后的for循环中...也请从你的Console WriteLine中删除("")它的基本上没什么,只是让你的代码更慢

这些循环也是如此

foreach (var kvp in highTable)

foreach (var kvp in scoreTable)

简单的事情,使您的代码更加轻松和高效

问候