0

シンプルな52枚のカードゲームを作っています。カードの取り扱い、シャッフル、保管に多くの問題があります。各プレイヤーに5枚のカードを配る必要があります。ゲームはハーツに似ていますが、各プレイヤーは5枚のカードしか受け取りません。これまでのコードは次のとおりです。

    int[] deck = new int[52];
    String[] suits = { "Spades", "Hearts", "Diamonds", "Clubs" };
    String[] ranks = { "A ", "2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ",
            "9 ", "10", "J ", "Q ", "K " };


    // Initialize the cards
    for (int i = 0; i < deck.length; i++)
        deck[i] = i;

    // Shuffle the cards
    for (int i = 0; i < deck.length; i++) {

        // Generate an index randomly

        int index = (int) (Math.random() * deck.length);
        int temp = deck[i];
        deck[i] = deck[index];
        deck[index] = temp;

    }

    // Display the first five cards
    System.out.println("player 1 has:");
    for (int i = 0; i < 5; i++) {
    String suit = suits[deck[i] / 13];
    String rank = ranks[deck[i] % 13];
    selectionSort(deck);
    System.out.println("Card number " + deck[i] + " : " + rank + " of "
                + suit);

    }

    int player1[] = deck;

    // Display the first five cards
    System.out.println("\n" + "player 2 has:");
    for (int i = 5; i < 10; i++) {
    String suit = suits[deck[i] / 13];
    String rank = ranks[deck[i] % 13];
    System.out.println("Card number " + deck[i] + " : " + rank + " of "
                + suit);

    }

    int player2[] = deck;

    // Display the first five cards

    System.out.println("\n" + "player 3 has:");
    for (int i = 10; i < 15; i++) {
    String suit = suits[deck[i] / 13];
    String rank = ranks[deck[i] % 13];

    System.out.println("Card number " + deck[i] + " : " + rank + " of "
    + suit );

    }
    int player3[] = deck;

}

public static void selectionSort(int[] deck) {

    for (int i = 52; i < deck.length - 1; i--){
    int currentMax = deck[i];
    int currentMaxIndex = i;

    for (int j = i + 1; j < deck.length; j++) {
        if (currentMax > deck[j]) {
            currentMax = deck[j];
            currentMaxIndex = j;
        }}
    if (currentMaxIndex != i) {
        deck[currentMaxIndex] = deck[i];
        deck[i] = currentMax;

        }
    }
}
 }
4

3 に答える 3

2

「カード」をオブジェクトにします。

これにより、コードが大幅に簡素化されます。文字列の配列を使用する代わりに、各カード オブジェクトにランクとスーツの 2 文字だけを配置します。

次に、「デッキ」をカードの配列にすることができます。

プレイヤーにも、独自のカードの配列を持つオブジェクトを持たせます。

必要に応じて、デッキをオブジェクトにすることもできます。

deck.shuffle();
于 2012-12-30T18:03:29.780 に答える
1

わかりました。これは長期的には役立つと思います。私はあなたのコードを約2秒間見て、いくつかの深刻な構造上の問題があることを確認しました。ここから始める必要があります。

public enum Suit
{
     CLUBS, SPADES, HEARTS, DIAMONDS
}

public enum Value
{
    TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
}

public class Card
{
    private Value value;
    private Suit suit;


    public Card(Suit theSuit, Value theValue)
    {
        suit = theSuit;
        value = theValue;
    }

    public Value getValue()
    {
        return value;
    }

    public Suit getSuit()
    {
        return suit;
    }
}

ベースが確立されたら、ハンドとデッキはどちらもカードのスタックであるため、非常に似ていると言えます。そのため、ベースを構築する必要があります。

public class CardStack
{
    public ArrayList<Card> cards;

    public CardStack()
    {
        cards = new ArrayList<Card>();
    }

    public Card draw()
    {
        if(cards.size() > 0)
        {
            Card card = cards.objectAt(cards.size() - 1);
            cards.remove(card);
            return card;
        }

        return null;
    }

    public void addCard(Card card)
    {
        cards.add(card);
    }
}

残りはあなた次第です。DeckとHandは両方ともCardStackを拡張する必要があります。非常に長い回答で申し訳ありませんが、これは長期的には役立ちます。構造が間違っていると、プログラムは正しく実行されません。明らかにPlayerクラスが必要であり、HandのインスタンスとGameクラス、またはデッキのインスタンスが必要です。デッキにはシャッフルとリセットの方法が必要です。手は本当に透明な肉棒のように必要なだけです。これがお役に立てば幸いです。幸運を!


アップデート

列挙型が何であるかわからない場合は、列挙型について詳しく説明します。

一般的なOOP情報は次のとおりです。

これらの両方を読むと、コードが劇的に改善されます。さらにサポートが必要な場合は、さらにコードを投稿できます。

于 2012-12-30T19:13:42.550 に答える
0

コメントや回答で他の人が指摘しているように、オブジェクト指向にすることでデザインを改善することができます。

私はその意図を理解していると思います。最初にデッキを作成してシャッフルします。その後、の最初の5つの要素はdeckプレーヤー1の手を表し、次の5つの要素はプレーヤー2の手を表します。

プレーヤー1の手を印刷している間、ループ内でselectionSortが呼び出されます。このメソッドは、配列全体をselectionSortソートします。deckよく調べてみると、selectionSort何も起こらないことがわかります。修正後selectionSort、出力にランダムなカードが表示され、最後のスーツ(クラブ)の最後の4枚のカードが続きます。

selectionSortプレーヤー1のハンドを印刷するループ内で呼び出すことにより、プレーヤー1のハンドをソートしようとしていると仮定します。

私の仮定が正しければselectionSort、配列のどの部分をソートするかを指定する下位インデックスと上位インデックスを指定できるように変更する必要があります。次のようになります。

public static void selectionSort(int[] deck, int start, int end) {

    for (int i = start; i<end; i++) {
        int currentMax = deck[i];
        int currentMaxIndex = i;

        for (int j = i + 1; j < end; j++) {
            if (currentMax < deck[j]) {
                currentMax = deck[j];
                currentMaxIndex = j;
            }
        }
        if (currentMaxIndex != i) {
            deck[currentMaxIndex] = deck[i];
            deck[i] = currentMax;
        }
    }
}

プレーヤー1のカードを並べ替えるには、を呼び出しますselectionSort(deck, 0, 5);。プレーヤー2の場合はselectionSort(deck, 5, 10);。プレーヤー3の場合selectionSort(deck, 10, 15);

于 2012-12-30T18:36:48.513 に答える