Things I like:
- ๐ป making stuff (web dev, app dev, learning other things)
- ๐ basketball, volleyball
- ๐ท photography, cameras
- ๐จ drawing, graphic design
- โจ๏ธ mechanical keyboards
Fun times - some code experiments
Home Page: https://kungpaogao.github.io/playground/
This issue is related to the tic tac toe project. I wrote this for future reference.
The issue is jumping back to an event more than once yields unexpected behavior. Take the following test case:
update("sq4"); // X
buildHistory(0); // jump back to event 0 the first time
assert(!isX, "next move should be an O"); // passes
update("sq2"); // O
update("sq3"); // X
update("sq5"); // O
buildHistory(0); // jump back to event 0 the second time
assert(!isX, "next move should be an O, again"); // FAILS
assert(state.length === 1, "state length should be 1"); // FAILS
The second time, the restored state
ends up being the current state
, i.e., board is not reset (even though the history table is reset). The same thing occurs when calling undo multiple times to the same event.
Additionally, isX
is not set in buildHistory()
, so the correct turn is not restored.
The reason for this problem is the way buildHistory()
and undo()
reset state
. Currently, both just do the following:
// index is given, since we know which event we want
const event = history[index]; // get event at index
state = event.st; // set the current state to the event's state
However, event.st
is an array, which is an object in JavaScript. As I learned when trying to build this "time travel" originally, storing objects is not as easy as setting a variable equal to another. This ends up storing a reference to event.st
.
So, when we update state
again by calling something like
state.push(st); // == to event.st.push(st)
We end up updating the array stored in the event. And, when we jump back to that event to restore the state
, we end up restoring a mutated version, not the expected historical version (i.e., we just "restore" the current state).
For setting isX
, we just never make a call to reset it in buildHistory()
.
The solution to this restoration issue is simply copying the array object and setting the current state
to that copy.
const event = history[index]; // get event at index
state = [...event.st];
Now, updating state
won't update the state stored in event.st
. This needs to happen in both buildHistory()
and undo()
.
The solution to setting isX
is just copying the code from undo()
:
isX = state[state.length - 1].val === "O";
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.