首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Card DeckDriver java

Card DeckDriver java
EN

Stack Overflow用户
提问于 2012-05-11 09:51:52
回答 2查看 1K关注 0票数 0

我已经修复了这个问题,现在它可以正常运行了,我的问题是,我的构造函数有什么问题?正如有人指出的那样,有一个问题,但我无法找到它,而且我想从我的牌组中提取和删除一张卡,有什么关于如何做到这一点的指示吗?

我的deck类:

代码语言:javascript
复制
 import java.util.*;


public class Deck {

    private static Card[] cards;
    private static int counter;

    int i=52;
    int x=0;

    public Deck(Card[] card){
this.cards=card;
    }



    public int createDeck(){

        cards = new Card[52];
        for (int a=0; a<=3; a++)
        {
            for (int b=0; b<=12; b++)
            {
                cards[x] = new Card(a,b);
                x++;
                counter++;
            }
        }

    }


    public Card[] resetDeck(){
        createDeck();
        return cards;
    }

    public static void getRandomCard(){
        int chosen = (int) (52*Math.random())+1;
        System.out.println(cards[chosen]);
    }

    public static int getCounter(){
        return counter;
    }

    public static void print(){

         for(int x=0;x<52;x++){
            System.out.println(Deck.cards[x]);
        }
    }


}

我的DecktestDriver

代码语言:javascript
复制
public class DeckTestDriver {
    public static void main(String[] args){

        Deck test = new Deck(new Card[52]);
        test.createDeck();

        int input =test.getCounter();
        System.out.println("Number of cards in Deck = "+input);

        System.out.print("looking at one card in the deck-----");
        test.getRandomCard();
        System.out.println(".........................");

        System.out.println("Printing full list of cards in deck");
        System.out.println(".........................");

    test.print();
    System.out.println();
    System.out.println(".........................");
    System.out.println();

    System.out.println("Creating New Deck");
    System.out.println(".........................");
    System.out.println();

    Deck test1 = new Deck(new Card[52]);
    test1.createDeck();
    System.out.println();

    System.out.println("Printing new full list of cards in deck");
    System.out.println(".........................");

test1.print();
System.out.println(".........................");
System.out.println();

int input1 =test1.getCounter();
System.out.println("Number of cards in Deck = "+input1);

System.out.print("looking at one card in the deck-----");
test1.getRandomCard();
System.out.println(".........................");

    }
}

我的卡片类:

代码语言:javascript
复制
public class Card {

    private int CardFaceValue;
    private int CardFaceSuit;
    private int cardFlippedNum;
    private int cardFlippedSuit;
    final static int MIN_VALUE_NUM=1;
    final static String MIN_VALUE_SUIT="Diamond";
    final static int MAX_VALUE_NUM=13;
    final static String MAX_VALUE_SUIT="Spade";



    public Card(int cardFlippedNum,int cardFlippedSuit){
        cardFlippedNumber();
        cardFlippedSuitType();

    }



    public int cardFlippedNumber(){
        int cFn = (int) (Math.random()*13)+1;
        cardFlippedNum = cFn;
        return CardFaceValue;

    }
    public int cardFlippedSuitType(){
        int cFs = (int)(Math.random()*4)+1;

        cardFlippedSuit = cFs;

        return CardFaceSuit;

    }



    public int getNum(){

        return cardFlippedNum;
    }

    public int getSuit(){
        return cardFlippedSuit;

    }

    public String toString() {      
        return (getCardName() + " of " + getSuitName());
    }

    public String getCardName() {
        switch (cardFlippedNum) {           //Change return cases to numbers if you want a number shown e.g: 1 of Hearts
        case 1:
            return ("Ace"); 
        case 2:
            return ("TWO");
        case 3:
            return ("THREE");
        case 4:
            return ("FOURTH");
        case 5:
            return ("FIVE");
        case 6:
            return ("SIX");
        case 7:
            return ("SEVEN");
        case 8:
            return ("EIGHT");
        case 9:
            return ("NINE");
        case 10:
            return ("TEN");
        case 11:
            return ("Jack");
        case 12:
            return ("Queen");
        case 13:
            return ("King");
        default:
            return ("" + cardFlippedNum);
        }
    }

    public String getSuitName() {
        switch (cardFlippedSuit) {
        case 1:
            return ("Diamonds");
        case 2:
            return ("Clubs");
        case 3:
            return ("Hearts");
        case 4:
            return ("Spades");
        default:
            return ("Invalid");
        }
    }



}

我的输出:

代码语言:javascript
复制
     Number of cards in Deck = 52
