无法获得卡/卡座对象程序

时间:2017-10-28 22:41:09

标签: java arrays playing-cards

这是我第一次在论坛上发帖,我不完全确定我的问题是否有效,但我会尝试具体并遵循指南。遵循指南,这是一个基于类分配的问题。这项任务是采用创建“甲板”的代码。代表一副牌并增加几个特征的对象。我目前陷入困境。

我的问题在于此代码:

    public class SilasAlmgrenS6L1CardCreate {
public static void main(String args[]) {
    Deck d = new Deck();
    d.shuffle();

   Hand f = new Hand(d); //Returns error 'Hand cannot be resolved to a type'

}

public static class Deck {
    Card[] cardArray = new Card[52];
    Deck() { //constructor
        int suits = 4;
        int cardType = 13;
        int cardCount = 0;
        for (int i = 1; i <= suits; i++)
            for (int j = 1; j <= cardType; j++) {
                cardArray[cardCount] = new Card(i, j);
                cardCount++;
            } //End loop
    } //End deck() constructor


    ////////////////////////////////////////////////////////
    //My code starts here

    public class Hand {
        Hand(Deck a) {
            Card[] Hand = {a.cardArray[0], a.cardArray[1], a.cardArray[2], a.cardArray[3], a.cardArray[4]};
            Card[] playerHand = {Hand[0], Hand[1]};
            System.out.println("You have " + playerHand[0] + " and " + playerHand[1] );

        } //End hand constructor
    } //End hand class

    public void shuffle() {
        //Runs loop for the length of the deck
        for(int i = 0; i < cardArray.length; i++) {
            int num = (int) (Math.random() * (51 - 0)) + 0; //Creates a random number between 0 and 51; used to shuffle
            Card placeHolder = cardArray[i]; //Picks a place holder card from the deck
            cardArray[i] = cardArray[num]; //Picks two random cards and make them equal
            cardArray[num] = placeHolder; //Assigns one of the duplicate cards to the placeholder value 
        } //End for
    } //End shuffle


    //And ends here
    /////////////////////////////////////////////////


    public void print() {
        for (int i = 0; i < cardArray.length; i++)
            System.out.println(cardArray[i]);
    } //End print loop
} //End print class



public static class Card {
    String suit, name;
    int points;
    Card(int n1, int n2) {
        suit = getSuit(n1);
        name = getName(n2);
        points = getPoints(name);
    } //End card class

    public String toString() {
        return "The " + name + " of " + suit;
    } //End toString

    public String getName(int i) {
        if (i == 1) return "Ace";
        if (i == 2) return "Two";
        if (i == 3) return "Three";
        if (i == 4) return "Four";
        if (i == 5) return "Five";
        if (i == 6) return "Six";
        if (i == 7) return "Seven";
        if (i == 8) return "Eight";
        if (i == 9) return "Nine";
        if (i == 10) return "Ten";
        if (i == 11) return "Jack";
        if (i == 12) return "Queen";
        if (i == 13) return "King";
        return "error";
    } //End getName String

    public int getPoints(String n) {
        if (n == "Jack" || n == "Queen" || n == "King" || n == "Ten")
            return 10;
        if (n == "Two")
            return 2;
        if (n == "Three")
            return 3;
        if (n == "Four")
            return 4;
        if (n == "Five")
            return 5;
        if (n == "Six")
            return 6;
        if (n == "Seven")
            return 7;
        if (n == "Eight")
            return 8;
        if (n == "Nine")
            return 9;
        if (n == "Ace")
            return 11;
        return -1;
    } //End int getPoints

    public String getSuit(int i) {
        if (i == 1) return "Diamonds";
        if (i == 2) return "Clubs";
        if (i == 3) return "Spades";
        if (i == 4) return "Hearts";
        return "error";
    } //End getSuit String        
} //End Deck class

}

这个程序的大部分已经提供了作业,但我们要添加几个功能。第一个功能是shuffle方法,我能够做到。在下一个功能中,我们将创建一个&#39; Hand&#39;向用户交易牌并计算他们拥有多少分的类,就好像它是一个二十一点游戏。 这是这一步的确切措辞:

&#39;添加一个包含5个Card引用数组的Hand Class。让程序交易Hand两张牌并显示给用户。告诉用户他们有多少积分并询问他们是否想要另一张卡。继续允许玩家添加牌,直到他们达到5张牌或总数大于21张。&#39;

我已经尝试了几种方式,我觉得我可以创建这个类,但似乎没有一个工作。这个当前的迭代就像我得到的那样接近。不过,我现在很难过。我的问题是;我不知道为什么我在尝试使用Hand类时遇到类型错误而我不知道如何实现getPoints()方法。创建Hand类之后有几个步骤,但我相信如果我能弄清楚如何使这个类工作,我可以通过它们。我已经在墙上打了一个洞,所以任何修复这段代码的帮助都绝对值得赞赏。

1 个答案:

答案 0 :(得分:0)

第一个问题是你的Hand类是静态Deck类的子类。这样您就无法在main方法中使用它,因为那不是封闭类。

修改

所以,我在这里有点过分了。希望对你有意义。

首先,我们有你的主要课程。我为了不想输入那个长名而改名。我将HandDeck个对象添加为变量,因为Hand既不属于Deck,也不属于其他方式,但它们都是Main类的一部分,让&#39; s称它为游戏。

