-
Notifications
You must be signed in to change notification settings - Fork 1
/
Scrambler.ts
203 lines (191 loc) · 6.15 KB
/
Scrambler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Purpose: Generate scrambles for various WCA puzzles
enum PuzzleType {
'2x2' = '2x2',
'3x3' = '3x3',
'4x4' = '4x4',
'5x5' = '5x5',
'6x6' = '6x6',
'7x7' = '7x7',
'pyraminx' = 'pyraminx',
'megaminx' = 'megaminx',
'skewb' = 'skewb',
'clock' = 'clock',
}
class ScrambleGenerator {
private active: Set<string> = new Set<string>(); // Set to keep track of active moves
// Map to store opposite moves for intersection removal
private opposite: { [key: string]: string } = {
U: 'D',
D: 'U',
L: 'R',
R: 'L',
F: 'B',
B: 'F',
};
// Generate a random number within a specified limit
private randomNum(limit: number): number {
return Math.floor(Math.random() * limit);
}
// Remove intersecting moves and their opposites from the active set
private removeIntersections(curr: string): void {
this.active.forEach((move) => {
if (move !== curr && move !== this.opposite[curr]) this.active.delete(move);
});
}
// Generate a scramble for a given puzzle type
public generateScramble(type: PuzzleType): string[] {
switch (type) {
case PuzzleType['2x2']:
return this.standardScramble('small', 12);
case PuzzleType['3x3']:
return this.standardScramble('small', 25);
case PuzzleType['4x4']:
return this.standardScramble('medium', 40);
case PuzzleType['5x5']:
return this.standardScramble('medium', 60);
case PuzzleType['6x6']:
return this.standardScramble('large', 85);
case PuzzleType['7x7']:
return this.standardScramble('large', 95);
case PuzzleType.pyraminx:
return this.pyraminxScramble();
case PuzzleType.megaminx:
return this.megaminxScramble();
case PuzzleType.skewb:
return this.skewbScramble();
case PuzzleType.clock:
return this.clockScramble();
}
}
// Get the list of available puzzle types
public getScrambleTypes() {
return Object.keys(PuzzleType).map((key) => PuzzleType[key]);
}
// Generate a standard scramble for a given size and length
private standardScramble(size: string, length: number): string[] {
const moves = ['U', 'D', 'L', 'R', 'F', 'B'];
const endings = size === 'small' ? ['', "'", '2'] : ['', "'", '2', 'w', "w'", 'w2'];
const n = moves.length;
const m = endings.length;
const scramble: string[] = [];
let count = 0;
while (count < length) {
const randMove = moves[this.randomNum(n)];
if (
(scramble[count - 1] &&
(scramble[count - 1].startsWith(randMove) ||
(size !== 'large' ? false : scramble[count - 1].startsWith('3' + randMove)))) ||
this.active.has(randMove)
)
continue;
this.active.add(randMove);
const num = this.randomNum(m);
scramble.push(
(size === 'large' && num >= 3 && Math.random() < 0.5 ? '3' : '') + randMove + endings[num]
);
this.removeIntersections(randMove);
count++;
}
return scramble;
}
//generate scramble for pyraminx
private pyraminxScramble(): string[] {
const moves = ['U', 'L', 'R', 'B', 'u', 'l', 'r', 'b'];
const endings = ['', "'", '2'];
const movedTips = new Set<string>();
const n = moves.length;
const m = endings.length;
const scramble: string[] = [];
let count = 0;
while (count < 25) {
const randMove = moves[this.randomNum(n)];
if (
(scramble[count - 1] && scramble[count - 1].startsWith(randMove)) ||
movedTips.has(randMove)
)
continue;
if (/^[ulrb]$/.test(randMove)) movedTips.add(randMove);
scramble.push(randMove + endings[this.randomNum(m)]);
count++;
}
return scramble;
}
//generate scramble for megaminx
private megaminxScramble(): string[] {
const moves = ['U', 'R', 'D'];
const active = {
U: false,
D: false,
};
const n = moves.length;
const scramble: string[] = [];
let count = 0;
while (count < 70) {
const randMove = moves[this.randomNum(n)];
if (
(scramble[count - 1] && scramble[count - 1].startsWith(randMove)) ||
active[randMove as keyof typeof active]
)
continue;
if (randMove in active) active[randMove as keyof typeof active] = true;
else if (randMove === 'R') {
active['U'] = false;
active['D'] = false;
}
scramble.push(
randMove +
(randMove === 'U' ? (Math.random() < 0.5 ? "'" : '') : Math.random() < 0.5 ? '++' : '--')
);
count++;
}
return scramble;
}
//generate scramble for skewb
private skewbScramble(): string[] {
const moves = ['U', 'L', 'R', 'B'];
const n = moves.length;
const scramble: string[] = [];
let count = 0;
while (count < 12) {
const randMove = moves[this.randomNum(n)];
if (scramble[count - 1] && scramble[count - 1].startsWith(randMove)) continue;
scramble.push(randMove + (Math.random() < 0.5 ? "'" : ''));
count++;
}
return scramble;
}
//generate scramble for clock
private clockScramble(): string[] {
const pins = ['UR', 'DR', 'DL', 'UL'];
const backMoves = ['U', 'R', 'D', 'L', 'ALL'];
const frontMoves = pins.concat(backMoves);
const randomClockScrambleString = (): string[] => {
let filteringMoveCount = 0;
const randomSuffix = (): string => {
const amount = this.randomNum(12);
if (amount !== 0) filteringMoveCount++;
if (amount <= 6) return `${amount}+`;
else return `${12 - amount}-`;
};
const moves: string[] = [];
const side = (families: string[]): void => {
for (const family of families) {
moves.push(`${family}${randomSuffix()}`);
}
};
side(frontMoves);
moves.push('y2');
side(backMoves);
if (filteringMoveCount < 2) return randomClockScrambleString();
for (const pin of pins) {
if (this.randomNum(2) === 0) moves.push(pin);
}
return moves;
};
return randomClockScrambleString();
}
}
// Create a new instance of the ScrambleGenerator class
const scrambler = new ScrambleGenerator();
// Export the ScrambleGenerator class and the PuzzleType enum
export { scrambler, PuzzleType };