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;
时 时
时 时
抱歉把代码都算进去了 但我不知道哪部分引起额外打印
这是纸牌游戏战争。 它应该自己在两个玩家之间玩游戏, 然后打印赢家。 但是每当我玩了这个游戏, 胜利信息( 不管谁被打印出来) 就会打印一个可变次数 。 我猜这与我打到哪里去, 多少次, 包括一些方法, 或战争方法中的战争循环号召。
我与爪哇的唯一经验是 本学年我上过一个AP电脑科学课, 所以我确信,按照每个人阅读这个的 标准,这是一个禁忌。