循环纸牌游戏

时间:2014-12-10 10:03:16

标签: java

问题:我的游戏不会遍历整个" Deck"。

代码:

import javax.swing.JOptionPane;

public class PlayWar{
 public static void main(String args[]){ 
  Deck theDeck; 
  Hand redHand,blackHand;
  PlayingCard tempCard, redCard,blackCard;
  int index;
  theDeck=new Deck(); 
  redHand=new Hand(Deck.MAX_CARDSPER_DECK/2); 
  blackHand=new Hand(Deck.MAX_CARDSPER_DECK/2);
  tempCard=theDeck.getNextCard(); 
 for (index=0; index<Deck.MAX_CARDSPER_DECK; index++){
      if (tempCard.getSuitValue()==PlayingCard.HEARTS||tempCard.getSuitValue()==PlayingCard.DIAMONDS){
           redHand.addCard(tempCard);}//end if
      else{
         blackHand.addCard(tempCard);}//emd else
     redHand.addCard(tempCard);
     blackHand.addCard(tempCard);}//end for 
 blackCard = blackHand.turnCard(tempCard);
 redCard = redHand.turnCard(tempCard); 
      //as long as the red and black hand getCouts don't equal zeros, playWar
 while(redHand.getCount()!=0&& blackHand.getCount()!=0){
     if (redCard.getSuitValue() > blackCard.getSuitValue()){
         System.out.println("The " +  redCard.toString() +
                    " is greater than the " + blackCard.toString()); 
         blackHand.tossFromHand(tempCard);
      }//end if

     if (redCard.getSuitValue()<blackCard.getSuitValue()){
         System.out.println("The " + blackCard.toString() +
                            " is higher than "+redCard.toString());
         redHand.tossFromHand(tempCard);                
       }//end if

     if (redCard.getSuitValue()==blackCard.getSuitValue()){
         System.out.println("TIE - Tossing both the "+blackCard.toString()+
                            "and the "+redCard.toString());
         blackHand.tossFromHand(tempCard); 
         redHand.tossFromHand(tempCard);                                       
       }//end if 
     }//end while  
     //out of while loop do if stamets that print out wich one wins or if its a tie
      if(redHand.getCount()!=0&& blackHand.getCount()==0)
         System.out.println("Black Wins!");
      if(redHand.getCount()==0&& blackHand.getCount()!=0)
         System.out.println("Red Wins!");
      if (redHand.getCount()==0&& blackHand.getCount()==0)
         System.out.println("It's a Tie");

       System.exit(0);//need this when import JOptionPane
      }//end main method
 }//end class

输出:

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

TIE - 抛出两个CLUBS和两个CLUBS

这是一个领带

问题:为什么只输出两个俱乐部?我错放了一个支架吗?

1 个答案:

答案 0 :(得分:2)

您似乎永远不会在tempCard循环中更改for的值,因此redHandblackHand都会保持完全相同的卡片,即多次引用同样的初始tempCard

也许你必须在循环的每次迭代中选择一个新的tempCard

for (index=0; index<Deck.MAX_CARDSPER_DECK; index++){
    tempCard=theDeck.getNextCard();  // move this line here?

此外,根据颜色,首先将卡添加到redHandblackHand,然后再将再次添加到双手,这似乎很奇怪所以每张卡实际上有三份副本。


与第一个问题类似,您永远不会在redCard循环中更新blackCardwhile;在整个比赛期间,它们在循环之前保持同一张牌。您必须在while循环结束时转换新卡,例如

    blackCard = blackHand.turnCard(tempCard);
    redCard = redHand.turnCard(tempCard); 
} // end while

此外,像blackHand.tossFromHand(tempCard);这样的行应该读blackHand.tossFromHand(blackCard);(类似于红色)。