C#随阵列播放

时间:2015-07-24 14:41:52

标签: c# arrays algorithm shuffle

我的任务是创建一个shuffle方法,该方法接受多次刷洗的参数,但是对于每次shuffle,1)将牌组(阵列)拆分为一半,2)从第一个牌组开始,交替牌两者之间:

示例:在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]);
         } 

这是我到目前为止的主要内容:

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();
            }
        }

    } 

} 

}

关于如何做到这一点的任何想法?

1 个答案:

答案 0 :(得分:1)

非常简单:构建新阵列,从0位置取一张牌,从N / 2取第二张,从N / 2 + 1取第三张,等等。