For a tic tac toe game, I'm writing a function that is supposed to find the best next move. So far I pass on the current state of the game and will then recurse through all possible moves.
var fieldModel = [
["x", "", ""],
["", "", ""],
["", "", "x"]
];
function aiMove() {
for (var i = 0; i < fieldModel.length; i++) {
for (var j = 0; j < fieldModel[i].length; j++) {
if (fieldModel[i][j] === "") {
function findBestMove(i, j) {
var simModel = fieldModel.slice();
simModel[i][j] = "o";
console.log("simulation", simModel);
}
findBestMove(i, j);
}
}
}
}
aiMove();
The problem is that, as the output shows, each log shows the state of the array after all iterations have run. So obviously simModel
is not as local as I want it to be.
How can I achieve that?