为一副牌中的不同牌设定价值

时间:2016-09-15 19:54:10

标签: java arrays

是的,这是一个家庭作业,但我正在努力学习如何编写更多面向对象的代码。我需要为每张卡设置不同的值,这样当我运行compareTo方法时,我不仅可以比较卡的数值,还可以比较卡的套装等级。所以作为一个例子,ACE的值为0,13,26和39.我创建了一个卡类,当我通过deckOfCard类运行时,一切正常。我只是想更好地写入Card方法,所以没有那么多代码。我知道必须有一个更好的方法来做它,而不是在嵌套的if / else语句上有一堆。这是我的卡类。任何帮助将不胜感激。

public class Card{
private int number;
private String suit;
private char value;
private final int MAX_CARDS = 52;

/**
method: Card(int number)
Purpose: (Constructor)Creates a new Card based on the number

@1param   number of card to assign suit and value
@return  
*/
public Card(int number)
{
  //Assigning Values to the card
  if(number == 0 || number == 13 || number == 26 || number == 39)
  {
     value = 'A';
  }
  else if(number == 10 || number == 23 || number == 36 || number == 49)
  {
     value = 'J'; 
  }
  else if(number == 11 || number == 24 || number == 37 || number == 50)
  {
     value = 'Q';
  }
  else if(number == 12 || number == 25 || number == 38 || number == 51)
  {
     value = 'K';
  }
  else if(number == 1 || number == 14 || number == 27 || number == 40)
  {
     value = '2';
  }
  else if(number == 2 || number == 15 || number == 28 || number == 41)
  {
     value = '3';
  }
  else if(number == 3 || number == 16 || number == 29 || number == 42)
  {
     value = '4';
  }
  else if(number == 4 || number == 17 || number == 30 || number == 43)
  {
     value = '5';
  }
  else if(number == 5 || number == 18 || number == 31 || number == 44)
  {
     value = '6';
  }
  else if(number == 6|| number == 19 || number == 32 || number == 45)
  {
     value = '7';
  }
  else if(number == 7 || number == 20 || number == 33 || number == 46)
  {
     value = '8';
  }
  else if(number == 8 || number == 21 || number == 34 || number == 47)
  {
     value = '9';
  }
  else if(number == 9 || number == 22 || number == 35 || number == 48)
  {
     value = 'T';
  }
  //Assigning Suit
  // Hearts > Spades > Diamonds > Clubs
  if(number>=0 && number<=12)
  {
     suit = "Clubs";
  }
  else if(number>=13 && number<=25)
  {
     suit = "Diamonds";
  }
  else if(number>=26 && number<=38)
  {
     suit = "Spades";
  }
  else
  {
     suit = "Hearts";
  }

}   
/**
method: getValue()
Purpose: (Getter)returns value

@param   
@return  value
*/   
public char getValue()
{   

  return value;
}
/**
method: getNumber()
Purpose: (Getter)returns number

@param   
@return  number
*/   
public int getNumber()
{
  return number;
}                  

/**
method: getSuit(int number)
Purpose: returns suit based on number value

@param   number of card to assign suit
@return  suit
*/ 
public String getSuit()
{             

  return suit;            
}
public String toString()
{
  return value + " of " + suit;
}
/**
method: compareTo(Card aCard)
Purpose: Return an integer comparison of two cards

@param   aCard reference to the card being compared
@return  integer difference between two cards
*/
public int compareTo(Card aCard)
{
  int cardSuitValueA = -1;
  int cardSuitValueB = -1;
  int cardsPerSuit = 13;
  if(this.getNumber() % cardsPerSuit - aCard.getNumber() % cardsPerSuit !=     0)
  {
     return this.getNumber() % cardsPerSuit - aCard.getNumber() %  cardsPerSuit;
  }
  else
  {
     if (this.getSuit().equalsIgnoreCase("Hearts"))
     {
        cardSuitValueA = 3;
     }
     else if (this.getSuit().equalsIgnoreCase("Spades"))
     {
        cardSuitValueA = 2;
     }
     else if (this.getSuit().equalsIgnoreCase("Diamonds"))
     {
        cardSuitValueA = 1;
     }         
     else if (this.getSuit().equalsIgnoreCase("Clubs"))
     {
        cardSuitValueA = 0;
     }
     if (aCard.getSuit().equalsIgnoreCase("Hearts"))
     {
        cardSuitValueB = 3;
     }
     else if (aCard.getSuit().equalsIgnoreCase("Spades"))
     {
        cardSuitValueB = 2;
     }
     else if (aCard.getSuit().equalsIgnoreCase("Diamonds"))
     {
        cardSuitValueB = 1;
     }         
     else if (aCard.getSuit().equalsIgnoreCase("Clubs"))
     {
        cardSuitValueB = 0;
     }
     return cardSuitValueA - cardSuitValueB;
   }        
 }   

}

3 个答案:

答案 0 :(得分:1)

您可以使用enum s。例如,这可以显着压缩您的compareTo

public class Card implements Comparable<Card> {
    private final int number;
    private final Rank rank;
    private final Suit suit;

    private static final int CARDS_PER_SUIT = 13;

    private Card(Suit suit, Rank rank, int number) {
        this.suit = suit;
        this.rank = rank;
        this.number = number;
    }

    public Card(Suit suit, Rank rank) {
        this(suit, rank, suit.ordinal() * CARDS_PER_SUIT + rank.ordinal());
    }

    public Card(int number) {
        this(Suit.ofNumber(number), Rank.ofNumber(number), number);
    }

    //... Boilerplate methods

    private static final Comparator<Card> defaultComparator
        = Comparator.comparing(Card::getRank).thenComparing(Card::getSuit);

    @Override
    public int compareTo(Card aCard) {
        return defaultComparator.compare(this, aCard);
    }

    public enum Suit {
        Clubs,
        Diamonds,
        Spades,
        Hearts;

        public static Suit ofNumber(int number) {
            return values()[number / CARDS_PER_SUIT];
        }
    }

    public enum Rank {
        Ace,
        Two,
        Three,
        Four,
        Five,
        Six,
        Seven,
        Eight,
        Nine,
        Ten,
        Jack,
        Queen,
        King;

        public static Rank ofNumber(int number) {
            return values()[number % CARDS_PER_SUIT];
        }
    }

}

答案 1 :(得分:0)

也许这样的事情(我只做了排名,而不是套装,但是你明白了,你可以用西装做同样的事情,或者就像你现在一样离开它):

private static Map<Integer, Character> map = new HashMap<Integer, Character>();
static{
  map.put(0, 'A');
  map.put(10, 'J');
  map.put(11, 'Q');
  map.put(12, 'K');
  ...
}

public Card(int number){
  value = map.get(number%13);
}

这样你的compareTo方法也可以大大减少。如果您对地图不满意,也可以将这些值放在普通数组而不是地图中(索引0将为'A',索引10将为'J'等。)

答案 2 :(得分:0)

您可以使用模运算符使其更简单

translate

或保留一个数组并使用public Card(int number) { //Assigning Values to the card if((number % 13) == 0) { value = 'A'; } else if((number % 13) == 10) { value = 'J'; } else if((number % 13) == 11) { value = 'Q'; } else if((number % 13) == 12) { value = 'K'; } else if((number % 13) == 1) { value = '2'; } 为其编制索引。当(number % 13)除以13时,(number % 13)是余数。

number