Compare commits

...

5 Commits

Author SHA1 Message Date
Adam Piontek 98909d2296 updated mocha dependency 2022-09-18 11:33:12 -04:00
Courtney Pitcher a86e3fe61e
More solvable games 2019-08-20 12:32:23 +02:00
Courtney Pitcher 741f4a4592
More solvable deals 2019-08-07 00:39:28 +02:00
Courtney Pitcher aa444b5ce7 Added unit tests 2019-08-03 15:02:18 +02:00
Courtney Pitcher 22294a69c2
Fixed grammar 2019-07-23 18:33:20 +02:00
6 changed files with 1718 additions and 4 deletions

91
.gitignore vendored Normal file
View File

@ -0,0 +1,91 @@
# WebStorm
.idea/
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# next.js build output
.next
# nuxt.js build output
.nuxt
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/

View File

@ -9,4 +9,4 @@ You can see it in action on my [website](https://igniparoustempest.github.io/pyr
## Notes
This is probably quite a poor implementation. Please don't fault me, I am still teaching myself javascript.
This is probably quite a poor implementation. Please don't fault me, I am teaching myself javascript.

1571
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

22
package.json Normal file
View File

@ -0,0 +1,22 @@
{
"name": "javascript-pyramid-solitaire-solver",
"version": "1.0.0",
"description": "A brute force solver for pyramid solitaire written in javascript",
"main": "solver.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/IgniparousTempest/javascript-pyramid-solitaire-solver.git"
},
"author": "Courtney Pitcher",
"license": "ISC",
"bugs": {
"url": "https://github.com/IgniparousTempest/javascript-pyramid-solitaire-solver/issues"
},
"homepage": "https://github.com/IgniparousTempest/javascript-pyramid-solitaire-solver#readme",
"devDependencies": {
"mocha": "^10.0.0"
}
}

View File

@ -79,9 +79,18 @@ class MoveString {
static resetStock() {return "Reset stock.";}
}
const GameStates = Object.freeze({"won": 1, "lost": 2, "inProgress": 3});
const GameStates = Object.freeze({"won": true, "lost": false});
function solve(pyramidArray, stockArray, stockIndex, remainingStocks, moveArray) {
/**
* Solves a Pyramid solitaire game.
* @param pyramidArray The cards in the pyramid, starting in the top card. 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 remainingStocks The number of times the stock can be reset.
* @param moveArray The list of moves that have been made to get a deck in this configuration.
* @returns {*[]|[*, *]|[*, *]|[*, *]|[*, *]|*}
*/
function solve(pyramidArray, stockArray, stockIndex = 0, remainingStocks = 3, moveArray = []) {
let newMoveArray = JSON.parse(JSON.stringify(moveArray));
let pyramid = new Pyramid(pyramidArray);
@ -197,5 +206,7 @@ function solve(pyramidArray, stockArray, stockIndex, remainingStocks, moveArray)
}
// This node was useless
return [GameStates.inProgress, moveArray];
return [GameStates.lost, moveArray];
}
exports.solve = solve;

19
test.js Normal file
View File

@ -0,0 +1,19 @@
const assert = require('assert');
const solver = require('./solver');
const solvable_games = [
"4C 8C 6S 7H 4D 6C 8S JH 2S 8H 4H KC AS 6H KD 7D JS TH AH 8D 9D AD 9S QS 7S TC 2D 5C 2C 9C TD 5S QC AC KH 9H 3C 7C 3S 3H 2H JD 5H 4S 6D QD 3D KS QH JC TS 5D",
"KD 8S 9D QC JC 4H 5C 5D 4C 3H 2D 7C 6S TH KH 8C 8D AS TS AD 2S 4S TC 3C JH 3S KS 6D 6C QD 2H JS 8H QS 5H 7S AC 9S KC TD 3D 9H 7D 5S 4D 2C JD 6H QH 9C 7H AH",
"2S 5D 7S 5H 4D AS TH KS AC AD QC 7H 3H 2H KC 4S JD KD 9C 9H 5C 6S 8C QS KH 3C 8H 6C AH QD 3D 5S JC JH 9D 7C 6D JS 9S TD 2D 4C 8D 2C TS TC 8S 4H 7D QH 6H 3S",
"3C TS 3D 8S QH AS 2H JS JH KD 4S 6H 6D 4C 9H AC KH 4H 5C 9D 5H QD TD 6S 2S 7D AD 7C KC JD TH 8H 5S 8D AH 7H 4D 9S 2D 3H QS 8C KS JC TC QC 5D 6C 3S 2C 9C 7S",
"6H JS KD 8H 2H 7S TH 8C 9S 5C 6C 7D 4S 5S QC 8S 9H 2D 3C 7H TS 3S JD 6S 5D KC AC QS AH TC JC AD 5H QH KH KS 4C 4D 8D AS 4H 3D 6D JH TD QD 3H 2S 2C 9C 7C 9D"
];
it('should solve known games', () => {
assert.equal(true, true);
solvable_games.forEach(function (i) {
const array = i.split(' ');
const result = solver.solve(array.slice(0, 28), array.slice(28, 52), 0, 3, []);
assert.equal(result[0], true);
});
});