[英]Efficiently tracking first and second click Java - 2d array gui
我正在寻找一种更有效的跟踪第一次和第二次点击的方法。 开发棋盘游戏以及我现在的工作方式,我觉得我无法在其中添加所有要求
这是我目前跟踪点击次数的方式。
public void actionPerformed(ActionEvent ae){
for(int row = 0; row < boardGame.length; row++){
for(int col = 0; col < boardGame.length; col++){
if(ae.getSource() == boardGame[row][col]){
if(isFirstClick){
fromRow = boardGame[row][col].getRow();
fromCol = boardGame[row][col].getCol();
this.checkTurn();
System.out.println(boardGame[row][col].getPlayer());
isFirstClick = false;
}// end if
else {
toRow = boardGame[row][col].getRow();
toCol = boardGame[row][col].getCol();
movePiece();
isFirstClick = true;
createBoard();
}// end else
}// end get source
}//end col
} //end row
}
因此,尽管这适用于我目前拥有的功能。.我似乎无法使其与移动验证一起使用。 有没有更好的方法来获得第一次和第二次点击?
您的游戏看起来像国际象棋。 我建议您看一下Command模式。 每次用户单击您的开发板或板上的某个单元时,或您生成新事件的任何事件,该事件都会转换为将其存储在列表或队列中的命令。 然后,您将始终知道进行了多少次单击和其他操作以及何时进行。 此外,您将能够实现取消移动之类的功能。
这些是一个揭示我想法的例子:
static abstract class Event {
}
static class CellClicked extends Event {
int userId;
int row;
int col;
}
static abstract class Command {
void execute();
}
static class MovePieceCommand extends Command {
//... implementation
}
static class SelectPieceCommand extends Command {
//... implementation
}
static class EventHandler {
void handle(Event event) {
//..
}
}
事件侦听器会将您的事件转换为命令 。 执行命令并在其中实现游戏的业务逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.