Created
June 25, 2018 18:26
-
-
Save thatlittlegit/a1a9e65e12ab59c18bde82ed833924c1 to your computer and use it in GitHub Desktop.
Dice-rolling program. Run with Lodash.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* [41] dice.js: Utilities for dice-rolling. Intended for large numbers of | |
* divisions in Risk. | |
*/ | |
/*** DEPENDENCIES *** | |
* lodash | |
*/ | |
/*** USAGE *** | |
* rollDice(): Run rollDice() with an array of the number of dices to roll. | |
* For example, rollDice([1, 2]) will roll a die, and two dice, and return the | |
* result. Passing a number will cast it into an array. | |
* | |
* highest(): Pick the highest element in an array. | |
*/ | |
/*** LICENSE *** | |
* This code is licensed under the Unlicense. | |
* | |
* > This is free and unencumbered software released into the public domain. | |
* > | |
* > Anyone is free to copy, modify, publish, use, compile, sell, or | |
* > distribute this software, either in source code form or as a compiled | |
* > binary, for any purpose, commercial or non-commercial, and by any | |
* > means. | |
* > | |
* > In jurisdictions that recognize copyright laws, the author or authors | |
* > of this software dedicate any and all copyright interest in the | |
* > software to the public domain. We make this dedication for the benefit | |
* > of the public at large and to the detriment of our heirs and | |
* > successors. We intend this dedication to be an overt act of | |
* > relinquishment in perpetuity of all present and future rights to this | |
* > software under copyright law. | |
* > | |
* > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
* > EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
* > MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
* > IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
* > OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
* > ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
* > OTHER DEALINGS IN THE SOFTWARE. | |
* > | |
* > For more information, please refer to <http://unlicense.org/> | |
*/ | |
/*** AUTHORS *** | |
* This code was written by thatlittlegit (thatlittlegit@outlook.com). | |
*/ | |
// code start | |
// Check for dependencies | |
if (!_.random) { | |
throw new Error('Missing dependencies for dice.js'); | |
} | |
/** | |
* Main dice-rolling function. Rolls dice as specified in `rolls` and | |
* returns the reduced dice rolls. | |
* | |
* @param {Number|Array[Number]} rolls The selection of dice rolls. | |
* @returns {Array[Number]} The result of the rolls | |
* @example | |
* rollDice(1); | |
* // => [4] | |
* rollDice([1]); | |
* // => [6] | |
* rollDice([20, 12]); | |
* // => [60, 48] | |
*/ | |
function rollDice(rolls) { | |
return (Array.isArray(rolls) ? rolls : [rolls]) | |
.map(rollCount => ( | |
Array.from(Array(rollCount)) | |
.map(() => (_.random || random)(1, 6)) | |
.reduce((x, y) => x + y) | |
)); | |
} | |
/** | |
* When given an array of values, returns the largest one. | |
* | |
* @param {Array} input The array to reduce. | |
* @returns {Array[Number]} The largest value and its corresponding index. | |
* @example | |
* highest([1, 2, 5]); | |
* // => 5 | |
* highest([5, 3, 2]); | |
* // => 5 | |
*/ | |
function highest(input) { | |
const result = Array.from(input) | |
.reduce((current, old, index) => current > old | |
? current | |
: old); | |
return [ | |
result, | |
input.indexOf(result) | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment