Skip to content

Commit

Permalink
Merge pull request #71 from torjusti/develop
Browse files Browse the repository at this point in the history
Resurrect package
  • Loading branch information
torjusti committed Sep 10, 2023
2 parents 31873e7 + baf6dd7 commit e3ba7e8
Show file tree
Hide file tree
Showing 12 changed files with 3,056 additions and 6,314 deletions.
17 changes: 5 additions & 12 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
{
"extends": "airbnb-base",
"parser": "babel-eslint",
"extends": "eslint:recommended",
"parser": "@babel/eslint-parser",
"env": {
"jest": true
},
"rules": {
"no-bitwise": "off",
"no-mixed-operators": "off",
"no-param-reassign": "off",
"class-methods-use-this": ["error", { "exceptMethods": ["handleSolution"] }],
"import/no-named-as-default": "off",
"max-len": ["error", 125],
"object-curly-newline": "off"
"jest": true,
"browser": true,
"node": true
}
}
17 changes: 0 additions & 17 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Torjus Iveland
Copyright (c) 2017 - 2023 Torjus Iveland

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# cube-solver

![travis](https://travis-ci.org/torjusti/cube-solver.svg?branch=master)
[![codecov](https://codecov.io/gh/torjusti/cube-solver/branch/master/graph/badge.svg)](https://codecov.io/gh/torjusti/cube-solver)

This module contains a collection of Rubik's cube solvers implemented in JavaScript. It can solve a given cube using the fantastic Kociemba two-phase algorithm, generate random state scrambles, solve steps such as the cross or the Roux first block, as well as provide a variety of different scramble types.
This module contains a collection of Rubik's cube solvers implemented in JavaScript. It can solve a given cube using the fantastic Kociemba two-phase algorithm, generate random state scrambles, solve steps such as the cross or the Roux first block, and provide a variety of different scramble types.

## Installation

To install, simply run `yarn install cube-solver` or similar, and require the `cube-solver` module. You can also manually add the bundle file to your webpage using [this unpkg link](https://unpkg.com/cube-solver/dist/bundle.js), in which case the solver will be available globally as `cubeSolver`.
Run `yarn install cube-solver` or similar, then require the `cube-solver` module. You can also manually add the bundle file to your webpage using [this unpkg link](https://unpkg.com/cube-solver/dist/bundle.js), in which case the solver will be available globally as `cubeSolver`.

## Examples

Expand All @@ -22,17 +19,17 @@ cubeSolver.scramble('zbll'); // => R B2 R' U' L U' L U' F2 R2 U' B2 U R2 D' F2 U

## Notes

The full 3x3 solver is pretty slow compared to other solvers such as the GWT compiled version of min2phase - it rather aims to be simple and extensible. Unless explicitly initialized, the Kociemba solver initializes when solving the first cube, which usually takes around 2 seconds. Generating and solving a random cube takes around 100ms on average, but for some cubes this number can be quite high. The optimal EOLine, FB, Cross and XCross solvers initialize pretty quickly, with the exception of the XCross solver which also takes around 2 seconds. *As a consequence, you probably want to use this library in conjunction with web workers.*
The full 3x3 solver is pretty slow compared to other solvers such as the GWT compiled version of min2phase - it rather aims to be simple and extensible. Unless explicitly initialized, the Kociemba solver initializes when solving the first cube, which usually takes around 2 seconds. Generating and solving a random cube takes around 100ms on average, but for some cubes the number can be quite high. The EOLine, FB, and Cross solvers initialize pretty quickly, while the XCross solver also takes around 2 seconds. *As a consequence, you probably want to use this library in conjunction with web workers.*

## Documentation

The solver exposes three main methods.

- `cubeSolver.scramble(type = '3x3')` lets you generate scrambles for the entire cube or subsets of it --- see below for a list of available scramblers.
- `cubeSolver.solve(scramble, type = 'kociemba')` lets you solve either the entire cube or a subset of it for a given scramble. See below for a list of available solvers.
- `cubeSolver.initialize(solver)` lets you initialize solvers so that the first solve is quicker. See below for a list of available solvers. To speed up random-state *scrambles*, the Kociemba solver should be initialized.
- `cubeSolver.initialize(solver)` lets you initialize solvers so that the first solve is quicker. See below for a list of available solvers. Note that to speed up ramdom-state *scramblers*, the Kociemba solver should be initialized.

In general, the solvers will put the feature being solved for on the *bottom*. For example, using normal orientation, solving for an XCross will yield a yellow cross with the front-left pair solved. To solve other positions, you can apply pre-moves to your scrambles. This example shows how to find a bottom-left yellow XCross:
In general, the solvers will put the feature being solved for on the *bottom*. For example, using normal scrambling orientation, solving for an XCross will yield a yellow cross with the front-left pair solved. To solve other positions, you can apply pre-moves to your scrambles. This example shows how to find a bottom-left yellow XCross:

```javascript
const scramble = `U2 B2 D R' F R2 U2 L D R2 L2 F2 U R2 U' L2 D F2 R2`;
Expand Down
34 changes: 12 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Solve Rubik's cube using the Kociemba algorithm.",
"author": "Torjus Iveland <torjusti@gmail.com>",
"main": "dist/bundle.js",
"type": "module",
"types": "index.d.ts",
"repository": {
"type": "git",
Expand All @@ -27,31 +28,20 @@
"build": "rollup -c",
"lint": "eslint .",
"test": "jest",
"codecov": "codecov",
"prepublishOnly": "yarn build"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/plugin-transform-exponentiation-operator": "^7.0.0",
"@babel/preset-env": "^7.7.7",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.3",
"babel-jest": "^23.4.2",
"babel-loader": "^8.0.6",
"chance": "^1.1.4",
"codecov": "^3.6.1",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-plugin-import": "^2.19.1",
"eslint-plugin-jest": "^23.1.1",
"jest": "^24.9.0",
"rollup": "^1.27.14",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-uglify": "^6.0.4",
"webpack": "^4.41.4",
"webpack-cli": "^3.3.10"
"@babel/core": "^7.22.17",
"@babel/eslint-parser": "^7.22.15",
"@babel/preset-env": "^7.22.15",
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-terser": "^0.4.3",
"chance": "^1.1.11",
"eslint": "^8.49.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jest": "^27.2.3",
"jest": "^29.6.4",
"rollup": "^3.29.1"
},
"jest": {
"collectCoverageFrom": [
Expand Down
12 changes: 6 additions & 6 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import babel from 'rollup-plugin-babel';
import { uglify } from 'rollup-plugin-uglify';
import builtins from 'rollup-plugin-node-builtins';
import { babel } from '@rollup/plugin-babel';
import terser from '@rollup/plugin-terser';

export default {
input: 'src/index.js',
Expand All @@ -13,10 +12,11 @@ export default {

plugins: [
babel({
exclude: 'node_modules/**',
babelHelpers: 'bundled',
}),

uglify(),
builtins(),
terser({
maxWorkers: 4
})
],
};
2 changes: 1 addition & 1 deletion src/algorithms.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const normalize = (moves) => {
for (let i = 0; i <= pow; i += 1) {
acc = acc.concat(wideMoves[axis]);
}

return acc;
}

Expand Down
2 changes: 1 addition & 1 deletion src/scramblers/tests/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Chance from 'chance'; // eslint-disable-line import/no-extraneous-dependencies
import Chance from 'chance';
import { adjustUpperFace } from '../scramblePieces';

export const configureMathMock = (seed) => {
Expand Down
4 changes: 2 additions & 2 deletions src/tests/__snapshots__/algorithms.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`algorithms should strip rotations from algorithms correctly 1`] = `
Array [
[
3,
9,
8,
Expand All @@ -18,7 +18,7 @@ Array [
`;

exports[`algorithms should strip rotations from algorithms correctly 2`] = `
Array [
[
9,
9,
9,
Expand Down
20 changes: 10 additions & 10 deletions src/tests/__snapshots__/cube.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`cube performs a corner orientation move 1`] = `
Array [
[
1,
2,
0,
Expand All @@ -14,7 +14,7 @@ Array [
`;

exports[`cube performs a corner permutation move 1`] = `
Array [
[
1,
5,
2,
Expand All @@ -27,8 +27,8 @@ Array [
`;

exports[`cube performs an algorithm correctly 1`] = `
Object {
"co": Array [
{
"co": [
0,
1,
0,
Expand All @@ -38,7 +38,7 @@ Object {
0,
0,
],
"cp": Array [
"cp": [
0,
2,
3,
Expand All @@ -48,7 +48,7 @@ Object {
6,
7,
],
"eo": Array [
"eo": [
1,
1,
0,
Expand All @@ -62,7 +62,7 @@ Object {
0,
0,
],
"ep": Array [
"ep": [
1,
3,
2,
Expand All @@ -80,7 +80,7 @@ Object {
`;

exports[`cube performs an edge orientation move 1`] = `
Array [
[
0,
1,
0,
Expand All @@ -97,7 +97,7 @@ Array [
`;

exports[`cube performs an edge permutation move 1`] = `
Array [
[
0,
9,
2,
Expand All @@ -114,7 +114,7 @@ Array [
`;

exports[`cube resets the edge orientation when doing a move twice 1`] = `
Array [
[
0,
0,
0,
Expand Down
4 changes: 2 additions & 2 deletions src/tests/algorithms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('algorithms', () => {
it('should not allow parsing invalid algorithms', () => {
expect(() => {
algorithms.parseAlgorithm("R U R' U' 1337 R' F R F'");
}).toThrowError();
}).toThrow();
});

it('should correctly invert a given algorithm', () => {
Expand All @@ -17,7 +17,7 @@ describe('algorithms', () => {
.toMatchSnapshot();

// Regression: Wide moves described using inverted rotations would
// add powers multiple times to a single move, crashing the parser.
// add powers multiple times to a single move, crashing the parser.
expect(algorithms.parseAlgorithm("f' r' u' b' l' d'")).toMatchSnapshot();
});
});
Loading

0 comments on commit e3ba7e8

Please sign in to comment.