How to resolve the algorithm RPG attributes generator step by step in the REXX programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm RPG attributes generator step by step in the REXX 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 REXX programming language
Source code in the rexx programming language
/* REXX
Generates 4 random, whole values between 1 and 6.
Saves the sum of the 3 largest values.
Generates a total of 6 values this way.
Displays the total, and all 6 values once finished.
*/
Do try=1 By 1
ge15=0
sum=0
ol=''
Do i=1 To 6
rl=''
Do j=1 To 4
rl=rl (random(5)+1)
End
rl=wordsort(rl)
rsum.i=maxsum()
If rsum.i>=15 Then ge15=ge15+1
sum=sum+rsum.i
ol=ol right(rsum.i,2)
End
Say ol '->' ge15 sum
If ge15>=2 & sum>=75 Then Leave
End
Say try 'iterations'
Say ol '=>' sum
Exit
maxsum: procedure Expose rl
/**********************************************************************
* Comute the sum of the 3 largest values
**********************************************************************/
m=0
Do i=2 To 4
m=m+word(rl,i)
End
Return m
wordsort: Procedure
/**********************************************************************
* Sort the list of words supplied as argument. Return the sorted list
**********************************************************************/
Parse Arg wl
wa.=''
wa.0=0
Do While wl<>''
Parse Var wl w wl
Do i=1 To wa.0
If wa.i>w Then Leave
End
If i<=wa.0 Then Do
Do j=wa.0 To i By -1
ii=j+1
wa.ii=wa.j
End
End
wa.i=w
wa.0=wa.0+1
End
swl=''
Do i=1 To wa.0
swl=swl wa.i
End
Return strip(swl)
/*REXX program generates values for six core attributes for a RPG (Role Playing Game).*/
do until m>=2 & $$>=75; $$= 0; list= /*do rolls until requirements are met. */
m= 0 /*the number of values ≥ 15 (so far).*/
do 6; $= 0 /*6 values (meet criteria); attrib. sum*/
do d=1 for 4; @.d= random(1, 6) /*roll four random dice (six sided die)*/
$= $ + @.d /*also obtain their sum (of die pips).*/
end /*d*/ /* [↓] use of MIN BIF avoids sorting.*/
$= $ - min(@.1, @.2, @.3, @.4) /*obtain the sum of the highest 3 rolls*/
list= list $; $$= $$ + $ /*append $──►list; add $ to overall $$.*/
$$= $$ + $ /*add the $ sum to the overall sum. */
m= m + ($>=15) /*get # of rolls that meet the minimum.*/
end /*do 6*/ /* [↑] gen six core attribute values. */
end /*until*/ /*stick a fork in it, we're all done. */
say 'The total for ' list " is ──► " $$', ' m " entries are ≥ 15."
/*REXX program generates values for six core attributes for an RPG (Role Playing Game).*/
Do n=1 By 1 until m>=2 & tot>=75;
slist=''
tot=0
m=0
Do 6
sum=0
Do d=1 To 4;
cast.d=random(1,6)
sum=sum+cast.d
End
min=min(cast.1,cast.2,cast.3,cast.4)
sum=sum-min
slist=slist sum
tot=tot+sum
m=m+(sum>=15)
end
Say 'the total for' space(slist) 'is -->' tot', 'm' entries are >= 15.'
end
Say 'Solution found with' n 'iterations'
You may also check:How to resolve the algorithm Enforced immutability step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Equilibrium index step by step in the C programming language
You may also check:How to resolve the algorithm Text processing/Max licenses in use step by step in the PL/I programming language
You may also check:How to resolve the algorithm Sparkline in unicode step by step in the J programming language
You may also check:How to resolve the algorithm Department numbers step by step in the XPL0 programming language