looking at one card in the deck-----Jack of Clubs
.........................
Printing full list of cards in deck
.........................
TWO of Spades
SIX of Clubs
NINE of Hearts
TWO of Diamonds
FOURTH of Clubs
FOURTH of Spades
TEN of Clubs
Jack of Spades
EIGHT of Diamonds
Queen of Diamonds
Queen of Diamonds
TEN of Spades
EIGHT of Hearts
Ace of Hearts
SIX of Diamonds
King of Clubs
THREE of Diamonds
TWO of Hearts
SIX of Spades
SIX of Hearts
THREE of Spades
EIGHT of Hearts
FIVE of Clubs
EIGHT of Diamonds
Jack of Clubs
Ace of Diamonds
NINE of Diamonds
SEVEN of Hearts
TEN of Diamonds
SEVEN of Diamonds
SEVEN of Diamonds
EIGHT of Hearts
FIVE of Hearts
THREE of Clubs
THREE of Spades
FIVE of Spades
TWO of Diamonds
TWO of Clubs
NINE of Hearts
FIVE of Hearts
SIX of Spades
TEN of Diamonds
FOURTH of Hearts
King of Hearts
Ace of Spades
THREE of Spades
NINE of Spades
King of Spades
King of Diamonds
King of Diamonds
Jack of Hearts
THREE of Clubs

.........................

Creating New Deck
.........................


Printing new full list of cards in deck
.........................
Ace of Clubs
SEVEN of Hearts
Queen of Clubs
TWO of Diamonds
King of Spades
Ace of Hearts
Ace of Spades
FOURTH of Spades
NINE of Spades
TWO of Hearts
FOURTH of Hearts
THREE of Hearts
THREE of Spades
Ace of Spades
Ace of Diamonds
Jack of Spades
TWO of Diamonds
Queen of Clubs
SIX of Hearts
TEN of Clubs
EIGHT of Diamonds
TWO of Spades
King of Hearts
TWO of Hearts
King of Hearts
NINE of Spades
FOURTH of Hearts
FIVE of Hearts
SIX of Clubs
Jack of Hearts
FOURTH of Spades
Queen of Clubs
TWO of Clubs
Ace of Clubs
NINE of Spades
TEN of Clubs
SIX of Spades
Jack of Spades
Queen of Spades
TWO of Diamonds
EIGHT of Spades
SIX of Hearts
Ace of Diamonds
FOURTH of Diamonds
Queen of Diamonds
Jack of Hearts
TWO of Clubs
FOURTH of Diamonds
SIX of Diamonds
King of Diamonds
TWO of Spades
TEN of Diamonds
.........................

Number of cards in Deck = 104
looking at one card in the deck-----SIX of Clubs
.........................
EN

回答 2

Stack Overflow用户

发布于 2012-05-11 09:55:36

之后

代码语言:javascript
复制
Deck test = new Deck(new Card[52]);

您从未调用过createDeck()。顺便说一句,你的构造函数和createDeck有点精神分裂。您将deck传递给构造函数,但是如果不创建一个新的数组,您就无法创建一个空白的deck。

编辑

  1. 关于精神分裂症..。

对于Deck的构造函数,您具有以下内容:

公共甲板(Card[]卡){ this.cards=card;}

奇怪的是,我把一副牌看作是一堆牌,而你却把这些牌传给了这副牌。这可能只是一个风格问题。然而,我更期待的是:

public Deck() { createDeck();}

  • createDeck()有一个奇怪的地方--它应该返回一个int,但没有return语句。也许在签名中返回“void”会更好。我会把它设为私人的。如果有人想要一个新的Deck,他们应该直接使用Deck deck = new Deck();,而不是调用createDeck();

  • I永远不会让Deck将Cards[]数组返回给用户。Deck中的数据结构和其他内部结构不关任何人的事。resetDeck()就是这么做的。不返回Card[]意味着Deck必须提供Card getTopCard()Card getRandomCard()等方法,因为它不再“打开和服”。但这是一件好事-我们希望甲板(一个非常好的名词)有动词式的方法,允许人们操纵甲板。

  • 我看到了一个非标准的循环结构:for (int b=0; b<=12; b++) This is not wrong。但是,一般来说,循环的条件部分是不使用<=编写的。相反,使用<。所以我想我会说,当循环不是从0开始的时候,我忽略了这个半标准,

  • ,In,for(int b=0; b<13; b++),I,

  • ,I,public String getCardName()。考虑一个稍微不同的实现。重点是通过定义数据结构而不是通过编写显式代码来删除代码行(从而消除错误)。看看stringy switch语句是如何被一行运行时代码替代的。cardNames的字符串数组是新的,但它是编译时代码,不太可能引入错误。

公共类卡{ ...私有静态最终String[] cardName =新String[] {'ONE','TWO','THREE',...etc};...公有String getCardName() { return cardNamecardFlippedNum;} }

票数 6
EN

Stack Overflow用户

发布于 2012-05-11 10:02:32

你需要先创建52张带循环的卡片,我建议在Deck的构造函数中完成这些操作。使用伪代码编写:

代码语言:javascript
复制
public Deck() {
    cards = new Cards[52];
    for(i=0; i<52; i++) {
        cards[i] = new Card(i % 13, i / 13);
    }
}

在此之后,Deck的卡片阵列将如下所示:

代码语言:javascript
复制
 {0,0}, {1,0}, {2,0}..{12,0}
 {0,1}, {1,1}, {2,1}..{12,1}
 {0,2}, {1,2}, {2,2}..{12,2}
 {0,3}, {1,3}, {2,3}..{12,3}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10544398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档