2015-09-19 58 views
0

所以,當我運行我的程序時,大約每四分之一就會開始無限循環。其他時候它工作正常。請注意,我的戰爭遊戲在技術上並不完全正確。我不太清楚如何儲存這些牌,讓玩家贏得他們自己的獨立套牌,然後在牌被用完後將他們洗牌回來。而我的決鬥方法可能不是最好的。這真的很長。目前我需要解決這個循環問題。卡片遊戲「戰爭」中的無限循環 - Java

哦,我也只使用了24張卡片,而且每張卡片使用的卡片只有5,6,7,8,9和10張。

主要

import java.util.Random; 
public class Main { 
static Player player1 = new Player("player1"); 
static Player player2 = new Player("player2"); 

static Card[] deck = {new Card("Hearts", 5), new Card("Hearts", 6), new Card("Hearts", 7), new Card("Hearts", 8), new Card("Hearts", 9), new Card("Hearts", 10), 
     new Card("Diamonds", 5), new Card("Diamonds", 6), new Card("Diamonds", 7), new Card("Diamonds", 8), new Card("Diamonds", 9), new Card("Diamonds", 10), 
     new Card("Spades", 5), new Card("Spades", 6), new Card("Spades", 7), new Card("Spades", 8), new Card("Spades", 9), new Card("Spades", 10), 
     new Card("Clubs", 5), new Card("Clubs", 6), new Card("Clubs", 7), new Card("Clubs", 8), new Card("Clubs", 9), new Card("Clubs", 10) 
}; 

public static void main(String args[]){ 
    shuffleArray(deck); 
    dealDeck(); 
    int i=0; 
    while (i<1){ 
     printCard(); 
     if(player1.cards.size()>0 && player2.cards.size()>0){ 
      Duel(); 
     } 
     else{ 
      if(player1.numCards>player2.numCards){ 
       System.out.println("Player 1 has won."); 
       i++; 
      } 
      else if(player2.numCards>player1.numCards){ 
       System.out.println("Player 2 has won."); 
       i++; 
      } 
      duelsWon(); 
      totalCompares(); 
     } 
    } 
} 

private static void shuffleArray(Card[] array) 
{ 
    int index; 
    Card temp; 
    Random random = new Random(); 
    for (int i = array.length - 1; i > 0; i--) 
    { 
     index = random.nextInt(i + 1); 
     temp = array[index]; 
     array[index] = array[i]; 
     array[i] = temp; 
    } 
} 
public static void dealDeck(){ 
    for(int i=0;i<24;i=i+2){ 
     player1.dealCard(deck[i]); 
     player2.dealCard(deck[i+1]); 
    } 
} 
public static void duelsWon(){ 
    System.out.printf("%-20s %-20s\n", player1.name+"'s duels won", player2.name+"'s duels won"); 
    System.out.printf("%-20s %-20s\n", player1.duelsWon, player2.duelsWon); 
} 
public static void totalCompares(){ 
    System.out.printf("%-10s\n", "Total Compares"); 
    System.out.printf("%-10s\n", player1.totalCompares+player2.totalCompares); 
} 
public static void printCard(){ 
    System.out.printf("%-20s %-20s\n", player1.name, player2.name); 
    for(int i = 0;i<24;i++){ 
     System.out.printf("%-20s %-20s\n", player1.getCard(i), player2.getCard(i)); 
    } System.out.println(); 
} 
public static void Duel(){ 
    Card p1 = player1.drawCard(); 
    Card p2 = player2.drawCard(); 
    System.out.println(p1); 
    System.out.println(p2); 
    if(p1.compareTo(p2)>0){ 
     player1.dealCard(p1); 
     player1.dealCard(p2); 
     player1.duelsWon++; 
     player1.totalCompares++; 
    } 
    else if(p2.compareTo(p1)>0){ 
     player2.dealCard(p1); 
     player2.dealCard(p2); 
     player2.duelsWon++; 
     player2.totalCompares++; 
    } 
    else{ 
     if(player1.cards.size()>=3 && player2.cards.size()>3){ 
      int i=0; 
      while(i<1){ 
       Card p1C1 = player1.drawCard(); 
       Card p1C2 = player1.drawCard(); 
       Card p1C3 = player1.drawCard(); 
       Card p2C1 = player2.drawCard(); 
       Card p2C2 = player2.drawCard(); 
       Card p2C3 = player2.drawCard(); 
       if(p1C3.compareTo(p2C3)>0){ 
        player1.dealCard(p1); 
        player1.dealCard(p2); 
        player1.dealCard(p1C1); 
        player1.dealCard(p1C2); 
        player1.dealCard(p1C3); 
        player1.dealCard(p2C1); 
        player1.dealCard(p2C2); 
        player1.dealCard(p2C3); 
        player1.duelsWon++; 
        player1.totalCompares++; 
        i++; 
       } 
       else if(p2C3.compareTo(p1C3)>0){ 
        player2.dealCard(p1); 
        player2.dealCard(p2); 
        player2.dealCard(p1C1); 
        player2.dealCard(p1C2); 
        player2.dealCard(p1C3); 
        player2.dealCard(p2C1); 
        player2.dealCard(p2C2); 
        player2.dealCard(p2C3); 
        player2.duelsWon++; 
        player2.totalCompares++; 
        i++; 
       } 
       else{ 
        if(p1C2.compareTo(p2C2)>0){ 
         player1.dealCard(p1); 
         player1.dealCard(p2); 
         player1.dealCard(p1C1); 
         player1.dealCard(p1C2); 
         player1.dealCard(p1C3); 
         player1.dealCard(p2C1); 
         player1.dealCard(p2C2); 
         player1.dealCard(p2C3); 
         player1.duelsWon++; 
         player1.totalCompares++; 
         i++; 
        } 
        else if(p2C2.compareTo(p1C2)>0){ 
         player2.dealCard(p1); 
         player2.dealCard(p2); 
         player2.dealCard(p1C1); 
         player2.dealCard(p1C2); 
         player2.dealCard(p1C3); 
         player2.dealCard(p2C1); 
         player2.dealCard(p2C2); 
         player2.dealCard(p2C3); 
         player2.duelsWon++; 
         player2.totalCompares++; 
         i++; 
        } 
        else{ 
         if(p1C1.compareTo(p2C1)>0){ 
          player1.dealCard(p1); 
          player1.dealCard(p2); 
          player1.dealCard(p1C1); 
          player1.dealCard(p1C2); 
          player1.dealCard(p1C3); 
          player1.dealCard(p2C1); 
          player1.dealCard(p2C2); 
          player1.dealCard(p2C3); 
          player1.duelsWon++; 
          player1.totalCompares++; 
          i++; 
         } 
         else if(p2C1.compareTo(p1C1)>0){ 
          player2.dealCard(p1); 
          player2.dealCard(p2); 
          player2.dealCard(p1C1); 
          player2.dealCard(p1C2); 
          player2.dealCard(p1C3); 
          player2.dealCard(p2C1); 
          player2.dealCard(p2C2); 
          player2.dealCard(p2C3); 
          player2.duelsWon++; 
          player2.totalCompares++; 
          i++; 
         } 
         else{ 

         } 
        } 
       } 
      } 
     } 
     else{ 
      if(player2.cards.size()==3){ 
       Card p1C1 = player1.drawCard(); 
       Card p1C2 = player1.drawCard(); 
       Card p1C3 = player1.drawCard(); 
       Card p2C1 = player2.drawCard(); 
       Card p2C2 = player2.drawCard(); 
       Card p2C3 = player2.drawCard(); 
       if(p1C3.compareTo(p2C3)>0){ 
        player1.dealCard(p1C1); 
        player1.dealCard(p1C2); 
        player1.dealCard(p1C3); 
        player1.dealCard(p2C1); 
        player1.dealCard(p2C2); 
        player1.dealCard(p2C3); 
       } 
       else if(p2C3.compareTo(p2C3)>0){ 
        player2.dealCard(p1C1); 
        player2.dealCard(p1C2); 
        player2.dealCard(p1C3); 
        player2.dealCard(p2C1); 
        player2.dealCard(p2C2); 
        player2.dealCard(p2C3); 
       } 
       else{ 
        if(p1C2.compareTo(p2C2)>0){ 
         player1.dealCard(p1C1); 
         player1.dealCard(p1C2); 
         player1.dealCard(p1C3); 
         player1.dealCard(p2C1); 
         player1.dealCard(p2C2); 
         player1.dealCard(p2C3); 
        } 
        else if(p2C2.compareTo(p1C2)>0){ 
         player2.dealCard(p1C1); 
         player2.dealCard(p1C2); 
         player2.dealCard(p1C3); 
         player2.dealCard(p2C1); 
         player2.dealCard(p2C2); 
         player2.dealCard(p2C3); 
        } 
        else{ 
         if(p1C1.compareTo(p2C1)>0){ 
          player1.dealCard(p1C1); 
          player1.dealCard(p1C2); 
          player1.dealCard(p1C3); 
          player1.dealCard(p2C1); 
          player1.dealCard(p2C2); 
          player1.dealCard(p2C3); 
         } 
         else if(p2C1.compareTo(p1C1)>0){ 
          player2.dealCard(p1C1); 
          player2.dealCard(p1C2); 
          player2.dealCard(p1C3); 
          player2.dealCard(p2C1); 
          player2.dealCard(p2C2); 
          player2.dealCard(p2C3); 
         } 
         else{ 
          player1.dealCard(p1C1); 
          player1.dealCard(p1C2); 
          player1.dealCard(p1C3); 
          player1.dealCard(p2C1); 
          player1.dealCard(p2C2); 
          player1.dealCard(p2C3); 
         } 
        } 
       } 
      } 
      else if(player1.cards.size()==3){ 
       Card p1C1 = player1.drawCard(); 
       Card p1C2 = player1.drawCard(); 
       Card p1C3 = player1.drawCard(); 
       Card p2C1 = player2.drawCard(); 
       Card p2C2 = player2.drawCard(); 
       Card p2C3 = player2.drawCard(); 
       if(p1C3.compareTo(p2C3)>0){ 
        player1.dealCard(p1C1); 
        player1.dealCard(p1C2); 
        player1.dealCard(p1C3); 
        player1.dealCard(p2C1); 
        player1.dealCard(p2C2); 
        player1.dealCard(p2C3); 
       } 
       else if(p2C3.compareTo(p2C3)>0){ 
        player2.dealCard(p1C1); 
        player2.dealCard(p1C2); 
        player2.dealCard(p1C3); 
        player2.dealCard(p2C1); 
        player2.dealCard(p2C2); 
        player2.dealCard(p2C3); 
       } 
       else{ 
        if(p1C2.compareTo(p2C2)>0){ 
         player1.dealCard(p1C1); 
         player1.dealCard(p1C2); 
         player1.dealCard(p1C3); 
         player1.dealCard(p2C1); 
         player1.dealCard(p2C2); 
         player1.dealCard(p2C3); 
        } 
        else if(p2C2.compareTo(p1C1)>0){ 
         player2.dealCard(p1C1); 
         player2.dealCard(p1C2); 
         player2.dealCard(p1C3); 
         player2.dealCard(p2C1); 
         player2.dealCard(p2C2); 
         player2.dealCard(p2C3); 
        } 
        else{ 
         if(p1C1.compareTo(p2C1)>0){ 
          player1.dealCard(p1C1); 
          player1.dealCard(p1C2); 
          player1.dealCard(p1C3); 
          player1.dealCard(p2C1); 
          player1.dealCard(p2C2); 
          player1.dealCard(p2C3); 
         } 
         else if(p2C1.compareTo(p1C1)>0){ 
          player2.dealCard(p1C1); 
          player2.dealCard(p1C2); 
          player2.dealCard(p1C3); 
          player2.dealCard(p2C1); 
          player2.dealCard(p2C2); 
          player2.dealCard(p2C3); 
         } 
         else{ 
          player2.dealCard(p1C1); 
          player2.dealCard(p1C2); 
          player2.dealCard(p1C3); 
          player2.dealCard(p2C1); 
          player2.dealCard(p2C2); 
          player2.dealCard(p2C3); 
         } 
        } 
       } 
      } 
      else if(player2.cards.size()==2){ 
       Card p1C1 = player1.drawCard(); 
       Card p1C2 = player1.drawCard(); 
       Card p2C1 = player2.drawCard(); 
       Card p2C2 = player2.drawCard(); 
       if(p1C2.compareTo(p2C2)>0){ 
        player1.dealCard(p1C1); 
        player1.dealCard(p1C2); 
        player1.dealCard(p2C1); 
        player1.dealCard(p2C2); 
       } 
       else if(p2C2.compareTo(p1C2)>0){ 
        player2.dealCard(p1C1); 
        player2.dealCard(p1C2); 
        player2.dealCard(p2C1); 
        player2.dealCard(p2C2); 
       } 
       else{ 
        if(p1C1.compareTo(p2C1)>0){ 
         player1.dealCard(p1C1); 
         player1.dealCard(p1C2); 
         player1.dealCard(p2C1); 
         player1.dealCard(p2C2); 
        } 
        else if(p2C1.compareTo(p1C1)>0){ 
         player2.dealCard(p1C1); 
         player2.dealCard(p1C2); 
         player2.dealCard(p2C1); 
         player2.dealCard(p2C2); 
        } 
        else{ 
         player1.dealCard(p1C1); 
         player1.dealCard(p1C2); 
         player1.dealCard(p2C1); 
         player1.dealCard(p2C2); 
        } 
       } 
      } 
      else if(player1.cards.size()==2){ 
       Card p1C1 = player1.drawCard(); 
       Card p1C2 = player1.drawCard(); 
       Card p2C1 = player2.drawCard(); 
       Card p2C2 = player2.drawCard(); 
       if(p1C2.compareTo(p2C2)>0){ 
        player1.dealCard(p1C1); 
        player1.dealCard(p1C2); 
        player1.dealCard(p2C1); 
        player1.dealCard(p2C2); 
       } 
       else if(p2C2.compareTo(p1C2)>0){ 
        player2.dealCard(p1C1); 
        player2.dealCard(p1C2); 
        player2.dealCard(p2C1); 
        player2.dealCard(p2C2); 
       } 
       else{ 
        if(p1C1.compareTo(p2C1)>0){ 
         player1.dealCard(p1C1); 
         player1.dealCard(p1C2); 
         player1.dealCard(p2C1); 
         player1.dealCard(p2C2); 
        } 
        else if(p2C1.compareTo(p1C1)>0){ 
         player2.dealCard(p1C1); 
         player2.dealCard(p1C2); 
         player2.dealCard(p2C1); 
         player2.dealCard(p2C2); 
        } 
        else{ 
         player2.dealCard(p1C1); 
         player2.dealCard(p1C2); 
         player2.dealCard(p2C1); 
         player2.dealCard(p2C2); 
        } 
       } 
      } 
      else if(player2.cards.size()==1){ 
       Card p1C1 = player1.drawCard(); 
       Card p2C1 = player2.drawCard(); 
       if(p1C1.compareTo(p2C1)>0){ 
        player1.dealCard(p1C1); 
        player1.dealCard(p2C1); 
       } 
       else if(p2C1.compareTo(p1C1)>0){ 
        player2.dealCard(p1C1); 
        player2.dealCard(p2C1); 
       } 
       else{ 
        player1.dealCard(p1C1); 
        player1.dealCard(p2C1); 
       } 
      } 
      else if(player1.cards.size()==1){ 
       Card p1C1 = player1.drawCard(); 
       Card p2C1 = player2.drawCard(); 
       if(p1C1.compareTo(p2C1)>0){ 
        player1.dealCard(p1C1); 
        player1.dealCard(p2C1); 
       } 
       else if(p2C1.compareTo(p1C1)>0){ 
        player2.dealCard(p1C1); 
        player2.dealCard(p2C1); 
       } 
       else{ 
        player2.dealCard(p1C1); 
        player2.dealCard(p2C1); 
       } 
      } 
     } 
    } 
} 
} 

