How to resolve the algorithm RPG attributes generator step by step in the jq programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm RPG attributes generator step by step in the jq programming language
Table of Contents
Problem Statement
RPG = Role Playing Game.
You're running a tabletop RPG, and your players are creating characters. Each character has six core attributes: strength, dexterity, constitution, intelligence, wisdom, and charisma. One way of generating values for these attributes is to roll four, 6-sided dice (d6) and sum the three highest rolls, discarding the lowest roll. Some players like to assign values to their attributes in the order they're rolled. To ensure generated characters don't put players at a disadvantage, the following requirements must be satisfied: However, this can require a lot of manual dice rolling. A programatic solution would be much faster.
Write a program that:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm RPG attributes generator step by step in the jq programming language
Source code in the jq programming language
def count(s): reduce s as $x (0; .+1);
# Output: a PRN in range(0;$n) where $n is .
def prn:
if . == 1 then 0
else . as $n
| (($n-1)|tostring|length) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
# Output: [$four, $sum]
# where $four is an array of 4 pseudo-random integers between 1 and 6 inclusive,
# and $sum records the sum of the 3 largest values.
def generate_RPG:
[range(0; 4) | (1 + (7|prn) )] as $four
| [$four, ($four|sort|.[-3:]|add)];
# Input: $six as produced by [range(0;6) | generate_RPG]
# Determine if the following conditions are met:
# - the total of all 6 of the values at .[-1] is at least 75;
# - at least 2 of these values must be 15 or more.
def ok:
([.[][-1]] | add) as $sum
| $sum >= 75 and
count( (.[][1] >= 15) // empty) >= 2;
# First show [range(0;6) | generate_RPG]
# and then determine if it meets the "ok" condition;
# if not, repeat until a solution has been found.
def task:
[range(0;6) | generate_RPG] as $six
| ([$six[][-1]] | add) as $sum
| $six[], "Sum: \($sum)",
if $six | ok then "All done."
else "continuing search ...",
({}
| until(.emit;
[range(0;6) | generate_RPG] as $six
| ([$six[][-1]] | add) as $sum
| if $six | ok
then .emit = {$six, $sum}
else .
end).emit
| (.six[], "Sum: \(.sum)" ) )
end;
task
You may also check:How to resolve the algorithm Delete a file step by step in the Lingo programming language
You may also check:How to resolve the algorithm Knight's tour step by step in the Scala programming language
You may also check:How to resolve the algorithm Simulate input/Keyboard step by step in the Nim programming language
You may also check:How to resolve the algorithm Natural sorting step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Read a file character by character/UTF8 step by step in the zkl programming language