本文介绍了如何使用我的Consturctor循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创造一个二十一点游戏,到目前为止,我已经制作了一个卡片类,甲板课和鞋类。卡类工作和甲板类工作,但我需要在我的鞋子构造函数中设置一个循环来为_shoe中的每个甲板调用Deck类构造函数。我不知道如何做到这一点
任何帮助将不胜感激
这里我需要放置循环
I am creating a blackjack game and so far I have made a card class, deck class and shoe class. The card class works and deck class works but I need to Set up a loop in my Shoe Constructor to call the Deck class constructor for each of the decks in the _shoe.I am not sure on how to do this
Any help would be appreciated
Here's where I need to put the loop
public Shoe(Int32 shoeDecks, Int32 shoePercantage, Int32 shuffleSwaps)
{
ShoeDecks = _shoeDecks;
ShuffleSwaps = _shuffleSwaps;
SetRedCardIndex(shoePercantage);
Shuffle();
}
如果需要,这是我的整个鞋类
And Here's my entire shoe class if needed
class Shoe
{
private Deck[] _shoe;
private Int32 _cardCount = 0;
private Int32 _shoeDecks;
private Int32 _redCardIndex;
private Int32 _shuffleSwaps;
private Random _random = new Random();
public Shoe(Int32 shoeDecks, Int32 shoePercantage, Int32 shuffleSwaps)
{
ShoeDecks = _shoeDecks;
ShuffleSwaps = _shuffleSwaps;
SetRedCardIndex(shoePercantage);
Shuffle();
}
private Int32 ShoeDecks
{
get
{
return _shoeDecks;
}
set
{
if (value <= 0)
{
throw new Exception("Number cannot be nagative");
}
else
{
value = _shoeDecks;
}
}
}
private Int32 ShuffleSwaps
{
get
{
return _shuffleSwaps;
}
set
{
if (value <= 10000)
{
throw new Exception("value out of range");
}
else
{
value = _shuffleSwaps;
}
}
}
public Boolean IsPastRedCard()
{
Boolean IsPastRedCard = false;
if (_cardCount > _redCardIndex)
IsPastRedCard = true;
return IsPastRedCard;
}
private void SetRedCardIndex(Int32 shoePercantage)
{
if (shoePercantage > 50)
{
throw new Exception("Invalid Value");
}
if (shoePercantage < 75)
{
throw new Exception("Invalid Value");
}
else
{
shoePercantage = _redCardIndex;
}
}
public Card GetCard()
{
Card card;
card = _shoe[_cardCount / 52].GetCard(_cardCount % 52);
++_cardCount;
return card;
}
public void Shuffle()
{
Int32 card1 = _random.Next(0, 52);
Int32 card2 = _random.Next(0, 52);
Int32 deck1 = _random.Next(0, ShoeDecks);
Int32 deck2 = _random.Next(0, ShoeDecks);
ShuffleSwaps = deck1;
ShuffleSwaps = deck1; _shoe[deck1].SetCard(card1, _shoe[deck2].GetCard(card2)); deck2 = ShuffleSwaps;
}
}
推荐答案
_shoe = new Deck[ShoeDecks];
// still you can't call _shoe[index].SomeMethod
// because you have null values in the array
// sample code to initialize the array with defaults Deck items
for ( int i = 0; i < _shoe.Length;i++ ) {
_shoe[i] = new Deck();
}
这篇关于如何使用我的Consturctor循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!