0
import java.util.ArrayList;
import java.lang.Math;

public class War {
    ArrayList deck = new ArrayList(0);
    ArrayList player1 = new ArrayList(0);
    ArrayList player2 = new ArrayList(0);
    int sum1 = 0;
    int sum2 = 0;
    int count = 0;

private void setup ()
{
    for (int x = 1; x <= 13; x++)
    {
        for (int y = 1; y <= 4; y++)
        {
            deck.add(x);
        }
    }

    while (deck.size() > 26)
    {
        double x = Math.random() * deck.size();
        int y = (int) x;

        player1.add(deck.remove(y));
    }

    while (deck.size() > 0)
    {
        double x = Math.random() * deck.size();
        int y = (int) x;

        player2.add(deck.remove(y));
    }

    for (int x = 0; x < 26; x++)
    {
        sum1 += (int) player1.get(x);
        sum2 += (int) player2.get(x);
    }

    System.out.println("Player 1's starting power is " + sum1 + ".");
    System.out.println();
    System.out.println("Player 2's starting power is " + sum2 + ".");
    System.out.println();

    if (sum1 == sum2)
    {
        System.out.println("The two player's starting powers are equal! This'll be a good one, folks!");
    }
}

public void play ()
{
    if (hasSomeoneWon() || count == 0)
    {
        setup();
    }

    while (!player1.isEmpty() && !player2.isEmpty())
    {
        int a = (int) player1.get(0);
        int b = (int) player2.get(0);

        if (a > b)
        {
            player1.add(player1.remove(0)); // The winner's card is re-added to his deck before
            player1.add(player2.remove(0)); // the loser's is added to the winner's deck.
        }

        if (a < b)
        {
            player2.add(player2.remove(0));
            player2.add(player1.remove(0));
        }

        if (a == b)
        {
            war();
        }
    }

    victory();
}

private void war ()
{
    ArrayList temp1 = new ArrayList(0);
    ArrayList temp2 = new ArrayList(0);
    temp1.add(player1.remove(0));
    temp2.add(player2.remove(0));
    int x = 0;

    while (!(player1.isEmpty() || player2.isEmpty()) && x < 3)
    {
        temp1.add(player1.remove(0));
        temp2.add(player2.remove(0));
        x++;
    }

    int a = (int) temp1.get(temp1.size() - 1);
    int b = (int) temp2.get(temp2.size() - 1);

    if (a == b)
    {
        if (temp1.size() != temp2.size())
        {
            if (temp1.size() > temp2.size())
            {
                while (!temp1.isEmpty())
                {
                    player1.add(temp1.remove(0));
                }

                while (!temp2.isEmpty())
                {
                    player1.add(temp2.remove(0));
                }
            }

            else
            {
                while (!temp2.isEmpty())
                {
                    player2.add(temp2.remove(0));
                }

                while (!temp1.isEmpty())
                {
                    player2.add(temp1.remove(0));
                }
            }
        }

        else
        {
            if (player1.isEmpty() || player2.isEmpty())
            {
                if (player1.isEmpty())
                {
                    while (!temp2.isEmpty())
                    {
                        player2.add(temp2.remove(0));
                    }

                    while (!temp1.isEmpty())
                    {
                        player2.add(temp1.remove(0));
                    }
                }

                else
                {
                    while (!temp1.isEmpty())
                    {
                        player1.add(temp1.remove(0));
                    }

                    while (!temp2.isEmpty())
                    {
                        player1.add(temp2.remove(0));
                    }
                }
            }

            else
            {
                war();
            }
        }
    }

    else
    {
        if (a > b)
        {
            while (!temp1.isEmpty())
            {
                player1.add(temp1.remove(0));
            }

            while (!temp2.isEmpty())
            {
                player1.add(temp2.remove(0));
            }
        }

        else
        {
            while (!temp2.isEmpty())
            {
                player2.add(temp2.remove(0));
            }

            while (!temp1.isEmpty())
            {
                player2.add(temp1.remove(0));
            }
        }

        play();
    }
}

private void victory ()
{
    if (player1.isEmpty() && sum2 > sum1)
    {
        System.out.println("Player 2 has won!");
    }

    if (player1.isEmpty() && sum1 > sum2)
    {
        System.out.println("Upset! Player 2 has won!");
    }

    if (player2.isEmpty() && sum1 > sum2)
    {
        System.out.println("Player 1 has won!");
    }

    if (player2.isEmpty() && sum2 > sum1)
    {
        System.out.println("Upset! Player 1 has won!");
    }

    hasSomeoneWon();
}

private boolean hasSomeoneWon ()
{
    if (player1.isEmpty() || player2.isEmpty())
    {
        count++;
        return true;
    }

    return false;
}

}

很抱歉包含了我所有的代码,但我不知道哪个部分导致了额外的打印。

这是纸牌游戏战争。它应该自己玩两个玩家之间的游戏,然后打印获胜者。但是,每当我玩过它时,胜利信息(无论打印哪个)都会被打印多次。我猜这与我在哪里以及多久包含对某些方法的调用或 war() 方法中对 war 的递归调用有关。

我对 Java 的唯一体验是我本学年参加的 AP 计算机科学课程,所以按照每个阅读本文的人的标准,我肯定是个菜鸟。

4

1 回答 1

0

war()您递归调用的方法中war()war()最终将通过调用来结束play()。所以会有多个实例在执行play()。最终,当一个玩家的牌用完时,所有这些实例都会在堆栈展开并继续执行时重新出现play()。最后当然victory()是多次调用和打印它(应该是war()被调用的次数)。

因此,一旦到达递归的底部,每个 war() 都可能会打印出玩家获胜的情况。我不认为调用play()inwar()是必要的,无论如何它都会play()在完成该方法后返回该war()方法。

于 2012-05-26T03:45:42.807 回答