1

我最近开始学习 java 并且大部分都进​​展顺利。为了帮助理解我正在学习的内容,我决定编写一个纸牌游戏引擎。我使用我认为正确的抽象和封装方法构建了卡片和甲板类。但是,在构建经销商类时,我遇到了一个问题,我不知道如何从经销商类中访问卡类数据。从甲板类我可以访问卡类所需的所有权限,但是一旦我进入经销商类,我就不确定如何访问封装在 2 个深度类中的数据。

这是我到目前为止所拥有的,虽然可以解决问题的代码表示赞赏,但如果有人可以指出我的文档或书籍的方向以帮助更好地理解我所经历的,将不胜感激。

只是要清楚。我希望能够访问与甲板数组中的各个卡片有关的数据,例如来自经销商类的实例变量值。我知道如果我从 println 语句中引用卡片,它会调用卡片到字符串方法。但我正在寻找的是卡片的实际数据。

最后一节课只是我用来测试我写的方法的东西。

   import java.util.*;
   public class Card {

        public Card() {
            suit = 0;
            value = 0;
            setName(value);                             
        }

        public Card(int cSuit, int cValue) {
            suit = cSuit;
            value = cValue;
            setName(cValue);
        }

        public String getNewCard() {
                suit = rgen.nextInt(4)+1;
            value = rgen.nextInt(13)+1;
            setName(value);

            return name +" of " +getSuitAsString();             
        }

        public int getValue() {
            return value;
        }

        public int getSuit() {
            return suit;
        }

        public String getName() {
            setName(value);
            return name;
        }

        public String getName(int val) {
            setName(val);
            return name;
        }

        public String getSuitAsString() {

            switch(suit) {
            case 1: return "Diamonds";
            case 2: return "Hearts";
            case 3: return "Spades";
                        case 4: return "Clubs";
                      default: return null;
            }
        }

        public String getSuitAsString(int cSuit) {
            switch (cSuit) {
                case 1: return "Diamonds";
            case 2: return "Hearts";
            case 3: return "Spades";
                    case 4: return "Clubs";
                default:return null;
            }
        }

        public String toString() {
            return name +" of "+ getSuitAsString();
        }

        private void setName(int value) {
            switch (value) {
            case 0 : name = "null";
            break;
            case 1 : name =  "Ace";
            break;
            case 2 : name =  "Two";
            break;
            case 3 : name =  "Three";
            break;
            case 4 : name =  "Four";
            break;
            case 5 : name =  "Five";
            break;
            case 6 : name =  "Six";
            break;
            case 7 : name =  "Seven";
            break;
            case 8 : name =  "Eight";
            break;
            case 9 : name =  "Nine";
            break;
            case 10 : name = "Ten";
            break;
            case 11: name =  "Jack";
            break;
            case 12 : name = "Queen";
            break;
            case 13 : name = "King";
            break;
            }


        }

    private int suit;
    private String name;
    private int value;


    private Random rgen = new Random();
    }
    import java.util.Random;


    public class Deck {

    //Constructor assembles an initial deck of 52 cards in order by suit.
    //Array element [0] is never used.
        public Deck(){
            int cards = 1;
            int cardsPerSuit = 13;
            int suits = 4;
            while(cards < DECK_ARRAY_SIZE){
                for(int i = 1; i <= suits ; i++){
                    for(int j = 1; j <= cardsPerSuit; j++){
                    deck[cards++] = new Card(i , j);


                }
            }
        }   
    }

    //  Constructor creates and empty deck of 53 indexes set to null.
    //  Array element [0] is never used.
        public Deck(int deckArraySize){
            for(int i = 1; i < deckArraySize ; i++){
                deck[i] = new Card();
                }
        }

        public Deck(int suitCount , int cardsPerSuit , int deckArraySize){
    //  Constructor for special deck configuration.     
    }



        public void shuffle(){
            int SHUFFLE_COUNT = 100000;
            int arrayPos1 = 0;
            int arrayPos2 = 0;
            int count = 0;

            while(count < SHUFFLE_COUNT){
                Card card1 = deck[rgen.nextInt(DECK_ARRAY_SIZE)];
                if(card1 == deck[0]) card1 = deck[1];//This prevents the NullPointerException error.broke the always use braces rule.
                Card card2 = deck[rgen.nextInt(DECK_ARRAY_SIZE)];
                if(card2 == deck[0]) card2 = deck[52];//This prevents the NullPointerException error.broke the always use braces rule.



                for(int i = 1; i < DECK_ARRAY_SIZE; i++){
                    for (int j = 1; j < DECK_ARRAY_SIZE; j++){
                        if (deck[i].equals(card1)){
                            arrayPos1 = i;
                            if (deck[j].equals(card2)){
                            arrayPos2 = j;

                            }
                        }
                    }   
                    Card temp = deck[arrayPos1];
                    deck[arrayPos1] = deck[arrayPos2];
                    deck[arrayPos2] = temp;
                    count++;    

                }       

        }           

    }


    //  Gets the top card of the deck.
        public Card getTopCard(){
            Card topCard = deck[1];
            return topCard;

        }
    //  Gets a card at specified index. 
        public Card getCardAt(int cardAtPos){
            Card cardAt = deck[cardAtPos];  
            return  cardAt; 

        }


    //This method makes an implicit call to the Card classes toString method when string manipulation is performed .    
    //This is done by the compiler automatically.
        public void getDeckInfo(){
            for(int i = 1; i < DECK_ARRAY_SIZE ; i++){
            System.out.print(i +" ");
            System.out.println(deck[i]);
            }
        }


        public String toString(){
    //      getDeckInfo();
            return "Nothing to see here, move along.";

        }

    private Random rgen = new Random();
    public static final int DECK_ARRAY_SIZE = 53;
    private Card[] deck = new Card[DECK_ARRAY_SIZE];

    }
    public class Dealer {

    public Dealer(){
        playDeck = new Deck();
        discardStack = new Deck(Deck.DECK_ARRAY_SIZE);
    }

    public int getDeckCardCount(){
// this count should go down anytime a card is dealt to a player 
        return deckCardCount;
    }

    public int getDiscardCount(){
// this count should go up anytime a card is removed from the deck or from play.        
        return discardCount;
    }

    public void dealCards(){

// should be self explanatory.      
    }

    public void shuffle(){
// need to make sure that shuffle compensates for cards removed 
// from deck and move cards to the front of the array before the shuffle.
// make sure that the empty indexes at the end of the array are not included in the shuffle.
// also need a shuffle method for shuffles at the end of the game. or just 
// throw out he deck and get a new deck.... 
// maybe pass the remaining cards in the deck to the shuffle method .

    }

    public String toString(){

        return "Nothing to see here , move along.";
}
public Deck playDeck;   
public Deck discardStack;
private int deckCardCount;
private int discardCount;
    }

