0

I am creating a simple blackjack game backed by database

In my Card is

public class Card{
 private Face face;
 private Suit suit;

 //setters.. getters
}

where face and suit are enums

I have an entity Bet with the following

@Entity
public class Bet{
   private Player player;
   private String cards;
   //...
}

Currently when I'm dealing cards I parse the suit and face to string and concatenate them in the cards field and then parse the cards if I want to calculate the score. I find this cumbersome so I want to change my field "cards" to List in the Bet Entity.

Now, if that's the case I would have to make the Card class an entity as well. But my cardService, which is where I get my cards, does not rely on the database, it just creates random cards so it does not make sense to make card an entity - am I right?

1
  • 1
    @user3308224 You should have either deleted the original question before reposting here or flagged it for migration.
    – ChrisF
    Sep 11 '14 at 8:38
0

A card is a value, not an entity.

Cards do not have identity -- if the pack contains two cards with the same value it is of no interest to identify them separately. They only have value.

Programming languages tend to provide values like integer, string and struct. You need to pick one of those to represent the value of a card. I would suggest using integer for the value, and write a class that can convert that integer to and from string or image representation, and to return the rank, suit and order (which may not be the same as the integer order).

Then your generator simply generates integers within a specified range.


The usual deck of cards contains 53 distinct cards, including a Joker. There are games (500) that have up to 63 distinct cards but the principle is the same, and an integer is a perfectly convenient way to encode the value. I would use that in the database too.

For a hand or a deal you have multiple cards, up to 13 in Bridge or 52 if you want to keep a complete deal. You could encode that as a space-separated list of integers or some other way, depending on what you need. That's getting off the original point.


For clarity, I do not suggest for a moment that one should use actual integers in the code to represent cards. In Java or JavaScript I would use a class, in C# or C++ a struct. The problem is that an object has identity but we want to deal with values. We want two different card objects that have the same value to compare equal. The simplest way (but not the only way) to do that is for the card object to contain an integer so that two cards with the same integer value are the same value. This is done by implementing a 'Compare Equal' operator in the class that compares the integer value.

This is a subtle issue, the representation of value types, and not easily addressed with an audience of widely different skills.

7
  • i think i get. just to clarify, for example the deck can be represented by 1-52 ints. is that what you meant? what if, for example, i want to store/persist in database cards given/dealt to a particular bet(which can be more than 1)? should i user string or is there a better way Sep 11 '14 at 10:08
  • A desk is 52 (or 53) integers. See edit.
    – david.pfx
    Sep 11 '14 at 11:21
  • 3
    I disagree with representing cards as integers. Cards are values identified by their suit and rank, and that's precisely what his code will want to deal with. He may want to establish an encoding from cards to integers for, say, database storage or random card generation, but that doesn't mean he should be dealing with integers in his business logic.
    – Doval
    Sep 11 '14 at 11:34
  • @Doval: His code needs to deal with CARDS (not suits or ranks) and they need to compare as values. See edit.
    – david.pfx
    Sep 11 '14 at 13:37
  • That's better, though I don't see the need to store an integer in the card to implement equality testing. You can simply compare the suit and rank of the cards.
    – Doval
    Sep 11 '14 at 13:48
1

It's a design decision. You can make Card an entity, with attributes like rank, suit, blackjack value (1..11), etc. and drive the game play from the data. That would let you modify the game play by changing data, and allow you to produce a useful report on the game play from the database alone.

Or you could make Card a value class (enum or whatever), and specify how it will be translated to/from a numeric or string value in the database. Depending on how you represent the cards in the database, it could be more difficult to generate a report independent of the application code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.