你首先洗牌。当你有甲板时,你可以得到一张随机卡。这是由甲板提供的。 (我很快就会谈到这个方法)。

只要您有一手牌,您就可以向用户显示他们拥有的积分数量,并询问他们是否需要额外的卡片(如果numberOfPoints&lt; 22和numberOfCards&lt; 5)。如果他们确实想要一张额外的卡片,请向牌组索取随机卡并将其添加到手中。继续,直到满足任何边界。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CardExample {

private Hand playHand;
private Deck playDeck;
private Scanner reader = new Scanner(System.in);  // Reading from System.in


public static void main(String args[]) {
    new CardExample().play();
}

public void play(){
    playDeck = new Deck();
    playDeck.shuffle();

    Card firstCard = playDeck.getRandomCard();
    Card secondCard = playDeck.getRandomCard();

    List<Card> startCards = new ArrayList<>();
    startCards.add(firstCard);
    startCards.add(secondCard);

    playHand = new Hand(startCards);
    requestInput();
}

private void requestInput(){
    System.out.println("You have " + playHand.getPoints() + " points");
    System.out.println("New card? (Y/N)");
    String input = reader.next();
    if(input.equalsIgnoreCase("y")){
        Card newCard = playDeck.getRandomCard();
        playHand.addCard(newCard);

        if(playHand.getNumberOfCards() < 5 && playHand.getPoints() < 22) {
            requestInput();
        }else if(playHand.getPoints() >= 22){
            System.out.println("You have " + playHand.getPoints() + "points. You're dead, sorry.");
            reader.close();
        } else{
            System.out.println("You have 5 cards, that's the max");
            reader.close();
        }
    }else{
        System.out.println("Your score is " + playHand.getPoints() + " points");
        reader.close();

    }
}
}

甲板课

 public class Deck {

private Card[] cardArray = new Card[52];
private int currentIndex = 0;

public Deck() { //constructor
    int suits = 4;
    int cardType = 13;
    int cardCount = 0;
    for (int i = 1; i <= suits; i++)
        for (int j = 1; j <= cardType; j++) {
            cardArray[cardCount] = new Card(i, j);
            cardCount++;
        } //End loop
}

public void shuffle() {
    //Runs loop for the length of the deck
    for(int i = 0; i < cardArray.length; i++) {
        int num = (int) (Math.random() * (51 - 0)) + 0; //Creates a random number between 0 and 51; used to shuffle
        Card placeHolder = cardArray[i]; //Picks a place holder card from the deck
        cardArray[i] = cardArray[num]; //Picks two random cards and make them equal
        cardArray[num] = placeHolder; //Assigns one of the duplicate cards to the placeholder value
    } //End for
} //End shuffle

public Card[] getCardArray() {
    return cardArray;
}

public Card getRandomCard(){
    Card nextCard = cardArray[currentIndex];
    currentIndex += 1;
    return nextCard;
}


//And ends here
/////////////////////////////////////////////////


public void print() {
    for (int i = 0; i < cardArray.length; i++)
        System.out.println(cardArray[i]);
} //End print loop
}

手课 addCard更新积分,以便每当获得numberOfCards或积分时,都会返回正确的值

import java.util.ArrayList;
import java.util.List;

public class Hand {
private int points = 0;
private int numberOfCards = 0;
private List<Card> hand = new ArrayList<>();


public Hand(List<Card> cards) {
    hand.addAll(cards);
    numberOfCards += cards.size();
    for(Card card: cards){
        points += card.points;
    }
} //End hand constructor

public void addCard(Card card){
    hand.add(card);
    points += card.points;
    numberOfCards += 1;
}

public int getNumberOfCards() {
    return numberOfCards;
}

public int getPoints() {
    return points;
}


}

卡类(未更改,但是单独的文件)

public class Card {
String suit, name;
int points;
Card(int n1, int n2) {
    suit = getSuit(n1);
    name = getName(n2);
    points = getPoints(name);
} //End card class

public String toString() {
    return "The " + name + " of " + suit;
} //End toString

public String getName(int i) {
    if (i == 1) return "Ace";
    if (i == 2) return "Two";
    if (i == 3) return "Three";
    if (i == 4) return "Four";
    if (i == 5) return "Five";
    if (i == 6) return "Six";
    if (i == 7) return "Seven";
    if (i == 8) return "Eight";
    if (i == 9) return "Nine";
    if (i == 10) return "Ten";
    if (i == 11) return "Jack";
    if (i == 12) return "Queen";
    if (i == 13) return "King";
    return "error";
} //End getName String

public int getPoints(String n) {
    if (n == "Jack" || n == "Queen" || n == "King" || n == "Ten")
        return 10;
    if (n == "Two")
        return 2;
    if (n == "Three")
        return 3;
    if (n == "Four")
        return 4;
    if (n == "Five")
        return 5;
    if (n == "Six")
        return 6;
    if (n == "Seven")
        return 7;
    if (n == "Eight")
        return 8;
    if (n == "Nine")
        return 9;
    if (n == "Ace")
        return 11;
    return -1;
} //End int getPoints

public String getSuit(int i) {
    if (i == 1) return "Diamonds";
    if (i == 2) return "Clubs";
    if (i == 3) return "Spades";
    if (i == 4) return "Hearts";
    return "error";
} //End getSuit String
} //End Deck class