The rank of the cards in the game

I am a beginner Java student. Therefore, any help would be greatly appreciated!

I developed the Cards class and Class Deck, which I will use in the game I created. So far, my game seems to work very well, but I have a problem in my card class. I would like to use my getRank () method as a way of accumulating points for cards that a player has (regardless of their correspondence).

For example, if they draw 2 diamonds, it will cost 2 points, 3 hearts 3 points ... ect. I created String[]under the name ranksto try to do this, and I think he does it. However, I would like all face cards (for example, king, queen ...) to have a value of 10. But not all of these cards are at index 10 in my array. Can I get some recommendations on how to do this?

Thanks in advance.

int rank;
int suit;
String[]ranks = { " "," ", " " } //all 13 ranks from Ace to King 
String[]suits = { " ", " ", " "} //all ranks

public Card(int rank, int suit){
   this.rank = rank;
   this.suit = suit; 
}

public void setRank(int rank){
   this.rank = rank;
}

public int getRank(){
   return rank;
}

//more code for suits

public String toString(){
   return ranks[rank]+" of "+suits[suit];
}
+4
source share
2 answers

- . , int , ( ace ), , ( 13). , , .

public enum CardRank  {
   ACE(1),
   TWO(2),
   THREE(3),
   ...
   TEN(10),
   JACK(10),
   QUEEN(10),
   KING(10);

   private final int value;
   CardRank(int value)  {
      this.value = value;
   }
   public int getValue()  {
      return  value;
   }
}

,

public void Card(CardRank rank, CardSuit suit)  {
   this.rank = rank;
   this.suit = suit;
}
public int getRankValue()  {
   return  rank.getValue();
}

- , . int s, , , , . .

CardSuit :)

:

+7

, "" , . , . , getValue(Card c), , . :

public int getValue(Card c) {
    if (c.getRank() > 10) {
        return 10;
    } else {
        return c.getRank();
    }
}
+1

Source: https://habr.com/ru/post/1548133/


All Articles