C# 随机排列数组
C# Shuffle with Arrays
我的任务是创建一个洗牌方法,该方法接受洗牌次数的参数,但对于每次洗牌,1) 将牌组(数组)分成两半,以及 2) 从第一副牌开始,在两者之间交替卡片:
示例:洗牌前- 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]);
}
这是我目前主要的内容:
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取第四张,依此类推
我的任务是创建一个洗牌方法,该方法接受洗牌次数的参数,但对于每次洗牌,1) 将牌组(数组)分成两半,以及 2) 从第一副牌开始,在两者之间交替卡片:
示例:洗牌前- 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]);
}
这是我目前主要的内容:
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取第四张,依此类推