0
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);

I am writing a Tic tac toe game Ai by using Minimax algorithm.I currently have some problems with javascript.I found that when I passed array as argument into function and then revise it in the function.It will change the array passing even outside the function.The console results is below:

[ [ 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 ] ]

Then I found it seems to use

new_array  = array.slice()

inside the function should avoid it, so I add it in my function.The results still don't change.I get quite confused here.

2
  • can you describe which array you are talking about exactly as i am unable to understand exact problem. Other thing you might be operating on the global variable? Commented Dec 12, 2016 at 17:42
  • There is an array called chessboard on line 1.I used that to test the function. Commented Dec 12, 2016 at 17:44

1 Answer 1

2

slice performs a shallow copy of an array. That means that the array itself is copied but not any of the objects inside of it.

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('');

You need to perform a deep copy, meaning you copy not just the outer array but also the inner arrays.

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));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.