2016-07-30 76 views
0

我正在嘗試在JavaFX中爲TicTacToe遊戲製作此代碼。但是現在即使有贏家(也就是說有人把他的三個X或O排成一排/一列/對角線),該程序仍然允許另一個玩家將他的牌放在棋盤上。只有在獲勝按鈕被更改並且按鈕被設置爲禁用之後。我該如何改變它?TicTacToe遊戲中的一個不必要的轉向

package tictactoe; 

import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.scene.Node; 
import javafx.scene.control.Button; 
import javafx.scene.control.MenuItem; 
import javafx.scene.layout.GridPane; 

public class TicTacToeController { 
private boolean isFirstPlayer = true; 

public void buttonClickHandler(ActionEvent evt) { 
    this.find3InARow(); 

    Button clickedButton = (Button) evt.getTarget(); 
    String buttonLabel = clickedButton.getText(); 

    if ("".equals(buttonLabel) && isFirstPlayer){ 
     clickedButton.setText("X"); 
     isFirstPlayer = false; 
    } else if("".equals(buttonLabel) && !isFirstPlayer) { 
     clickedButton.setText("O"); 
     isFirstPlayer = true; 
    } 

} 
@FXML Button b1; 
@FXML Button b2; 
@FXML Button b3; 
@FXML Button b4; 
@FXML Button b5; 
@FXML Button b6; 
@FXML Button b7; 
@FXML Button b8; 
@FXML Button b9; 
@FXML GridPane gameBoard; 
@FXML MenuItem Play; 

private boolean find3InARow() { 
    if (""!=b1.getText() && b1.getText() == b2.getText() 
     && b2.getText() == b3.getText()){ 
     highlightWinningCombo(b1,b2,b3); 
     return true; 
    } 

    if (""!=b4.getText() && b4.getText() == b5.getText() 
     && b5.getText() == b6.getText()){ 
     highlightWinningCombo(b4,b5,b6); 
     return true; 
    } 

    if (""!=b7.getText() && b7.getText() == b8.getText() 
     && b8.getText() == b9.getText()){ 
     highlightWinningCombo(b7,b8,b9); 
     return true; 
    } 

    if (""!=b1.getText() && b1.getText() == b4.getText() 
     && b4.getText() == b7.getText()){ 
     highlightWinningCombo(b1,b4,b7); 
     return true; 
    } 

    if (""!=b2.getText() && b2.getText() == b5.getText() 
     && b5.getText() == b8.getText()){ 
     highlightWinningCombo(b2,b5,b8); 
     return true; 
    } 

    if (""!=b3.getText() && b3.getText() == b6.getText() 
     && b6.getText() == b9.getText()){ 
     highlightWinningCombo(b3,b6,b9); 
     return true; 
    } 

    if (""!=b1.getText() && b1.getText() == b5.getText() 
     && b5.getText() == b9.getText()){ 
     highlightWinningCombo(b1,b5,b9); 
     return true; 
    } 

    if (""!=b3.getText() && b3.getText() == b5.getText() 
     && b5.getText() == b7.getText()){ 
     highlightWinningCombo(b3,b5,b7); 
     return true; 
    } 
    return false; 

    } 

    private void highlightWinningCombo(Button first, Button second, Button third) { 
     gameBoard.setDisable(true); 
     first.getStyleClass().add("winning-button"); 
     second.getStyleClass().add("winning-button"); 
     third.getStyleClass().add("winning-button"); 
    } 

    public void menuClickHandler(ActionEvent evt){ 
     MenuItem clickedMenu = (MenuItem) evt.getTarget(); 
     String menuLabel = clickedMenu.getText(); 

     if ("Play".equals(menuLabel)){ 
      ObservableList<Node> buttons = gameBoard.getChildren(); 
gameBoard.setDisable(false); 

      buttons.forEach(btn -> { 
       ((Button) btn).setText(""); 
       btn.getStyleClass().remove("winning-button"); 
      }); 
      isFirstPlayer = true; 
     } 
     else if("Quit".equals(menuLabel)) { 
      System.exit(0); 
    } 

    } 

}

+0

用調試程序運行該程序以檢查它出錯的位置。 – Henry

回答

0

由玩家設定的最後輸入之前要檢查的板,所以如果玩家已經作出一個成功的舉動,只會被檢查完獲獎條件設定,所以該節目不知道最後一名球員是否贏了。爲了解決這個問題,你首先需要處理輸入,然後檢查獲勝條件。要做到這一點,只需將this.find3InARow();移至方法buttonClickHandler()的末尾即可。

+0

非常感謝!它確實有幫助! – Felix

+0

@Felix如果你喜歡我的回答,你能接受嗎? –

+0

對不起,我是新來的。我標記了。 – Felix