2015-04-17 35 views
2

我正在创建一个简单的高/低卡牌游戏作为我的第一个游戏模式,在这里你需要猜测下一张卡片是高于还是低于当前卡片。我已经实现了一个GUI,作为JPanel,游戏逻辑主要驻留在一个单独的类中。如何正确结束简单的纸牌游戏?

我的问题是我无法让游戏在Game Over中正常停止。如果猜测是正确的,GUI更新和卡片上的分数就会移动到下一个分数。如果回答错误,则显示最终得分并用重启按钮替换高/低按钮。目前在逻辑上有些问题,我不知道它是什么,因为游戏似乎很随机地结束,即使答案应该是正确的,并且有时在它应该时也不会结束。

有人可以帮我发现问题吗?

一般来说,追踪Java游戏是否结束的最佳方法是什么?

这是本场比赛的JPanel

/*imports were here*/ 
public class gamePanel_highLow extends javax.swing.JPanel { 

    private highLow game = new highLow(); 
    private boolean playedGame = false; 


    /** 
    * Initializes the game view and makes restart button invisible 
    */ 
    public gamePanel_highLow() { 
     initComponents(); 
     restartButton.setVisible(false); 
    } 

    /* stuff for init components made by gui editor were here */       

    private void lowerButtonActionPerformed(java.awt.event.ActionEvent evt) {            
     Card card = game.getCurrentCard();   
     BufferedImage cardIMG = card.getCardImage(); 
     Image scaledCard = cardIMG.getScaledInstance(90, 138, java.awt.Image.SCALE_SMOOTH) ; 
     cardImage.setIcon(new ImageIcon(scaledCard));    
     currentCard.setText(game.getCurrentCard().toString() + " [" + game.getCurrentCard().getCardIntValue() + "]");      

     playedGame = game.chooseLow(); 

      if(playedGame == true) { // maybe not best wording, as check is gameOver = true 
       score.setText("---"); 
       cardIMG = card.getCardBackImage(); 
       scaledCard = cardIMG.getScaledInstance(90, 138, java.awt.Image.SCALE_SMOOTH) ; 
       cardImage.setIcon(new ImageIcon(scaledCard));    
       lowerButton.setVisible(false); 
       higherButton.setVisible(false); 
       text_or.setVisible(false); 
       restartButton.setVisible(true); 
       currentCard.setText(game.getCurrentCard().toString() + " [" + game.getCurrentCard().getCardIntValue() + "]" + "final score: " + game.getCurrentScore()); 
      } 
      else { 
       score.setText(game.getCurrentScore() + ""); 
      } 
    }           

    private void higherButtonActionPerformed(java.awt.event.ActionEvent evt) {            
     Card card = game.getCurrentCard();   
     BufferedImage cardIMG = card.getCardImage(); 
     Image scaledCard = cardIMG.getScaledInstance(90, 138, java.awt.Image.SCALE_SMOOTH) ; 
     cardImage.setIcon(new ImageIcon(scaledCard));    
     currentCard.setText(game.getCurrentCard().toString() + " [" + game.getCurrentCard().getCardIntValue() + "]");      

     playedGame = game.chooseHigh(); 

      if(playedGame == true) { 
       score.setText("---"); 
       cardIMG = card.getCardBackImage(); 
       scaledCard = cardIMG.getScaledInstance(90, 138, java.awt.Image.SCALE_SMOOTH) ; 
       cardImage.setIcon(new ImageIcon(scaledCard));    
       lowerButton.setVisible(false); 
       higherButton.setVisible(false); 
       text_or.setVisible(false); 
       restartButton.setVisible(true); 
       currentCard.setText(game.getCurrentCard().toString() + " [" + game.getCurrentCard().getCardIntValue() + "]" + "final score: " + game.getCurrentScore()); 
      } 
      else { 
       score.setText(game.getCurrentScore() + ""); 
      } 
    }            