public class CardTest  {

public static void main(String[] args){


    Card singleCard = new Card();
    Deck deck = new Deck();
    Deck playDeck = new Deck();
    Deck discardStack = new Deck(53); 
    Dealer dealer = new Dealer();

System.out.println("value: "+singleCard.getValue());
System.out.println("Name: "+singleCard.getName());
System.out.println("Suit: "+singleCard.getSuitAsString());
System.out.println("Card to string: "+singleCard.toString());
System.out.println("New card: " +singleCard.getNewCard());
System.out.println("New card: " +singleCard.getNewCard());
System.out.println("New card: " +singleCard.getNewCard());
System.out.println("Pass a 4 to the getSuitAsString() method:    "+singleCard.getSuitAsString(4));
System.out.println("Pass a 12 to the getName() method: "+singleCard.getName(12));
deck.getDeckInfo();
System.out.println("Top card is: "+deck.getTopCard());

System.out.println("Shuffling...");
int count =0;
while(count < 500){
dealer.playDeck.shuffle();
discardStack.shuffle();
count++;
}

deck.getDeckInfo();
System.out.println("Top card is: "+deck.getTopCard());
System.out.println("Card at position ??: "+deck.getCardAt(5));
playDeck.getDeckInfo();
discardStack.getDeckInfo();
playDeck.shuffle();
playDeck.getDeckInfo();
discardStack.shuffle();
discardStack.getDeckInfo();
dealer.playDeck.getDeckInfo();
dealer.playDeck.shuffle();
System.out.println("Shuffling...");
dealer.playDeck.getDeckInfo();
System.out.println(dealer.playDeck.getCardAt(5));
System.out.println(dealer.discardStack.getCardAt(5));   


}

}

