如何实现经销商类不存储一副扑克牌?扑克牌、如何实现、经销商

2023-09-11 03:33:46 作者:一场一个人的私奔

问题

即使只有52卡, permutationIndex ,我描述的说明的部分,将是一个庞大的数字;它是一个在数 52 之一,需要29字节来存储。

Even only 52 cards, the permutationIndex where I describe in Explanations section, would be a huge number; it is a number in one of 52!, and need 29 bytes to store.

这样的我不知道一个简单的方法以计算一个大范围的 permutationIndex ,和索引存储与成本极小子,或者它也可以计算出来。我想这个问题的解决办法是三种算法:

Thus I don't know a simple way to calculate the permutationIndex of a huge range, and store the index with a mininal cost, or maybe it can also be calculated. I'm thinking solution of this question would be three algorithms:

该计算的算法正确的 permutationIndex 为实施处理 方法

An algorithm which compute the correct permutationIndex to implement the Dealing method

该计算的算法正确的 permutationIndex 为实施收集 方法

An algorithm which compute the correct permutationIndex to implement the Collect method

这是算法店(或计算) permutationIndex 以最低的成本

An algorithm which stores(or computes) permutationIndex with a minimal cost

说明

我本来试图从 int.MinVale 实施一系列的整数手柄发电机到 int.MaxValue 使用置换。

I originally try to implement a integer handle generator of a range from int.MinVale to int.MaxValue using permutation.

由于范围确实是巨大的对于这一点,所以我从开始实施的的在经销商类,52张牌并不真正存储平台卡的类似的HashSet或数组,甚至不想随机(除初始)。

Because the range is really huge for that, thus I start from implement a Dealer class with 52 cards which doesn't really store a deck of cards like hashset or array, and even don't want random(except initial).

通过序数一个给定的范围内,我认为全排列之一的每个序列有一个指标,并把它命名为 permutationIndex 。我用的是指数记住哪个频带排列它并没有真正存储序列。该序列是卡的走带机构的可能的顺序中的一个。

With a given range of ordinal numbers, I consider every sequence of one of full permutations has a index, and named it permutationIndex. I use the index to remember which permutation it is and don't really store a sequence. The sequence is one of the possible order of the deck of card.

这里是模拟的动画图形的一个例子来说明我的想法的。

And here is an example of emulation in animated graphics to show what I thought of.

每当我处理一张卡,我改变了 permutationIndex 处理(中发牌数),我知道哪些卡是那些处理,并且仍然在手。当我收集了处理卡回来,我就知道卡号,并把它放在上面,它也成为了卡下次来处理。在动画中, colleted 是卡号的。

Everytime I dealt a card, I change the permutationIndex and dealt(count of dealt cards), that I know which cards are those dealt, and which are still in hand. When I collect a dealt card back, I'll know the card number, and put it on the top, it's also become the card for next time to deal. In the animation, colleted is the card number.

有关详细信息,如下所示。

For more information, as follows.

的code说明

Description of code

一个概念性样品经销商班只有三个3如下。 在code被写入在 C# ,而且我也在考虑任何语言无关解决方案。

A conceptual sample Dealer class for only three 3 is as following. The code is written in c#, and I'm also considering any language-agnostic solutions.

样本code我这里还有说明

Here're some descriptions of the sample code

   枣庄方言扑克牌,看了乐半天

使用方法处理(),我们得到的号的它当作处理卡。它总是返回的的最右边的的号码(相关数组),然后滚动,通过改变从它留下的号码(说下可用)到最右侧的位置 permutationIndex

With the method Dealing(), we get a number of the card which treat as dealt. It always returns the right most number (relevant to the array) and then rolls the number left from it (say the next available) to the right most position by changing permutationIndex.

方法收集(INT)是收集并把处理卡放回甲板。   它会改变 permutationIndex 同时,根据是什么卡的号的被返回给经销商。

The method Collect(int) is for collecting and put the dealt cards back into the deck. It would change permutationIndex also, according to what the number of card was returned back to the dealer.

整数处理讲述有多少卡,我们已经处理;从的最左边的的以存储在计数处理的发牌。随着 permutationIndex ,我们知道卡的顺序。

The integer dealt tells how many cards we've dealt; from the left most to the count stored in dealt are dealt cards. With permutationIndex, we know the sequence of cards.

INT [,] 数组中的样本code没有被使用,只是为了帮助想象中的排列。在开关的声明被认为与计算的算法来实现的 permutationIndex

The int[,] array in the sample code is not used, just for helping imagine the permutations. The switch statements are considered to be implemented with algorithms which compute for the permutationIndex.

permutationIndex 是的这个答案所描述的同样的事情   Fast排列 - >数 - >置换映射算法

The permutationIndex is the same thing described in this answer of Fast permutation -> number -> permutation mapping algorithms

样品code

public static class Dealer {
    public static void Collect(int number) {
        if(1>dealt)
            throw new IndexOutOfRangeException();

        switch(permutationIndex) {
            case 5:
            case 0:
                switch(number) {
                    case 3:
                        break;

                    case 2:
                        permutationIndex=1;
                        break;

                    case 1:
                        permutationIndex=4;
                        break;
                }

                break;

            case 4:
            case 3:
                switch(number) {
                    case 3:
                        permutationIndex=5;
                        break;

                    case 2:
                        permutationIndex=2;
                        break;

                    case 1:
                        break;
                }

                break;

            case 2:
            case 1:
                switch(number) {
                    case 3:
                        permutationIndex=0;
                        break;

                    case 2:
                        break;

                    case 1:
                        permutationIndex=3;
                        break;
                }

                break;
        }

        --dealt;
    }

    public static int Dealing() {
        if(dealt>2)
            throw new IndexOutOfRangeException();

        var number=0;

        switch(permutationIndex) {
            case 5:
                permutationIndex=3;
                number=3;
                break;

            case 4:
                permutationIndex=0;
                number=1;
                break;

            case 3:
                permutationIndex=1;
                number=1;
                break;

            case 2:
                permutationIndex=4;
                number=2;
                break;

            case 1:
                permutationIndex=5;
                number=2;
                break;

            case 0:
                permutationIndex=2;
                number=3;
                break;
        }

        ++dealt;
        return number;
    }

    static int[,] sample=
        new[,] {
            { 1, 2, 3 }, // 0
            { 1, 3, 2 }, // 1
            { 3, 1, 2 }, // 2
            { 3, 2, 1 }, // 3
            { 2, 3, 1 }, // 4
            { 2, 1, 3 }, // 5
        };

    static int permutationIndex;
    static int dealt;
}

推荐答案

您已经工作 - 和非常高效的C#示例的的顺序的第k个排列ñ的(又名PermutationIndex )在这个非常旧的文章:

You have working - and extremely efficient c# example for The kth Permutation of Order n (aka PermutationIndex) at this very old post:

http://msdn.microsoft.com/en-us/库/ Aa302371.aspx#permutat_topic3​​

对于那些有兴趣在组合主题:

For those interested in Combinations topic:

http://msdn.microsoft.com/en-us/magazine/ cc163957.aspx http://msdn.microsoft.com/en -us /库/ Aa289166(VS.71)的.aspx http://msdn.microsoft.com/en-us/magazine/cc163957.aspx http://msdn.microsoft.com/en-us/library/Aa289166(VS.71).aspx

我建议你阅读,才能进入具体实施。

I suggest that you read through, before going into specific implementation.