Search code examples
javapoker

How to operate the poker game by Java?


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.

  1. 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"......};
    
  2. 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?


Solution

  • What you should be considering for this sort of classification is Enums:

    public enum Suit {
      Hearts, Spades, Diamonds, Clubs;
    }
    public enum Card {
      A, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K;
    }
    

    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.