【问题标题】:C# Shuffle with ArraysC# 使用数组随机播放
【发布时间】:2015-07-24 14:41:52
【问题描述】:

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

示例:Shuffle 之前-AS、2S、3S、...、QC、KC

1 次洗牌后: AD, AS, 2D, 2S, ..., KC, KH

20 次洗牌后: 3C、5D、7H、9S、质控

这是我的甲板构造器:

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 循环到 HALF。每个循环采用ii + HALF,将它们添加到结果列表中。尝试一下,如果遇到困难就回来
  • 或者你可以转换this javascript solution我刚做的。尽管我在 20 次洗牌后得到的结果与你不同,即使我切换到从牌组的后半部分开始:/
  • 就算法而言,这是极其微不足道的。你试过什么吗?
  • 我还没有开始我的随机播放方法。我不确定我能用数组做什么,或者是否有更简单的方法来解决它。我要试试musefan的方法。

标签: c# arrays algorithm shuffle


【解决方案1】:

这很简单:构造一个新数组,从位置 0 取出一张卡片,从 N/2 取出第二张,从 1 取出第三张,从 N/2+1 取出第四张,等等。

【讨论】:

  • 那是不同的东西。我现在就试试。
  • 谢谢文森特。我现在都准备好了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-07
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 2023-03-10
相关资源
最近更新 更多