RPG attributes generator: Difference between revisions

Added Easylang
(Added Easylang)
 
(11 intermediate revisions by 6 users not shown)
Line 1,403:
 
=={{header|Common Lisp}}==
===Mapping functions===
<syntaxhighlight lang="lisp">
(defpackage :rpg-generator
Line 1,423 ⟶ 1,424:
(return scores)))
</syntaxhighlight>
=== Loop macro ===
 
''Draft''
 
==== Note ====
 
See [https://gigamonkeys.com/book/loop-for-black-belts.html Loop for Black Belts ]
 
==== Program ====
 
<syntaxhighlight lang="lisp">;; 22.11.07 Draft
 
(defun score-jet-des ()
(loop :for resultat = (+ (random 6) 1)
:repeat 4
:sum resultat :into total
:minimize resultat :into minimum
:finally (return (- total minimum))))
 
(defun calcule-attributs-personnage ()
(loop named a
:do (loop :for score = (score-jet-des)
:repeat 6
:collect score :into scores
:sum score :into total
:count (>= score 15) :into frequence
:finally (when (and (>= total 75) (>= frequence 2))
(return-from a (values scores total))))))
</syntaxhighlight>
 
==== Execution ====
 
<pre>
(multiple-value-bind (scores total)
(calcule-attributs-personnage)
(list scores total))
</pre>
 
{{out}}
<pre>
((16 9 15 12 14 14) 80)
</pre>
 
''cyril nocton (cyril.nocton@gmail.com) w/ google translate''
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
Line 1,740 ⟶ 1,785:
 
<pre>attribs: [103, 133, 110, 130, 120, 101], sum=697, (good sum, high vals=6) - success</pre>
 
=={{header|EasyLang}}==
{{trans|BASIC256}}
<syntaxhighlight>
func rollstat .
for i to 4
h = randint 6
s += h
min = lower min h
.
return s - min
.
state$[] = [ "STR" "CON" "DEX" "INT" "WIS" "CHA" ]
len stat[] 6
repeat
sum = 0
n15 = 0
for i to 6
stat[i] = rollstat
sum += stat[i]
if stat[i] >= 15
n15 += 1
.
.
until sum >= 75 and n15 >= 2
.
for i to 6
print state$[i] & ": " & stat[i]
.
print "-------"
print "TOT: " & sum
</syntaxhighlight>
 
{{out}}
<pre>
STR: 13
CON: 11
DEX: 16
INT: 8
WIS: 17
CHA: 18
-------
TOT: 83
</pre>
 
=={{header|Factor}}==
Line 1,916 ⟶ 2,005:
{{Out}}
<pre>roll str:13 dex:15 con:14 int:8 wis:17 cha:10 (total:77) ok</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
_elements = 6
 
local fn min( a as long, b as long ) as long
long result
if ( a < b )
result = a : exit fn
else
result = b : exit fn
end if
end fn = result
 
local fn d6 as long
long result
result = 1 + int( rnd(_elements) )
end fn = result
 
local fn roll_stat as long
long result
long a = fn d6, b = fn d6, c = fn d6, d = fn d6
result = a + b + c + d - fn min( fn min( a, b ), fn min( c, d ) )
end fn = result
 
local fn DoIt
CFArrayRef statnames = @[@"Strength",@"Constitution",@"Dexterity",@"Intelligence",@"Wisdom",@"Charisma"]
long stat(_elements), n15, sum, i
BOOL acceptable = NO
randomize
do
sum = 0
n15 = 0
for i = 1 to _elements
stat(i) = fn roll_stat
sum = sum + stat(i)
if stat(i) >= 15 then n15++
next
if sum >= 75 and n15 >= 2 then acceptable = YES
until ( acceptable = YES )
for i = 1 to _elements
printf @"%12s %3ld", fn StringUTF8String( statnames[i -1] ), stat(i)
next
printf @"------------"
printf @"%13s %3ld", "Total:", sum
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Strength 17
Constitution 11
Dexterity 18
Intelligence 12
Wisdom 19
Charisma 11
------------
Total: 88
</pre>
 
 
Line 2,281 ⟶ 2,435:
79 -> [15,15,11,17,12,9]
76 -> [14,12,9,15,15,11]</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq.'''
 
In this entry, /dev/random is used as a source of entropy,
via the invocation:
<pre>
< /dev/random tr -cd '0-9' | fold -w 1 | jq -Mcnr -f rgp-attributes.jq
</pre>
where rgp-attributes.jq is a file containing the jq program shown below.
 
'''The jq program'''
<syntaxhighlight lang=jq>
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
</syntaxhighlight>
{{Output}}
''Example of a run which only one round of throwing the four dice''
<pre>
[[7,7,1,1],15]
[[1,5,3,7],15]
[[5,3,2,1],10]
[[2,4,7,5],16]
[[7,4,2,7],18]
[[3,6,2,4],13]
Sum: 87
All done.
</pre>
''Example of a run requiring more than one round of throwing the four dice''
<pre>
[[6,6,1,3],15]
[[3,3,6,5],14]
[[7,2,7,5],19]
[[6,6,5,5],17]
[[5,7,3,4],16]
[[6,1,2,5],13]
Sum: 94
continuing search ...
[[7,7,2,7],21]
[[7,4,1,6],17]
[[7,3,3,1],13]
[[7,7,6,4],20]
[[2,3,6,5],14]
[[6,1,5,2],13]]
Sum: 98
</pre>
 
=={{header|Julia}}==
Line 2,417 ⟶ 2,664:
Attribute value total: 81
Attribule count >= 15: 2 </pre>
 
=={{header|LDPL}}==
<syntaxhighlight lang="ldpl">data:
attributes is number list
i is number
attr is number
sum is number
count is number
 
procedure:
sub attribute
parameters:
result is number
local data:
min is number
n is number
i is number
procedure:
store 6 in min
for i from 0 to 4 step 1 do
get random in n
in n solve 6 * n + 1
floor n
add n and result in result
if n is less than min then
store n in min
end if
repeat
subtract min from result in result
end sub
create statement "get attribute in $" executing attribute
 
label generate-attributes
for i from 0 to 6 step 1 do
get attribute in attr
add attr and sum in sum
if attr is greater than 14 then
add count and 1 in count
end if
push attr to attributes
store 0 in attr
repeat
 
if sum is less than 75 or count is less than 2 then
display "Failed..." lf
store 0 in sum
store 0 in count
clear attributes
goto generate-attributes
end if
 
for each attr in attributes do
display attr " "
repeat
display lf "Sum: " sum lf ">14: " count lf
</syntaxhighlight>
{{out}}
<pre>
Failed...
Failed...
Failed...
Failed...
11 15 12 16 15 15
Sum: 84
>14: 4
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Line 3,978 ⟶ 4,291:
=={{header|Wren}}==
{{libheader|Wren-sort}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./sort" for Sort
 
var rand = Random.new()
Line 4,085 ⟶ 4,398:
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
 
const dice = 6;
const rolls = 4;
const stat_count = 6;
 
// requirements
const min_stat_sum = 75;
const min_high_stat_count = 2;
const high_stat_threshold = 15;
 
const RollResult = struct { stats: [stat_count]u16, total: u16 };
 
fn roll_attribute(rand: std.rand.Random) u16 {
var min_roll: u16 = dice;
var total: u16 = 0;
 
for (0..rolls) |_| {
const roll = rand.uintAtMost(u16, dice - 1) + 1;
if (min_roll > roll) {
min_roll = roll;
}
total += roll;
}
 
return total - min_roll;
}
 
fn roll_stats(rand: std.rand.Random) RollResult {
var result: RollResult = RollResult{
.stats = undefined,
.total = 0,
};
var high_stat_count: u16 = 0;
 
var i: u16 = 0;
while (i < stat_count) {
// roll a stat
result.stats[i] = roll_attribute(rand);
result.total += result.stats[i];
if (result.stats[i] >= high_stat_threshold) high_stat_count += 1;
 
// find the maximum possible total
const stats_remain = stat_count - i - 1;
const max_possible_total = result.total + dice * (rolls - 1) * stats_remain;
 
// if it is below the minimum or there are not enough stats over 15 reset
if (max_possible_total < min_stat_sum or high_stat_count + stats_remain < 2) {
i = 0;
result.total = 0;
high_stat_count = 0;
} else {
i += 1;
}
}
 
return result;
}
 
pub fn main() !void {
// Create random generator
var prng = std.rand.DefaultPrng.init(blk: {
var seed: u64 = undefined;
try std.os.getrandom(std.mem.asBytes(&seed));
break :blk seed;
});
const rand = prng.random();
 
const stats = roll_stats(rand);
const stdout = std.io.getStdOut().writer();
 
try stdout.print("Total: {}\n", .{stats.total});
try stdout.print("Stats: [ ", .{});
for (stats.stats) |stat| {
try stdout.print("{} ", .{stat});
}
try stdout.print("]\n", .{});
}
</syntaxhighlight>
{{out}}
<pre>Total: 79
Stats: [ 14 13 5 16 15 16 ]</pre>
 
 
=={{header|zkl}}==
1,983

edits