添加&#34;卡&#34;到Java </integer>中的ArrayList <integer>

时间:2014-10-24 23:49:55

标签: java arraylist

我正在尝试制作一款GoFish游戏。我正在制作套牌。我知道如何*把所有数字*&#34;卡&#34;在ArrayList中。但由于有四种不同类型的牌不是整数(King,Queen,Jack,Ace),这会导致一些速度障碍。

将这四张卡添加到此ArrayList的最佳方法是什么?

ArrayList<Integer> cards = null;

另外,我如何确保套牌中每张卡有四张?


@ user3189142如何从计算机手中搜索并取出猜测的卡片并将其添加到玩家的手中?

public static void playOneGame(ArrayList<Integer> cards, Scanner input)
{
    ArrayList<Integer> computer = new ArrayList<Integer>();
    ArrayList<Integer> person = new ArrayList<Integer>();
    ArrayList<Integer> computerPile = new ArrayList<Integer> ();
    ArrayList<Integer> personPile = new ArrayList<Integer>();

// TODO: Deal Cards

    for (int p = 0; p < 7; p++)
    {
        person.add(cards.get(0));
        cards.remove(0);
    }

    for (int c = 0; c < 7; c++)
    {
        computer.add(cards.get(0));
        cards.remove(0);
    }

// TODO: Show the person their starting hand

    System.out.println("Your cards are: " + person);

    // next line is for testing, not apart of final program
    System.out.println("Computer's cards are: " + computer);

//TODO: Play one turn with the person doing the choosing

//TODO: Let the player draw from the deck

    while (computerPile.size() + personPile.size() < 52 || !cards.isEmpty())
    {
        if (!person.isEmpty())
        {
            //TODO: Play one turn with the person doing the choosing
            System.out.println("What card do you want?");
            int card = input.nextInt();

            playPersonTurn(card, person, computer, personPile, computerPile, cards);

        }
        else
        {
            //TODO: Let the player draw from the deck
            person.add(cards.get(0));
            cards.remove(0);
        }

        showGameState(person, computerPile, personPile);

        //TODO: Play one turn with the computer doing the choosing
        if (!computer.isEmpty())
        {
            int card = computer.get((int)(Math.random()*computer.size()));  
            System.out.println("Do you have any "  + card + "'s ?");


            playComputerTurn(card, person, computer, personPile, computerPile, cards);

        }
        else if (!cards.isEmpty())
        {
            //TODO: Let the computer draw from the deck
                computer.add(cards.get(0));
                cards.remove(0);
        }

        //TODO: Create piles from hands if applicable
        checkPiles(person, personPile, computer, computerPiles);
        showGameState(person, computerPile, personPile);

    }   

桩检查方法:

public static void checkPiles(ArrayList<Integer> person, ArrayList<Integer> personPile, ArrayList<Integer> computer, ArrayList<Integer> computerPile)
    {
        for (int i = 1; i < 14; i++)
        {
            if (Collections.frequency(person,i) == 4)
            {
                for (int j = 0; j < 4; j++)
                {
                    personPile.add(person.remove(person.indexOf(i)));
            }
            else if (Collections.frequency(computer,i) == 4)
            {
                for (int k = 0; k < 4; k++)
                {
                    computerPile.add(computer.remove(computer.indexOf(i)));
                }
            }
        }
    }

现在代码运作良好,除非形成一堆,它显示所有四个相同数量的卡,如下面显示的数字1.有没有办法,我们只能显示一个1?< / p>

What card do you want?
1
You took the computer's 1.
Here are your cards: 
2 1 13 12 12 1 6 1 6 10 9 1 
You don't have any piles yet. Computer doesn't have any piles yet. 

Do you have any 7's?
Computer went fishing. Computer drew one card from deck.
Here are your cards: 
2 13 12 12 6 6 10 9 
Here is your pile: 
1 1 1 1 
Computer doesn't have any piles yet. 

What card do you want?

2 个答案:

答案 0 :(得分:1)

如果我这样做,我会将Ace添加为1,将Jack添加为11,将Queen添加为12,将King添加为13。确保套牌中每张卡有4个非常简单,使用嵌套循环:

for (int i = 0; i < 4; i++)
{
    for (int j = 1; j < 14; j++)
        cards.add(j);
}

这会将13张牌加入牌组4次。我假设你也需要输出卡片,这就是你遇到麻烦的原因。在输出时,您可以使用一些简单的if来检查Ace,King,Queen和Jack卡,并输出这些名称:

outputCard(int cardNumber)
{
    if (cardNumber == 1)
        System.out.println("Ace");
    else if (cardNumber == 11)
        System.out.println("Jack");
    else if (cardNumber == 12)
        System.out.println("Queen");
    else if (cardNumber == 13)
        System.out.println("King");
    else
        System.out.println(cardNumber);
}

要将前七张牌添加到另一个阵列列表,我们将其称为hand,请尝试以下操作:

for (int k = 0; k < 7; k++)
    hand.add(cards.remove(0));

然后hand应该在cards列表

的顶部有7张卡片

要向计算机索取一张卡,如果计算机有卡,请将其放入播放器的手中,请尝试以下操作:

//Get the card choice from the user
int cardChoice = input.nextInt();
if (computer.contains(cardChoice))
    player.add(computer.remove(computer.indexOf(cardChoice)));

这样做首先检查计算机手中至少有1张你要求的牌,如果有,它会找到该牌的索引(computer.indexOf),然后将其删除({{ 1}}),返回存储在该位置的整数,并将其添加到玩家手computer.remove。如果您需要多行,请告诉我,我会稍微改变一下。

好的,为了检查一只手是否有4张相同的牌,我们会使用一对圈来进行循环,如下所示:

player.add

对于计算机手,只需复制粘贴并将//For the players hand for (int i = 1; i < 14; i++) { if (Collections.frequency(player,i) == 4) { for (int j = 0; j < 4; j++) { playerPile.add(player.remove(i)) } } } 更改为playerPile,将computerPile更改为player

您获得的错误可能是因为您的computer函数具有返回值,但该值未分配给任何内容。它也可能不需要是2个函数,你应该能够将它们合并在一起:

pileCheck

通过将循环更改为:

,您可以使堆中仅显示其中一种类型的卡
public static void checkPiles()
{
    for (int i = 1; i < 14; i++)
    {
        if (Collections.frequency(player,i) == 4)
        {
            for (int j = 0; j < 4; j++)
            {
                playerPile.add(player.remove(player.indexOf(i))) //Updated this
            }
        }
        else if (Collections.frequency(computer,i) == 4)
        {
            for (int k = 0; k < 4; k++)
            {
                computerPile.add(computer.remove(computer.indexOf(i))) //Updated this
            }
        }
    }
}

请注意,如果您这样做,则while while循环将需要更改为:

if (Collections.frequency(player,i) == 4)
{
    for (int j = 0; j < 4; j++)
    {
        player.remove(player.indexOf(i));
    }
    playerPile.add(i);
}

因为当找到所有匹配时,两个桩中的总数只会增加到13而不是52

答案 1 :(得分:0)

使用字符串的ArrayList而不是Integers。卡片组由52张扑克牌组成,每张扑克牌都有等级和套装。要编写createDeck()方法,请参阅下面的伪代码:

ArrayList<String> cardDeck;

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

enum Suit
{
    CLUB , DIAMOND , HEART , SPADE;
}

createDeck()
{
      ...... Write your logic to create a slide deck here ...........

      for(Suit aSuit : Suit.values())
          for (Rank aRank : Rank.values())
              cardDeck.add(aRank + somedelimeter + aSuit)

}