gpt4 book ai didi

c# - 在 C# 中创建一副纸牌

转载 作者:行者123 更新时间:2023-11-30 22:57:49 24 4
gpt4 key购买 nike

所以我正在尝试为我的一个编程类(class)创建一副纸牌。我从来没有真正做过这样的事情,如果我犯了一些愚蠢的错误,我深表歉意。我在 Visual Studio 中对此进行编码(根据类规则)。我正在尝试为我的 Deck 创建一个 Card 对象数组。我遇到的问题是,当我尝试打印数组时,我得到的只是 52 行 Card_Games.Card(命名空间是 Card_Games)。我在卡片类中做错了什么,没有正确地将卡片的值和花色分配给该卡片对象?

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
Deck.FillDeck();
Deck.PrintDeck();
}
}

class Card
{
public int Value;
public static string[] SuitsArray = new string[] {"Hearts", "Diamonds", "Clubs", "Spades"};
public string Suit;

public Card(int value, string suit)
{
Value = value;
Suit = suit;
}

public Card(string input)
{
string tempValue = "";
string suitSentence = "";
switch (Value)
{
case 11:
tempValue = "Jack";
break;
case 12:
tempValue = "Queen";
break;
case 13:
tempValue = "King";
break;
case 14:
tempValue = "Ace";
break;
default:
tempValue = Value.ToString();
break;
}
switch (Suit)
{
case "Hearts":
suitSentence = " of Hearts";
break;
case "Diamonds":
suitSentence = " of Diamonds";
break;
case "Clubs":
suitSentence = " of Clubs";
break;
case "Spades":
suitSentence = " of Spades";
break;
}
input = tempValue + suitSentence;
}
}

class Deck
{
public static Object[] deck = new Object[52];


public static void FillDeck()
{
int index = 0;
foreach (string suit in Card.SuitsArray)
{
for (int value = 2; value <= 14; value++)
{
Card card = new Card(value, suit);
deck[index] = card;
index++;
}
}
}

public static void PrintDeck()
{
for (int i=0; i<52; i++)
{
System.Diagnostics.Debug.WriteLine(deck[i]);
}
}
}

最佳答案

这是一个稍微优化过的版本。

我添加了 setter/getter 来提取命名值和全名,例如“黑桃 A”。并为套件使用 Enum

然后我使用了一个带有模运算符 (%) 和 Math.Floor 的循环来填充卡片组。这就是枚举发挥作用的地方,因为使用枚举和整数很简单。

public class Card
{
public enum Suites
{
Hearts = 0,
Diamonds,
Clubs,
Spades
}

public int Value
{
get;
set;
}

public Suites Suite
{
get;
set;
}

//Used to get full name, also useful
//if you want to just get the named value
public string NamedValue
{
get
{
string name = string.Empty;
switch (Value)
{
case (14):
name = "Ace";
break;
case (13):
name = "King";
break;
case (12):
name = "Queen";
break;
case (11):
name = "Jack";
break;
default:
name = Value.ToString();
break;
}

return name;
}
}

public string Name
{
get
{
return NamedValue + " of " + Suite.ToString();
}
}

public Card(int Value, Suites Suite)
{
this.Value = Value;
this.Suite = Suite;
}
}

public class Deck
{
public List<Card> Cards = new List<Card>();
public void FillDeck()
{
public void FillDeck()
{
//Can use a single loop utilising the mod operator % and Math.Floor
//Using divition based on 13 cards in a suited
for (int i = 0; i < 52; i++)
{
Card.Suites suite = (Card.Suites)(Math.Floor((decimal)i/13));
//Add 2 to value as a cards start a 2
int val = i%13 + 2;
Cards.Add(new Card(val, suite));
}
}
}

public void PrintDeck()
{
foreach(Card card in this.Cards)
{
Console.WriteLine(card.Name);
}
}
}

演示:https://dotnetfiddle.net/Xuj7b6

关于c# - 在 C# 中创建一副纸牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53383468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com