How to resolve the algorithm RPG attributes generator step by step in the Factor programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm RPG attributes generator step by step in the Factor 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 Factor programming language

Source code in the factor programming language

USING: combinators.short-circuit dice formatting io kernel math
math.statistics qw sequences ;
IN: rosetta-code.rpg-attributes-generator

CONSTANT: stat-names qw{ Str Dex Con Int Wis Cha }

: attribute ( -- n )
    4 [ ROLL: 1d6 ] replicate 3 <iota> kth-largests sum ;
    
: stats ( -- seq ) 6 [ attribute ] replicate ;

: valid-stats? ( seq -- ? )
    { [ [ 15 >= ] count 2 >= ] [ sum 75 >= ] } 1&& ;
    
: generate-valid-stats ( -- seq )
    stats [ dup valid-stats? ] [ drop stats ] until ;
    
: stats-info ( seq -- )
    [ sum ] [ [ 15 >= ] count ] bi
    "Total: %d\n# of attributes >= 15: %d\n" printf ;
    
: main ( -- )
    generate-valid-stats dup stat-names swap
    [ "%s: %d\n" printf ] 2each nl stats-info ;
    
MAIN: main


  

You may also check:How to resolve the algorithm Harshad or Niven series step by step in the OCaml programming language
You may also check:How to resolve the algorithm Terminal control/Dimensions step by step in the Axe programming language
You may also check:How to resolve the algorithm Padovan sequence step by step in the Julia programming language
You may also check:How to resolve the algorithm Bitmap/Histogram step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Approximate equality step by step in the Lua programming language