    private void restartButtonActionPerformed(java.awt.event.ActionEvent evt) {            
     lowerButton.setVisible(true); 
     higherButton.setVisible(true); 
     text_or.setVisible(true); 
     restartButton.setVisible(false); 

     Card card = game.getCurrentCard(); 
     BufferedImage cardIMG = card.getCardImage(); 
     Image scaledCard = cardIMG.getScaledInstance(90, 138, java.awt.Image.SCALE_SMOOTH) ; 
     cardImage.setIcon(new ImageIcon(scaledCard)); 

     currentCard.setText(game.getCurrentCard().toString() + " [" + game.getCurrentCard().getCardIntValue() + "]"); 

     playedGame = false; 
     game = new highLow(); 
    } 

,这里是游戏逻辑类:

public class highLow { 

     private int correctGuesses = 0;  
     private CardDeck deck;   
     private Card currentCard; 
     private Card nextCard;  
     private Boolean gameOver = false; 

    /** 
    * Initializes the game 
    */ 
    public highLow() { 
     deck = new CardDeck(); 
     deck.fillDeck(); 
     deck.shuffleDeck(); 
     correctGuesses = 0; 
     gameOver = false; 
     currentCard = deck.dealCard(); 
     nextCard = null; 
    } 

    public Card getCurrentCard() { 
     return currentCard; 
    } 

    public Card getNextCard() { 
     return nextCard; 
    } 

    public int getCurrentScore() { 
     return correctGuesses; 
    } 

    public boolean getGameOver() { 
     return gameOver; 
    } 

    /** 
    * Guesses next card is higher than current card. 
    * @return If correct, add 1 to correctGuesses and return 1. 
    * @return If wrong, set game over and return 0. 
    */ 
    public boolean chooseHigh() { 

     nextCard = deck.dealCard(); 

     if(nextCard.getCardIntValue() > currentCard.getCardIntValue()) { 
       correctGuesses++; 
       gameOver = false; 
     } 
     else { 
      gameOver = true; 
     } 
    currentCard = nextCard; 
    return gameOver; 
}  

