Class for representing a card in Java?

后端 未结 6 855
无人及你
无人及你 2021-01-15 22:04

I\'m making a blackjack program in Java, and I was starting to write the class declaration for an object Card. Will this be sufficient, or are there some methods I should ha

6条回答
  •  甜味超标
    2021-01-15 22:42

    Yeah, your start looks good. Switch the ranks and suits to enums - that would be a good idea. As far as methods, create them as you go and discover you need them. Depending on the game you're writing, you may need a completely different set of methods.

    Java has a very powerful enum. Check out the example below.

    public enum Rank {
      ACE(1, "Ace"),
      TWO(2, "Two"),
      ... etc
      KING(13, "King");
    
      private int value;
      private String display;
    
      private Rank (int value, String display) {
        this.value = value;
        this.display = display;
      }
    
      public int getValue() {
        return this.value;
      }
    
      public int getDisplay() {
        return this.display;
      }
    }
    

提交回复
热议问题