RosettaCodeData/Task/RPG-attributes-generator/Ksh/rpg-attributes-generator.ksh

71 lines
1.8 KiB
Bash
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
#!/bin/ksh
# RPG attributes generator
2026-04-30 12:34:36 -04:00
# # Variables:
2023-07-01 11:58:00 -04:00
#
typeset -a attribs=( strength dexterity constitution intelligence wisdom charisma )
integer MINTOT=75 MIN15S=2
2026-04-30 12:34:36 -04:00
# # Functions:
2023-07-01 11:58:00 -04:00
#
# # Function _diceroll(sides, number, reportAs) - roll number of side-sided
# # dice, report (s)sum or (a)array (pseudo) of results
#
function _diceroll {
typeset _sides ; integer _sides=$1 # Number of sides of dice
typeset _numDice ; integer _numDice=$2 # Number of dice to roll
typeset _rep ; typeset -l -L1 _rep="$3" # Report type: (sum || array)
typeset _seed ; (( _seed = SECONDS / $$ )) ; _seed=${_seed#*\.}
typeset _i _sum ; integer _i _sum=0
typeset _arr ; typeset -a _arr
RANDOM=${_seed}
for (( _i=0; _i<_numDice; _i++ )); do
(( _arr[_i] = (RANDOM % _sides) + 1 ))
[[ ${_rep} == s ]] && (( _sum += _arr[_i] ))
done
if [[ ${_rep} == s ]]; then
echo ${_sum}
else
echo "${_arr[@]}"
fi
}
2026-04-30 12:34:36 -04:00
# # Function _sumarr(n arr) - Return the sum of the first n arr elements
2023-07-01 11:58:00 -04:00
#
function _sumarr {
2026-04-30 12:34:36 -04:00
typeset _n ; integer _n=$1
typeset _arr ; nameref _arr="$2"
typeset _i _sum ; integer _i _sum
2023-07-01 11:58:00 -04:00
2026-04-30 12:34:36 -04:00
for ((_i=0; _i<_n; _i++)); do
(( _sum+=_arr[_i] ))
done
echo ${_sum}
2023-07-01 11:58:00 -04:00
}
######
# main #
######
until (( total >= MINTOT )) && (( cnt15 >= MIN15S )); do
2026-04-30 12:34:36 -04:00
integer total=0 cnt15=0
unset attrval ; typeset -A attrval
for attr in ${attribs[*]}; do
unset darr ; typeset -a darr=( $(_diceroll 6 4 a) )
set -sK:nr -A darr
attrval[${attr}]=$(_sumarr 3 darr)
(( total += attrval[${attr}] ))
(( attrval[${attr}] > 14 )) && (( cnt15++ ))
done
2023-07-01 11:58:00 -04:00
done
for attr in ${attribs[*]}; do
2026-04-30 12:34:36 -04:00
printf "%12s: %2d\n" ${attr} ${attrval[${attr}]}
2023-07-01 11:58:00 -04:00
done
print "Attribute value total: ${total}"
print "Attribule count >= 15: ${cnt15}"