File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How would i shuffle my deck of cards?

 
Tom-Colin Kelly
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom-Colin Kelly
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you'd put the cards in a List instead of an array, then you could use Collections.shuffle(...) to shuffle the List.
 
Tom-Colin Kelly
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It wouldn't be a lot harder to put them in a List:
 
Tom-Colin Kelly
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic