可以不用数组标记来标记是否已经累加过。我的方法如下:(用一个变量idx来标记)
function moveLeft() {
if(!canMoveLeft(board)) {
return false;
}else {
for(var i=0;i<4;i++) {
var idx = 0; //定义idx 用来标记
for(var j=1;j<4;j++) {
if(board[i][j]!=0) {
//便利其所有左侧位置
for(var k=idx;k<j;k++) {
if(board[i][k]==0&&noHorizontal(i,k,j,board)) {
//move
showMoveAnimation(i,j,i,k);
//reset
board[i][k] = board[i][j];
board[i][j] = 0;
continue;
}else if(board[i][k]==board[i][j]&&noHorizontal(i,k,j,board)) {
//move
showMoveAnimation(i,j,i,k);
//add reset
board[i][k] += board[i][j];
board[i][j] = 0;
idx++; //累加过的数字,在下一次循环中跳过,即从下一个位置计算就可以了
score += board[i][k];
updateScore(score);
continue;
}
}
}
}
}
}
setTimeout("updateBoardView()",200);
return true;
}