Skip to content

Commit

Permalink
extract all stickers if no turn is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbedard committed Aug 7, 2021
1 parent e473035 commit 95605bf
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ puzzle.scramble()

#### `stickers`

Get all stickers effected by a turn.
Get stickers affected by a turn. If no turn notation is provided, all stickers will be returned.

```js
const stickers = puzzle.stickers('R')
Expand Down
15 changes: 9 additions & 6 deletions src/puzzles/cube/cube.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { error } from '@/utils/function';
import { extract, inject, rotate } from '@/utils/matrix';
import { flattenBy, isUniform, last, sample, times, without } from '@/utils/array';
import { flattenBy, flattenDeep, isUniform, last, sample, times, without } from '@/utils/array';
import { floor, max, rand } from '@/utils/number';
import { keys } from '@/utils/object';
import { lowercase } from '@/utils/string';
Expand Down Expand Up @@ -203,17 +203,20 @@ export class Cube extends Puzzle<CubeOptions, CubeState, CubeSimpleState, CubeTu
}

/**
* Get stickers that are part of a turn.
* Get stickers.
*
* @param {string} turn turn to extract stickers from
* @param {string} turnNotation turn to extract stickers from
*/
stickers(turnNotation: string): CubeSticker[] {
stickers(turnNotation?: string): CubeSticker[] {
if (!turnNotation) {
return flattenDeep(Object.values(this.state));
}

const turn = this.parse(turnNotation);
const { u, l, f, r, b, d } = this.state;

// return all stickers for whole-puzzle rotations
if (turn.target === 'x' || turn.target === 'y' || turn.target === 'z') {
return [].concat(u, l, f, r, b, d);
return flattenDeep(Object.values(this.state));
}

const stickers: CubeSticker[] = [];
Expand Down
11 changes: 7 additions & 4 deletions src/puzzles/dodecaminx/dodecaminx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,20 @@ export class Dodecaminx extends Puzzle<DodecaminxOptions, DodecaminxState, Dodec
}

/**
* Get stickers that are part of a turn.
* Get stickers.
*
* @param {string} turnNotation turn to extract stickers from
*/
stickers(turnNotation: string): DodecaminxSticker[] {
stickers(turnNotation?: string): DodecaminxSticker[] {
if (!turnNotation) {
return flattenDeep(Object.values(this.state));
}

const turn = this.parse(turnNotation);
const { b, bl, br, d, dbl, dbr, dl, dr, f, l, r, u } = this.state;

// return all stickers for whole-puzzle rotations
if (turn.whole) {
return flattenDeep([b, bl, br, d, dbl, dbr, dl, dr, f, l, r, u]);
return flattenDeep(Object.values(this.state));
}

const stickers: DodecaminxSticker[] = [];
Expand Down
6 changes: 3 additions & 3 deletions src/puzzles/puzzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ export abstract class Puzzle<
}

/**
* Get stickers that are part of a turn.
* Get stickers.
*
* @param {string} turn turn to extract stickers from
* @param {string} turnNotation turn to extract stickers from
*/
abstract stickers(turn: string): Sticker[];
abstract stickers(turnNotation?: string): Sticker[];

/**
* Test if the puzzle is solved
Expand Down
7 changes: 7 additions & 0 deletions tests/puzzles/cube.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ describe('Cube', () => {
});

describe('stickers', () => {
it('all stickers', () => {
const model = new Cube({ size: 3 });
const stickers = model.stickers();

expect(stickers.length).toBe((3 ** 2) * 6);
});

it('X', () => {
const model = new Cube({ size: 3 });
const stickers = model.stickers('X');
Expand Down
7 changes: 7 additions & 0 deletions tests/puzzles/dodecaminx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ describe('Dodecaminx', () => {
});

describe('stickers', () => {
it('all stickers', () => {
const model = new Dodecaminx({ size: 3 });
const stickers = model.stickers();

expect(stickers.length).toBe(11 * 12);
});

it('u', () => {
const model = new Dodecaminx({ size: 3 });
const stickers = model.stickers('u');
Expand Down

0 comments on commit 95605bf

Please sign in to comment.