球員

import java.util.ArrayList; 
public class Player { 
public ArrayList<Card> cards=new ArrayList<Card>(); 
public int numCards; 
public int topOfDeck; 
public String name; 
public int duelsWon; 
public int totalCompares; 

public Player(String name){ 
    this.name=name; 
    numCards=0; 
    topOfDeck = 0; 
    duelsWon = 0; 
    totalCompares = 0; 
} 
public void dealCard(Card newCard){ 
    cards.add(newCard); 
    numCards++; 
} 

public String getCard(int n){ 
    if(n < cards.size()) { 
     return cards.get(n).toString(); 
    }else{ 
     return "\\"; 
    } 
} 
public Card drawCard(){ 
    Card c = cards.remove(0); 
    topOfDeck++; 
    topOfDeck = topOfDeck % 24; 
    return c; 
} 
} 

public class Card implements Comparable<Card> { 
private String suit; 
private int number; 

public Card(String suit, int number){ 
    this.suit = suit; 
    this.number = number; 
} 

public String getSuit(){ 
    return suit; 
} 
public int getNumber(){ 
    return number; 
} 
@Override 
public int compareTo(Card otherCard){ 
    if (this.number>otherCard.getNumber()){ 
     return 1; 
    }else if(otherCard.getNumber()>this.number){ 
     return -1; 
    }else{ 
     return 0; 
    } 
} 
@Override 
public String toString(){ 
    return number+" of "+suit; 
} 

} 
+0

