I am programming with Java (server side) and JavaScript a card game and I do not know how to give properties (on server side) to every card that I create. For a match I need 20 different cards from a complete set of cards (52). Now I want that these 20 cards get a value (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) and a symbol (like Spades, Hearts, Diamonds and Clubs).

How should I approach this? I thought using if clauses like I used on client side would be ok (JavaScript):

p.initialize = function (id) {

this.card= new createjs.Bitmap(images[id]);

if (id > 0 && id < 14)
this.symbol = Card.HEARTS;

else if (id > 13 && id < 28)
this.symbol = Card.DIAMONDS;

else if(id > 27 && id <42)
this.symbol = Card.CLUBS;

else if(id > 41 && id <53)
this.symbol = Card.SPADES;

this.VALUE = id%13;

This is the way I did it on client side. Should I do the same with Java or are there better possibilities?

  • \$\begingroup\$ Is this the only piece of code that you can provide? From just this one can not really review your code. You should at least include your Card class. Also if you do not have a class to encapsulate a set/deck logic you're doing it wrong. Well, may not be wrong, but major of card game (like yours) relies on having at least one set. So there is no point of not having one.. \$\endgroup\$ Commented Jan 22, 2014 at 16:57

3 Answers 3


Slight improvements of your current code

  • Get rid of the comments and read three lines below which symbol you are currently declaring the conditions for. Your code is very self-documenting, no need to add comments.
  • You should know that there is a more than or equal to operator, which can be used to make your code a lot more readable (here I have also removed the braces since your statement is only one line)

    if (id >= 1 && id <= 13) this.symbol = Card.HEARTS;
    else if (id >= 14 && id <= 28) this.symbol = Card.KARO;
    else if (id >= 28 && id <= 41) this.symbol = Card.CLUBS;
    else if (id >= 42 && id <= 52) this.symbol = Card.SPADES;

This can in turn also be simplified to:

    if (id <= 0) { /* mega-super-error-should-probably-not-happen-and-if-it-does-then-it's-no-symbol */ }
    else if (id <= 13) this.symbol = Card.HEARTS;
    else if (id <= 28) this.symbol = Card.KARO;
    else if (id <= 41) this.symbol = Card.CLUBS;
    else if (id <= 52) this.symbol = Card.SPADES;

Weird King

Are you aware that in your current code, you will have the values 1 2 3 4 5 6 7 8 9 10 11 12 0? This feels weird. If Ace is 1 then 0 is king I assume? But using 0 as king does not make sense.

Adding a parameter to the function

Even though @rolfl provides a short way to do this, I just want to object to one thing: The use of the id parameter.

It seems like you are creating a card from an ID. Now what if you some day totally forget about how to calculate these IDs from a specified suite and rank? (or vice versa). I would instead use a function with two parameters: Suite and rank. I'm not sure how you are calling this method today, but to me it feels cleaner to create cards by using a nested for-loop to loop first over suite and then over rank, rather than looping from 1 to 52.

Also, I wouldn't use an uppercase property VALUE when the others, symbol and card is lowercase. It's better to use lowercase value also. Or, you could call it rank instead. And you might want to rename the card property to picture.

As for how to do it in Java, I would say that you should try to make it the same in both Java and JavaScript. Tidy up the JavaScript and then make it the same in Java.


Can't help but think that a trick with integer division and clever Card enum setup would help a lot:

this.symbol = Card.values()[(id - 1) / 13];

Replace all your code with the above 1-liner ;-)


I did the Java server side of a Go Fish card game a while back, with a second person doing a Javascript client and a third one doing an AS3 client. The way that we implemented it was similar to rolfl's suggestion.

Java, in the Card object class:

public int getRank() {
    return id % 13;

public int getSuit() {
    return id / 13;

Javascript, in the Card object class. I'm not sure why Steve used Math.floor on the % results.

this.rank = Math.floor(id % 13);
this.suit = Math.floor(id/13);

AS3, in the Card object class.

static public function getRankForId(id:int):int {
    var rank:int = (id % 13);
    //trace("id " + id + " rank: " + rank);
    return rank;

static public function getSuitForId(id:int):int {
    var suit:int = Math.floor(id / 13);
    //trace("id " + id + " suit: " + suit);
    return suit;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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