Reputation: 45
I am making a blackjack card game for android. I am having trouble assigning values to images. I have 52 card images that I want to assign a suit/value. I am not sure what the best way to go about this would be.
Upvotes: 1
Views: 2784
Reputation: 811
Personally, I would make a Card
class that contains an Image, a String (or int) for the suit, and a value for the card.
For example:
public class Card {
private String imagePath;
private String suit;
private int value;
public Card(String imgPath, String suit, int value) {
this.imagePath = imgPath;
this.suit = suit;
this.value = value;
}
public getImagePath() { return this.imagePath; }
public getSuit() { return this.suit; }
public getValue() { return this.value; }
}
From there you could use an Image
instead of a file path. Really, it depends on how you want to do it. Classes exist to group data together and make it easy to access, so use them.
EDIT: A lower level implementation is to name the file by its suit and value, for example, '10-Hearts.png', and then use a regex, or simple parsing algorithm to retrieve the two parts of the name, but this solution is very clumsy:
//start adding to suit after passing a '-' character
boolean doSuit;
String value = ""; String suit = "";
for (int i = 0; i < filename.length(); i++) {
if (filename.charAt[i] != '-') {
value = value + filename.charAt[i];
} else if (doSuit && filename.charAt[i] != '-') {
suit = suit + filename.charAt[i];
} else {
doSuit = true;
}
}
I used to hack together solutions like this second one. They aren't pretty, and when they fail, they do so in the most spectacular ways. Please, for the love of god, use a class.
Upvotes: 2