cubing / cubing.js

🛠 A library for displaying and working with twisty puzzles. Also currently home to the code for Twizzle.
https://js.cubing.net/cubing/
GNU General Public License v3.0
232 stars 42 forks source link

[Feature request] Validate moves for experimentalAddMove() #331

Open TwitchSolvesCube opened 4 weeks ago

TwitchSolvesCube commented 4 weeks ago

Goal

The goal of this feature is to prevent the twisty player from getting frozen when an invalid move is added. For example when an invalid move r is added to a 2x2x2 puzzle with experimentalAddMove() valid moves, such as R, will no longer work.

Possible solution

The current work around discussed with @lgarron is to check if the move can be transformed to a kpuzzle with moveToTransformation() before using experimentalAddMove() as the aforementioned function will throw an error.

Here's my current implementation.

    try {
      var result = this.kpuzzle.moveToTransformation(new Move(myMove));
      if (typeof result === 'object') {
        const newMove = new Move(myMove);
        this.player.experimentalAddMove(newMove);
        this.puzzleState = this.puzzleState.applyMove(newMove);
      }
    } catch (error) {
      console.log(error);
    }

The solution would to be to implement this sort of check within the experimentalAddMove() function before appending the move to the twisty player.

Alternatives

No response

lgarron commented 4 weeks ago

Definitely a worthwhile addition, I'll try to implement this when I get a chance or I'd be happy to review a PR.