Using Lists of Lists for Random Card Shuffles

I'm making an app to play the card game "War", and my first problem is getting a random shuffle. I have a list with 13 other lists in it, each with the numbers 1-4 (representing the card values, then the suits). Since suits don't matter in War, I was going to just use indexes of the first "layer" to determine which card is of higher value. I have an identical list of numbers that will represent the "position" of each card (your vs opponent's discard pile or deck).


At the start of the game, I want to sort 26 random cards into two separate piles, without overlap of course (every card is sorted once). With my lists of lists setup, I'm not quite sure how indexes would work or how to set up the random shuffle. If this can be done with this setup, I'd be very grateful.
If it would work better for me to use a 52-item list or some dictionary format, I can also figure out how to do that.
(any help with the mechanic where you draw the same card and "go to war" would also be very much appreciated)

Sajal's Shuffling Lists tutorial

Dropping from 2 dimensional lists to single dimensional lists would simplify your problem considerably.

Consider combining the suit (S=Spades, H=Hearts, D=Diamonds, C=Clubs) and the value (01=Ace, 02-10, 11=Jack,12=Queen,13=King) into text representations of each card, like

  • 01S = Ace of Spades
  • 13H = King of Hearts
  • 03C = Three of Clubs

That gives you unique card IDs, that fit well with using the text Segment block for extraction of value and suit from the card name, and the leading zeroes let you compare numerically using text comparison.

Numerical values can be used as indexes into a constant list of value names ("Ace", Two", ...,"Queen", "King")

1 Like

@Mary_L220 I'll be back in hour or so with a working one. Or at least an example.
@ABG I'm using a list of list of numbers combined with their "family", Spades 1, etc.

@Mary_L220 I think I have a final version. Tell me if it works.

Blocks

Here are the blocks used:

Example project

List_of_lists_Random_sort.aia (5.6 KB)

I checked to see if there were duplicates by putting an if statement evaluating whether that unique card id was already added to the list. I also randomly sorted them using a random integer block to decide which deck to add it to. Finally, when all that was done, I had it check whether the decks' indexes were of the same length. If it was, it would set the listviewers player 1 and player 2 to the lists deck 1 and deck 2. otherwise it would call it again. And again if need be until they are. If there is lag, it will be because of this.
You should add some way that checks to see if it's done (and doesn't let the user move on until it is) or notifies them that they can move on.

It isn't the most efficient, but I'm proud of it. The fact is, I didn't even use a tutorial.

@ABG @SteveJG @Taifun What do you guys think?
I added you @Taifun because you have tutorials for everything.

Edit: ALL BLOCKS can be DRAGGED into AI2. AI2 will read the hidden text in the image, and recreate the blocks.

Another card shuffling demo (aia is big because it includes cards!!) that may help.

You can shuffle a full deck then view each card in turn by clicking on the deck

CardsShuffle.aia (3.0 MB)

1 Like

@Mary_L220 Has it worked?

It works, but I think I might take ABG's advice and do a single list. It's easier for me to understand and implement. Props to you for doing it without a tutorial, though!

1 Like

I figured out my own method. I changed it to have two lists with 52 items, one with the cards and one that holds the corresponding "positions" of each card (1=your deck, 2=opponent's deck). At the start of each game, I need to change half of the values in the "position" list to 1 and the other half to 2. I came up with this procedure. The beginning is actually the only time you'll ever need to shuffle in War, so I can get away with assuming all the values in "position" will be 0 before the procedure is run. I tested it and it works!


The procedure puts a list of random index values into its local indexList if that idex is not already present. It then goes through every item in "position" with a matching index and sets its value to 1, then goes through "position" again and sets the rest of the values to 2.
Thanks to all that have helped me!

The easiest way is to use list (not list of list);

  1. generate a list containing number 1,2,3, ... 52, in which, 1-13 for spades, 14-26 for hearts.....
  2. shuffle the list
  3. seperate the list/cards to 2 player, for example, item 1 to 26 for player1, 27 to 52 for player2
  4. use the Quotient of (num-1) / 13 +1 to check the suit, 1 for spade, 2 for hearts....
    and the reminder of (num-1)/13 +1 to get the index/value. 1 for ace, 11 for jack, 12 for queen....
3 Likes

That's what I eventually did. @ABG and @TIMAI2 have been helping me to get a better version, and what you said kinda sound like my souped-up one I'm using. It is so cool that for something as simple (yet complex when you try it) as sorting and dealing a deck has so many different ways and solutions to do it. Thanks @Mary_L220 for the idea, and @KevinWang for yet another way to do it.

Here's my current version:

https://community.appinventor.mit.edu/uploads/short-url/pgwpCwP9zjUIRNdOtSntmRmUCZn.aia

Note that the extra procedures are for future games, like solitaire and war. Right now I'm working on the cards in bobsprite

Can someone help me on how to unshuffle the cards ? I am creating sound game app with matching type. But the photos are shuffling and the sound of the animals is not shuffling. Can somebody help ? Please .

Can you share your relevant blocks and explain in detail the problem you have?

EDIT: Anyway, I suppose you are doing something like this:

maybe you can find the solution (or a better way) there.

(Canned Reply: ABG - Download those blocks and post them here)

Please download and post each of those event block(s)/procedures here ...
P.S. These blocks can be dragged directly into your Blocks Editor workspace.

See Download Block Images for a demo.

do you means that, you have 2 lists, one for photos one for sounds, you shuffled both lists, but now they are not in same order?

You can make them in one list, like
[ ["name1", "sound1"], ["name2", "sound2"],["name3", "sound3"],["name4", "sound4"] ]

now you can shuffle your list, the photo name and sound file are in a list, name at index 1, and sound at index 2.

1 Like

Sir here is my project sir . Can you check sir ?

The list of lists should be hidden behind a value procedure mapping pictures into sounds.

Only the pictures would need shuffling in this approach.

Can you send ang image of sample code sir ?


1 Like

Can i send to you my apk or aia sir. For you to see my codes ?