gpt4 book ai didi

java - 单击菜单栏项目后尝试启动新的 Tic Tac Toe 游戏

转载 作者:太空宇宙 更新时间:2023-11-04 09:25:54 25 4
gpt4 key购买 nike

我有一个用 java GUI 制作的 tic tac toe 游戏,并试图通过让用户单击菜单栏项来实现一种重新启动游戏的方法。

请告诉我还有哪些其他方法可以尝试获得正常重启功能。

到目前为止,我尝试的是在单击菜单栏项时调用 playGame() 方法,但这不起作用,因为 playGame 方法中的 while 循环使其卡住。


public TicTacToe()
{
/* Create JFrame and set up the size and visibility */
frame = new JFrame("Tic Tac Toe");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

/* Create menu bar*/
JMenuBar menubar = new JMenuBar();
JMenu game = new JMenu("Game");
newGame = new JMenuItem("New Game");
quit = new JMenuItem("Quit");

newGame.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt) {
newGameActionPerformed(evt);
}
});

quit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt) {
quitActionPerformed(evt);
}
});

game.add(newGame);
game.add(quit);
menubar.add(game);
frame.setJMenuBar(menubar);

/* Add text area to frame. */
frame.add(scrollableTextArea);
frame.setLocationRelativeTo(null);
frame.setSize(400,400);
frame.setVisible(false);
}

private void quitActionPerformed(ActionEvent evt){
System.exit(0);
}

private void newGameActionPerformed(ActionEvent evt) {
clearBoard();
}
public void playGame()
{
frame.setVisible(true);
clearBoard(); // clear the board
// loop until the game ends
while (winner==EMPTY) { // game still in progress
while (playerWent == false){
if (topRight.getModel().isPressed()){
topRight.setText(player);
topRight.setEnabled(false);
playerWent = true;
}
else if (topMid.getModel().isPressed()){
topMid.setText(player);
topMid.setEnabled(false);
playerWent = true;
}
else if (topLeft.getModel().isPressed()){
topLeft.setText(player);
topLeft.setEnabled(false);
playerWent = true;
}
else if (midRight.getModel().isPressed()){
midRight.setText(player);
midRight.setEnabled(false);
playerWent = true;
}
else if (center.getModel().isPressed()){
center.setText(player);
center.setEnabled(false);
playerWent = true;
}
else if (midLeft.getModel().isPressed()){
midLeft.setText(player);
midLeft.setEnabled(false);
playerWent = true;
}
else if (botRight.getModel().isPressed()){
botRight.setText(player);
botRight.setEnabled(false);
playerWent = true;
}
else if (botMid.getModel().isPressed()){
botMid.setText(player);
botMid.setEnabled(false);
playerWent = true;
}
else if (botLeft.getModel().isPressed()){
botLeft.setText(player);
botLeft.setEnabled(false);
playerWent = true;
}
}
numFreeSquares--; // decrement number of free squares

// see if the game is over
if (haveWinner()) {
winner = player; // must be the player who just went
if (winner.equals(PLAYER_X)){
gameState.setText("Player X wins");
return;
} else {
gameState.setText("Player O wins");
return;
}
}
else if (numFreeSquares==0) {
winner = TIE; // board is full so it's a tie
gameState.setText("Tie game");
return;
}


// change to other player (this won't do anything if game has ended)
if (player==PLAYER_X) {
player=PLAYER_O;
gameState.setText("Game in progress, Player O's turn");
playerWent = false;
}
else {
player=PLAYER_X;
gameState.setText("Game in progress, Player X's turn");
playerWent = false;
}
}
}

最佳答案

循环将覆盖您尝试实现的重新启动功能。我的建议是,您尝试重构代码以实现事件驱动。为了保持简短:事件驱动编程是响应(并确认)用户输入的用户界面。您可以通过管理游戏回合的类来完成此操作。

这里有一些示例代码供引用,我已将其用于我自己的井字游戏项目(JavaFX 框架):

 private void playersGameProcess() {
//Looping through the button field to add mouseclick events, if there is pressed on a button the game's state switches between CROSS and NOUGHT.
for (int i = 0; i < buttons2D.length; i++) {
for (int j = 0; j < buttons2D[i].length; j++) {
Button button = buttons2D[i][j];
buttons2D[i][j].setOnMouseClicked(event -> {
if (game.getGameState() == "X" && button.getId() == null) {
game.incrementTurnCount();
System.out.println("Turn: " + game.getTurnCount());
notification.setText("It's nought's turn!");
game.setGameState("O");
button.setId("buttonClickX");
checkWinningConditions("X");
} else if (game.getGameState() == "O" && button.getId() == null) {
game.incrementTurnCount();
System.out.println("Turn: " + game.getTurnCount());
notification.setText("It's cross's turn!");
button.setId("buttonClickO");
game.setGameState("X");
checkWinningConditions("O");
}
});
}
}
}

关于java - 单击菜单栏项目后尝试启动新的 Tic Tac Toe 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57717234/

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