Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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
创建一副扑克牌-不要参与扑克牌类-从Visual C#2010开始_C# - Fatal编程技术网

创建一副扑克牌-不要参与扑克牌类-从Visual C#2010开始

创建一副扑克牌-不要参与扑克牌类-从Visual C#2010开始,c#,C#,我是C#的新手,也是这个论坛的新手。两个月前决定学习c语言,并从2010年开始学习Visual c语言。直到现在才需要任何帮助。在本章(第10章)中,我必须创建一副牌。我已经用军衔和西装做了两次列举。在此之后,我创建了card类: public class Card { public readonly Rank rank; public readonly Suit suit; private Card() { } public Card(Su

我是C#的新手,也是这个论坛的新手。两个月前决定学习c语言,并从2010年开始学习Visual c语言。直到现在才需要任何帮助。在本章(第10章)中,我必须创建一副牌。我已经用军衔和西装做了两次列举。在此之后,我创建了card类:

public class Card
{
    public readonly Rank rank;
    public readonly Suit suit;

    private Card()
    {

    }

    public Card(Suit newSuit, Rank newRank)
    {
        suit = newSuit;
        rank = newRank;
    }

    public override String ToString()
    {
        return "The " + rank + "of " + suit + "s";
    }
}
在此之后,我必须上甲板课:

public class Deck
{
    private Card[] cards;

    public Deck()
    {
        cards = new Card[52];
        for (int suitVal = 0; suitVal < 4; suitVal++)
        {
            for (int rankVal = 1; rankVal < 14; rankVal++)
            {
                **cards[suitVal * 13 + rankVal -1] = new Card((Suit)suitVal,(Rank)rankVal);**
            }                
        }
    }
公共类甲板
{
私人卡[]卡;
公共甲板()
{
卡片=新卡片[52];
对于(int-suiteval=0;suiteval<4;suiteval++)
{
对于(int rankVal=1;rankVal<14;rankVal++)
{
**卡片[Suiteval*13+rankVal-1]=新卡片((套装)Suiteval,(等级)rankVal)**
}                
}
}
还有更多的牌类,但我只是没有得到加粗的部分(13至少有意义,感觉whise,因为有13张牌每套西装,但我真的不能放置-1)。到底发生了什么在甲板类,特别是在部分加粗


提前感谢

这是一个介于
0..51之间的索引:

for (int suitVal = 0; suitVal < 4; suitVal++)
{
    for (int rankVal = 1; rankVal < 14; rankVal++)
    {
        int cardIndex = suitVal * 13 + rankVal - 1;
        cards[cardIndex] = new Card((Suit)suitVal,(Rank)rankVal);
    }                
}
for(int-suiteval=0;suiteval<4;suiteval++)
{
对于(int rankVal=1;rankVal<14;rankVal++)
{
int cardIndex=suitVal*13+rankVal-1;
卡片[cardIndex]=新卡片((套装)suitVal,(等级)rankVal);
}                
}
使用
(suitVal*13)+(rankVal-1)
可以访问阵列中的特定卡。因为
rankVal
1开始,所以必须减去一个。

suitVal = 0; rankVal = 1;
您需要制作牌组中的第一张牌,第一张牌位于索引位置
0

suitVal * 13 + rankVal - 1 = 0 * 13 + 1 - 1 = 0; <-- exactly what you need
等等,等等,直到

suitVal = 3; rankVal = 13;  //index should be 51, last one
suitVal * 13 + rankVal - 1 = 3 * 13 + 13 - 1 = 51; <-- exactly what you need
suitVal=3;rankVal=13;//索引应该是51,最后一个

suitVal*13+rankVal-1=3*13+13-1=51;检查两个循环。外部循环从零开始,内部循环从1开始。因为一套衣服有13张牌长,但计数从[0]开始,而不是[1]1被减去以说明这一事实。我希望秩不是一个0索引的枚举。。。
suitVal = 1; rankVal = 1;  //index should be 13
suitVal * 13 + rankVal - 1 = 1 * 13 + 1 - 1 = 13; <-- exactly what you need
suitVal = 3; rankVal = 13;  //index should be 51, last one
suitVal * 13 + rankVal - 1 = 3 * 13 + 13 - 1 = 51; <-- exactly what you need