简体   繁体   English

用Java表示卡的类?

[英]Class for representing a card in Java?

I'm making a blackjack program in Java, and I was starting to write the class declaration for an object Card. 我正在用Java开发二十一点程序,并且开始为对象Card编写类声明。 Will this be sufficient, or are there some methods I should have that I'm glossing over? 这是否足够,或者我应该使用某些方法来掩饰?

public class Card {
    public int suit; //Value 1-4 to represent suit
    public int value; //Value 1-13 to represent value (i.e. 2, J)
    public Card(int suit, int value) {
        //Not yet implemented
    } 
}

Also, is there a good way to have an something like C++'s enum data structure in Java, because that would be nice for card names and suits? 另外,是否有一种很好的方法可以使Java中具有C ++的enum数据结构,因为这样对卡名和西装很合适?

public enum Suite {
    HEART, DIAMOND, CLUB, SPADE
}
public enum Value {
    TWO, THREE, ... , JACK, QUEEN, KING, ACE 
}

and the rest you can figure out. 其余的您可以弄清楚。

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. Java有一个非常强大的枚举。 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;
  }
}

I would (have) used an enum with the suit and value as properties. 我会(已)使用一个以西装和价值作为属性的枚举。 Suit and value can also be enums. 衣服和价值也可以是枚举。

Java as of 1.5 fully supports enums . Java从1.5版本开始完全支持枚举 Coincidentally, the example provided in the link uses a card class as an example much as you are trying to do. 巧合的是,链接中提供的示例与您尝试执行的操作一样,使用卡类作为示例。

The base class you have there is sufficient with the addition of getters. 您所拥有的基类足以添加getter。 Implementing Comparable could prove valuable should you need to do sorting at some point. 如果您需要在某个时候进行排序,那么实施Comparable可能会很有价值。

With respect to "are there some methods I should have that I'm glossing over", that's really subjective and based the context in which the class is used. 关于“我应该掩盖某些方法”,这确实是主观的,并且基于使用类的上下文。 For example, if you never need to print the object, you may not need a 'toString()' function, but if you do, it might come in handy (especially if you want output formatted in a specific manner). 例如,如果您永远不需要打印对象,则可能不需要'toString()'函数,但是如果这样做,它可能会派上用场(特别是如果您希望以特定方式格式化输出)。

Java also has enum's, see here for a tutorial. Java也有枚举,请参见此处的教程。

You might want to make subclasses for different games. 您可能想为不同的游戏创建子类。 PokerCard and SolitareCard can be Comparable; PokerCard和SolitareCard可以比较; in one, Ace is greater than King; 在其中之一中,王牌大于国王; in the other, Ace is less than King. 另一方面,王牌比国王少。

For BlackjackCard, comparison is irrelevant. 对于BlackjackCard,比较是无关紧要的。 You might consider having an intValue(), where intValue() of K or Q or J is 10, but that won't work easily for BlackjackCard because an Ace can be one or eleven. 您可能会考虑拥有一个intValue(),其中K或Q或J的intValue()为10,但这对于BlackjackCard来说并不容易,因为Ace可以是1或11。 So this is a good exercise in designing the model to match the real world: here is a problem where the integer value of the card is not completely intrinsic to the card but is dependent on the card's context. 因此,这是设计模型以匹配现实世界的好习惯:这是一个问题,其中卡的整数值不是卡的完全固有值,而是取决于卡的上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM