OOP BlackJack游戏(创建甲板) [英] OOP BlackJack Game (Creating Deck)

查看:131
本文介绍了OOP BlackJack游戏(创建甲板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个OOP友好的Java BlackJack游戏来提升我的知识。

I am attempting to create an OOP friendly Java BlackJack game to progress my knowledge.

我碰壁了,我只是不知道看不到问题。想知道是否有人可以指出我的问题。

I've hit a wall and I just don't know enough to see the problem. Was wondering if anyone could point out my problems.

此外,在谷歌搜索相关主题后,我发现人们一次又一次地说使用枚举会更有益,作为初学者,这会被建议吗?或者我应该暂时坚持使用String数组。

Additionally, after googling relevant topics about this I've found people time and time again saying using enums would be more beneficial, as a beginner would this be advised? Or should I stick with String arrays for the time being.

谢谢。

我的代码:

public class BlackJack{

BlackJack() {
    Deck deck =  new Deck();
    deck.createDeck();
    System.out.println(deck.deckList);
}

public static void main(String[] args) {

    new BlackJack();


   }
}


public class Card{

private String valueCard;
private String suitCard;

public Card(String value, String suit) {
    this.valueCard = value;
    this.suitCard = suit;
}

public String getValue() {
    return valueCard;
}
public String getSuit() {
    return suitCard;
  }
}

import java.util.ArrayList;


public class Deck {

ArrayList<Card> deckList = new ArrayList<Card>();

String[] value = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
        "Jack", "Queen", "King", "Ace"};
String[] suit = {"Hearts", "Clubs", "Spades", "Diamonds"};


Deck() {

    deckList = new ArrayList<Card>();

}

public void createDeck() {
    for (int i = 0; i < value.length; i++) {
        for (int x = 0; x < suit.length; x++) {
            Card card = new Card(value[i], suit[x]);
            deckList.add(card);

          }
      }
  }
}

编辑:目前我的println输出是:
[卡@ addbf1,卡@ 42e816,卡@ 9304b1,...等]
这是什么意思?

edit: at the moment my out-put from my println is: [Card@addbf1, Card@42e816, Card@9304b1, ... etc] What does this mean?

感谢您的时间。

编辑:未来还需要答案的任何人:

Anyone who also needs an answer to this in the future:

已添加:

    @Override
public String toString(){
    return valueCard + " of " + suitCard;
}
 }  

到我的Card类,然后在甲板上使用它class:

to my Card class and then used it in the Deck class:

import java.util.ArrayList;


public class Deck {

ArrayList<Card> deckList = new ArrayList<Card>();

String[] value = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
        "Jack", "Queen", "King", "Ace"};
String[] suit = {"Hearts", "Clubs", "Spades", "Diamonds"};


Deck() {

    deckList = new ArrayList<Card>();

}

public void createDeck() {
    for (int i = 0; i < value.length; i++) {
        for (int x = 0; x < suit.length; x++) {
            Card card = new Card(value[i], suit[x]);
            deckList.add(card);
            card.toString();
        }
    }
}
}

ENUM :
公共类CardEnum {

ENUM: public class CardEnum {

public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX,
    SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }

public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }


}


public class Card{

private CardEnum.Rank rank;
private CardEnum.Suit suit;

public Card(CardEnum.Rank rank, CardEnum.Suit suit) {
    this.rank = rank;
    this.suit = suit;
}

public Rank getRank() {
    return rank;
}
public Suit getSuit() {
    return suit;
}

@Override
public String toString(){
    return rank + " of " + suit;
}
}


推荐答案

基于在你的编辑中,我看到你的代码打印出Object的 toString()方法返回的默认值,该方法是当前对象类的名称和对象的hashCode。要纠正这个并让你的字符串有意义,你必须为每个类提供一个 public String toString()方法覆盖。 Card类应该有一个toString()方法,它返回一个描述当前卡片的套装和值的String,而Deck类应该遍历每张Card并调用卡片的toString()。

Based on your edit, I see that your code is printing out the default value returned by Object's toString() method, which is the name of the current object's class and the object's hashCode. To correct this and to have your Strings make sense, you must give each of your classes a public String toString() method override. The Card class should have a toString() method that returns a String that describes the suit and value of the current card, and the Deck class should iterate through each Card and call the card's toString().

类似于:

@Override
public String toString() {
  return /* code in here to get the contents of the fields held by the Card object */
}

编辑关于:


我正准备进入枚举我只是很好奇我在看:docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html但我有两个单独的类:Card&甲板。我是否必须将两组枚举复制到两个类中才能使用?所以我有卡{enum声明,私人决赛,卡(价值,套装){}然后在甲板课我将不得不再次复制枚举列表?有点困惑我的套牌和卡片类将如何使用枚举。感谢您对Noobie的耐心!

I'm just about to delv into enum's I was just curious as I am looking at: docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html But I have two separate classes: Card & Deck. Do I have to copy both sets of enums into both classes for this to work? So I have Card{ enum declaration, private final's, card(Value value, Suit suit) {} then in the deck class will I have to copy the list of enums there again? Bit confused how my deck and card class will be using the enums. Thanks for your patience with a noobie!

不,该示例中的枚举表现得好像它们被声明为公共静态 - 所以它们不需要Card实例访问,并且可以在Card类之外使用。你只需要限定他们使用它们。即,Card.Rank.FOUR访问Rank的四个实例。总而言之,您无需在任何地方重新声明枚举。我个人认为将每个枚举文件放在自己的文件中,与Card类相同的包中,但不在同一个文件中更清晰。

No, the enums in that example behave as if they were declared public static -- so they do not need a Card instance to access and are available outside of the Card class. You just have to qualify them to use them. i.e., Card.Rank.FOUR to access Rank's FOUR instance. So in sum, you do not have to re-declare the enums anywhere. I personally think that it is cleaner to have each of the enums in their own file, in the same package as the Card class, but not in the same file.

编辑2 您声明:


确定我已创建公共类CardEnum {public enum RANK {// ranking} public enum Suit {//适合}如何让它与我的Card类进行交互?

OK I've created public class CardEnum { public enum RANK {//ranks} public enum Suit { //suits} how do I get it to interact with my Card class?

你会给Card两个私人字段,一个CardEnum.Rank类型(请注意枚举名称不应该是所有的上限)和CardEnum.Suit类型之一。然后,您将在Card的构造函数中分配这些值。我会让这个课程不可变,这意味着一旦你设定了卡的等级和套装,就不应该让它改变。所以我给每个字段一个getter方法,但我不会给它一个setter方法。

You would give Card two private fields, one of CardEnum.Rank type (note that the enum name should not be all cap) and one of CardEnum.Suit type. Then you would assign these values in Card's constructor. I would make this class "immutable", meaning once you've set the Rank and the Suit for a Card, it should never be allowed to change. So I'd give each of these fields a getter method but I wouldn't give it a setter method.

所以

public class Card {
  private CardEnum.Suit suit;
  private CardEdum.Rank rank;

  public Card(CardEnum.Suit suit, CardEnum.Rank rank) {
     this..... etc...
  }

  // getter methods here, but no setter methods

  // ....

编辑3


我的get方法的最后一个问题:public Rank getRank(){return rank; }给出错误:排名无法解决我用我当前的代码编辑了我的主帖,使其易于阅读。

one last issue with my get methods: public Rank getRank() { return rank; } gives the error: "rank cannot be resolved" I have editted my main post with my current code to make it easy to read.

您已将Rank enum嵌入CardEnum类中,因此您必须使用CardEnum类名限定Rank和Suit。因此你会这样做:

You have embedded your Rank enum inside of a CardEnum class and so you have to qualify Rank and Suit with the CardEnum class name. Thus you would do:

public CardEnum.Rank getRank() {
   return rank;
}

一个建议摆脱所有额外的措辞 - 只是摆脱完全是CardEnum,而是将每个枚举放在自己的文件中,一个Rank.java文件和一个Suit.java文件。 ie

One suggestion to get rid of all the extra verbiage -- just get rid of CardEnum altogether, and instead put each enum in its own file, a Rank.java file and a Suit.java file. i.e.

// this guy goes in the Suit.java file
public enum Suit {
   CLUBS, DIAMONDS, HEARTS, SPADES
}

然后当你提到西装时,你不会需要称之为CardEnum.Suit。

Then when you refer to Suit, you don't need to call it CardEnum.Suit.

这篇关于OOP BlackJack游戏(创建甲板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