Hey,
So I have a property called 'tiles' which contains information about a checkers board's state. I'm trying to push this property to an array called 'moves' everytime I make a legal move and at the start of the game. But the problem is, everytime I push the new tiles property, the previous elements in the array change to the values of the latest pushed tiles.
Here is the snippet of code stripped to explain my problem which causes this behaviour.
App = function () {
var self = this;
//more code
wade.loadJson(tilesFile, data, function () {
self.tiles = wade.cloneObject(data.data.tiles);
}
//more code
this.startGame = function () {
self.moves.push(self.tiles);
};
this.makeMove = function () {
self.moves.push(self.tiles);
};
};
So now if I console 'self.moves' all the elements contain values of the latest pushed 'self.tiles'.
Can someone please explain this behaviour and how I can stop older elements being re-written by the latest tiles property and only push it as the last element in the array?
I want to save this information to implement undo move. I want each move's state to be able to go back to previous state with the help of tiles property. Hope I was clear in explaining the problem. Any help would be highly appreciated.