Deck.split() weirdness

Deck.split() weirdness - Woman Wearing Make-up and Black Contact Lens

So, I'm trying to take a deck of cards, split it into piles, and then shuffle one each of another stack of cards into the piles. The split() function is perfect, as I want the piles to end up 90* to each other anyway. So the script works like this so far:

  1. Get the two decks from the zones they're sitting in (deck and discard zones)
  2. Count the cards in the "discard" deck
  3. Split the main deck by that count
  4. Move one each of the "discard" cards into each pile
  5. Shuffle the piles

So far so good. The game itself expects the "small" plies to be on the bottom. This, with the split() function, is behaving a bit weird though - the counts are a little bizarre. In my test setup (and how the game will start), I have 48 cards in the main deck, with 5 cards to split in from the "discard" deck. This results in splitting the main deck into 5 piles, of 10,10,10,9,9.

I get piles that size, but if I use a for loop to get which index of the resulting table has which counts, I get the following - indexes 1, 2, and 5 have 10 cards, while 3 and 4 have 9. Even more maddeningly, after a fair amount of testing, I find that Index 1 is the second pile from the bottom, while Index 5 is the bottom pile! The piles count UP from the second from the bottom, and wrap around from the top (which is index 4) back to the bottom!

Complicate matters with the fact that the number of cards in either deck could vary during the game (more "deck" cards to split, or more "discard" cards to force more piles to be made), so I'd like to TRY to avoid some sort of hacky solution.

Anyone have any idea what is going on? Any suggestions about how to get things the way I want them? Pastebin with the code: https://pastebin.com/Q7V0y1ET



Best Answer

My suggestion:

Be liberal in what you accept, and conservative in what you emit.

In other words, make no assumptions about the order and size of stacks.

  1. Put the references to the stacks in an array,
  2. sort that array by size,
  3. Place the elements of the sorted array in that order on top of eachother.

Possible cause (untested!):

These two lines of code jump out at me:

for i=1, splits do
    print(i, " = ", deck1split[i].getQuantity())

Computers usually count from zero. Tabletop-simulator may be trying to 'help' you by wrapping your array indexes for you. It makes sense, index 5 is actually index 0, which is at the bottom of the pile and 10 long. What happens if you countfor i=0, splits-1?

If that works, then a much simpler solution is to count backwards from splits-1 to zero. Mind to test if this works rigorously, with various sizes of pile, both divisible and indivisible by the number of splits.




Pictures about "Deck.split() weirdness"

Deck.split() weirdness - Mysterious little girl standing in darkness covered with white blanket as ghost and shining flashlight on face
Deck.split() weirdness - Creepy Eyes
Deck.split() weirdness - Colorful Cubes And Puzzle Piece





THIS DECK WILL BE TIER 0 !!! OVERPOWERED SPLIGHT COMBOS ft. Frogs \u0026 Evil Twins ! Yu-Gi-Oh




Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: Elīna Arāja, Charles Parker, Elīna Arāja, Mike B