你能確定哪個循環導致無限循環嗎?例如,你是否試圖在任何循環中添加一個調試'System.out.print(「loop1」)'?至少由於我們將記錄問題循環,我們知道哪些循環會陷入困境。 – MKreegs

+0

我沒有,但我會嘗試下。我即將出門,但當我回來時,我會給它一個鏡頭。謝謝。 – Aero

+0

請嘗試提供[MCVE(最小,完整和可驗證示例)](http://stackoverflow.com/help/mcve)。 –

回答

0

不知道這是否是問題,但你需要有else分支在你的if/else如果跳轉到涵蓋所有可能性。兩名玩家有可能擁有相同數量的牌。

while (i<1){ 
      printCard(); 
      if(player1.cards.size()>0 && player2.cards.size()>0){ 
       Duel(); 
      } 
      else{ 
       if(player1.numCards>player2.numCards){ 
        System.out.println("Player 1 has won."); 
        i++; 
       } 
       else if(player2.numCards>player1.numCards){ 
        System.out.println("Player 2 has won."); 
        i++; 
       }else{ 
        break; 
        // or put i++; 
       } 
       duelsWon(); 
       totalCompares(); 
      } 
     } 
+0

這並沒有解決問題,但我沒有注意到。謝謝,我添加了它。 – Aero

+0

它不是唯一的地方 - 確保你處理所有情況。這是一個非常複雜的代碼,有很多if-else,想想如何簡化這個...你會更容易地發現錯誤 –