Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我如何用C压缩这段代码#_C# - Fatal编程技术网

C# 我如何用C压缩这段代码#

C# 我如何用C压缩这段代码#,c#,C#,我想知道如何压缩这个工作代码。这只是一个简单的围棋游戏,我在C#class的第三天和一个搭档做的。这似乎是一大堆代码,我相信可以浓缩成几行 C#编码需要帮助的部件 int count1 = 0; int count2 = 0; int count3 = 0; int count4 = 0; int count5 = 0; int count6 = 0;

我想知道如何压缩这个工作代码。这只是一个简单的围棋游戏,我在C#class的第三天和一个搭档做的。这似乎是一大堆代码,我相信可以浓缩成几行

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"));
                }


            }

将卡片计数存储在数组中,每个卡片类型使用元素。将卡片类型用作卡片数组中的索引,以增加计数器或获取给定类型的当前卡片计数:

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()))

谷歌csharp数组。计数数组和开关语句你能给我指一个我能看到的例子吗that@DavidHollenbeck我相信这是一个值得思考的问题。移动后,请不要忘记指定
getListObject()
methody的详细信息。我将查看它,以获取帮助。非常抱歉,耽搁了