    /** 
    * Guesses next card is lower than current card. 
    * @return If correct, add 1 to correctGuesses and return 1. 
    * @return If wrong, set game over and return 0. 
    */ 
    public boolean chooseLow() { 

     nextCard = deck.dealCard(); 

     if(nextCard.getCardIntValue() < currentCard.getCardIntValue()) { 
       correctGuesses++; 
       gameOver = false; 
     } 
     else { 
      gameOver = true; 
     } 
    currentCard = nextCard; 
    return gameOver; 
} 
} 

这是非常准系统,肯定的,但我不明白为什么gameOver没有按”不工作,也不知道如何修复它。一直在,如果有一段时间,但一个新的修复打破别的东西。

编辑: 这里的Card类:

/*imports*/ 

public class Card implements Comparable { 
private CardSuit suit; 
private CardValue value; 
private BufferedImage image; 
private static boolean sortByValue = true; 

/** 
* 
* @param card_suit  Suit of the card (clubs, spades, diamonds, hearts) 
* @param card_value Value of the card (1-14) 
* @param card_image Image file used in GUI 
*/ 
public Card (CardSuit card_suit, CardValue card_value, BufferedImage card_image) { 
    suit = card_suit; 
    value = card_value; 
    image = card_image; 
} 

/** 
* 
* @return The card's suit 
*/ 
public CardSuit getCardSuit() { 
    return suit; 
} 

/** 
* 
* @return The card's value 
*/ 
public CardValue getCardValue() { 
    return value; 
} 

/** 
* 
* @param suit Card suit for filename 
* @param value Card value for filename 
* @return The specific filename located in cardImages folder 
*/ 
public static String getImageFilename(CardSuit suit, CardValue value) { 
    return suit.getSuitAcronym() + value.getValueAcronym() + ".png"; 
} 

/** 
* 
* @return Buffered image of a specific card to be used in GUI 
*/ 
public BufferedImage getCardImage() { 
    return image; 
} 

/** 
* 
* @return get image representing back of the card 
*/ 
public BufferedImage getCardBackImage() { 

    String imageFile = "src/main/java/cardImages/back.png"; // Stores image filename. 
       BufferedImage img = null; // Initializes image as null. 
       try { 
        img = ImageIO.read(new File(imageFile)); // Places proper image path to img variable 
       } 
       catch (IOException e) { 
        e.printStackTrace(); // Traces any errors 
       } 
    return img;     
} 

/** 
* 
* @return Cards value in integer form for easier comparison 
*/ 
public int getCardIntValue() { 
    return value.getCardInt(); 
} 

/** 
* 
* @return prints suit name 
*/ 
public String suitToString() { 
    return suit.toString(); 
} 

/** 
* 
* @return prints card value 
*/ 
public String valueToString() { 
    return value.getValue(); 
} 

public String toString() { //print card suit and value 
    return value.toString() + " of " + suit.toString(); 
} 

/** 
* 
* @return 
*/ 
public String toStringWithIntegers() { //print card suit and numerical version of value 
    return value.getCardInt() + " of " + suit.toString(); 
} 

/** 
* card sorting aid for deck and shuffle 
*/ 
public static void sortCardsBySuit() { 
     sortByValue = false; 
} 

/** 
* card sorting aid for deck and shuffle 
*/ 
public static void sortCardsByValue() { 
     sortByValue = true; 
} 

/** 
* 
* @param card card object to compare with 
* @return compares suit and value to another card object. 
*/ 
public boolean sameAs(Card card) { 
    if ((value != card.value) || (suit != card.suit)) 
    return false; 
    else 
    return true; 
} 

public int compareTo(Object otherCardObj) { //another comparison used only for sorting cards and creating a deck. 
    Card otherCard = (Card) otherCardObj; 
    int value_difference = value.compareTo(otherCard.value); 
    int suit_difference = suit.compareTo(otherCard.suit); 

    if (sortByValue) { 
    if (value_difference != 0) 
     return value_difference; 
    else 
     return suit_difference; 
    } 
    else { 
    if (suit_difference != 0) 
     return suit_difference; 
    else 
     return value_difference; 
    } 
} 
} 

而且CardValue类分配值卡。 SuitValue是不是真的用在这个游戏,所以可以肯定这里没有必要:

/*imports*/ 

public class CardValue implements Comparable { 
private String name;  // full name of value 
private String acronym; // acronym of value, used in the image files 

private CardValue(String value_name, String value_acronym) { 
    name = value_name; 
    acronym = value_acronym; 
} 

// All card values 

public final static CardValue TWO = new CardValue("Two", "2"); 
public final static CardValue THREE = new CardValue("Three", "3"); 
public final static CardValue FOUR = new CardValue("Four", "4"); 
public final static CardValue FIVE = new CardValue("Five", "5"); 
public final static CardValue SIX = new CardValue("Six", "6"); 
public final static CardValue SEVEN = new CardValue("Seven", "7"); 
public final static CardValue EIGHT = new CardValue("Eight", "8"); 
public final static CardValue NINE = new CardValue("Nine", "9"); 
public final static CardValue TEN = new CardValue("Ten", "10"); 
public final static CardValue JACK = new CardValue("Jack", "11"); 
public final static CardValue QUEEN = new CardValue("Queen", "12"); 
public final static CardValue KING = new CardValue("King", "13"); 
public final static CardValue ACE = new CardValue("Ace", "14"); 

String getValue() { 
    return name; 
} 

/** 
* 
* @return get value acronym 
*/ 
public String getValueAcronym() { 
    return acronym; 
} 

/** 
* 
* @return get value as int 
*/ 
public int getCardInt() { 
    return Integer.parseInt(acronym); 
} 

/** 
* 
* @return print name of value eg. Jack 
*/ 
    @Override 
public String toString() { 
    return name; 
} 

// All values in a list for comparison. 

