在javascript中使用数组作为传递参数

时间:2016-12-12 17:38:03

标签: javascript arrays parameter-passing minimax

var chessboard = [[2,1,0],[2,1,0],[0,0,0]];
function checkwins(array){}//The function is too long.I will explain here.It decides 
//whether there is a winner.If there is a winner it will return 1 or 0
 //(1 stand for number 2's win on the chessboard 0 stands for number 1's win)If there is no winner, it will return 2)
function score(board,depth){
    if(checkwins(board)===0)return depth-10;
    if(checkwins(board)==1)return 10-depth;
    else return 0;
}

function count_move(board,depth,current_turn){
    board = board.slice();
    var possible_moves = possible_movements(board);
    if(checkwins(board)!=2|| possible_moves.length===0)return score(board,depth);
    var move_score;
    var new_board;
    depth++;
    if(current_turn===0)move_score = -1000;
    else move_score = 1000;
    if(!current_turn){
        possible_moves.forEach(function(possible_location){
            var new_board = board.slice();
            new_board[possible_location[0]][possible_location[1]] = 1;
            var current_score = count_move(new_board,depth,1);
            if(current_score > move_score)move_score = current_score;
        });
    }else{   
            possible_moves.forEach(function(possible_location){
            var new_board = board.slice();
            new_board[possible_location[0]][possible_location[1]] = 2;
            var current_score = count_move(new_board,depth,0);
            if(current_score < move_score)move_score = current_score;
        });
    }
    return move_score;
    }
    function ai(board){
        var possible_moves = possible_movements(board);
        var best_move;
        var move_score = -1000;
        var current_score ;
        possible_moves.forEach(function(move){
            var next_board = board.slice();
            next_board[move[0]][move[1]] = 1;
            current_score = count_move(next_board,0,1);
            console.log("Current Move :"+move+"\nCurrent Score :"+current_score+'\nCurrent Board :'+next_board+'\n');
            if(current_score > move_score){
                move_score = current_score;
                best_move = move;
            }

        });
     console.log(best_move);
    }
console.log(chessboard);
ai(chessboard);
console.log(chessboard);

我正在使用Minimax算法编写一个Tic tac toe游戏Ai。我目前在使用javascript时遇到了一些问题。我发现当我将数组作为参数传递给函数然后在函数中修改它。它将改变数组传递甚至在功能之外。控制台结果如下:

[ [ 2, 1, 0 ], [ 2, 1, 0 ], [ 0, 0, 0 ] ]
Current Move :0,2
Current Score :-8
Current Board :2,1,1,2,1,2,2,2,2

Current Move :1,2
Current Score :10
Current Board :2,1,1,2,1,1,2,2,2

Current Move :2,0
Current Score :-10
Current Board :2,1,1,2,1,1,1,2,2

Current Move :2,1
Current Score :-10
Current Board :2,1,1,2,1,1,1,1,2

Current Move :2,2
Current Score :-10
Current Board :2,1,1,2,1,1,1,1,1

[ 1, 2 ]
[ [ 2, 1, 1 ], [ 2, 1, 1 ], [ 1, 1, 1 ] ]

然后我发现似乎使用了

new_array  = array.slice()
函数里面的

应该避免它,所以我在我的函数中添加它。结果仍然没有改变。我在这里感到很困惑。

1 个答案:

答案 0 :(得分:2)

slice执行数组的浅拷贝。这意味着数组本身被复制但不是其中的任何对象。

&#13;
&#13;
var a = [ [1], [2], [3] ];
var b = a.slice();
b.push(4);

// Change b does not change a
console.log('A:', JSON.stringify(a));
console.log('B:', JSON.stringify(b));
console.log('');

// However, changing the internal arrays will affect both
b[0][0] = 10;
console.log('A:', JSON.stringify(a));
console.log('B:', JSON.stringify(b));
console.log('');
&#13;
&#13;
&#13;

您需要执行深层复制,这意味着您不仅要复制外部数组,还要复制内部数组。

&#13;
&#13;
function copy2DArray(array) {
  var copy = [];
  array.forEach(function(subArray) {
    var copiedSubArray = subArray.slice();
    copy.push(copiedSubArray);
  });
  return copy;
}

var a = [ [1], [2], [3] ];
var b = copy2DArray(a);

// Now you won't change a by modifying b
b[0][0] = 10;
console.log('A:', JSON.stringify(a));
console.log('B:', JSON.stringify(b));
&#13;
&#13;
&#13;