使用for循环的方法不会随机播放数组元素或重写所有数组元素

时间:2019-01-27 18:44:01

标签: java arrays for-loop shuffle

我有一个类Deck_of_Cards,它具有一个默认的构造函数,该构造函数将一个新定义的大小为52的数组分配给'deck',然后激活该类的刷新方法。

refresh方法使用嵌套的for循环将卡片分配到卡组中,然后激活class shuffle方法,您可能已经猜到了,该方法使用for循环来随机化'deck'数组的所有元素的位置。执行测试代码应产生五张不同的卡,而不是五次获得一张卡,示例如下所示。

Jack of Diamonds
Jack of Diamonds
Jack of Diamonds
Jack of Diamonds
Jack of Diamonds

测试代码

public class test {

    Deck_of_Cards X = new Deck_of_Cards();

    int drawCount = 5;

    for(int i = 0; i<drawCount; i++)
    {
        printCard(X.drawCard());
    }

    //Outputs the given card's data as "<rank> of <suit>".
    public static void printCard(Card c) {
        String st;

        if (c.getRank() == '1')
            st = c.getRank() + "0 of " + c.getSuit();

        else if (c.getRank() == 'A')
            st = "Ace of " + c.getSuit();

        else if (c.getRank() == 'J')
            st = "Jack of " + c.getSuit();

        else if (c.getRank() == 'Q')
            st = "Queen of " + c.getSuit();

        else if (c.getRank() == 'K')
            st = "King of " + c.getSuit();

        else
            st = c.getRank() + " of " + c.getSuit();


        switch (c.getSuit()) {

卡类

public class Card {
    private char suit;
    private char rank;

    //Constructor 
    public Card(char s, char r) 
    {
        // Suit if-statement
        if (s == 'C' || s == 'D' || s == 'H' || s == 'S' )
        {
            s = suit;
        }
        else 
        {
            throw new IllegalArgumentException("Not a valid suit!");
        }

        // Rank if-statement
        if (r == 'A' || r == '2' || r == '3' || r == '4' || r == '5' || r == '6' || r == '7' || r == '8' || r == '9' || r == 'J' || r == 'Q' || r == 'K')
        {
            r = rank;
        }
        else 
        {
            throw new IllegalArgumentException("Not a valid rank!");
        }

    }

    // Accessors
    public char getRank()
    {
        return rank;
    }

    public char getSuit()
    {
        return suit;
    }

Deck_of_Cards类

import java.util.*;

public class Deck_of_Cards {

    private Card deck [];
    private int position;


    Random rng = new Random();


    public Deck_of_Cards ()
    {
        deck = new Card[51];


        refresh();
    }

    public void refresh()
    {   
        char suit[] = {'C', 'D', 'H', 'S'};
        char rank[] = {'A', '2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K'};

        int index = 0;
        for (int i = 0; i < suit.length; i++)
        {
            for (int j = 0; j < rank.length; j++) 
            {
                deck[index] = new Card(suit[i], rank[j]);
                index++;
            }
        }

        shuffle();
    }

    public void shuffle()
    {
        for (int x = deck.length; x >= 0; x--)
        {
            position = rng.nextInt(x+1);
            Card g = deck [x];

            deck[x] = deck[position];
            deck[position] = g;

            //Reseting position back to zero
            if(x == 0)
            {
                position = 0;
            }
        }



    }

    public Card drawCard()
    {
        return deck[position];
    }

}



1 个答案:

答案 0 :(得分:1)

问题是在drawCard中:

public Card drawCard()
{
    return deck[position];
}

您无需更改position,因此每次调用它时,都会一次又一次地绘制同一张卡片。更改为:

public Card drawCard()
{
    return deck[position++];
}

要递增position(或position--,具体取决于您如何看待“甲板”)