Skip to content

Commit

Permalink
added support for invalid but reasonable moves like "y3"
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyson Decker committed May 31, 2022
1 parent 2af1b81 commit 220b429
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dist/bundle/srVisualizer.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/bundle/srVisualizer.min.js.map

Large diffs are not rendered by default.

28 changes: 26 additions & 2 deletions dist/lib/cube/parsing/algorithm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports.parseCase = exports.parseAlgorithm = void 0;
var constants_1 = require("./../constants");
var simulation_1 = require("../simulation");
var constants_2 = require("../constants");
var turnRegex = /([2-9]+)?([UuFfRrDdLlBbMESxyz])(w)?(2\'|\'2|2|\')?/g;
var turnRegex = /([2-9]+)?([UuFfRrDdLlBbMESxyz])(w)?(\d+\'|\'\d+|\d+|\')?/g;
var Opposite = (_a = {},
_a[simulation_1.TurnType.Clockwise] = simulation_1.TurnType.CounterClockwise,
_a[simulation_1.TurnType.CounterClockwise] = simulation_1.TurnType.Clockwise,
Expand Down Expand Up @@ -90,6 +90,30 @@ function getTurnType(rawType) {
case constants_2.TurnAbbreviation.DoubleCounter2:
return simulation_1.TurnType.Double;
default:
throw new Error("Invalid move modifier (" + rawType + ")");
// Attempt to parse non standard turn type
// (for invalid but reasonable moves like "y3")
var reversed = false;
if (rawType.charAt(0) === "'") {
reversed = true;
rawType = rawType.substring(1, rawType.length);
}
else if (rawType.charAt(rawType.length - 1) === "'") {
reversed = true;
}
var turns = parseInt(rawType) % 4;
if (isNaN(turns)) {
throw new Error("Invalid move modifier (" + rawType + ")");
}
if (turns === 0) {
return simulation_1.TurnType.None;
}
if (turns === 3) {
reversed = !reversed;
turns = 1;
}
if (turns == 2) {
return simulation_1.TurnType.Double;
}
return reversed ? simulation_1.TurnType.CounterClockwise : simulation_1.TurnType.Clockwise;
}
}
3 changes: 2 additions & 1 deletion dist/lib/cube/simulation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Turn } from './parsing/algorithm';
export declare enum TurnType {
Clockwise = 0,
CounterClockwise = 1,
Double = 2
Double = 2,
None = 3
}
export declare class CubeData {
private cubeSize;
Expand Down
4 changes: 4 additions & 0 deletions dist/lib/cube/simulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var TurnType;
TurnType[TurnType["Clockwise"] = 0] = "Clockwise";
TurnType[TurnType["CounterClockwise"] = 1] = "CounterClockwise";
TurnType[TurnType["Double"] = 2] = "Double";
TurnType[TurnType["None"] = 3] = "None";
})(TurnType = exports.TurnType || (exports.TurnType = {}));
var faceIdentity = function (stickerNumber, cubeSize) { return stickerNumber; };
var counterClockwiseSticker = function (stickerNumber, cubeSize) {
Expand Down Expand Up @@ -280,6 +281,9 @@ var CubeData = /** @class */ (function () {
this.zLayersRotation(0, turnType === TurnType.Clockwise, turnType === TurnType.Double, this.cubeSize);
};
CubeData.prototype.turn = function (turn) {
if (turn.turnType === TurnType.None) {
return;
}
var slices = this.safeSlices(turn.slices);
switch (turn.move) {
case constants_1.AlgorithmUnit.F:
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sr-visualizer",
"version": "1.0.12",
"version": "1.0.13",
"description": "",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand All @@ -26,4 +26,4 @@
"/dist",
"LICENSE"
]
}
}
33 changes: 31 additions & 2 deletions src/cube/parsing/algorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface Turn {
slices: number
}

const turnRegex = /([2-9]+)?([UuFfRrDdLlBbMESxyz])(w)?(2\'|\'2|2|\')?/g
const turnRegex = /([2-9]+)?([UuFfRrDdLlBbMESxyz])(w)?(\d+\'|\'\d+|\d+|\')?/g

const Opposite = {
[TurnType.Clockwise]: TurnType.CounterClockwise,
Expand Down Expand Up @@ -96,6 +96,35 @@ function getTurnType(rawType: string): TurnType {
case TurnAbbreviation.DoubleCounter2:
return TurnType.Double
default:
throw new Error(`Invalid move modifier (${rawType})`)
// Attempt to parse non standard turn type
// (for invalid but reasonable moves like "y3")
let reversed = false
if (rawType.charAt(0) === "'") {
reversed = true
rawType = rawType.substring(1, rawType.length)
} else if (rawType.charAt(rawType.length - 1) === "'") {
reversed = true
}

let turns = parseInt(rawType) % 4

if (isNaN(turns)) {
throw new Error(`Invalid move modifier (${rawType})`)
}

if (turns === 0) {
return TurnType.None
}

if (turns === 3) {
reversed = !reversed
turns = 1
}

if (turns == 2) {
return TurnType.Double
}

return reversed ? TurnType.CounterClockwise : TurnType.Clockwise
}
}
5 changes: 5 additions & 0 deletions src/cube/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export enum TurnType {
Clockwise,
CounterClockwise,
Double,
None,
}

// Given sticker N return sticker number after rotation
Expand Down Expand Up @@ -299,6 +300,10 @@ export class CubeData {
}

turn(turn: Turn) {
if (turn.turnType === TurnType.None) {
return
}

let slices = this.safeSlices(turn.slices)
switch (turn.move) {
case AlgorithmUnit.F:
Expand Down

0 comments on commit 220b429

Please sign in to comment.