Randomizing Decks

I am trying to make War, the card game, and I want to efficiently be able to randomize and save each player’s deck at the start of the game. My current system seemed good, but it isn’t working. Here’s what I currently have:

  • Properties “DeckLeft” and “DeckRight” are text and start out blank. They represent the two players’ randomized decks.
  • Property “idx” is a number and starts at 52 (the number of cards in the deck).
  • Property “Unshuffled” is text and starts at 01020304050607080910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 (the numbers 01 through 52).
  • When the game starts, the following recursive block code is run:
  • Once idx reaches 26, it triggers the following near-identical recursive code, which sets up the second player’s deck:
  • At the end of this, the “ShuffleDone” channel broadcast signals, well, the shuffle being done. However, when I set two texts to set themselves to the left and right decks when the shuffle is finished, I get these results over three trials:
    image image image

What is the problem with my code?? Lmk if I should write a full explanation of what the block code does.

and yes i did take multiple block code screenshots and perfectly align them to get those long screenshots
4 Likes

Hi, do you want pictures?

like of what the code should look like? sure

Ok, this might take me some time to find and edit the code.

If you know what the problem is could you just explain how to fix it?

2 Likes

Ok, I’ll try. [1]


  1. Haven’t see you post on a long time. Welcome back ↩︎

1 Like

In the last if statement for both blocks, the greater than should instead be a less than or equal to. Since IDX starts at 52 and only decrements by one each time, it will instantly trigger both if statements and end the block after only 1 card has been drawn for either player.

3 Likes

Um I changed that and it didn’t work. I think that if statement is working as intended—in the first part it repeats itself if it is less than halfway through the deck, but once it’s used up the first half it gives it over to the second deck.

3 Likes

IDX counts down, such that it is the number of remaining cards, no? So when there are less or equal to 0 cards remaining it should end the block, and when there are less than or equal to 26 cards remaining, it should switch to the other deck.
I think there might be another problem with your block, I’ll look a little further.

Edit:
Nevermind! I mixed up what the if statement and else statement were doing.

3 Likes

I think you’re confusing the channels. The first one broadcasting on GameSetup is triggering itself, repeating its code, while GameSetup2 switches it to the other half.

Edit: Yeah

5 Likes

You didn’t mention having a rand property. You do have one, yes?

4 Likes

Yes, i just didn’t mention it because it doesn’t matter what it starts at. It is a number

4 Likes

You can’t take a substring that starts at letter number zero. Strings in blocks start at index 1. Instead of putting an integer 1 there, I’d recommend clicking the “letter #” drop-down and selecting first letter. Do the same for the place where you get the substring from the end of the card to the last letter of unfinished.

4 Likes


Here’s a slightly annotated version highlighting what each part does

5 Likes

Mmm I thought that might be the problem. But then what it picks card 01? If the substring starts at the first letter, won’t it always end up keeping the first letter, even if it should be removed?

3 Likes

I think you might actually be able to just leave it as-is. As long as one of the values of the substring block is zero, the code breaks and just returns an empty string. This will cause the beginning/ending part to simply not be added to the new unfinished string. A band-aid fix, I know, but you don’t have a ton of blocks remaining and if it works than I wouldn’t complain.

Btw, in both blocks rand can instead be a variable rather than a property, which is more block-efficient since you don’t need the additional get property block. It also saves you a property device, which probably won’t matter much, but you do only get 128 of them.

I suppose that’d work too. It’s probably a more… rational fix as well.

6 Likes

Oh yeah I forgot variables exist

Also maybe I just start the deck with a random symbol appended as the first and last letter, and have all the get letter of blocks be increased/decreased by 1 accordingly?

5 Likes

image
IT WORKS!!! This looks good I think!

5 Likes

Great! Make sure to mark a solution so the topic gets closed!

Edit:
Hmm. That’s a troubling issue. I have to go for now, but I’ll come back to help you later.

4 Likes

Wait… look at the end of the second deck. “010201” that’s two instances of 01.
I just ran it again and got this:
image
and it has the same issue at the end.
ruh roe

edit: just got this as well in another trial
image

Seems like there’s still an issue with the removing of used cards

I gtg for a bit but lmk if you find a solution

4 Likes