【发布时间】:2015-09-06 09:44:51
【问题描述】:
我正在用 java 编写一个简单的井字游戏。 基本实现就完成了。 Player vs Player 工作完美。
但是现在,当我实施人工智能时,我遇到了问题。 我已经计算了计算机的移动,但我无法让计算机在 JPanel 上单击该位置 [使用 (x, y) 坐标]。
看过Robot类,但是没有点击到需要的地方,好像是在窗口外点击了。
这是在 JPanel 上绘制电路板的代码:
private class Board extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
public Board(){
this.setSize(new Dimension(board_width, board_height));
this.setPreferredSize(new Dimension(board_width, board_height));
this.setBackground(new Color(0x84ACFF));
this.setOpaque(true);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//g.setColor(new Color(0xf9d1af));
g.setColor(Color.white);
g.drawLine((board_width/3), 0, (board_width/3), board_height);
g.drawLine((board_width/3)*2, 0, (board_width/3)*2, board_height);
g.drawLine(0, (board_height/3), (board_width), (board_height/3));
g.drawLine(0, (board_height/3)*2, (board_width), (board_height/3)*2);
g.finalize();
}
}
这是我在 JPanel 中使用的 mouseListener:
@Override
public void mousePressed(MouseEvent e) {
Graphics g = this.getGraphics();
int X = e.getX();
int Y = e.getY();
//JOptionPane.showMessageDialog(null, e.getXOnScreen()+":"+e.getYOnScreen());
int block_x = board_width/3;
int block_y = board_height/3;
X = X/block_x;
Y = Y/block_y;
//For array Assignment
Click_X = Y;
Click_Y = X;
//Keep copy of the originals
Orig_X = X;
Orig_Y = Y;
//------------------------------------------
// Assign to array & Draw
//------------------------------------------
if (computer){
if (start.equals("Computer")){
if (game_board[Click_X][Click_Y] != ' ') return;
moves++;
game_board[Click_X][Click_Y] = Cross;
drawCross(((X+1)*block_x)+25, ((Y+1)*block_y)+25, 50, 50, g);
dcsn = new Decision(game_board, moves);
}
else{
if (game_board[Click_X][Click_Y] != ' ') return;
moves++;
game_board[Click_X][Click_Y] = Nought;
drawNought(((X+1)*block_x), ((Y+1)*block_y), 50, 50, g);
dcsn = new Decision(game_board, moves);
}
}
else{
if (start.equals("Human")){
if (game_board[Click_X][Click_Y] != ' ') return;
moves++;
game_board[Click_X][Click_Y] = Cross;
drawCross(((X+1)*block_x)+25, ((Y+1)*block_y)+25, 50, 50, g);
dcsn = new Decision(game_board, moves);
}
else{
if (game_board[Click_X][Click_Y] != ' ') return;
moves++;
game_board[Click_X][Click_Y] = Nought;
drawNought(((X+1)*block_x), ((Y+1)*block_y), 50, 50, g);
dcsn = new Decision(game_board, moves);
}
}
switch (dcsn.gameOver()){
case "X":
JOptionPane.showMessageDialog(null,"X has won!");
break;
case "O":
JOptionPane.showMessageDialog(null,"O has won!");
break;
case "draw":
JOptionPane.showMessageDialog(null,"Draw!");
break;
case " ":
break;
}
switchTurn();
}
现在如何让计算机点击计算出的位置? 有什么想法吗?
这里是完整源代码的链接: Download Source
否则很难重现这种情况。
【问题讨论】:
-
您可以创建一个方法
assignToArrayAndDraw(),您可以从mousePressed()方法或您的 AI 中调用该方法。 -
@LuxxMiner :不,Click_X = Y 没问题,因为我需要 [(00),(01),(02)] 格式的东西,用于将交叉/空值存储在数组中.
-
我已经添加了源代码,否则在这种情况下很难发布 MCVE。 @LuxxMiner。
-
重点是不需要机器人点击板子。你的 AI 应该直接调用
mousePressed()中的代码(除非你应该将此代码打包到另一个方法中,这将独立于鼠标坐标 - 它只会使用网格坐标)
标签: java swing graphics mouselistener tic-tac-toe