I am working on a Connect Four app for a school project. Currently, I am trying to JSON.stringify an object containing a two-dimensional array of "hole" objects (each containing keys: x, y, column, row, p1, and p2) so that I can send it to the server, and have it broadcast it two clients -> This will keep the game state updated for both clients.
Right before I JSON.stringify(holeObjArray), I console.logged the contents of the object and they show the updated game state:
I've highlighted (in red): holeObjArray[5][6] contains a key 'p1' = 1, and this corresponds to the current game state (on the right).
When I JSON.stringify the object, seen here:
function sendGrid()
{
console.log(holeObjArray);
JSONStr = JSON.stringify(holeObjArray);
console.log(JSONStr);
//ws.send(JSONStr);
}
The output of JSONStr shows the following:
As you can see, the stringified object does not represent the current game state. In fact, if I continue playing, the stringified object will always represent the state of the board on the last play. I don't understand why JSON.stringify() does this? Especially because the holeObjArray correctly represents the grid before I stringify it, but once I do, the JSONStr string represents the grid on the move before the one that was just made. Any help with this issue would be greatly appreciated,
Thanks,
Alex