我不打算发布所有代码,只发布与之关联的部分。我对创建自己的听众并不太熟悉,本书还没有涉及到这一点。也许我做错了但你可以查看下面的代码并亲自看看。基本上当我改变它不断尝试设置值或一次又一次的值时,我试图用断点跟踪它,看看我是否能解决问题,但也许我错过了一些东西......追踪问题在那个特定的代码行设置了BooleanProperty值,并且每当我向前移动一行时就停留在那里。
这是我的Objects构造函数和数据字段
private BooleanProperty endTurn;
/** Constructor */
public PlayerHand() {
// Set the Data Field values
super.playerCards = new ArrayList<>();
this.chips = 100;
this.lastBet = 0;
this.endTurn = new SimpleBooleanProperty(this, "endTurn", false);
}
这是getter,setter和property方法
/** Get whether the player has ended the turn */
public boolean getEndTurn() {
return this.endTurn.get();
}
/** Set's the end of turn */
public void setEndTurn(boolean endTurn) {
this.endTurn.setValue(endTurn);
}
/** Property getter method for ending the turn */
public BooleanProperty endTurnProperty() {
return this.endTurn;
}
这是创建对象的类中的侦听器
// Set the binds for each player
for (int i = 0; i < players - 1; i++) {
alPlayerHand.get(i).endTurnProperty().addListener(e -> {
progressTurn();
});
}
编辑:我忘了添加这条线是否被卡住了
public void progressTurn() {
// Have AI make their decisions and turn rules on and off
if (alPlayerHand.get(playersTurn).getLastBet() == 0) {
if (alPlayerHand.get(playersTurn).isLittleBlind() &&
currentGameRound == 1) {
alPlayerHand.get(playersTurn).setBet(littleBlind);
lastPlayer = "Little Blind";
} else if (alPlayerHand.get(playersTurn).isBigBlind() &&
currentGameRound == 1) {
alPlayerHand.get(playersTurn).setBet(bigBlind);
lastPlayer = "Big Blind";
} else if (alPlayerHand.get(playersTurn).isAI()) {
alPlayerHand.get(playersTurn).decide();
lastPlayer = alPlayerHand.get(playersTurn).getLastMove();
}
}
// Add the last bet to the pot
pot += alPlayerHand.get(playersTurn).getLastBet();
alPlayerHand.get(playersTurn).setBet(0);
if ((playersTurn + 1) <= players - 1 &&
alPlayerHand.get(playersTurn).isAI()) {
playersTurn++;
} else if ((playersTurn + 1) > players - 1 &&
alPlayerHand.get(playersTurn).isAI())
playersTurn = 0;
endTurn(); // Debug stops here and stays here
}
编辑:再次忘记代码
/** End the current players turn */
public void endTurn() {
// End Players turn
if (alPlayerHand.get(playersTurn).getEndTurn()) {
alPlayerHand.get(playersTurn).setEndTurn(false);
} else
alPlayerHand.get(playersTurn).setEndTurn(true);
}
最后这里是错误 - 大多数已被删除,因为它只是重复了
Exception in thread "JavaFX Application Thread" java.lang.StackOverflowError
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:143)
at javafx.beans.property.BooleanProperty.setValue(BooleanProperty.java:77)
at Casino_Poker.PlayerHand.setEndTurn(PlayerHand.java:188)
at Casino_Poker.Main.endTurn(Main.java:1043)
at Casino_Poker.Main.progressTurn(Main.java:1014)
at Casino_Poker.Main.lambda$TexasHoldEm$16(Main.java:972)
at Casino_Poker.Main$$Lambda$13/477174400.invalidated(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:135)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:103)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:143)
at javafx.beans.property.BooleanProperty.setValue(BooleanProperty.java:77)
at Casino_Poker.PlayerHand.setEndTurn(PlayerHand.java:188)
at Casino_Poker.Main.endTurn(Main.java:1041)
at Casino_Poker.Main.progressTurn(Main.java:1014)
at Casino_Poker.Main.lambda$TexasHoldEm$16(Main.java:972)
at Casino_Poker.Main$$Lambda$13/477174400.invalidated(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:135)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:103)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:143)
at javafx.beans.property.BooleanProperty.setValue(BooleanProperty.java:77)
at Casino_Poker.PlayerHand.setEndTurn(PlayerHand.java:188)
at Casino_Poker.Main.endTurn(Main.java:1043)
at Casino_Poker.Main.progressTurn(Main.java:1014)
at Casino_Poker.Main.lambda$TexasHoldEm$16(Main.java:972)
at Casino_Poker.Main$$Lambda$13/477174400.invalidated(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:135)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:103)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:143)
at javafx.beans.property.BooleanProperty.setValue(BooleanProperty.java:77)
at Casino_Poker.PlayerHand.setEndTurn(PlayerHand.java:188)
at Casino_Poker.Main.endTurn(Main.java:1043)
at Casino_Poker.Main.progressTurn(Main.java:1014)
at Casino_Poker.Main.lambda$TexasHoldEm$16(Main.java:972)
at Casino_Poker.Main$$Lambda$13/477174400.invalidated(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:135)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:103)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:143)
at javafx.beans.property.BooleanProperty.setValue(BooleanProperty.java:77)
at Casino_Poker.PlayerHand.setEndTurn(PlayerHand.java:188)
at Casino_Poker.Main.endTurn(Main.java:1041)
at Casino_Poker.Main.progressTurn(Main.java:1014)
at Casino_Poker.Main.lambda$TexasHoldEm$16(Main.java:972)
at Casino_Poker.Main$$Lambda$13/477174400.invalidated(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:135)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:103)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:143)
at javafx.beans.property.BooleanProperty.setValue(BooleanProperty.java:77)
at Casino_Poker.PlayerHand.setEndTurn(PlayerHand.java:188)
at Casino_Poker.Main.endTurn(Main.java:1043)
at Casino_Poker.Main.progressTurn(Main.java:1014)
at Casino_Poker.Main.lambda$TexasHoldEm$16(Main.java:972)
at Casino_Poker.Main$$Lambda$13/477174400.invalidated(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:135)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:103)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:143)
at javafx.beans.property.BooleanProperty.setValue(BooleanProperty.java:77)
at Casino_Poker.PlayerHand.setEndTurn(PlayerHand.java:188)
at Casino_Poker.Main.endTurn(Main.java:1041)
at Casino_Poker.Main.progressTurn(Main.java:1014)
at Casino_Poker.Main.lambda$TexasHoldEm$16(Main.java:972)
at Casino_Poker.Main$$Lambda$13/477174400.invalidated(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:135)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:103)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:143)
at javafx.beans.property.BooleanProperty.setValue(BooleanProperty.java:77)
at Casino_Poker.PlayerHand.setEndTurn(PlayerHand.java:188)
at Casino_Poker.Main.endTurn(Main.java:1043)
at Casino_Poker.Main.progressTurn(Main.java:1014)
at Casino_Poker.Main.lambda$TexasHoldEm$16(Main.java:972)
at Casino_Poker.Main$$Lambda$13/477174400.invalidated(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:135)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:103)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:143)
at javafx.beans.property.BooleanProperty.setValue(BooleanProperty.java:77)
at Casino_Poker.PlayerHand.setEndTurn(PlayerHand.java:188)
at Casino_Poker.Main.endTurn(Main.java:1041)
at Casino_Poker.Main.progressTurn(Main.java:1014)
at Casino_Poker.Main.lambda$TexasHoldEm$16(Main.java:972)
at Casino_Poker.Main$$Lambda$13/477174400.invalidated(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:135)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:103)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:143)
at javafx.beans.property.BooleanProperty.setValue(BooleanProperty.java:77)
at Casino_Poker.PlayerHand.setEndTurn(PlayerHand.java:188)
at Casino_Poker.Main.endTurn(Main.java:1043)
at Casino_Poker.Main.progressTurn(Main.java:1014)
at Casino_Poker.Main.lambda$TexasHoldEm$16(Main.java:972)
at Casino_Poker.Main$$Lambda$13/477174400.invalidated(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:135)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:103)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:143)
at javafx.beans.property.BooleanProperty.setValue(BooleanProperty.java:77)
at Casino_Poker.PlayerHand.setEndTurn(PlayerHand.java:188)
at Casino_Poker.Main.endTurn(Main.java:1043)
at Casino_Poker.Main.progressTurn(Main.java:1014)
at Casino_Poker.Main.lambda$TexasHoldEm$16(Main.java:972)
at Casino_Poker.Main$$Lambda$13/477174400.invalidated(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:135)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:103)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
我真的很感激任何关于我做错了什么的建议,这本书到目前为止只涵盖了绑定属性的创建......但从未涉及内部工作,只是如何在没有声明的情况下设置方法可见。
基本上我想要玩家进行移动,如果玩家是AI控制的,那么计算机做出决定,并且在任一结束时它设置endTurn值,然后使得侦听器调用progressTurn方法并且循环继续
答案 0 :(得分:0)
当我在endTurn()方法的if语句中调用getter获取boolean属性时,我假设它正在启动我的监听器,所以我所做的是用'if(playersTurn%2 ==)替换它0)'