我似乎无法在我的主体中创建任何物体?

时间:2015-07-26 21:50:27

标签: java

该计划的目的是展示扑克和桥牌手。那些在我运行程序的时候,我可以让它显示扑克描述然后退出。我希望它继续并显示一个扑克手,然后是桥牌描述和一个桥牌手。我觉得我的问题来自抽象的DeckOfCards课,但我真的不确定。

我将所有类放在同一个文件中,以便在编程时更容易理解和编辑。

当程序失败时会出现这些错误

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 52
at DeckOfCards.creatCardDeck(PlayCardGames.java:106)
at DeckOfCards.DeckOfCards(PlayCardGames.java:95)
at PlayCardGames.main(PlayCardGames.java:14)

这是自己的程序代码(感谢您编辑它以更好地格式化)

    import javax.swing.*;
    import java.util.Random;

    public class PlayCardGames {

       public static void main(String[] args) {


           Poker playPoker = new Poker();
           playPoker.DeckOfCards();
           playPoker.displayDescription();
           playPoker.deal();

           Bridge playBridge = new Bridge();
           playBridge.DeckOfCards();
           playBridge.displayDescription();
           playBridge.deal();


       }

    }

    //--------------------------------------------------------------------------------\\

    interface DeckConstants{
       final static int CARDS_IN_SUIT = 13;
       final static int SUITS_IN_DECK = 4;
       final static int CARDS_IN_DECK = CARDS_IN_SUIT * SUITS_IN_DECK;
    }

    //================================================================================\\

    class Card{

       private String suit;
       private String rank;
       private int rankIndex;

       public Card(int suitIndex, int rankIndex){
           setSuit(suitIndex);
           setRank(rankIndex);
    }

    public String getSuit() {
        if (suit.equalsIgnoreCase("Hearts")){ suit = "\u2665";}
        else if (suit.equalsIgnoreCase("Diamonds")) { suit = "\u2666"; }
        else if (suit.equalsIgnoreCase("Clubs")) { suit = "\u2663"; }
        else if(suit.equalsIgnoreCase("Spades")) { suit = "\u2660"; }

        return suit;
    }

    public void setSuit(int suitIndex) {
        if (suitIndex == 1) {suit = "Spades";}
        else if (suitIndex == 2) {suit = "Hearts";}
        else if (suitIndex == 3) {suit = "Diamonds";}
        else if(suitIndex == 4) {suit = "Clubs";}

    }

    public String getRank() {
        if (rankIndex == 1) {rank = "Ace";}
        else if (rankIndex == 11) {rank = "Jack";}
        else if (rankIndex == 12) {rank = "Queen";}
        else if(rankIndex == 13) {rank = "King";}

        return rank;
    }

    public void setRank(int rankIndex) {
        if (this.rankIndex >= 13){
            this.rankIndex = 13;}
        else if(this.rankIndex <= 1) {
            this.rankIndex = 1;}

    }

    public String toString(String rank, String suit) {
        return getRank() +" of " + getSuit();
    }

    }

    //=================================================================================\\

    abstract class DeckOfCards implements DeckConstants{

       protected Card[] deck = new Card[CARDS_IN_DECK];

       public void DeckOfCards(){
        creatCardDeck();
        shuffle(deck);
    }

       public void creatCardDeck() {
        int numberOfCards = 0;

        for (int suitCounter = 1; suitCounter < CARDS_IN_SUIT; suitCounter++)
        {
            for (int rankCounter = 1; rankCounter < CARDS_IN_SUIT; rankCounter++)
            {
                deck[numberOfCards] = new Card(suitCounter, rankCounter);
                numberOfCards++;
            }
        }
    }

    public void shuffle(Card[] temp){
        Random rnd = new Random();

        for (int k = temp.length; k > 1; k--){
            int i = k - 1;
            int j = rnd.nextInt(k);

            Card tmp = temp[i];
            temp[i] = temp[j];
            temp[j]= tmp;

        }
    }

    public abstract void displayDescription();

    public abstract void deal();

    }

    //===============================================================================\\

    class Poker extends DeckOfCards{
       private int cardsDealt = 5;
       private int index = 0;

    public void Poker(){
        displayDescription();
        deal();
    }

    public void displayDescription(){
        String desc = "In poker, players bet on hands" +
                   "\n Winner can bluff or must have the highest hand if called";

        JOptionPane.showMessageDialog(null, desc);

    }

    public void deal() {

        String message = "Your Poker hand:\n";

        for (int x = index; x < cardsDealt; x++){
            message += deck[index] + "\n";
            index++;
        }

        if (index == CARDS_IN_DECK){
            shuffle(deck);
            index = 0;
        }

        JOptionPane.showMessageDialog(null, message);
    }


}

    //===============================================================================\\

    class Bridge extends DeckOfCards{
       private int cardsDealt = 13;
       private int index = 0;

    public void Bridge(){
        displayDescription();
        deal();
    }

    public void displayDescription(){
        String desc = "In bride, partners bid on how many tricks they will take." +
                "\n The high bid determines a trump suit";

        JOptionPane.showMessageDialog(null, desc);

    }

    public void deal() {
        String message = "Your Bridge hand:\n";

        for (int x = index; x < cardsDealt; x++){
            message += deck[index] + "\n";
            index++;
        }

        if (index == CARDS_IN_DECK){
            shuffle(deck);
            index = 0;
        }
        JOptionPane.showMessageDialog(null, message);

    }

}

1 个答案:

答案 0 :(得分:1)

你知道吗,我觉得这很简单: 这是你的代码:

for (int suitCounter = 1; suitCounter < CARDS_IN_SUIT; suitCounter++)
        {
            for (int rankCounter = 1; rankCounter < CARDS_IN_SUIT; rankCounter++)
            {

我想也许应该是:

for (int suitCounter = 1; suitCounter < SUITS_IN_DECK; suitCounter++)
            {
                for (int rankCounter = 1; rankCounter < CARDS_IN_SUIT; rankCounter++)
                {