patternjavaMinor
Turn-based game setting properties for playcards
Viewed 0 times
turnpropertiesplaycardssettinggameforbased
Problem
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):
This is the way I did it on client side. Should I do the same with Java or are there better possibilities?
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]);
// HEARTS
if (id > 0 && id 13 && id 27 && 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?
Solution
Slight improvements of your current code
-
You should know that there is a
This can in turn also be simplified to:
Weird King
Are you aware that in your current code, you will have the values
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
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.
- 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 = 14 && id = 28 && 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.
Code Snippets
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;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;Context
StackExchange Code Review Q#39823, answer score: 5
Revisions (0)
No revisions yet.