• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How would i shuffle my deck of cards?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It wouldn't be a lot harder to put them in a List:
 
Tom-Colin Kelly
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic