Java中的纸牌游戏:创造“手牌”

时间:2014-01-04 21:14:46

标签: java

我想首先说这不是家庭作业,而是我正在Java工作的宠物项目,以便更好地理解完整的Java程序,并希望这是我第一次尝试制作自己的GUI。

除此之外,我有一个关于从我当前的代码创建一手牌的问题。就目前而言,我有一个名为DummyRummy的主类,一个用于创建卡片的类,一个创建标准牌组的类(其中包括Jokers并在创建新牌组时“改组”牌组),以及一个PlayerHands课程。我正在使用ArrayList s严格处理所有代码,PlayersHands类应返回将使用的两个ArrayList。但是,当我尝试在PlayerHands()中调用public static void main()方法时,由于某种原因无法找到PlayerHands ...以下是PlayerHands的代码:

package dummyrummy;
public class PlayerHands {
    private Card[] playerOneCards;
    private Card[] playerTwoCards;
    private int[] value;

    PlayerHands(deck d, int round)
    {
        value = new int[round+3];
        playerOneCards = new Card[round+2];
        playerTwoCards = new Card[round+2];
               //(round+2) is the handsize at any given time
        for (int x=0; x<round+3; x++)
        {
        playerOneCards[x] = d.drawFromDeck(); //fills up one hand.
        playerTwoCards[x] = d.drawFromDeck(); //fills up second hand.
        }
    }
}

这是DummyRummy类。

package dummyrummy;
import java.util.*;
public class DummyRummy {
    public static void main(String[] args) {
        deck testDeck;
        testDeck = new deck();

        System.out.println(testDeck.getTotalCards());
        System.out.println(testDeck.getClass());
        int round = 1;
        PlayerHands(testDeck, round); //This is where the error is occurring        
        }//End of arguments
    }//End of DummyRummy class

以下是Card类:

package dummyrummy;
public class Card
{
    private short rank, suit;
    private static String[] suits = { "Hearts", "Spades", "Diamonds", "Clubs", "Joker"    };
    private static String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
    private static String[] jokers = {"Joker", "Joker"};
    private static String[] ranks2 = {"0", "0"};
    public static String rankAsString( int __rank ) {
        if (__rank != 0){
            return ranks[__rank];
        }//End of if statement
        return ranks2[__rank];
    }//End of rankAsString class

    Card(short suit, short rank)
    {
        this.rank=rank;
        this.suit=suit;    
    }//End of Card Initializer

    public @Override String toString()
    {
        if(suit == 5){
            return "Joker";
        }//End of if statement that calls jokers
        if(rank == 0){
            return "Joker";
        }
        return ranks[rank] + " of " + suits[suit];              
    }//End of toString method

    public short getRank() {
         return rank;
    }//End of getRank method

    public short getSuit() {
        return suit;
    }//End of getSuit method
}//End of Card

最后,这是deck()类:

package dummyrummy;
import java.util.Random;
import java.util.ArrayList;

public class deck {
    private ArrayList<Card> cards;
    deck()
    {
        cards = new ArrayList<Card>();
        int index_1, index_2;
        Random generator = new Random();
        Card temp;

       short jokerSuit=5;
       short jokerRank = 0;
           cards.add(new Card(jokerSuit, jokerRank));
           cards.add(new Card(jokerSuit,jokerRank));
        for (short suit=0; suit<=3; suit++)
        {
             for (short rank=0; rank<=12; rank++)
             {
                  cards.add(new Card(suit,rank));
             }
        }//End of for-loop       
        int deckSize = 54;             
        for (int i=0; i<1000; i++)
        {
            index_1 = generator.nextInt( cards.size() );
            index_2 = generator.nextInt( cards.size() );
            temp = cards.get( index_2 );                    
            cards.set( index_2 , cards.get( index_1 ) );
            cards.set( index_1, temp );
        }//End of for-loop
    }//End of deck()
    public Card drawFromDeck()
    {
    /*
     * This method removes the top card of the already shuffled deck.          
     * The next step to take with this class is put the drawn card into another
     *     array that represents a players hand.  
     * This will take two arrays, and must be called depending on what player   'drawsFromDeck'.
     */        
        return cards.remove( 0 );
    }//End of drawFromDeck()
    public int getTotalCards()
    {
        return cards.size();   
    }//End of getTotalCards()
}//End of class deck

感谢您的时间,感谢您提供的任何帮助。如有必要,我也非常乐意提供其他代码。

编辑:我已经添加了上面的类和包。

2 个答案:

答案 0 :(得分:2)

PlayerHands(deck, int)是一个构造函数。因此,您必须像这样(在课程DummyRummy中)调用它:

new PlayerHands(testDeck, round);

因为我猜你想要使用你创建的实例,你应该保存对变量的引用:

PlayerHands playerHands = new PlayerHands(testDeck, round);

答案 1 :(得分:0)

PlayerHands(deck d, int round)不是方法,它是构造函数。

要正确获取双手,您可以使用以下代码:

    ...
    int round = 1;
    PlayerHands playerHands = new PlayerHands(testDeck, round); //This creates a new instance of the PlayerHands class
    //access the players' hands like this:
    Card[] player1Hand = playerHands.playerOneCards; //sets "player1Hand" to the "Card[] playerOneCards" contained in "playerHands"
    Card[] player2Hand = playerHands.playertwoCards; //sets "player2Hand" to the "Card[] playerTwoCards" contained in "playerHands"

    }//End of arguments
}//End of DummyRummy class