I’ve learned Java for 1 month. This time, I’d like to create a poker game. There are two questions about my program. I hope somebody can help me to fix it.
Each card has it value, for A as 1, king as 10, but I find out that this is a String array. How could I give each “String value “as “int value” so that I can do the operation?
String[] suits = { "hearts", "spades", "diamonds", "clubs" }; String[] number = {"A","2","3"......};
I hope this system can use random number to chose cards at first, and second time when it run, it can void previous number (There have 52 card). Does there has something that I can do to figure out this problem?
I don't think you want a String array for cards, what you really need is an
enum
. Here's a nice tutorial onenum
types: http://docs.oracle.com/javase/tutorial/java/javaOO/enum.htmlAlso, here's how you generate random numbers in Java: http://javamex.com/tutorials/random_numbers/
For your first question, you could map a card name to its value:
For the second, I'd rely on java shuffle function. Create a list of all cards
and then simply shuffle the deck
Now you can iterate through the deck and you will see the cards in a random order:
Note: the
Card
class does not extist (yet). I simply invented the name to keep it simple)this is probably more than you need, but you can do a lot with enums:
What you should be considering for this sort of classification is Enums:
This benefits your situation since enums can be assigned value easily, to whatever you like, and then they can be compared based on this value. In fact, the Java documentation provides a card game for the introduction to enums.