aspose file tools*
The moose likes Beginning Java and the fly likes How would i shuffle my deck of cards? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How would i shuffle my deck of cards? " Watch "How would i shuffle my deck of cards? " New topic
Author

How would i shuffle my deck of cards?

Tom-Colin Kelly
Greenhorn

Joined: Dec 09, 2011
Posts: 27
i am making some top trumps consisting of 20 cards (10 per player)
i want to shuffle to deck before it is distributed to the player at the start of the game so they dont end up with the same card over and over..
could you help?

oh and the top trumps are minecraft top trumps with the yogscast along with random mobs
and the numbers next to it are the ratings for that character, which are based in another class.

This is the code:
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
I assume you mean to shuffle the deck(s) and then deal the deck(s) as we would by hand: one at a time, in order, from the top of the deck. Another approach that works well in a program is to deal the cards chosen randomly from the deck, something like:


This method has some additional benefits: the deck is shuffled as the cards are dealt; the deck is restored (but shuffled), ready to be used again, simply by setting the sizeOfDeck to its original number, etc.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Tom-Colin Kelly
Greenhorn

Joined: Dec 09, 2011
Posts: 27
ok this looks like it would work well, havent tried it yet, i will now, although i havent made a main class yet so cannot run it, thanks a lot
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

If you'd put the cards in a List instead of an array, then you could use Collections.shuffle(...) to shuffle the List.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Tom-Colin Kelly
Greenhorn

Joined: Dec 09, 2011
Posts: 27
Jesper de Jong wrote:If you'd put the cards in a List instead of an array, then you could use Collections.shuffle(...) to shuffle the List.


yeah that would be a good idea, but i believe that it is easier in an array with the attributes of the toptrumps (the numbers after the characters)
like attack, block n stuff?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

It wouldn't be a lot harder to put them in a List:
Tom-Colin Kelly
Greenhorn

Joined: Dec 09, 2011
Posts: 27
Jesper de Jong wrote:It wouldn't be a lot harder to put them in a List


ah ok i did it, thanks a lot
was very helpful
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jesper de Jong wrote:It wouldn't be a lot harder to put them in a List:


Or even...


And, finally, I would make the Card an enum rather than a class. You may not have studied enums yet, so don't worry about it, but keep it in mind when you get there. I think a Card enum is an example in Oracle's tutorial too.

Tom-Colin Kelly
Greenhorn

Joined: Dec 09, 2011
Posts: 27
Ah thats very good thankyou
yeah i havent learnt about them yet, i will soon hopefully, i also havent learnt about images etc,
i want to be able to make the game actually a game instead of just lots of text!
Tom-Colin Kelly
Greenhorn

Joined: Dec 09, 2011
Posts: 27
Just incase anyone was interested i hgave solved it, last week, just frogot to post
i imported the 'import java.util.*;'
and then made it from the math part of that

here is the code:


thanks a lot for your ideas and help, i really appreciate it
 
 
subject: How would i shuffle my deck of cards?