Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement a dealer class without storing a deck of cards?
    primarykey
    data
    text
    <ul> <li><p><strong>Question</strong></p> <p>Even only 52 cards, the <code>permutationIndex</code> where I describe in <em>Explanations</em> section, would be a huge number; it is a number in one of <code>52!</code>, and need 29 bytes to store. </p> <p>Thus <strong>I don't know a simple way</strong> to calculate the <code>permutationIndex</code> 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:</p> <ol> <li><p>An algorithm which compute the correct <code>permutationIndex</code> to <strong>implement the <code>Dealing</code></strong> method </p></li> <li><p>An algorithm which compute the correct <code>permutationIndex</code> to <strong>implement the <code>Collect</code></strong> method </p></li> <li><p>An algorithm which stores(or computes) <code>permutationIndex</code> <strong>with a minimal cost</strong></p></li> </ol></li> <li><p><strong>Explanations</strong></p> <p>I originally try to implement a <em>integer handle generator</em> of a range from <code>int.MinVale</code> to <code>int.MaxValue</code> using permutation. </p> <p>Because the range is really huge for that, thus I start from implement <strong><em>a <code>Dealer</code> class with 52 cards which doesn't really store a deck of cards</em></strong> like hashset or array, and even <strong>don't want random</strong>(except initial). </p> <p>With a given range of ordinal numbers, I consider every sequence of one of full permutations has a index, and named it <code>permutationIndex</code>. 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. </p> <p>And here is an example of emulation in animated graphics to show what I thought of. <img src="https://i.stack.imgur.com/wyZKK.gif" alt="Enter image description here"> </p> <p>Everytime I dealt a card, I change the <code>permutationIndex</code> and <code>dealt</code>(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, <code>colleted</code> is the <em>card number</em>. </p></li> </ul> <hr> <p>For more information, as follows. </p> <ul> <li><p>Description of code</p> <p>A conceptual sample <code>Dealer</code> class for only three 3 is as following. The code is written in <a href="/questions/tagged/c%23" class="post-tag" title="show questions tagged &#39;c#&#39;" rel="tag">c#</a>, and I'm also considering any <a href="/questions/tagged/language-agnostic" class="post-tag" title="show questions tagged &#39;language-agnostic&#39;" rel="tag">language-agnostic</a> solutions. </p> <p>Here're some descriptions of the sample code</p> <blockquote> <ul> <li><p>With the method <code>Dealing()</code>, we get a <em>number</em> of the card which treat as dealt. It always returns the <strong><em>right most</em></strong> number (relevant to the array) and then rolls the number left from it (say the next available) to the right most position by changing <code>permutationIndex</code>.</p></li> <li><p>The method <code>Collect(int)</code> is for collecting and put the dealt cards back into the deck. It would change <code>permutationIndex</code> also, according to what the <em>number</em> of card was returned back to the dealer.</p></li> <li><p>The integer <code>dealt</code> tells how many cards we've dealt; from the <strong><em>left most</em></strong> to the count stored in <code>dealt</code> are dealt cards. With <code>permutationIndex</code>, we know the sequence of cards.</p></li> <li><p>The <code>int[,]</code> array in the sample code is not used, just for helping imagine the permutations. The <em>switch</em> statements are considered to be implemented with algorithms which compute for the <code>permutationIndex</code>.</p></li> <li><p>The <code>permutationIndex</code> is the same thing described in this answer of <br/> <a href="https://stackoverflow.com/questions/1506078/fast-permutation-number-permutation-mapping-algorithms/1506337#1506337">Fast permutation -&gt; number -&gt; permutation mapping algorithms</a></p></li> </ul> </blockquote></li> <li><p>Sample code</p> <pre><code>public static class Dealer { public static void Collect(int number) { if(1&gt;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&gt;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; } </code></pre></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload