使用javaFX制作一个Tic-Tac-Toe小游戏
时间: 2023-08-14 17:05:00 浏览: 112
JavaFX_TicTacToe:这是使用JavaFX开发的基本的迷你Tic Tac Toe游戏
好的,下面是一个简单的Tic-Tac-Toe小游戏的JavaFX代码示例:
```java
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class TicTacToe extends Application {
private char currentPlayer = 'X';
private Button[][] buttons = new Button[3][3];
private Text statusText = new Text();
@Override
public void start(Stage primaryStage) {
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
gridPane.setHgap(10);
gridPane.setVgap(10);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
Button button = new Button();
button.setPrefSize(100, 100);
button.setFont(Font.font("Arial", FontWeight.BOLD, 50));
button.setOnAction(event -> handleButtonAction(button, row, col));
buttons[row][col] = button;
gridPane.add(button, col, row);
}
}
statusText.setFont(Font.font("Arial", FontWeight.BOLD, 20));
statusText.setFill(Color.RED);
gridPane.add(statusText, 0, 3, 3, 1);
Scene scene = new Scene(gridPane, 350, 450);
primaryStage.setTitle("Tic Tac Toe");
primaryStage.setScene(scene);
primaryStage.show();
}
private void handleButtonAction(Button button, int row, int col) {
if (button.getText().isEmpty()) {
button.setText(Character.toString(currentPlayer));
if (checkWin(row, col)) {
statusText.setText("Player " + currentPlayer + " wins!");
disableButtons();
} else if (checkDraw()) {
statusText.setText("Game is a draw!");
} else {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
statusText.setText("Player " + currentPlayer + "'s turn");
}
}
}
private boolean checkWin(int row, int col) {
return (buttons[row][0].getText().equals(currentPlayer)
&& buttons[row][1].getText().equals(currentPlayer)
&& buttons[row][2].getText().equals(currentPlayer))
|| (buttons[0][col].getText().equals(currentPlayer)
&& buttons[1][col].getText().equals(currentPlayer)
&& buttons[2][col].getText().equals(currentPlayer))
|| (row == col && buttons[0][0].getText().equals(currentPlayer)
&& buttons[1][1].getText().equals(currentPlayer)
&& buttons[2][2].getText().equals(currentPlayer))
|| (row + col == 2 && buttons[0][2].getText().equals(currentPlayer)
&& buttons[1][1].getText().equals(currentPlayer)
&& buttons[2][0].getText().equals(currentPlayer));
}
private boolean checkDraw() {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (buttons[row][col].getText().isEmpty()) {
return false;
}
}
}
return true;
}
private void disableButtons() {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
buttons[row][col].setDisable(true);
}
}
}
public static void main(String[] args) {
launch(args);
}
}
```
这个代码示例创建了一个3x3的网格布局,并在每个单元格中放置一个按钮。每当玩家点击一个按钮时,将检查是否有玩家获胜或平局,并在状态文本中显示相应的消息。如果有玩家获胜或平局,所有按钮将被禁用,以防止进一步的操作。
阅读全文