如果仅通过 doClick() 与程序交互,则 JFrame 永远不会呈现;
JFrame never renders if program is only interacted with through doClick();
我正在使用 Swing 编写黑白棋游戏,这里是一般代码:(该代码段不包括游戏 end/same 玩家再玩的情况,这只是基本的游戏循环。)
View/Controller:
public class OthelloGUI extends JFrame {
JSquare[][] board;
final int BOARD_SIZE = 8;
OthelloModel model;
public OthelloGUI(OthelloModel m) {
model = m;
/* Window size, etc... */
setLayout(new GridLayout(BOARD_SIZE, BOARD_SIZE));
//add 8x8 buttons to GUI
ActionListener ml = new MoveListener();
board = new JSquare[BOARD_SIZE][BOARD_SIZE];
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
getContentPane.add(board[i][j] = new JSquare(new Point(i, j)));
board[i][j].addActionListener(ml);
}
}
update();
}
void update() {
//update GUI to reflect last move
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
//changes the JButton's text based on who owns it on the model's board
board[i][j].setText(model.getOwner(i, j).view());
}
}
//best place (I think) to have AI move is after the last move is processed
if (model.getCurrentPlayerType() == PlayerType.AI) {
//Computer player finds its next move, and clicks that button
int[] bestMove = GreedyAI.findMove(model);
board[bestMove[0]][bestMove[1]].doCLick();
}
}
static class JSquare extends JButton {
java.awt.Point p;
JSquare (Point p) {this.p = p;}
}
class MoveListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JSquare sq = (JSquare) e.getSource();
//places the piece on the model's "board" and flips all applicable pieces
int result = model.placeDisk(s.p, model.getCurrentColor());
if (result == 0) {//move executes successfully
update();
}
//otherwise... (illegal move, game end, same player again, etc...)
}
}
}
enum Owner {
NONE, BLACK, WHITE;
public String view() {
return this == BLACK ? "B" : this == WHITE ? "W" : ""
}
}
如果我开始 Human v Human
或 Human v AI
游戏,此代码可以正常执行,但是,如果我开始 AI v AI
游戏,则 GUI 永远不会呈现。我得到了用于游戏设置的弹出框,以及用于游戏结束和谁赢了的弹出框,但是带有棋盘的主要 ContentPane
从未呈现,所以我看不到 AI 的动作。
任何 ideas/suggestions?
人工智能是自动化的。人类是..不是。
- 当人类在玩人类时,代码必须等待人类完成每一轮。它必须是事件驱动的。
- 当人类玩 AI 时,AI 是自动化的,但代码仍必须等待来自人类的事件。
- 但是当 AI 播放 AI 时 整个过程 留给代码,这导致 EDT 被阻止。
不要阻塞 EDT(事件调度线程)。当发生这种情况时,GUI 将 'freeze'。有关详细信息和修复,请参阅 Concurrency in Swing。
我正在使用 Swing 编写黑白棋游戏,这里是一般代码:(该代码段不包括游戏 end/same 玩家再玩的情况,这只是基本的游戏循环。)
View/Controller:
public class OthelloGUI extends JFrame {
JSquare[][] board;
final int BOARD_SIZE = 8;
OthelloModel model;
public OthelloGUI(OthelloModel m) {
model = m;
/* Window size, etc... */
setLayout(new GridLayout(BOARD_SIZE, BOARD_SIZE));
//add 8x8 buttons to GUI
ActionListener ml = new MoveListener();
board = new JSquare[BOARD_SIZE][BOARD_SIZE];
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
getContentPane.add(board[i][j] = new JSquare(new Point(i, j)));
board[i][j].addActionListener(ml);
}
}
update();
}
void update() {
//update GUI to reflect last move
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
//changes the JButton's text based on who owns it on the model's board
board[i][j].setText(model.getOwner(i, j).view());
}
}
//best place (I think) to have AI move is after the last move is processed
if (model.getCurrentPlayerType() == PlayerType.AI) {
//Computer player finds its next move, and clicks that button
int[] bestMove = GreedyAI.findMove(model);
board[bestMove[0]][bestMove[1]].doCLick();
}
}
static class JSquare extends JButton {
java.awt.Point p;
JSquare (Point p) {this.p = p;}
}
class MoveListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JSquare sq = (JSquare) e.getSource();
//places the piece on the model's "board" and flips all applicable pieces
int result = model.placeDisk(s.p, model.getCurrentColor());
if (result == 0) {//move executes successfully
update();
}
//otherwise... (illegal move, game end, same player again, etc...)
}
}
}
enum Owner {
NONE, BLACK, WHITE;
public String view() {
return this == BLACK ? "B" : this == WHITE ? "W" : ""
}
}
如果我开始 Human v Human
或 Human v AI
游戏,此代码可以正常执行,但是,如果我开始 AI v AI
游戏,则 GUI 永远不会呈现。我得到了用于游戏设置的弹出框,以及用于游戏结束和谁赢了的弹出框,但是带有棋盘的主要 ContentPane
从未呈现,所以我看不到 AI 的动作。
任何 ideas/suggestions?
人工智能是自动化的。人类是..不是。
- 当人类在玩人类时,代码必须等待人类完成每一轮。它必须是事件驱动的。
- 当人类玩 AI 时,AI 是自动化的,但代码仍必须等待来自人类的事件。
- 但是当 AI 播放 AI 时 整个过程 留给代码,这导致 EDT 被阻止。
不要阻塞 EDT(事件调度线程)。当发生这种情况时,GUI 将 'freeze'。有关详细信息和修复,请参阅 Concurrency in Swing。