GithubHelp home page GithubHelp logo

kungpaogao / playground Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 30 KB

Fun times - some code experiments

Home Page: https://kungpaogao.github.io/playground/

CSS 3.46% HTML 17.37% JavaScript 78.91% Python 0.27%

playground's Introduction

Hello! I'm Andrew ๐Ÿฆ”

Things I like:

  • ๐Ÿ’ป making stuff (web dev, app dev, learning other things)
  • ๐Ÿ€ basketball, volleyball
  • ๐Ÿ“ท photography, cameras
  • ๐ŸŽจ drawing, graphic design
  • โŒจ๏ธ mechanical keyboards

playground's People

Contributors

kungpaogao avatar

Watchers

 avatar  avatar

playground's Issues

[Tic Tac Toe] Incorrectly restoring states and not setting isX in buildHistory

This issue is related to the tic tac toe project. I wrote this for future reference.

Problem

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.

Cause

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().

Solution

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.