需要帮助简化我的扑克评估代码
本文关键字:扑克 评估 代码 我的 帮助 | 更新日期: 2023-09-27 18:11:40
我目前正在创建一个扑克评估器,用于比较来自多个玩家的手牌并决定谁是赢家。然而,实际比较和平分的方法比它应该做的要多得多。有什么方法可以简化我的代码吗?由于我没有发布所有的源代码,我将讨论在
中调用的一些方法的功能:int handdeval (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]);
}
}
}
任何帮助或提示将非常感激!
你最大的错误是忽略了你使用的语言类型。你的风格是命令式的,不是面向对象的。
你要做的是创建类来处理这些东西,比如cardHand或scoreTable。然后你给它们getbestthand这样的方法。打印内容的那部分代码应该类似于下面的伪代码:
print "The winner is " + table.getBestHand.getPlayer.name
也就是说,在这部分代码中没有任何for循环或类似的语句。您希望将逻辑和输出分离。通常,当单个函数或方法的大小不超过屏幕的一半时,这是一个好兆头(当然也有例外)。把所有你能用一个词描述但超过五行代码的东西,用它来创建一个方法。
对于这个孤立的例子,这当然会产生更多的代码,但我想即使在扑克游戏中,拥有适当的类也会得到回报。如果不是在代码量上,那么至少在可读性和可维护性上。
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));
}
}
你基本上调用同一个for循环两次,你可以把
Console.WriteLine("" + potentialWinners[i] + "");
在它后面的for循环中…也请删除(""
)从你的控制台WriteLine ' s它做的基本上没有什么,但使你的代码变慢
同样适用于这些循环
foreach (var kvp in highTable)
和
foreach (var kvp in scoreTable)
简单的东西,使您的代码更小,更高效
问候