gpt4 book ai didi

java - 如何在 Gomoku javafx 游戏中启用轮流

转载 作者:行者123 更新时间:2023-11-29 04:07:18 26 4
gpt4 key购买 nike

<分区>

我很难在我的程序中启用轮流。

棋盘上的一个方 block 被点击,这些坐标被传递给 playMakeMove(),它在棋盘矩阵中移动并设置文本以直观地表示移动。

虽然当有 2 个玩家参与时(玩家、RandomAI),在使用循环交替轮流后,该方法不起作用。 RandomAI 只是连续进行所有移动,玩家只在完全相同的位置移动一次(不等待点击鼠标)。

我认为这个问题可以通过等待瓷砖被点击(运行方法 playerMakeMove)然后让 RandomAI 轮流来解决。不确定如何实现这一点。

下面是两个类 BoardGUI 和 Game 还有另一个类(未包含)称为 Board。

public class BoardGUI extends Application {

private final int BOARD_SIZE = 15;

private Tile[][] tileBoard = new Tile[BOARD_SIZE][BOARD_SIZE];

private Pane root = new Pane();

private Parent createContent(Game game) {
root.setPrefSize(755, 755);

for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
Tile tile = new Tile(i, j);
tile.setTranslateX(j * 50);
tile.setTranslateY(i * 50);

tile.setOnMousePressed(e -> {
System.out.println("tile clicked");

// sets coordinates of tileClicked in game - to be used in making a player move
game.getTile().setRow(tile.getTileRow());
game.getTile().setColumn(tile.getTileCol());
});

root.getChildren().add(tile);

tileBoard[i][j] = tile;
}
}
return root;
}

@Override
public void start(Stage primaryStage) {
Game game = new Game();
Scene scene = new Scene (game.GUI.createContent(game));
primaryStage.setScene(scene);
primaryStage.setTitle("Gomoku");
primaryStage.show();


for (int turns = 0; turns < (game.getBoardSize()*game.getBoardSize()); turns++) {
if(game.getGameOver()) break;

if(!game.isPlayerTurn()) {
//AI make move
game.makeMoveAIRandom(game.getGameBoard());
game.setPlayerTurnTrue();
}
else {
// player make move
game.playerMakeMove(game);
}
}
System.out.println("game over");
}

class Tile extends StackPane {

Text text = new Text();
int row, column;

Tile(int x, int y) {
this.row = x;
this.column = y;

Rectangle border = new Rectangle(50, 50);
border.setFill(Color.BURLYWOOD);
border.setStroke(Color.BLACK);
text.setFont(Font.font(40));
setAlignment(Pos.CENTER);
getChildren().addAll(border, text);
}

void makeMove(Board board, int player, int row, int col) {
System.out.println(row + " " + col);

if (board.isMoveAvailable(row, col)) {
drawTile(player);
makeMoveMatrix(board, player, row, col);
}
System.out.println("makeMove executed");
}

void drawTile(int player) {
System.out.println("setTextTile executed");
text.setText("O");
if (player == 1) text.setFill(Color.BLACK);
else text.setFill(Color.WHITE);
}

void makeMoveMatrix(Board board, int player, int row, int col) {
board.make_move(player, row, col);
}

int getTileRow() { return row; }

void setRow(int row) { this.row = row; }

void setColumn(int column) { this.column = column; }

int getTileCol() { return column; }

}

Tile[][] getTileBoard() { return tileBoard; }
}


public class Game extends BoardGUI {

private final int PLAYER_ONE = 1;
private final int PLAYER_TWO = 2;
BoardGUI GUI;
private Board gameBoard;
private int boardSize = 15;
private int winLength = 5;
private boolean turn = true;
private boolean isGameOver = false;
private Tile tileClicked = new Tile(0, 0);

Game() {
this.gameBoard = new Board(boardSize, winLength);
this.GUI = new BoardGUI();
}

void makeMoveAIRandom(Board gameBoard) {

Random random = new Random();
int index = random.nextInt(gameBoard.availableMoves.size());
int[] move = gameBoard.availableMoves.get(index);
int row = move[0];
int col = move[1];

GUI.getTileBoard()[row][col].makeMove(gameBoard, PLAYER_TWO, move[0], move[1]);

setGameOver(getGameBoard().check_win_all(PLAYER_TWO, row, col));

gameBoard.availableMoves.remove(index);

setPlayerTurnTrue();
}

void playerMakeMove(Game game) {

int row = game.getTile().getTileRow();
int col = game.getTile().getTileCol();

game.GUI.getTileBoard()[row][col].makeMove(game.getGameBoard(), PLAYER_ONE, row, col);

setGameOver(getGameBoard().check_win_all(PLAYER_TWO, row, col));

System.out.println("player taken turn");
setPlayerTurnFalse();
}


Board getGameBoard() { return gameBoard; }

int getBoardSize () { return boardSize; }

boolean isPlayerTurn() { return turn; }

private void setPlayerTurnFalse() { turn = false; }

void setPlayerTurnTrue() { turn = true; }

boolean getGameOver () { return isGameOver; }

private void setGameOver (boolean result) { isGameOver = result; }

Tile getTile () { return tileClicked; }
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com