This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-05-19 21:32:59

jsoares
New Scratcher
Registered: 2012-05-17
Posts: 5

How to manipulate a deck of playing cards

I have a regulation 52 card deck...I made it one sprite with 52 costumes. How can I deal cards out on the stage? It seems like I have to have all the costumes on the stage at once, which does not make sense.

How can I, for example, deal four cards to four plays(w est, north, east, and south)? South will be the dealer and he will deal one card to west, one to north, one to east, one to south and so on until each player has four cards.

The tutorials don't seem to cover this though I've seen a few card games, but the logic was difficult to follow. All I want to know for now is how to deal 16 cards out on the stage for all to see.

Thanks.

[Name and email removed by mod - please don't share personal info]

Last edited by Harakou (2012-05-20 00:02:38)

Offline

 

#2 2012-05-19 22:35:16

ManaUser
Scratcher
Registered: 2009-03-11
Posts: 100+

Re: How to manipulate a deck of playing cards

Until cloning comes along, I would think Stamp is your best bet.


http://i.imgur.com/SPYSM.gif http://i.imgur.com/t9k1Z.gif http://i.imgur.com/OwYVa.gif http://i.imgur.com/0qlZq.gif

Offline

 

#3 2012-05-19 23:03:39

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: How to manipulate a deck of playing cards

First of all, don't post personal information such as full names or emails...Scratch team doesn't like it for liability reasons and such, plus it's not a good idea in general.  If you have 52 sprites this is going to be difficult...better to put all the costumes in one sprite.  However, either way you're going to need lists.  One list per hand probably, one for shuffling and one for being shuffled.

The list for being shuffled (call it "unshuffled") will simply have either the numbers 1-52 or 1-13 and one of the characters h, d, s, or c.  In order to shuffle this into the shuffled deck, use a script like this:

when gf clicked
delete [all v] of [shuffled v]
delete [all v] of [unshuffled v]
set [char v] to [d]
set [# v] to (0)
repeat (13)
  change [# v] by (1)
  add (join((#)|(char))) to [unshuffled v]
end
set [char v] to [s]
set [# v] to (0)
repeat (13)
  change [# v] by (1)
  add (join((#)|(char))) to [unshuffled v]
end
set [char v] to [h]
set [# v] to (0)
repeat (13)
  change [# v] by (1)
  add (join((#)|(char))) to [unshuffled v]
end
set [char v] to [c]
set [# v] to (0)
repeat (13)
  change [# v] by (1)
  add (join((#)|(char))) to [unshuffled v]
end
repeat (52)
  set [# v] to (pick random (1) to (length of [unshuffled v]))
  add (item (#) of [unshuffled v]) to [shuffled v]
  delete (#) of [unshuffled v]
end
This should produce a well-shuffled deck in the list called "shuffled."

After you "shuffle" the deck, you can distribute by simply "passing out" the items of your now shuffled lists to the hands of the players.

Last edited by AtomicBawm3 (2012-05-19 23:11:34)


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#4 2012-05-20 00:07:57

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: How to manipulate a deck of playing cards

AtomicBawm3 wrote:

First of all, don't post personal information such as full names or emails...Scratch team doesn't like it for liability reasons and such, plus it's not a good idea in general.  If you have 52 sprites this is going to be difficult...better to put all the costumes in one sprite.  However, either way you're going to need lists.  One list per hand probably, one for shuffling and one for being shuffled.

The list for being shuffled (call it "unshuffled") will simply have either the numbers 1-52 or 1-13 and one of the characters h, d, s, or c.  In order to shuffle this into the shuffled deck, use a script like this:

when gf clicked
delete [all v] of [shuffled v]
delete [all v] of [unshuffled v]
set [char v] to [d]
set [# v] to (0)
repeat (13)
  change [# v] by (1)
  add (join(#) (char)) to [unshuffled v]
end
set [char v] to [s]
set [# v] to (0)
repeat (13)
  change [# v] by (1)
  add (join(#) (char)) to [unshuffled v]
end
set [char v] to [h]
set [# v] to (0)
repeat (13)
  change [# v] by (1)
  add (join(#) (char)) to [unshuffled v]
end
set [char v] to [c]
set [# v] to (0)
repeat (13)
  change [# v] by (1)
  add (join(#) (char)) to [unshuffled v]
end
repeat (52)
  set [# v] to (pick random (1) to (length of [unshuffled v]))
  add (item (#) of [unshuffled v]) to [shuffled v]
  delete (#) of [unshuffled v]
end
This should produce a well-shuffled deck in the list called "shuffled."

After you "shuffle" the deck, you can distribute by simply "passing out" the items of your now shuffled lists to the hands of the players.

I just fixed up the "join" block. 

For dealing out the cards, you can create a list for each player's hand (north, south, east, west), and then do something like this.

repeat (round((52) / (NumberOfPlayers)))
add (item (1 v) of [ShuffledDeck v]) to [WestHand v]
delete (1 v) of [ShuffledDeck v]
add (item (1 v) of [ShuffledDeck v]) to [NorthHand v]
delete (1 v) of [ShuffledDeck v]
add (item (1 v) of [ShuffledDeck v]) to [EastHand v]
delete (1 v) of [ShuffledDeck v]
add (item (1 v) of [ShuffledDeck v]) to [SouthHand v]
delete (1 v) of [ShuffledDeck v]
end
After the loop finishes, each player (West, North, East, South) will have a hand of cards.  If you only wanted to deal out 5 cards at a time, you could just repeat (4 players) * (5 cards) = 20 times.

Offline

 

#5 2012-05-20 00:35:13

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Re: How to manipulate a deck of playing cards

You might want to study the following game source:

http://scratch.mit.edu/projects/BoltBait/2327569

I think you could learn a lot about deck manipulations from looking at the structure of it and the scripts.

EDIT:  You might also look at this project http://scratch.mit.edu/projects/BoltBait/662083

Last edited by BoltBait (2012-05-20 00:45:46)


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

#6 2012-05-20 16:28:16

jsoares
New Scratcher
Registered: 2012-05-17
Posts: 5

Re: How to manipulate a deck of playing cards

Thanks everyone for the help. I'll study all comments and code. I'm mainly interested in card games so if I can manipulate regulation card decks, the other required logic will be very simple.

Offline

 

#7 2012-05-20 18:18:37

jsoares
New Scratcher
Registered: 2012-05-17
Posts: 5

Re: How to manipulate a deck of playing cards

Got it to work just fine, although I made it 1-52 instead of 1-13s, etc. I was able to put all four hands, the face up table cards, the unshuffled cards and the shuffled cards each in their own lists. These lists display just fine(showing me numbers from 1 to 52, which are the costume numbers of the card deck) so I know all cards dealt are unique with no repetition.

The problem is that my card game requires that at least four cards be face up in the middle of the screen. Each player can match cards by rank and take one card off the table to match with a card in their hand(this is simplified). Each round has four turns, where the player either matches a card on the table or plays any card in his hand face up to the table. When the round is over, another round of four cards is dealt to each player. The table cards stay where they are. The same thing happens for the third and final round.

How do I get the table cards to stay face up in the middle of the screen? It seems that with changeCostume, I can only display one card at a time(I have a cardDeck sprite with 52 costumes). Is there some sort of command where I can just display all table cards(they are already in their own list)?

Thanks.

Offline

 

#8 2012-05-20 18:21:32

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: How to manipulate a deck of playing cards

If you store the positions you want to go, you can do something like this.  Every time you want to re-draw the screen, you execute this script.  The lists have to be "parallel," so the card you want to be at item 1 of XPositions, item 1 of YPositions, must be identified by item 1 of Deck. 

clear
set [i v] to (0)
repeat (NumberOfPositions)
change [i v] by (1)
go to x: (item (i) of [XPositions v]) y: (item (i) of [YPositions v])
switch to costume (item (i) of [Deck v])
stamp

Last edited by amcerbu (2012-05-20 18:23:12)

Offline

 

#9 2012-06-25 15:54:46

wp31
Scratcher
Registered: 2011-10-06
Posts: 1

Re: How to manipulate a deck of playing cards

help! i am making a card deck and i cant relate the list to the costume number of the cards. i cant find the number of a certain item, if you could tell me how to do that, it would be great! thanks in advance.

Offline

 

Board footer