Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6589419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:10:21+00:00 2026-05-25T17:10:21+00:00

I am trying to write a card shuffler , and I know the method

  • 0

I am trying to write a card shuffler, and I know the method by which I wish to shuffle the cards. However, I am at a loss of the best object-oriented way in which to write it.

The method, a rather common one, is as follows:

  • Assign each a random numeric value, between 0 and 2,147,483,647
  • If there is a duplicate key value (very unlikely), throw away the deck, and start again.
  • Store the cards in a set
  • Order the set by each cards key value

My problem lies in the best OOP way to write this. At first I came up with an object called Card, containing a suit value, a number value and the random key value. Then I would have a class called Deck that extended a HashSet, and I would store each card into the HashSet and then sort it by key value. Where I struggled was, what is the most efficient way to ‘generate’ the 52 Card objects in the first place, and how to order the set. Would I implement the interface, ‘SortedSet’, if so, how would I go about writing the comparators?

Quite a broad question, more based on OOP design practices, but I’d like this to be a really smooth and object based solution.

Cheers,

Tim.

EDIT:

Thanks for the help everyone. My solution was as follows:

  • 2 Enums (CardValues, CardSuits), containing the 4 suits and 13 possible values
  • Card class, that takes as constructor arguments a CardValue and a CardSuit.
  • Deck class that extends a TreeMap

When a new deck is created and shuffled, I loop through the CardSuit Enum and created Cards, then inside that loop, I go through the CardValue Enum. This creates the cards, I then generate a random key and put them in the TreeMap.

As there is always a small chance of key repetition, if the final deck size is not 52, I throw a new InvalidDeckException.

Thanks for the suggestions, I am much happier with this solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:10:22+00:00Added an answer on May 25, 2026 at 5:10 pm

    Use a TreeMap, for each card generate a random number that doesn’t exist in the map, and insert it in the map as the key for the card, done.

    The map is now ordered by the random numbers generated.

    See also http://en.wikipedia.org/wiki/Shuffling#Shuffling_algorithms

    Note that this is a retarded way to shuffle, just use Collections.shuffle().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying write a query to find records which don't have a matching record
I'm trying to write a blog post which includes a code segment inside a
I'm trying to write an image file to a directory on my sd card
I'm trying to write an applet to display packs of cards amongst other things.
We have a database of loyalty card holders. I'm trying to write a simple
I'm trying to write a (very minimal) WPF app which will look at a
I am trying to write a program to simulate a card game. I have
I'm trying to write an application which will search and retrieve a user's profile
iam trying to write a class which imports *.vcf files (Vcard), because i didn´t
Trying to write a PowerShell cmdlet that will mute the sound at start, unless

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.