gpt4 book ai didi

c# - 如何停止并确保 OOP 代码正常工作?

转载 作者:太空宇宙 更新时间:2023-11-03 12:55:44 25 4
gpt4 key购买 nike

这确实是我尝试使用 OOP 原则编写的第一个程序。我还不知道如何让所有的类相互交互。我想我已经初始化了一副纸牌,但我只想查看其中一个纸牌对象并确保它看起来正确。我想我可以用 Console.WriteLine(cards[0]) 来做到这一点,但不知道把它放在哪里才能显示。在主要方法中?在我初始化甲板的类(class)里?如果我把它放在 main 方法中,它不会识别 cards[0],如果我把 WriteLine 放在 deck 类中,它也不会显示任何内容。有没有不同的方法来逐步查看?谢谢!

卡片类

namespace BlackJack
{


public enum suits {
Spades, Hearts, Clubs, Diamonds
}

public enum cardValues {
Two=2,
Three=3,
Four =4,
Five=5,
Six=6,
Seven=7,
Eight=8,
Nine=9,
Ten=10,
Jack=10,
Queen=10,
King=10,
Ace=11

}
public class Card
{
public suits suit { get; set; }
public cardValues cardValue { get; set; }

}
}

甲板类

namespace BlackJack
{

public class Deck
{
private List<Card> cards;

public Deck()
{
this.newDeck();
}

public void newDeck()
{
cards = new List<Card>();

for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 13; j++)
{
cards.Add(new Card() { suit = (suits)i, cardValue = (cardValues)j });
Console.WriteLine(cards[1]);
Console.ReadLine();
}


}
}
}
}

最佳答案

好的,您需要做的一件事是重载 Card 类的 ToString(),以便它知道如何将其转换为字符串。

你是这样类的吗:

public class Deck
{
//This needs to be public on the get side to be visible outside
//of the Deck object
public IList<Card> Cards { get; private set; }

public Deck()
{
NewDeck();
}

#region Helper Methods

private void NewDeck()
{
Cards = new List<Card>();

for (var i = 0; i < 4; i++)
{
//Start your index at 2 since you enum starts at Two = 2
//just as an FYI it's bad practice to not start an enum
//without a zero based answer.
for (var j = 2; j < 13; j++)
{
Cards.Add(new Card {Suit = (Suit)i, Value = (CardValue)j } );
}
}
}

#endregion Helper Methods
}

public enum Suit
{
Spades,
Hearts,
Clubs,
Diamonds
}

public enum CardValue
{
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7,
Eight = 8,
Nine = 9,
Ten = 10,
Jack = 10,
Queen = 10,
King = 10,
Ace = 11

}
public class Card
{
public Suit Suit { get; set; }
public CardValue Value { get; set; }

//Tell your card class how to turn into a string representation
public override string ToString()
{

return $"{Value} of {Suit}"; //This is C# 6
//return string.Format("{0} of {1}", Value, Suit); <-- if less than C#6
}
}

由于您在主程序中使用 Console.WriteLine(),因此看起来您正在使用控制台应用程序,因此您可以在测试时以这种方式检查所有卡片。

public static void Main()
{
var deck = new Deck();
foreach (var card in deck.Cards)
{
Console.WriteLine(card.ToString());
}
}

我的最后一件事是,您将不得不更改初始化套牌的方式。因为你有 Ten,Jack、Queen、King 都等于 10,你的 Value = (CardValue)j 将不知道选择哪一个。但这应该足以让您摆脱在弄清楚这一点时看不到卡片值(value)的困境,然后您可以继续前进。

编辑:--------------------------------在使用 StackOverflow 聊天后,我们聊了聊并弄清楚了如何最好地初始化他的 Blackjack Value,同时又不破坏基本的 Card 对象超过它的 Suit 和 Face。由于对话时间变长,还添加了 Shuffle。但所有 OOP 都已满足。

public static void Main()
{
var deck = new BlackJackDeck();

deck.Shuffle();

foreach (var card in deck.Cards)
{
Console.WriteLine(card.ToString());
}
}
public class BlackJackDeck
{
private const int DECKSIZE = 52;
private Random random;

//Card is your key, and int is the value of the card
public IList<BlackJackCard> Cards { get; private set; }


public BlackJackDeck()
{
random = new Random();
NewDeck();
}

public void Shuffle()
{
//Documentation: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
for (var i = 0; i < Cards.Count; i++)
{
var position = random.Next(i);
var temp = Cards[i];
Cards[i] = Cards[position];
Cards[position] = temp;
}
}

#region Helper Methods

private void NewDeck()
{
Cards = new List<BlackJackCard>(DECKSIZE);

for (var i = 0; i < 4; i++)
{
for (var j = 0; j < 13; j++)
{
switch (j)
{
case 8:
case 9:
case 10:
case 11:
Cards.Add(new BlackJackCard { Suit = (Suit)i, Face = (CardFace)j, Value = 10});
break;
case 12:
Cards.Add(new BlackJackCard { Suit = (Suit)i, Face = (CardFace)j, Value = 11});
break;
default:
Cards.Add(new BlackJackCard { Suit = (Suit)i, Face = (CardFace)j, Value = (j + 2)});
break;
}
}
}
}

#endregion Helper Methods
}

public enum Suit
{
Spades,
Hearts,
Clubs,
Diamonds
}

public enum CardFace
{
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King,
Ace
}

public class BlackJackCard : Card
{
public int Value { get; set; }

public override string ToString()
{
//MSDN Documentation: https://msdn.microsoft.com/en-us/library/dn961160.aspx
//base.ToString() is the Card classes ToString()
return $"{base.ToString()} with value: {Value}";
}
}

//this is for making a basic card and deck of cards
//Inherit from this to create cards specific to different game rules, i.e BlackJack, Go Fish, etc
//Documentation: https://msdn.microsoft.com/en-us/library/k535acbf(v=vs.71).aspx
public abstract class Card
{
public Suit Suit { get; set; }
public CardFace Face { get; set; }

public override string ToString()
{
return $"{Face} of {Suit}";
}
}

关于c# - 如何停止并确保 OOP 代码正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33980025/

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