diff --git a/solver.js b/solver.js index 4badfa3..83cef5b 100644 --- a/solver.js +++ b/solver.js @@ -95,8 +95,8 @@ class MoveString { const GameStates = Object.freeze({ won: true, lost: false }); function getBestMoveArray(bestMoveArray, newMoveArray) { - let bestMoveLength = bestMoveArray.filter(s => s.startsWith("Move")).length; - let newMoveLength = newMoveArray.filter(s => s.startsWith("Move")).length; + let bestMoveLength = bestMoveArray.filter((s) => s.startsWith("Move")).length; + let newMoveLength = newMoveArray.filter((s) => s.startsWith("Move")).length; 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. * @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 newBestMoveArray = JSON.parse(JSON.stringify(bestMoveArray)); let pyramid = new Pyramid(pyramidArray); @@ -147,18 +153,30 @@ function solve(pyramidArray, stockArray, stockIndex = 0, moveArray = [], bestMov let newPyramidArray = JSON.parse(JSON.stringify(pyramidArray)); 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 we didn't win from this move tree, let's grab the best move array // if it's better than what we already have newBestMoveArray = getBestMoveArray(newBestMoveArray, result[2]); newBestMoveArray = JSON.parse(JSON.stringify(newBestMoveArray)); - } + } // Flip over a new card newMoveArray = JSON.parse(JSON.stringify(moveArray)); 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 we didn't win from this move tree, let's grab the best move array // if it's better than what we already have diff --git a/test.js b/test.js index f128993..3ab919a 100644 --- a/test.js +++ b/test.js @@ -11,8 +11,8 @@ const partial_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", () => { assert.equal(true, true);