2011-11-21 46 views
0

所以我正在寫一個玩紙牌遊戲的戰爭。我已經爲手牌,牌組以及與這些類型一起進行的不同方法(如洗牌,交易和丟棄牌)進行了單獨的分類。如何在特定條件滿足時防止進入while循環?

/*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); 

} 



} 

我的問題是,如果沒有足夠的卡來完成雙重戰爭,我該如何防止進入雙重戰爭?當時甲板上至少有三張牌。

此外,戰爭卡方法只是在甲板上的該位置返回卡。

這是我第一次發佈,所以我道歉,如果我做錯了什麼。

感謝

克里斯

+1

看到「從以前的實驗室」和評論頂部後,我認爲這是作業,並將該標籤添加到您的帖子。今後,請務必聲明,如果你是在做家庭作業! .....或隱藏證據;) –

+0

Java不是我的語言,所以不把它作爲答案,但我想你可以進入循環,檢查那裏的條件,並打破它是否滿足 –

+1

@Chris Michel:你有Java命名約定向後。 *「甲板樁=新甲板()」*應該讀*「甲板堆=新甲板()」*:) – TacticalCoder

回答

1

您可以隨時使用boolean來決定遊戲是否正在運行與否,然後在while循環開始時檢查其是否仍應被設置爲true ,例如:

boolean isPlaying = true; 

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

正是我所需要的......現在如何解決這些其他錯誤.... – ironcyclone

0

您已經停止進入雙戰如果任何球員有少卡

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

從代碼頂部的網站「如果你沒有足夠的卡來完成戰爭,你就輸了。」因爲這兩個玩家都不會有3張牌。你可以在這裏確定勝利者。

+0

是的,但我仍然得到一個錯誤,說沒有足夠的卡來完成代碼。這幾乎是一個無用的循環,我認爲。 – ironcyclone

相關問題