linted & formatted
This commit is contained in:
parent
cbb5405283
commit
01770ff9bd
2 changed files with 26 additions and 8 deletions
28
solver.js
28
solver.js
|
@ -95,8 +95,8 @@ class MoveString {
|
||||||
const GameStates = Object.freeze({ won: true, lost: false });
|
const GameStates = Object.freeze({ won: true, lost: false });
|
||||||
|
|
||||||
function getBestMoveArray(bestMoveArray, newMoveArray) {
|
function getBestMoveArray(bestMoveArray, newMoveArray) {
|
||||||
let bestMoveLength = bestMoveArray.filter(s => s.startsWith("Move")).length;
|
let bestMoveLength = bestMoveArray.filter((s) => s.startsWith("Move")).length;
|
||||||
let newMoveLength = newMoveArray.filter(s => s.startsWith("Move")).length;
|
let newMoveLength = newMoveArray.filter((s) => s.startsWith("Move")).length;
|
||||||
return newMoveLength > bestMoveLength ? newMoveArray : bestMoveArray;
|
return newMoveLength > bestMoveLength ? newMoveArray : bestMoveArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,7 +108,13 @@ function getBestMoveArray(bestMoveArray, newMoveArray) {
|
||||||
* @param moveArray The list of moves that have been made to get a deck in this configuration.
|
* @param moveArray The list of moves that have been made to get a deck in this configuration.
|
||||||
* @returns {*[]|([*, *, *]|[*, *, *]|[*, *, *])}
|
* @returns {*[]|([*, *, *]|[*, *, *]|[*, *, *])}
|
||||||
*/
|
*/
|
||||||
function solve(pyramidArray, stockArray, stockIndex = 0, moveArray = [], bestMoveArray = []) {
|
function solve(
|
||||||
|
pyramidArray,
|
||||||
|
stockArray,
|
||||||
|
stockIndex = 0,
|
||||||
|
moveArray = [],
|
||||||
|
bestMoveArray = []
|
||||||
|
) {
|
||||||
let newMoveArray = JSON.parse(JSON.stringify(moveArray));
|
let newMoveArray = JSON.parse(JSON.stringify(moveArray));
|
||||||
let newBestMoveArray = JSON.parse(JSON.stringify(bestMoveArray));
|
let newBestMoveArray = JSON.parse(JSON.stringify(bestMoveArray));
|
||||||
let pyramid = new Pyramid(pyramidArray);
|
let pyramid = new Pyramid(pyramidArray);
|
||||||
|
@ -147,7 +153,13 @@ function solve(pyramidArray, stockArray, stockIndex = 0, moveArray = [], bestMov
|
||||||
let newPyramidArray = JSON.parse(JSON.stringify(pyramidArray));
|
let newPyramidArray = JSON.parse(JSON.stringify(pyramidArray));
|
||||||
newPyramidArray[freeCardsIndices[i]] = 0;
|
newPyramidArray[freeCardsIndices[i]] = 0;
|
||||||
|
|
||||||
let result = solve(newPyramidArray, newStock, stockIndex, newMoveArray, newBestMoveArray);
|
let result = solve(
|
||||||
|
newPyramidArray,
|
||||||
|
newStock,
|
||||||
|
stockIndex,
|
||||||
|
newMoveArray,
|
||||||
|
newBestMoveArray
|
||||||
|
);
|
||||||
if (result[0] === GameStates.won) return result;
|
if (result[0] === GameStates.won) return result;
|
||||||
// if we didn't win from this move tree, let's grab the best move array
|
// if we didn't win from this move tree, let's grab the best move array
|
||||||
// if it's better than what we already have
|
// if it's better than what we already have
|
||||||
|
@ -158,7 +170,13 @@ function solve(pyramidArray, stockArray, stockIndex = 0, moveArray = [], bestMov
|
||||||
// Flip over a new card
|
// Flip over a new card
|
||||||
newMoveArray = JSON.parse(JSON.stringify(moveArray));
|
newMoveArray = JSON.parse(JSON.stringify(moveArray));
|
||||||
newMoveArray.push(MoveString.flipStock());
|
newMoveArray.push(MoveString.flipStock());
|
||||||
let result = solve(pyramidArray, stockArray, stockIndex + 1, newMoveArray, newBestMoveArray);
|
let result = solve(
|
||||||
|
pyramidArray,
|
||||||
|
stockArray,
|
||||||
|
stockIndex + 1,
|
||||||
|
newMoveArray,
|
||||||
|
newBestMoveArray
|
||||||
|
);
|
||||||
if (result[0] === GameStates.won) return result;
|
if (result[0] === GameStates.won) return result;
|
||||||
// if we didn't win from this move tree, let's grab the best move array
|
// if we didn't win from this move tree, let's grab the best move array
|
||||||
// if it's better than what we already have
|
// if it's better than what we already have
|
||||||
|
|
4
test.js
4
test.js
|
@ -11,8 +11,8 @@ const partial_games = [
|
||||||
];
|
];
|
||||||
|
|
||||||
const unsolvable_games = [
|
const unsolvable_games = [
|
||||||
"2D 6D AD 9S 4C 7C 7S 7D 9C 2S AC 8D 6S 6H 3C 5H QS JS 4S JH 5C AS 3H 3S AH TD 4D 5S TH 7H KS QH 6C KD 8S 2C TC JC 5D 3D 2H TS 4H JD KC KH 8H QC 8C QD 9D 9H"
|
"2D 6D AD 9S 4C 7C 7S 7D 9C 2S AC 8D 6S 6H 3C 5H QS JS 4S JH 5C AS 3H 3S AH TD 4D 5S TH 7H KS QH 6C KD 8S 2C TC JC 5D 3D 2H TS 4H JD KC KH 8H QC 8C QD 9D 9H",
|
||||||
]
|
];
|
||||||
|
|
||||||
it("should solve known games", () => {
|
it("should solve known games", () => {
|
||||||
assert.equal(true, true);
|
assert.equal(true, true);
|
||||||
|
|
Loading…
Reference in a new issue