-
Notifications
You must be signed in to change notification settings - Fork 3
/
solver.js
136 lines (116 loc) · 4.41 KB
/
solver.js
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
class Card {
constructor(cardString) {
this.rank = cardString[0];
this.suit = cardString[1];
}
/**
* Gets the face value of the card, from [0 .. 12].
* @returns {number} The value.
*/
get integerValue() {
return "A23456789TJQK".indexOf(this.rank);
}
/**
* Returns true if the other card has a face value one above or below this card.
* @param card Another card.
* @returns {boolean} True if sequential, false otherwise.
*/
isSequential(card) {
return (this.integerValue + 1) % 13 === card.integerValue || (this.integerValue - 1) % 13 === card.integerValue;
}
get toString() {
return this.rank + this.suit;
}
}
class Pyramid {
constructor(pyramidArray) {
this.array = pyramidArray;
}
get isCleared() {
for (let i = 0; i < this.array.length; i++) {
if (this.array[i] !== 0)
return false;
}
return true;
}
get freeCardIndices() {
let freeIndices = [];
for (let i = this.array.length - 1; i >= 0; i--) {
if (this.array[i] === 0)
continue;
const secondOffset = Math.floor((i - 3) / 2);
// This is the last row
if (i >= 18)
freeIndices.push(i);
// Third row
else if (i <= 17 && i >= 9 && this.array[i + 9] === 0 && this.array[i + 10] === 0)
freeIndices.push(i);
// Second row
else if (i <= 8 && i >= 3 && this.array[i + 6 + secondOffset] === 0 && this.array[i + 7 + secondOffset] === 0)
freeIndices.push(i);
// First row
else if (i <= 2 && i >= 0 && this.array[i + 3 + i] === 0 && this.array[i + 4 + i] === 0)
freeIndices.push(i);
}
return freeIndices;
}
static triangularNumber(n) {
return (n * (n + 1)) / 2;
}
}
class MoveString {
static gameWon() {return "You have won.";}
static gameLost() {return "There are no more valid moves.";}
static match(cardA, cardB) {return "Move " + cardA + " onto the stock.";}
static flipStock() {return "Draw a new stock card.";}
}
const GameStates = Object.freeze({"won": true, "lost": false});
/**
* Solves a Tri Peaks solitaire game.
* @param pyramidArray The cards in the pyramids, starting in the top-left peak. The cards are in left-to-right, then top-to-bottom order.
* @param stockArray The cards in the stock.
* @param stockIndex The index of the top stock card.
* @param moveArray The list of moves that have been made to get a deck in this configuration.
* @returns {*[]|([*, *]|[*, *]|[*, *])}
*/
function solve(pyramidArray, stockArray, stockIndex = 0, moveArray = []) {
let newMoveArray = JSON.parse(JSON.stringify(moveArray));
let pyramid = new Pyramid(pyramidArray);
// We cleared the pyramid
if (pyramid.isCleared) {
newMoveArray.push(MoveString.gameWon());
return [GameStates.won, newMoveArray];
}
// We have run out of stock cards
if (stockIndex >= stockArray.length) {
newMoveArray.push(MoveString.gameLost());
return [GameStates.lost, newMoveArray];
}
const topStock = new Card(stockArray[stockIndex]);
let freeCardsIndices = pyramid.freeCardIndices;
// match free cards with stock
for (let i = 0; i < freeCardsIndices.length; i++) {
let cardA = new Card(pyramidArray[freeCardsIndices[i]]);
if (!cardA.isSequential(topStock))
continue;
let newStock = JSON.parse(JSON.stringify(stockArray));
newStock.splice(stockIndex + 1, 0, cardA.toString);
stockIndex++;
newMoveArray = JSON.parse(JSON.stringify(moveArray));
newMoveArray.push(MoveString.match(cardA.toString, topStock.toString));
let newPyramidArray = JSON.parse(JSON.stringify(pyramidArray));
newPyramidArray[freeCardsIndices[i]] = 0;
let result = solve(newPyramidArray, newStock, stockIndex, newMoveArray);
if (result[0] === GameStates.won)
return result;
}
// Flip over a new card
newMoveArray = JSON.parse(JSON.stringify(moveArray));
newMoveArray.push(MoveString.flipStock());
let result = solve(pyramidArray, stockArray, stockIndex + 1, newMoveArray);
if (result[0] === GameStates.won)
return result;
// This node was useless
return [GameStates.lost, moveArray];
}
exports.solve = solve;