我想知道如何压缩这个工作代码。它仅仅是为了我在C#班的第三天与合作伙伴一起制作的简单钓鱼游戏。这似乎是一大堆代码,我肯定可以简化为几行代码。
C#代码部分需要帮助
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
int count7 = 0;
int count8 = 0;
int count9 = 0;
int count10 = 0;
int count11 = 0;
int count12 = 0;
int count13 = 0;
// System.Console.WriteLine("HI THERE LOOK AT ME " + player1.numOfCardsInHand());
for(int y = player1.numOfCardsInHand()-1; y >= 0 ; y--)
{
if(player1.getListObject()[y].val == 1)
{
count1++;
}
if(player1.getListObject()[y].val == 2)
{
count2++;
}
if(player1.getListObject()[y].val == 3)
{
count3++;
}
if(player1.getListObject()[y].val == 4)
{
count4++;
}
if(player1.getListObject()[y].val == 5)
{
count5++;
}
if(player1.getListObject()[y].val == 6)
{
count6++;
}
if(player1.getListObject()[y].val == 7)
{
count7++;
}
if(player1.getListObject()[y].val == 8)
{
count8++;
}
if(player1.getListObject()[y].val == 9)
{
count9++;
}
if(player1.getListObject()[y].val == 10)
{
count10++;
}
if(player1.getListObject()[y].val == 11)
{
count11++;
}
if(player1.getListObject()[y].val == 12)
{
count12++;
}
if(player1.getListObject()[y].val == 13)
{
count13++;
}
if(count1 == 4)
{
player1Points++;
player1.getListObject().RemoveAll(u => u.Equals("1"));
}
if(count2 == 4)
{
player1Points++;
player1.getListObject().RemoveAll(u => u.Equals("2"));
}
if(count3 == 4)
{
player1Points++;
player1.getListObject().RemoveAll(u => u.Equals("3"));
}
if(count4 == 4)
{
player1Points++;
player1.getListObject().RemoveAll(u => u.Equals("4"));
}
if(count5 == 4)
{
player1Points++;
player1.getListObject().RemoveAll(u => u.Equals("5"));
}
if(count6 == 4)
{
player1Points++;
player1.getListObject().RemoveAll(u => u.Equals("6"));
}
if(count7 == 4)
{
player1Points++;
player1.getListObject().RemoveAll(u => u.Equals("7"));
}
if(count8== 4)
{
player1Points++;
player1.getListObject().RemoveAll(u => u.Equals("8"));
}
if(count9 == 4)
{
player1Points++;
player1.getListObject().RemoveAll(u => u.Equals("9"));
}
if(count10 == 4)
{
player1Points++;
player1.getListObject().RemoveAll(u => u.Equals("10"));
}
if(count11 == 4)
{
player1Points++;
player1.getListObject().RemoveAll(u => u.Equals("11"));
}
if(count12 == 4)
{
player1Points++;
player1.getListObject().RemoveAll(u => u.Equals("12"));
}if(count13 == 4)
{
player1Points++;
player1.getListObject().RemoveAll(u => u.Equals("13"));
}
}
答案 0 :(得分:2)
使用每种卡类型的元素将数据存储在数组中。使用卡片类型作为卡片阵列中的索引来增加计数器或获取给定类型的当前卡片数:
int[] cards = new int[13];
for(int i = player1.numOfCardsInHand() - 1; i >= 0 ; i--)
{
int cardType = player1.getListObject()[i].val;
cards[cardType - 1]++;
if (cards[cardType - 1] == 4)
{
player1Points++;
player1.getListObject().RemoveAll(u => u.Equals(cardType.ToString()));
}
}
虽然如果你只是想获得点数(即所有四种给定类型的牌),看看手头还剩下什么,那么一切都可以简化为:
var fourOfType = player1.getListObject()
.GroupBy(c => c.val)
.Where(g => g.Count() == 4)
.Select(g => g.Key)
.ToList();
player1Points = fourOfType.Count;
foreach(var cardType in fourOfType)
player1.getListObject().RemoveAll(c => c.Equals(cardType.ToString()))