RPG attributes generator: Difference between revisions

Added Easylang
(Added Easylang)
 
(4 intermediate revisions by 4 users not shown)
Line 1,430:
==== Note ====
 
See [https://gigamonkeys.com/book/loop-for-black-belts.html Loop for Black Belts ]
 
==== Program ====
Line 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 2,620 ⟶ 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 4,181 ⟶ 4,291:
=={{header|Wren}}==
{{libheader|Wren-sort}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./sort" for Sort
 
var rand = Random.new()
Line 4,288 ⟶ 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,995

edits