我制作了使用javafx的井字游戏。我是新来的java,我试图让胜利或抽签结束比赛。没有必要检查输赢是什么。当您无法继续播放时,我想让该文本结束。我尝试使用数组,但我不知道该怎么做。如何结束我的Javafx tic-tac-toe?
这是我的代码:
import java.util.ArrayList;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class ticTacToe extends Application {
private boolean turn;
private int[][] locations;
public RistinollaSovellus() {
turn = true;
locations = new int[3][3];
}
public static void main(String[] args) {
launch(ticTacToe.class);
}
@Override
public void start(Stage window) throws Exception {
boolean endGame = false;
BorderPane layout = new BorderPane();
Label text = new Label("Turn: X");
text.setFont(Font.font("Monospaced", 40));
layout.setTop(text);
GridPane array = new GridPane();
if (!endGame) {
for (int x = 1; x <= 3; x++) {
for (int y = 1; y <= 3; y++) {
Button btn = new Button(" ");
btn.setFont(Font.font("Monospaced", 40));
array.add(nappi, x, y);
locations[x][y] = 0;
btn.setOnAction((event) -> {
if (btn.getText().equals(" ")) {
if (turn) {
btn.setText("X");
text.setText("Turn: O");
turn = false;
} else if (!turn) {
btn.setText("O");
text.setText("Turn: X");
turn = true;
}
}
});
}
}
}
layout.setCenter(array);
Scene showing = new Scene(layout);
window.setScene(showing);
window.show();
}
}
使用一个计数器来跟上做出动作的次数。一旦移动发数量到达9比赛结束 – Sedrick
@SedrickJefferson,如果有人连续得到3使用了所有9圈之前会发生什么? – Jonah
您可以使用for循环,因为您知道游戏拥有的最大移动次数。你也可以使用while循环。在任何胜利条件下打破循环。 – Sedrick