public void mousePressedMouseEvent e 游戏结束时不再能下 if gameOver paused return; String colorName=isBlack黑棋白棋; 将鼠标点击的坐标位置转换成网格索引 xIndex=egetX-MA
public void mousePressed(MouseEvent e){ //游戏结束时,不再能下 if (gameOver || paused) { JOptionPane.showMessageDialog(this, "当前处于暂停模式,请点击继续按钮继续游戏"); return; }
String colorName=isBlack?"黑棋":"白棋";
//将鼠标点击的坐标位置转换成网格索引
xIndex=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
yIndex=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
//落在棋盘外不能下
if(xIndex<0||xIndex>ROWS||yIndex<0||yIndex>COLS) {
JOptionPane.showMessageDialog(this, "该位置不在界面内,请在界面内下棋");
return;
}
//如果点击的位置已经有棋子存在,不能下
if(findChess(xIndex,yIndex)) {
JOptionPane.showMessageDialog(this, "该位置已经有棋子,请重新选择位置下棋!");
return;
}
//可以进行时的处理
Point ch=new Point(xIndex,yIndex,isBlack?Color.black:Color.white);
chessList[chessCount++]=ch;
repaint();//通知系统重新绘制
//如果胜出则给出提示信息,不能继续下棋
if(isWin()){
String msg=String.format("恭喜,%s赢了!", colorName);
JOptionPane.showMessageDialog(this, msg);
gameOver=true;
}
isBlack=!isBlack;
原文地址: http://www.cveoy.top/t/topic/g7fO 著作权归作者所有。请勿转载和采集!