    public final static java.util.List VALUES = 
    Collections.unmodifiableList(Arrays.asList(new CardValue[] { TWO, THREE, FOUR, FIVE, SIX, SEVEN, 
                   EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE })); 
@Override 
public int compareTo(Object otherValueObj) { 
    CardValue otherValue = (CardValue) otherValueObj; 
    return VALUES.indexOf(this) - VALUES.indexOf(otherValue); 
} 
} 

编辑2: 删除从选择低/高键的新游戏,现在只要在重新启动按钮。我还将Rihards的想法加入到逻辑中,但仍然不能很好地发挥作用。

这里的布局是什么样子:http://gyazo.com/b8b5fe50185a134cc1cf0c320d8597d1

后1-3点击死了,即使是那些可能只是运气/随机的。

+0

逻辑上似乎没什么问题。你有没有尝试添加一些输出,System.out.println到你回来的变量,你会发现是否有什么错误? – LudgerP

+0

我刚刚在game.chooseHigh()方法后添加了用于playingGame的输出,并且游戏按如下方式运行:五 - >七 - >十(失败此处)....所以它选择了一次十个gameOver,即使它是大于七:O – FroZenL1Qu1d

+0

粘贴您的卡类,也是你开始处理新卡 currentCard = deck.dealCard(); nextCard = deck.dealCard(); 然后在选择更高或更低的卡时再次抽出一张卡,以便浪费1张卡 currentCard = nextCard; nextCard = deck.dealCard(); 您还没有更新currentCard = nextCard; –

回答

3

我在这里看到2个问题,这些可能会导致一些问题:对,如果高于或低于你再抓一张牌艇员选拔

1)你要开始处理新卡

currentCard = deck.dealCard(); 
nextCard = deck.dealCard(); 

,然后,让1张卡被浪费了。

2)你总是检查nextCard作为当前的卡,这样你就不会真正看到在输出nextCard,你看CurrentCardpreviousCard

试试这个:

public boolean chooseHigh() { 
     nextCard = deck.dealCard(); 

      if(nextCard.getCardIntValue() > currentCard.getCardIntValue()) { 
        correctGuesses++; 
        gameOver = false; 
      } 
      else { 
       gameOver = true; 
      } 
     currentCard = nextCard; 
     return gameOver; 
    } 

并更改该上的init :

public highLow() { 
    ... 
    nextCard = null; 
} 

EDIT1: 好吧,我只注意到你检查下卡,但你显示前一个卡,新卡得到下一轮才会更新,或者你失去 后试试这个:

private void lowerButtonActionPerformed(java.awt.event.ActionEvent evt) {            

      playedGame = game.chooseLow(); 

       if(playedGame == true) { // maybe not best wording, as check is gameOver = true 
        score.setText("---"); 
        cardIMG = card.getCardBackImage(); 
        scaledCard = cardIMG.getScaledInstance(90, 138, java.awt.Image.SCALE_SMOOTH) ; 
        cardImage.setIcon(new ImageIcon(scaledCard));    
        lowerButton.setVisible(false); 
        higherButton.setVisible(false); 
        text_or.setVisible(false); 
        restartButton.setVisible(true); 
        currentCard.setText(game.getCurrentCard().toString() + " [" + game.getCurrentCard().getCardIntValue() + "]" + "final score: " + game.getCurrentScore()); 
       } 
       else { 
        score.setText(game.getCurrentScore() + ""); 
       } 

      Card card = game.getCurrentCard();   
      BufferedImage cardIMG = card.getCardImage(); 
      Image scaledCard = cardIMG.getScaledInstance(90, 138, java.awt.Image.SCALE_SMOOTH) ; 
      cardImage.setIcon(new ImageIcon(scaledCard));    
      currentCard.setText(game.getCurrentCard().toString() + " [" + game.getCurrentCard().getCardIntValue() + "]");  
     } 
+0

即使在这些改进之后,它的行为也相似。在答案上似乎也没有任何明确的模式,这使得追踪更难。 – FroZenL1Qu1d