This is a sample project which plays the game of war in JavaScript.
The game operates as described on Wikipedia with the following assumptions or rules:
- When a war occurs players involved must contribute one hidden card.
- If a player cannot contribute a hidden card or complete a war they lose the game.
- If all players involved in a war cannot contribute a hidden card or complete the war, the turn ends in a stalemate. The spoils are distributed evenly amongst the players that remain in the game, starting with a random player.
- The spoils that a winner of a move receives are placed at the bottom of that player's stack, in a random order.
- A game can end in a stalemate when all players drop out of a war at once.
- The number of players must be less than or equal to half of the number of cards in play.
This project uses npm and targets Node.js version 12.13.0 or greater.
In the root directory run:
npm install
This installs the project's dependencies.
A script is included to demonstrate the game.
To use the script via npm:
npm run demo [numSuits] [numRanks] [numPlayers]
To demo a game with a full deck and two players:
npm run demo 4 13 2
Tests are written with Jest, a JavaScript testing framework.
To run the tests:
npm test
To run the tests with a coverage report:
npm test -- --coverage
Documentation is generated from inline comments with jsdoc.
To generate and serve the documentation run:
npm run docs
Then visit localhost:8080 in your browser of choice to view the documentation.