【问题标题】:How to prevent entry into a while loop if certain conditions are met?如果满足某些条件,如何防止进入while循环?
【发布时间】:2011-11-21 23:01:53
【问题描述】:

所以我正在编写一个玩纸牌游戏战争的课程。我已经为手牌、牌组以及与这些类型相关的不同方法(例如洗牌、发牌和丢牌)分别创建了类。

/*Using the Card, Deck, and Hand classes from the previous lab, write a main program          that will play games of war.  The program should

1) Inatantiate a Deck and two Hands

2) Read in n, the number of games of war to play

3) n times, 
  a) shuffle and deal the deck 
  b) play a game of war, counting the number of turns, the number of wars, and the   number of double wars
4) After all n games have been played, print the average number of turns, average number of wars, and the average number of double wars.

The rules for the game can be found here:
 http://www.pagat.com/war/war.html

If a player runs out of cards during a war, use option 1 on that site.

*/



import java.util.Scanner;

public class War {
public static void main (String args [])
{

    //Instantiate Deck and two hands
    deck Pile = new deck();
    hand Player1 = new hand();
    hand Player2 = new hand();
    Scanner kb = new Scanner (System.in);
    int n, turns = 0, wars = 0, dubwars = 0;
    System.out.println("Please enter the number of times you would like the game   'war' to be played:");
    n = kb.nextInt();
    for (int i = 0; i<n; i++)
    {
        //Game playing code goes here
        Pile.shuffle();
        Pile.deal(Player1, Player2);

        while(Player1.size() != 0 && Player2.size() !=0)
        {

            Card Card1 = Player1.DropCard();
            Card Card2 = Player2.DropCard();
            Card P1WarCard = Player1.WarCard(1);//Cards Used in the   First War
            Card P2WarCard = Player2.WarCard(1);
            //if statement saying if the player does not have two cards then exit the program
            //Cards Used in the Second War
            Card P1DubWarCard = Player1.WarCard(2);  
            Card P2DubWarCard = Player2.WarCard(2);



        //If player 1 has a higher rank, then...
        if (Card1.rank > Card2.rank)
        {
            //Player 1 takes both cards
            Player1.add(Card1);
            Player1.add(Card2);
            turns ++;
        }
        else if (Card1.rank < Card2.rank)
        {
            //Player 2 takes both cards
            Player2.add(Card2);
            Player2.add(Card1);
            turns++;
        }
        else 
        {
            if (Player1.size() <3 || Player2.size() <3)
            {
                System.exit(0);
            }
            //War
            wars ++;
            turns++;
        //  while (Player1.size() >=2 && Player2.size() >=2)
            {
            if (P1WarCard.rank > P2WarCard.rank)
            {
                //Player1.add(Card1);
                Player1.add(Card2);
                //Player1.add(P1WarCard);
                Player1.add(P2WarCard);
                //Player 1 takes 4 cards
            }
            else if (P1WarCard.rank < P2WarCard.rank)
            {
                //Player2.add(Card2);
                Player2.add(Card1);
                Player2.add(P1WarCard);
                //Player2.add(P2WarCard);
                //Player 2 takes 4 cards
            }
            else
            {
                if (Player1.size() <3 || Player2.size() <3)
                {
                    System.exit(0);
                }
                //Dubwar
                dubwars++;
                turns++;
                //while (Player1.size() >=3 && Player2.size() >=3)
                {


                if (P1DubWarCard.rank > P2DubWarCard.rank)
                {
                    //Player1.add(Card1);
                    Player1.add(Card2);
                    //Player1.add(P1WarCard);
                    Player1.add(P2WarCard);
                    //Player1.add(P1DubWarCard);
                    Player1.add(P2DubWarCard);
                    //Player 1 takes 6 cards
                }
                else if (P1DubWarCard.rank < P2DubWarCard.rank)
                {
                    //Player2.add(Card2);
                    Player2.add(Card1);
                    Player2.add(P1WarCard);
                    //Player2.add(P2WarCard);
                    Player2.add(P1DubWarCard);
                    //Player2.add(P2DubWarCard);
                    //Player 2 takes 6 cards
                }
                else
                {
                    System.out.println("NUCLEAR WAR");
                    break;
                }
            }

        }
    }
}
        }
    }



System.out.println("The average number of turns was " +turns/n);
System.out.println("The average number of wars was " +wars/n);
System.out.println("The average number of double wars was " +dubwars/n);

}



}

我的问题是,如果没有足够的卡片来完成双重战争,我该如何防止进入双重战争?届时,一副牌中必须至少有三张牌。

战争卡方法也只是简单地返回卡组中那个位置的卡。

这是我第一次发帖,如果我做错了什么,我深表歉意。

谢谢

克里斯

【问题讨论】:

  • 在看到“来自上一个实验室”和顶部的评论后,我认为这是家庭作业,并将该标签添加到您的帖子中。将来,请务必说明您正在做作业! .....或隐藏证据;)
  • Java 不是我的语言,所以不要将此作为答案,但我想你可以进入循环并检查那里的条件,如果满足则中断
  • @Chris Michel:Java 命名约定倒退了。 "deck Pile = new deck()" 应改为 "Deck pile = new Deck()" :)
  • 是的,对于作业的事情很抱歉哈哈。我是新来的 ;)

标签: java arraylist while-loop


【解决方案1】:

您始终可以使用boolean 来确定游戏是否正在运行,然后在您的while 循环开始时检查它是否仍应设置为true,例如:

boolean isPlaying = true;

while (isPlaying) {
  if (numberOfCardsLeft <= cardsNeededToContinue) {
     isPlaying = false;
  }
  //rest of logic here
 }

【讨论】:

    【解决方案2】:

    如果任一玩家的牌较少,您就已经停止进入双重战争

        if (Player1.size() <3 || Player2.size() <3)
        {
            System.exit(0);
        }
    

    从代码顶部的网站“如果你没有足够的卡片来完成战争,你就输了。”因为永远不会出现任何玩家都没有

    【讨论】:

    • 是的,但我仍然收到一条错误消息,提示没有足够的卡片来完成代码。我认为这几乎是一个无用的循环。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 2019-01-29
    • 2013-11-22
    • 1970-01-01
    • 2017-08-01
    相关资源
    最近更新 更多