4

3 回答 3

1

该模型还不错,问题出在您的第二个和第三个甲板构造函数上。第一个构造函数确保了合法的甲板,而另外两个创建了一个甲板,您在下面的示例代码中会摸不着头脑。

问问自己为什么你需要一个“特殊结构”,如果你这样做......为什么你不能从你的第一个甲板构造器创建的正确的 52 张牌组开始并从那里开始工作......即......移除你没有的牌不需要。

最后,OO 哲学规定您的实例在构建后永远不应处于无效状态。拥有一个构造函数 Card() 根本不够好,因为你需要知道你想要最终得到什么卡.. 6 of heart Card.new(6, "hearts") 是一个有效的 ctor

于 2013-01-31T04:32:42.733 回答
0

我有一种感觉,我不明白一些东西,但不是把这个方法放在你的 Deck 类中那么简单:

public Card[] getCards() {
    return deck;
}

现在你Dealer可以打电话aDeck.getCards()并用它做任何事情。

例如,我把这段代码放在Dealer类中,它编译得很好:

public Dealer() {
    Deck deck = new Deck();
    for (Card card : deck.getCards()) {
        int suit = card.getSuit();
        int value = card.getValue();
    }
}
于 2013-01-31T04:29:06.440 回答
0

一些一般性说明:

  • 在某些时候,您将不得不开始学习/处理从索引 0 开始的数组。最好早点做,因为您正在做的事情(“//Array element [0] is never used.”将以后可能只会导致一个错误...

  • 只定义你真正需要的构造函数。空的构造函数只有在有合适的用例时才有用。通常在构造之后,您的对象实例应该处于有效状态 - 如果您调用默认值,情况并非如此newu Card()

  • 有许多可能的方式来处理字符串常量。你这样做的方式是最简单的,但也是最容易出错的。例如,如果您在"Diamonds"某处的字符串中打错字,则可能需要很长时间才能找到。现在,我建议使用常量,即一些public static final DIAMOND = "Diamond"定义。像这样,每当您需要您的钻石串时,您只需使用变量DIAMOND- 您会知道它包含正确的内容并且无法修改。(稍后,您还可以开始阅读有关Enum类型的内容。)

对你的问题

我觉得有些误解正在发生...

“我知道,如果我从 println 语句中引用卡片,它会调用卡片到字符串方法。但我要寻找的是卡片的实际数据。”

这不是真的。发生的情况是:如果您的卡片变量位于 Java 只需要一个字符串的位置 - 例如,当使用+运算符连接两个字符串时 - 然后它会访问该toString()方法。

Card card = // some card variable

// This will use card.toString()
system.out.println("Printing a card string " + dealer.playDeck.getCardAt(5));

// However, you can just print something like this:
system.out.println("Printing a card suit " + dealer.playDeck.getCardAt(5).getSuitAsString());

附录
关于您对其他答案的评论中的问题。

for(int i = 0; i < deck.getCards().length; i++){
    deck.getCards()[i] // this is the i-th card, do something with it
}
于 2013-01-31T07:22:11.520 回答