-3

I'm making a blackjack game, and I want to take my cards images and give each a value for the game.

I'm not sure how to determine their value. Should I just do a big switch statement with all the images names?

Or maybe should I check for contains with a big if statement? Like if the image name contains "king", then the value is 10, or maybe there is another way which is more efficient?

3
  • 1
    The logic should be in charge, not the visuals. Ensure you have a working Card or Deck class or whatever, then ensure that the correct image is loaded for the correct card. Commented Oct 5, 2017 at 19:28
  • NB: Stack Exchange requires much higher standards from question askers than this. I suggest you take some time and become familiar with the network and its social norms before asking your next question. Commented Oct 5, 2017 at 22:01
  • 1
    Since you're using Java, you should be leveraging the object oriented paradigm.... in which case, wouldn't it make sense for each Card object to have a value field? Commented Oct 6, 2017 at 0:52

2 Answers 2

1

You should avoid using strings to control your logic when there are better types available. (Using strings for everything is called stringly typed code and is considered an antipattern.) A better choice for representing the values cards can have would either be a simple int or an enum. I'd probably use an int between 2 and 9 for the numbered cards and use 10, 11, 12, and 13 for jack, king, queen, and ace.

I'd use an enum to represent the suits: hearts, spades, diamonds, and clubs. Something like this:

public enum Suit {
    HEARTS, SPADES, DIAMONDS, CLUBS
}

As suggested in the comments, I'd create a Card class. Each object in the Card class would have a Suit and a value between 2 and 13 inclusive. You could create a Deck class that held all 52 cards. It could have methods for shuffling the cards and dealing cards.

0

Have a Card class with members Value, ImageFileName, Image, Position, etc. and use an object oriented approach.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.