通过 char 数组返回一个随机值
Returning a random value via char array
我正在尝试为我的 C# class 创建一个 21 点游戏,并且我一直在尝试弄清楚如何从往复 char 数组中获取随机 suit/card 值。目标是 return 这些值到控制台应用程序到 "test" return 值并查看它们是否 correct/adequate.
我已经声明了字段、属性,并设置了构造函数。我只是不知道如何获取 return 的值以便对它们进行测试。我们也刚刚开始在我的 class 中使用这些东西。
[从下面 O.P. 的 "answer" 添加的附加信息]
现在,我们只是想获得一张转换成控制台应用程序的单卡,数组的设置是老师告诉我们使用的。
public class Card
{
//Declaration of fields
char[] suit = new char[] { 'D', 'H', 'S', 'C' }; //Backing Variables
char[] value = new char[]{'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'};
static Random random = new Random();
public Card(char s, char v) //Constructor
{
suit = s;
value = v;
}
public char Suit //Properties
{
int suitType = random.Next(0, suit.Length);
char s = suit[suitType];
get { return value; }
}
public char Value
{
get { return value; }
}
}
我自己,我会做这样的事情:
public enum CardSuit
{
Hearts = 1 ,
Spades = 2 ,
Diamonds = 3 ,
Clubs = 4 ,
}
public enum CardValue {
Ace = 1 ,
Two = 2 ,
Three = 3 ,
Four = 4 ,
Five = 5 ,
Six = 6 ,
Seven = 7 ,
Eight = 8 ,
Nine = 9 ,
Ten = 10 ,
Jack = 11 ,
Queen = 12 ,
King = 13 ,
}
public class Card : IEquatable<Card> , IComparable<Card>
{
public readonly CardSuit Suit ;
public readonly CardValue Value ;
public Card( CardSuit suit , CardValue value )
{
if ( ! Enum.IsDefined(typeof(CardSuit),suit) ) throw new ArgumentOutOfRangeException("suit") ;
if ( ! Enum.IsDefined(typeof(CardValue),value) ) throw new ArgumentOutOfRangeException("value") ;
this.Suit = suit ;
this.Value = value ;
return;
}
public override int GetHashCode()
{
int value = ((int)this.Suit << 16 )
| ((int)this.Value & 0x0000FFFF )
;
return value.GetHashCode() ;
}
public bool Equals( Card other )
{
return this.Suit == other.Suit && 0 == CompareTo(other) ;
}
public int CompareTo( Card other )
{
int cc = Math.Sign( (int)this.Value - (int) other.Value ) ;
return cc;
}
}
public class DeckOfCards
{
private static Random rng = new Random() ;
private readonly IList<Card> cards ;
public DeckOfCards()
{
this.cards = new List<Card>(52) ;
foreach( CardSuit suit in Enum.GetValues(typeof(CardSuit)) )
{
foreach( CardValue value in Enum.GetValues(typeof(CardValue)) )
{
cards.Add(new Card(suit,value));
}
}
}
public void Shuffle()
{
// Fisher-Yates (Durtensfeld) shuffle algorithm: http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
for ( int i = 0 ; i < cards.Count ; ++i )
{
int j = rng.Next(i,cards.Count) ;
Card t = cards[j] ;
cards[j] = cards[i] ;
cards[i] = t ;
}
return;
}
}
正确实现运算符比较运算符(==
、!=
、<
、<=
、>
、>=
)由你决定。
因为我们不能改变程序的结构,我会这样做:
public class Card
{
//Declaration of fields
private static char[] suits = new char[] { 'D', 'H', 'S', 'C' }; //Backing Variables
private static char[] values = new char[]{'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'};
private readonly char suit;
private readonly char value;
static Random random = new Random();
public Card() //Constructor
{
int suitIndex = random.Next(0, suits.Length);
suit = suits[suitIndex];
int valueIndex = random.Next(0, values.Length);
value = values[valueIndex];
}
public char Suit //Properties
{
get { return suit; }
}
public char Value
{
get { return value; }
}
}
字段的设置在构造函数中完成,无需传入任何参数。然后可以在您的控制台应用程序中使用这些属性,如下所示:
class Program
{
static void Main(string[] args)
{
Card aCard = new Card();
Console.WriteLine("Suit: {0}", aCard.Suit);
Console.WriteLine("Value: {0}", aCard.Value);
Console.Read();
}
}
将创建一张带有随机花色和值的卡片。正如其他人所说,随机 class 不能可靠地用于构建一副纸牌。
要构建套牌,您可以向 Card class 添加另一个构造函数,例如:
public Card(char suit, char value)
{
this.suit = suit;
this.value = value;
}
然后像这样添加一个简单的 Deck class(在此之后您可以从 Card class 中删除没有参数的构造函数以及花色和值数组):
public class Deck
{
private static char[] suits = new char[] { 'D', 'H', 'S', 'C' };
private static char[] values = new char[] { 'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K' };
private readonly List<Card> cards;
public Deck()
{
cards = new List<Card>();
foreach (char suit in suits)
{
foreach (char value in values)
{
Card card = new Card(suit, value);
cards.Add(card);
}
}
}
public List<Card> Cards { get { return cards; } }
}
然后在控制台应用程序中查看:
Deck deck = new Deck();
foreach (Card card in deck.Cards)
{
Console.WriteLine("Suit: {0}", card.Suit);
Console.WriteLine("Value: {0}", card.Value);
}
我正在尝试为我的 C# class 创建一个 21 点游戏,并且我一直在尝试弄清楚如何从往复 char 数组中获取随机 suit/card 值。目标是 return 这些值到控制台应用程序到 "test" return 值并查看它们是否 correct/adequate.
我已经声明了字段、属性,并设置了构造函数。我只是不知道如何获取 return 的值以便对它们进行测试。我们也刚刚开始在我的 class 中使用这些东西。
[从下面 O.P. 的 "answer" 添加的附加信息]
现在,我们只是想获得一张转换成控制台应用程序的单卡,数组的设置是老师告诉我们使用的。
public class Card
{
//Declaration of fields
char[] suit = new char[] { 'D', 'H', 'S', 'C' }; //Backing Variables
char[] value = new char[]{'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'};
static Random random = new Random();
public Card(char s, char v) //Constructor
{
suit = s;
value = v;
}
public char Suit //Properties
{
int suitType = random.Next(0, suit.Length);
char s = suit[suitType];
get { return value; }
}
public char Value
{
get { return value; }
}
}
我自己,我会做这样的事情:
public enum CardSuit
{
Hearts = 1 ,
Spades = 2 ,
Diamonds = 3 ,
Clubs = 4 ,
}
public enum CardValue {
Ace = 1 ,
Two = 2 ,
Three = 3 ,
Four = 4 ,
Five = 5 ,
Six = 6 ,
Seven = 7 ,
Eight = 8 ,
Nine = 9 ,
Ten = 10 ,
Jack = 11 ,
Queen = 12 ,
King = 13 ,
}
public class Card : IEquatable<Card> , IComparable<Card>
{
public readonly CardSuit Suit ;
public readonly CardValue Value ;
public Card( CardSuit suit , CardValue value )
{
if ( ! Enum.IsDefined(typeof(CardSuit),suit) ) throw new ArgumentOutOfRangeException("suit") ;
if ( ! Enum.IsDefined(typeof(CardValue),value) ) throw new ArgumentOutOfRangeException("value") ;
this.Suit = suit ;
this.Value = value ;
return;
}
public override int GetHashCode()
{
int value = ((int)this.Suit << 16 )
| ((int)this.Value & 0x0000FFFF )
;
return value.GetHashCode() ;
}
public bool Equals( Card other )
{
return this.Suit == other.Suit && 0 == CompareTo(other) ;
}
public int CompareTo( Card other )
{
int cc = Math.Sign( (int)this.Value - (int) other.Value ) ;
return cc;
}
}
public class DeckOfCards
{
private static Random rng = new Random() ;
private readonly IList<Card> cards ;
public DeckOfCards()
{
this.cards = new List<Card>(52) ;
foreach( CardSuit suit in Enum.GetValues(typeof(CardSuit)) )
{
foreach( CardValue value in Enum.GetValues(typeof(CardValue)) )
{
cards.Add(new Card(suit,value));
}
}
}
public void Shuffle()
{
// Fisher-Yates (Durtensfeld) shuffle algorithm: http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
for ( int i = 0 ; i < cards.Count ; ++i )
{
int j = rng.Next(i,cards.Count) ;
Card t = cards[j] ;
cards[j] = cards[i] ;
cards[i] = t ;
}
return;
}
}
正确实现运算符比较运算符(==
、!=
、<
、<=
、>
、>=
)由你决定。
因为我们不能改变程序的结构,我会这样做:
public class Card
{
//Declaration of fields
private static char[] suits = new char[] { 'D', 'H', 'S', 'C' }; //Backing Variables
private static char[] values = new char[]{'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'};
private readonly char suit;
private readonly char value;
static Random random = new Random();
public Card() //Constructor
{
int suitIndex = random.Next(0, suits.Length);
suit = suits[suitIndex];
int valueIndex = random.Next(0, values.Length);
value = values[valueIndex];
}
public char Suit //Properties
{
get { return suit; }
}
public char Value
{
get { return value; }
}
}
字段的设置在构造函数中完成,无需传入任何参数。然后可以在您的控制台应用程序中使用这些属性,如下所示:
class Program
{
static void Main(string[] args)
{
Card aCard = new Card();
Console.WriteLine("Suit: {0}", aCard.Suit);
Console.WriteLine("Value: {0}", aCard.Value);
Console.Read();
}
}
将创建一张带有随机花色和值的卡片。正如其他人所说,随机 class 不能可靠地用于构建一副纸牌。
要构建套牌,您可以向 Card class 添加另一个构造函数,例如:
public Card(char suit, char value)
{
this.suit = suit;
this.value = value;
}
然后像这样添加一个简单的 Deck class(在此之后您可以从 Card class 中删除没有参数的构造函数以及花色和值数组):
public class Deck
{
private static char[] suits = new char[] { 'D', 'H', 'S', 'C' };
private static char[] values = new char[] { 'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K' };
private readonly List<Card> cards;
public Deck()
{
cards = new List<Card>();
foreach (char suit in suits)
{
foreach (char value in values)
{
Card card = new Card(suit, value);
cards.Add(card);
}
}
}
public List<Card> Cards { get { return cards; } }
}
然后在控制台应用程序中查看:
Deck deck = new Deck();
foreach (Card card in deck.Cards)
{
Console.WriteLine("Suit: {0}", card.Suit);
Console.WriteLine("Value: {0}", card.Value);
}