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)
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")
@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.
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.
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!
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);
generate a list containing number 1,2,3, ... 52, in which, 1-13 for spades, 14-26 for hearts.....
shuffle the list
seperate the list/cards to 2 player, for example, item 1 to 26 for player1, 27 to 52 for player2
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....
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.
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 .