C# Shuffle with Arrays
本文关键字:Arrays with Shuffle | 更新日期: 2023-09-27 18:04:35
我的任务是创建一个洗牌方法,该方法接受洗牌次数的参数,但对于每次洗牌,1)将牌组(数组)分成两半,2)从第一副牌组开始,在两者之间交替使用牌组:
示例:Before Shuffle- AS, 2S, 3S,…、QC、KC
洗牌1次后:Ad, as, 2d, 2s,…, KC, KH
洗牌20次后:3C、5D、7H、9S、QC
这是我的桥牌构造函数:
class DeckOfCards {
private Card[] deck;
private int currentCard;
private const int NUMBER_OF_CARDS = 52; // constant number of Cards
private Random randomNumbers;
// constructor
public DeckOfCards() {
string[] faces = { "A", "2", "3", "4",
"5", "6", "7", "8",
"9", "T", "J", "Q", "K" };
string[] suits = { "S", "H", "D", "C" };
deck = new Card[NUMBER_OF_CARDS];
currentCard = 0;
randomNumbers = new Random();
for (int count = 0; count < deck.Length; count++)
deck[count] = new Card(faces[count % 13], suits[count / 13]);
}
这是我目前在main中的内容:
class Program {
static void Main(string[] args) {
int numShuffles = 0;
DeckOfCards myDeck = new DeckOfCards();
for (int i = 0; i < 13; i++) {
Console.WriteLine("{0} "+" {1} "+" {2} "+" {3}", myDeck.DealCard(), myDeck.DealCard(), myDeck.DealCard(), myDeck.DealCard());
}
Console.ReadLine();
Console.WriteLine("How Many Times Do You Want To Shuffle?");
string index = Console.ReadLine();
Console.WriteLine("You want to shuffle {0} times", index);
Console.ReadLine();
try {
numShuffles = Convert.ToInt32(index);
for (int i = 1; i <= numShuffles; i++) {
myDeck.Shuffle();
}
} catch (FormatException e) {
Console.WriteLine("Input string is not a sequence of digits.");
} catch (OverflowException e) {
Console.WriteLine("The number cannot fit in an Int32.");
Console.ReadLine();
} finally {
if (numShuffles < Int32.MaxValue) {
Console.WriteLine("Preparing to shuffle " + numShuffles + " times");
Console.ReadLine();
Console.WriteLine("{0} "+" {1} "+" {2} "+" {3} "+" {4}", myDeck.DealCard(), myDeck.DealCard(), myDeck.DealCard(), myDeck.DealCard(),
myDeck.DealCard());
Console.ReadLine();
} else {
Console.WriteLine("Your Input cannot be incremented beyond its current value");
Console.ReadLine();
}
}
}
}
}
有什么好主意吗?
这很简单:构造一个新的数组,从位置0取一张牌,从N/2取第二张牌,从1取第三张牌,从N/2+1取第四张牌,等等