Jump to content

RPG attributes generator: Difference between revisions

m (→‎{{header|Quackery}}: Use say instead of $ '...' echo$.)
Line 2,000:
Attributes total: 86
Success!</pre>
 
=={{header|Nim}}==
Unoptimized
<lang Nim>
import random, algorithm
#Importing random to get random numbers; algorithm to get sorting functions for arrays
 
randomize() #Randomize is necessary, otherwise every run will produce the same numbers
 
#Generates 4 random values between 1 and 6
proc diceFourRolls(): array[4,int] =
for i in 0 .. 3:
result[i] = rand(1..6)
 
#Saving the sum of the 3 highest values rolled
proc sumRolls(rolls: array[4,int]): int =
var sorted = rolls
sorted.sort()
#By sorting first and then starting the iteration on 1 instead of 0, the lowest number is discarded even if it is repeated
for i in 1 .. 3:
result += sorted[i]
 
func twoFifteens(attr: var array[6,int]): bool =
attr.sort()
#Similar to the proc above, sorting means the second to last number is equal to or lower than the last
if (attr[4]<15): false
else: true
 
var sixAttr : array[6,int]
var sumAttr : int
#Loop "forever"
while true:
sumAttr = 0
#Gets 6 summed rolls, maintains their order
for i in 0 .. 5:
sixAttr[i] = sumRolls(diceFourRolls())
for j in 0 .. 5:
sumAttr += sixAttr[j]
echo "The roll sums are, in order: ", sixAttr, ", which adds to ",sumAttr
if not twoFifteens(sixAttr) or sumAttr < 75: echo "Not good enough. Rerolling"
else: break #this breaks the while loop and finishes the program
</lang>
 
Sample output: <pre>
The roll sums are, in order: [8, 10, 16, 17, 10, 11], which adds to 72
Not good enough. Rerolling
The roll sums are, in order: [13, 13, 14, 13, 10, 16], which adds to 79
Not good enough. Rerolling
The roll sums are, in order: [12, 18, 16, 13, 17, 8], which adds to 84</pre>
 
=={{header|OCaml}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.