Repository navigation
Expand file tree
/
Copy pathsolver.ts
More file actions
200 lines (172 loc) · 5.37 KB
/
Copy pathsolver.ts
File metadata and controls
200 lines (172 loc) · 5.37 KB
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
type PyramidSlot = string | 0;
export type SolveResult =
| [won: true, moves: string[]]
| [won: false, moves: string[]];
class Card {
private readonly rank: string;
private readonly suit: string;
constructor(cardString: string) {
this.rank = cardString.charAt(0);
this.suit = cardString.charAt(1);
}
get integerValue(): number {
return "A23456789TJQK".indexOf(this.rank);
}
isSequential(card: Card): boolean {
const difference = Math.abs(this.integerValue - card.integerValue);
return difference === 1 || difference === 12;
}
get toString(): string {
return this.rank + this.suit;
}
}
class Pyramid {
private readonly array: readonly PyramidSlot[];
constructor(pyramidArray: readonly PyramidSlot[]) {
this.array = pyramidArray;
}
get isCleared(): boolean {
return this.array.every((card) => card === 0);
}
get mask(): number {
let mask = 0;
for (let index = 0; index < this.array.length; index++) {
if (this.array[index] !== 0) {
mask |= 1 << index;
}
}
return mask;
}
get freeCardIndices(): number[] {
const freeIndices: number[] = [];
for (let index = this.array.length - 1; index >= 0; index--) {
if (this.array[index] === 0) {
continue;
}
if (index >= 18) {
freeIndices.push(index);
} else if (index >= 9) {
if (this.array[index + 9] === 0 && this.array[index + 10] === 0) {
freeIndices.push(index);
}
} else if (index >= 3) {
const offset = Math.floor((index - 3) / 2);
if (
this.array[index + 6 + offset] === 0
&& this.array[index + 7 + offset] === 0
) {
freeIndices.push(index);
}
} else if (
this.array[index * 2 + 3] === 0
&& this.array[index * 2 + 4] === 0
) {
freeIndices.push(index);
}
}
return freeIndices;
}
}
class MoveString {
static gameWon(): string {
return "You have won.";
}
static gameLost(): string {
return "There are no more valid moves.";
}
static match(card: string): string {
return "Move " + card + " onto the stock.";
}
static flipStock(): string {
return "Draw a new stock card.";
}
}
/**
* Solves a Tri Peaks solitaire game.
*
* @param pyramidArray The 28 pyramid cards, ordered left-to-right and then top-to-bottom.
* @param stockArray The stock cards, starting with the current face-up card.
* @param stockIndex The index of the current face-up stock card.
* @param moveArray The moves made to reach the current configuration.
*/
export function solve(
pyramidArray: readonly string[],
stockArray: readonly string[],
stockIndex = 0,
moveArray: readonly string[] = [],
): SolveResult {
if (new Pyramid(pyramidArray).isCleared) {
return [true, [...moveArray, MoveString.gameWon()]];
}
if (stockIndex < 0 || stockIndex >= stockArray.length) {
return [false, [...moveArray, MoveString.gameLost()]];
}
const moves = findSolution(
pyramidArray,
stockArray,
stockIndex + 1,
new Card(stockArray[stockIndex]),
new Set<number>(),
new Map<number, number[]>(),
);
return moves === null
? [false, [...moveArray, MoveString.gameLost()]]
: [true, [...moveArray, ...moves, MoveString.gameWon()]];
}
function findSolution(
pyramidArray: readonly PyramidSlot[],
stockArray: readonly string[],
nextStockIndex: number,
topStock: Card,
deadStates: Set<number>,
freeCardsByMask: Map<number, number[]>,
): string[] | null {
const pyramid = new Pyramid(pyramidArray);
if (pyramid.isCleared) {
return [];
}
const pyramidMask = pyramid.mask;
const stateKey = pyramidMask * 512 + nextStockIndex * 16 + topStock.integerValue;
if (deadStates.has(stateKey)) {
return null;
}
let freeCardIndices = freeCardsByMask.get(pyramidMask);
if (freeCardIndices === undefined) {
freeCardIndices = pyramid.freeCardIndices;
freeCardsByMask.set(pyramidMask, freeCardIndices);
}
for (const cardIndex of freeCardIndices) {
const card = new Card(pyramidArray[cardIndex] as string);
if (!card.isSequential(topStock)) {
continue;
}
const nextPyramid = [...pyramidArray];
nextPyramid[cardIndex] = 0;
const result = findSolution(
nextPyramid,
stockArray,
nextStockIndex,
card,
deadStates,
freeCardsByMask,
);
if (result !== null) {
return [MoveString.match(card.toString), ...result];
}
}
if (nextStockIndex < stockArray.length) {
const result = findSolution(
pyramidArray,
stockArray,
nextStockIndex + 1,
new Card(stockArray[nextStockIndex]),
deadStates,
freeCardsByMask,
);
if (result !== null) {
return [MoveString.flipStock(), ...result];
}
}
deadStates.add(stateKey);
return null;
}