Java 如何正确结束简单的纸牌游戏?
我正在创建一个简单的高/低牌游戏作为我的第一个游戏模式,你需要猜测下一张牌是高于还是低于当前牌。我已经实现了一个GUI作为一个Java 如何正确结束简单的纸牌游戏?,java,swing,jpanel,Java,Swing,Jpanel,我正在创建一个简单的高/低牌游戏作为我的第一个游戏模式,你需要猜测下一张牌是高于还是低于当前牌。我已经实现了一个GUI作为一个JPanel,游戏逻辑主要驻留在一个单独的类中 我的问题是,我无法让游戏在游戏结束时正常停止。如中所示,如果猜测是正确的,GUI上的分数将更新,卡片将移动到下一张。如果答案错误,则显示最终分数,并用重新启动按钮替换高/低按钮。目前在逻辑上有一些错误,我不知道它是什么,因为游戏似乎是随机结束的,即使答案应该是正确的,有时它不会在应该的时候结束 有人能帮我找出这个问题吗 用j
JPanel
,游戏逻辑主要驻留在一个单独的类中
我的问题是,我无法让游戏在游戏结束时正常停止。如中所示,如果猜测是正确的,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;
}
}
和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:
从“选择低/高”按钮中删除了新游戏,现在只在“重新启动”按钮上。我还在逻辑中加入了里哈德的想法,但仍然没有完全正常运作
以下是布局的外观:
点击1-3次后死亡,甚至那些可能只是运气/随机的。我在这里看到两个问题,它们可能会导致一些问题: 1) 你开始发新牌了
currentCard = deck.dealCard();
nextCard = deck.dealCard();
然后在选择是否更高或更低时,您再次抽一张牌,因此浪费了一张牌
2) 您总是将nextCard
作为当前卡进行检查,因此您实际上没有在输出中看到nextCard
,而是看到CurrentCard
和previousCard
试试这个:
public boolean chooseHigh() {
nextCard = deck.dealCard();
if(nextCard.getCardIntValue() > currentCard.getCardIntValue()) {
correctGuesses++;
gameOver = false;
}
else {
gameOver = true;
}
currentCard = nextCard;
return gameOver;
}
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() + "]");
}
并在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() + "]");
}
我觉得这个逻辑很好。你有没有试着在你得到的变量中添加一些输出,System.out.println,你能看到那里是否有错误吗?我刚在游戏结束后为playedGame添加了一个输出。选择High()方法,游戏运行如下:五->七->十(此处失败)。。。。因此,当出现10张时,它选择了gameOver,即使它大于7张:Opaste your Card class,并且您正在开始处理新卡currentCard=deck.dealCard();nextCard=deck.dealCard();然后在选择是否更高或更低时,您再次抽取一张卡,因此1张卡被浪费currentCard=nextCard;nextCard=deck.dealCard();此外,您没有更新currentCard=nextCard;奇怪的缩进和小写的类名是怎么回事?请遵循合理的惯例。将Card和CardValue类添加到主帖子。即使在这些改进之后,其行为也类似。在答案上似乎也没有任何清晰的模式,这使得追踪更加困难。