Hi, everyone!
Have you ever played “Vampire: The Masquerade”? I did in my younger years. Trying to come up with an exercise that made me use most of what I’ve learned so far in the JavaScript course, I thought of writing a piece of code that could help players of that game to resolve ‘moves’ which involved rolling dices.
For those of you who don’t know the game, in order to overcome certain challenges which are met along the story that the player partakes in, that player must roll a particular amount of ten-sided dices against a specific difficulty (which is set by the narrator of the game) to check if the ‘move’ is successful or not.
I’ve written this that follows, and I believe it works kinda good enough for the explained purpose. Now, as always, any input will be so, so welcome
// Roll the dice calculator 'Vampire: The Masquerade' style
let value;
let result = [];
let totalSuccess = [];
const rollDices = (dices, difficulty) => {
function getValue () {
value = Math.floor(Math.random() * 9)
console.log(value); // Returns a random number among 0 and 9 and stores it as 'value'
return value;
}
for (let i = 0; i < dices; i++) {
result.push(getValue());
console.log(result) // Runs 'getValue()' as many times as dices have been determined thru the parameters of the function 'rollDices' and pushes each one of the results into the 'result' array.
};
result.forEach(number => {
let success = 0;
if (number === 1) {
success--;
success--;
} else if (number === 0) {
success++;
success++;
} else if (number >= difficulty) {
success = 1;
} else {
success = 0;
}
console.log(success);
/*
A specific score is set according to the result of every 'dice' rolled:
» 1 means a terrible failure, so it subtracts 2 points to the final 'success' score;
» 0 means 10, which is an awesome success (thus, it adds up 2 point to the final 'success' score);
» If the result of the 'dice' is equal or greater than the difficulty set, it is considered a 'simple' success (thus, it adds up 1 point to the final 'success' score);
» Other than that, it means nothing (no changes to the final success score)
*/
return totalSuccess.push(success)
});
console.log(totalSuccess); // An array is created with the values associated to the different possibilities of success or failure as indicated formerly. Now we're going to 'reduce' that array to a single value that will finally indicate the success degree of the player's 'move'.
const accumulatedSuccess = totalSuccess.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
});
let successDegree;
if (accumulatedSuccess < 0) {
successDegree = 'Awful! Everything goes opposite of what it was expected!';
} else if (accumulatedSuccess === 0) {
successDegree = "You didn't make it!";
} else if (accumulatedSuccess === 1 ) {
successDegree = 'You barely made it... but yet, you did!';
} else if (accumulatedSuccess === 2) {
successDegree = 'Not too bad, but you could have done so much better!';
} else if (accumulatedSuccess === 3) {
successDegree = 'Kinda impressive... you got it quite right!';
} else if (accumulatedSuccess === 4) {
successDegree = 'Wow. Almost excellent!';
} else if (accumulatedSuccess >= 5) {
successDegree = 'Boom! Critical damage!';
}
return successDegree;
}
console.log(rollDices(2,9));
So that’s that. What do you think?
Cheers!