Using lists to improve shuffling a deck

So I’ve been developing this card game for a personal project, it’s a class I have to take, and I’ve decided to make a virtual card game and I think this could be a good way to make sure the same card won’t be picked twice, but I think this creates unnecessary lag. Is there anyway to improve it?

You could maybe try making a list of cards that haven’t been used, and then have it draw from that.

I think that might work, but I don’t know how to use lists…

So I’ve been developing this card game for a personal project, it’s a class I have to take

Which is it, a personal project or your homework? That affects what kind of help we offer.

Those 52 nested if/else blocks look painful. One easy thing you could do to improve your code is to learn to love the multi-way IF:


… and so on.

But that’s still a 52-way branch. If you pick your random numbers from 0 to 51, you can use
untitled script pic (8)
for the suit of the card and
untitled script pic (10)
for the rank.

But broadcasting one of 52 different messages is still going to be painful, especially because it means that elsewhere in your code you have 52 almost-identical WHEN I RECEIVE scripts. Are you a recent immigrant from Scratch? In Snap! you can use procedure calls with inputs instead of BROADCAST, so you’d say
untitled script pic (11)
and not need any IFs at all!

But even that isn’t the best way to do this. If this is a game of cards, you’re going to keep on wanting more cards as the game progresses. So you should directly model the way you play with real cards: make a full deck of 52 cards, shuffle the deck, and then just deal cards from the top of the deck as needed.





Then make a DEAL CARD block that takes the first item of DECK as the value to report, and sets DECK to ALL BUT FIRST OF what it used to be.

The only tricky part about creating this code is that you have to find the
untitled script pic (17)
block in the palette and click the left arrowhead twice to remove its inputs.

set to ((list <>))

I would post a script pic, but I can’t for some reason. You’ll figure it out.

If you can’t send script pics, then you can use snapblocks. For example, you can do this to show that script.

[snapblocks]
set [list V] to (list [] @<>)
[/snapblocks]
set [list V] to (list [] @<> )

If you can’t send script pics, then you can use snapblocks.

… and you’ll be able to post pictures once you’ve been reading the forum for a little while. :~)

@s8928538: Is it okay if I move this thread to Tutorials? I think it’s a nice example of thinking with lists.

This is a really great “using lists” example. :+1:

the class is called personal project, it is homework

Yeah I don’t really mind

we aren’t supposed to do your homework…

we aren’t supposed to do your homework…

Right, we aren’t supposed to give them completed code. But we can answer questions with general principles rather than code specific to their problem. I probably said too much in my response, but clearly the OP wasn’t expected to know about lists yet altogether.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.