RPG attributes generator: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎JS Functional: Adjusted one function name)
(→‎JS Functional: Slight refactor – was needlessly retaining sequence of dice throws)
Line 292: Line 292:
'use strict';
'use strict';


// main :: IO ()
// main :: IO ()
const main = () =>
const main = () =>
console.log(
console.log(
unlines(
unlines(
map(
map(
xs => showJSON([sum(xs), xs]),
xs => showJSON([sum(xs), xs]),
// A sample of three character-attribute sets.
// A sample of three character-attribute sets.
take(3, character())
take(3, character())
)
)
)
);
)
);


// character :: () -> Gen [(Int, Int, Int, Int, Int, Int)]
// character :: () -> Gen [(Int, Int, Int, Int, Int, Int)]
function* character() {
function* character() {
while (true) {
while (true) {
yield until(
yield until(
xs => (75 <= sum(xs)) && (
xs => (75 <= sum(xs)) && (
2 <= length(filter(x => 15 <= x, xs))
2 <= length(filter(x => 15 <= x, xs))
),
),
sixAttributes,
sixAttributes,
sixAttributes()
sixAttributes()
)
)
}
}
}
}


// sixAttributes :: () -> (Int, Int, Int, Int, Int, Int)
// sixAttributes :: () -> (Int, Int, Int, Int, Int, Int)
const sixAttributes = () =>
const sixAttributes = () =>
map(
map(sumOfThreeHighest, enumFromTo(1, 6));
() => sum(

tail(
// sumOfThreeHighest :: () -> Int
const sumOfThreeHighest = () => {
sort(
const
map(
xs = map(
() => randomRInt(1, 6),
() => randomRInt(1, 6),
enumFromTo(1, 4)
enumFromTo(1, 4)
)))),
);
enumFromTo(1, 6)
);
return sum(_delete(minimum(xs), xs));
};



// GENERIC ABSTRACTIONS ---------------------------------
// GENERIC ABSTRACTIONS ---------------------------------

// delete :: Eq a => a -> [a] -> [a]
const _delete = (x, xs) => {
const go = xs => {
return 0 < xs.length ? (
(x === xs[0]) ? (
xs.slice(1)
) : [xs[0]].concat(go(xs.slice(1)))
) : [];
}
return go(xs);
};


// enumFromTo :: Int -> Int -> [Int]
// enumFromTo :: Int -> Int -> [Int]
Line 370: Line 356:
// map :: (a -> b) -> [a] -> [b]
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
const map = (f, xs) => xs.map(f);

// minimum :: Ord a => [a] -> a
const minimum = xs =>
0 < xs.length ? (
foldl1((a, x) => x < a ? x : a, xs)
) : undefined;


// randomRInt :: Int -> Int -> Int
// randomRInt :: Int -> Int -> Int
Line 388: Line 368:
// sum :: [Num] -> Num
// sum :: [Num] -> Num
const sum = xs => xs.reduce((a, x) => a + x, 0);
const sum = xs => xs.reduce((a, x) => a + x, 0);

// sort :: Ord a => [a] -> [a]
const sort = xs => xs.slice()
.sort((a, b) => a < b ? -1 : (a > b ? 1 : 0));

// tail :: [a] -> [a]
const tail = xs => 0 < xs.length ? xs.slice(1) : [];


// take :: Int -> [a] -> [a]
// take :: Int -> [a] -> [a]

Revision as of 20:25, 15 October 2018

Task
RPG attributes generator
You are encouraged to solve this task according to the task description, using any language you may know.

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:

  • The total of all character attributes must be at least 75.
  • At least two of the attributes must be at least 15.

However, this can require a lot of manual dice rolling. A programatic solution would be much faster.

Task

Write a program that:

  1. Generates 4 random, whole values between 1 and 6.
  2. Saves the sum of the 3 largest values.
  3. Generates a total of 6 values this way.
  4. Displays the total, and all 6 values once finished.

  • The order in which each value was generated must be preserved.
  • The total of all 6 values must be at least 75.
  • At least 2 of the values must be 15 or more.

C

Translation of: Go

<lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. include <time.h>

int compareInts(const void *i1, const void *i2) {

   int a = *((int *)i1);
   int b = *((int *)i2);
   return a - b;

}

int main() {

   int i, j, nsum, vsum, vcount, values[6], numbers[4];
   srand(time(NULL));
   for (;;) {
       vsum = 0;
       for (i = 0; i < 6; ++i) {
           for (j = 0; j < 4; ++j) {
               numbers[j] = 1 + rand() % 6;
           }
           qsort(numbers, 4, sizeof(int), compareInts);
           nsum = 0;
           for (j = 1; j < 4; ++j) {
               nsum += numbers[j];
           }
           values[i] = nsum;
           vsum += values[i];
       }
       if (vsum < 75) continue;
       vcount = 0;
       for (j = 0; j < 6; ++j) {
           if (values[j] >= 15) vcount++;
       }
       if (vcount < 2) continue;
       printf("The 6 random numbers generated are:\n");
       printf("[");
       for (j = 0; j < 6; ++j) printf("%d ", values[j]);
       printf("\b]\n");
       printf("\nTheir sum is %d and %d of them are >= 15\n", vsum, vcount);
       break;
   }
   return 0;

}</lang>

Output:

Sample run:

The 6 random numbers generated are:
[9 15 15 17 13 8]

Their sum is 77 and 3 of them are >= 15

C++

GCC 4.9.2, unoptimised. <lang cpp>#include <algorithm>

  1. include <ctime>
  2. include <iostream>
  3. include <cstdlib>
  4. include <string>

using namespace std;

int main() {

   srand(time(0));
   
   unsigned int attributes_total = 0;
   unsigned int count = 0;
   int attributes[6] = {};
   int rolls[4] = {};
   
   while(attributes_total < 75 || count < 2)
   {
       attributes_total = 0;
       count = 0;
       
       for(int attrib = 0; attrib < 6; attrib++)
       {            
           for(int roll = 0; roll < 4; roll++)
           {
               rolls[roll] = 1 + (rand() % 6);
           }
           
           sort(rolls, rolls + 4);
           int roll_total = rolls[1] + rolls[2] + rolls[3];            
                       
           attributes[attrib] = roll_total;
           attributes_total += roll_total;
           
           if(roll_total >= 15) count++;
       }
   }
   
   cout << "Attributes generated : [";
   cout << attributes[0] << ", ";
   cout << attributes[1] << ", ";
   cout << attributes[2] << ", ";
   cout << attributes[3] << ", ";
   cout << attributes[4] << ", ";
   cout << attributes[5];
   
   cout << "]\nTotal: " << attributes_total;
   cout << ", Values above 15 : " << count;
   
   return 0;

}</lang>

Output:

Sample run:

Attributes generated : [13, 13, 17, 14, 10, 16]
Total: 83, Values above 15 : 2

Factor

Works with: Factor version 0.98

<lang factor>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 )
   f [ dup valid-stats? ] [ drop stats ] do 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</lang>

Output:
Str: 9
Dex: 13
Con: 14
Int: 17
Wis: 17
Cha: 11

Total: 81
# of attributes >= 15: 2

Go

<lang go>package main

import (

   "fmt"
   "math/rand"
   "sort"
   "time"

)

func main() {

   s := rand.NewSource(time.Now().UnixNano())
   r := rand.New(s)
   for {
       var values [6]int
       vsum := 0
       for i := range values {
           var numbers [4]int
           for j := range numbers {
               numbers[j] = 1 + r.Intn(6)
           }
           sort.Ints(numbers[:])
           nsum := 0
           for _, n := range numbers[1:] {
               nsum += n
           }
           values[i] = nsum
           vsum += values[i]
       }
       if vsum < 75 {
           continue
       }
       vcount := 0
       for _, v := range values {
           if v >= 15 {
               vcount++
           }
       }
       if vcount < 2 {
           continue
       }
       fmt.Println("The 6 random numbers generated are:")
       fmt.Println(values)
       fmt.Println("\nTheir sum is", vsum, "and", vcount, "of them are >= 15")
       break
   }

}</lang>

Output:

Sample run:

The 6 random numbers generated are:
[16 15 7 14 9 15]

Their sum is 76 and 3 of them are >= 15

JavaScript

Imperative

<lang javascript>function roll() {

 const stats = {
   total: 0,
   rolls: []
 }
 let count = 0;
 for(let i=0;i<=5;i++) {
   let d6s = [];
   for(let j=0;j<=3;j++) {
     d6s.push(Math.ceil(Math.random() * 6))
   }    
   d6s.sort().splice(0, 1);
   rollTotal = d6s.reduce((a, b) => a+b, 0);
   stats.rolls.push(rollTotal);
   stats.total += rollTotal; 
 }
 
 return stats;

}

let rolledCharacter = roll();

while(rolledCharacter.total < 75 || rolledCharacter.rolls.filter(a => a >= 15).length < 2){

 rolledCharacter = roll();

}

console.log(`The 6 random numbers generated are: ${rolledCharacter.rolls.join(', ')}

Their sum is ${rolledCharacter.total} and ${rolledCharacter.rolls.filter(a => a >= 15).length} of them are >= 15`);</lang>

Output:

Sample run:

The 6 random numbers generated are:
11, 17, 12, 12, 9, 16

Their sum is 77 and 2 of them are >= 15

Functional

<lang javascript>(() => {

 'use strict';
 // main :: IO ()
 const main = () =>
   console.log(
     unlines(
       map(
         xs => showJSON([sum(xs), xs]),
         // A sample of three character-attribute sets.
         take(3, character())
       )
     )
   );
 // character :: () -> Gen [(Int, Int, Int, Int, Int, Int)]
 function* character() {
   while (true) {
     yield until(
       xs => (75 <= sum(xs)) && (
         2 <= length(filter(x => 15 <= x, xs))
       ),
       sixAttributes,
       sixAttributes()
     )
   }
 }
 // sixAttributes :: () -> (Int, Int, Int, Int, Int, Int)
 const sixAttributes = () =>
   map(
     () => sum(
       tail(
         sort(
           map(
             () => randomRInt(1, 6),
             enumFromTo(1, 4)
           )))),
     enumFromTo(1, 6)
   );
 // GENERIC ABSTRACTIONS ---------------------------------
 // enumFromTo :: Int -> Int -> [Int]
 const enumFromTo = (m, n) =>
   Array.from({
     length: 1 + n - m
   }, (_, i) => m + i)
 // filter :: (a -> Bool) -> [a] -> [a]
 const filter = (f, xs) => xs.filter(f);
 // foldl1 :: (a -> a -> a) -> [a] -> a
 const foldl1 = (f, xs) =>
   1 < xs.length ? xs.slice(1)
   .reduce(f, xs[0]) : xs[0];


 // Returns Infinity over objects without finite length
 // this enables zip and zipWith to choose the shorter
 // argument when one is non-finite, like cycle, repeat etc
 // length :: [a] -> Int
 const length = xs => xs.length || Infinity;
 // map :: (a -> b) -> [a] -> [b]
 const map = (f, xs) => xs.map(f);
 // randomRInt :: Int -> Int -> Int
 const randomRInt = (low, high) =>
   low + Math.floor(
     (Math.random() * ((high - low) + 1))
   );
 // showJSON :: a -> String
 const showJSON = x => JSON.stringify(x);
 // sum :: [Num] -> Num
 const sum = xs => xs.reduce((a, x) => a + x, 0);
 // sort :: Ord a => [a] -> [a]
 const sort = xs => xs.slice()
   .sort((a, b) => a < b ? -1 : (a > b ? 1 : 0));
 // tail :: [a] -> [a]
 const tail = xs => 0 < xs.length ? xs.slice(1) : [];
 // take :: Int -> [a] -> [a]
 // take :: Int -> String -> String
 const take = (n, xs) =>
   xs.constructor.constructor.name !== 'GeneratorFunction' ? (
     xs.slice(0, n)
   ) : [].concat.apply([], Array.from({
     length: n
   }, () => {
     const x = xs.next();
     return x.done ? [] : [x.value];
   }));
 // unlines :: [String] -> String
 const unlines = xs => xs.join('\n');
 // until :: (a -> Bool) -> (a -> a) -> a -> a
 const until = (p, f, x) => {
   let v = x;
   while (!p(v)) v = f(v);
   return v;
 };
 // MAIN ---
 return main();

})();</lang>

A sample of three character attribute sets:

[83,[16,7,14,15,13,18]]
[75,[7,16,13,12,15,12]]
[78,[10,13,11,14,15,15]]

Kotlin

<lang scala>// Version 1.2.51

import java.util.Random

fun main(args: Array<String>) {

   val r = Random()
   while (true) {
       val values = IntArray(6)
       for (i in 0..5) {
           val numbers = IntArray(4) { 1 + r.nextInt(6) }
           numbers.sort()
           values[i] = numbers.drop(1).sum()
       }
       val vsum = values.sum()
       val vcount = values.count { it >= 15 }
       if (vsum < 75 || vcount < 2) continue
       println("The 6 random numbers generated are:")
       println(values.asList())
       println("\nTheir sum is $vsum and $vcount of them are >= 15")
       break
   }

}</lang>

Output:

Sample run:

The 6 random numbers generated are:
[13, 14, 13, 15, 17, 8]

Their sum is 80 and 2 of them are >= 15

Perl 6

Works with: Rakudo Star version 2018.04.1

<lang perl6>my ( $min_sum, $hero_attr_min, $hero_count_min ) = 75, 15, 2; my @attr-names = <Str Int Wis Dex Con Cha>;

sub heroic { + @^a.grep: * >= $hero_attr_min }

my @attr; repeat until @attr.sum >= $min_sum

        and heroic(@attr) >= $hero_count_min {
   @attr = @attr-names.map: { (1..6).roll(4).sort(+*).skip(1).sum };

}

say @attr-names Z=> @attr; say "Sum: {@attr.sum}, with {heroic(@attr)} attributes >= $hero_attr_min";</lang>

Output:
(Str => 15 Int => 16 Wis => 13 Dex => 11 Con => 15 Cha => 6)
Sum: 76, with 3 attributes >= 15

PHP

<lang php><?php

$attributesTotal = 0; $count = 0;

while($attributesTotal < 75 || $count < 2) {

   $attributes = [];
   
   foreach(range(0, 5) as $attribute) {
       $rolls = [];
       
       foreach(range(0, 3) as $roll) {
           $rolls[] = rand(1, 6);
       }
       
       sort($rolls);
       array_shift($rolls);
       
       $total = array_sum($rolls);
       
       if($total >= 15) {
           $count += 1;
       }
       
       $attributes[] = $total;
   }
   
   $attributesTotal = array_sum($attributes);

}

print_r($attributes);</lang>

Python

Python: Simple

<lang python>import random random.seed() attributes_total = 0 count = 0

while attributes_total < 75 or count < 2:

   attributes = []
   for attribute in range(0, 6):
       rolls = []
       
       for roll in range(0, 4):
           result = random.randint(1, 6)
           rolls.append(result)
       
       sorted_rolls = sorted(rolls)
       largest_3 = sorted_rolls[1:]
       rolls_total = sum(largest_3)
       
       if rolls_total >= 15:
           count += 1
       
       attributes.append(rolls_total)
   attributes_total = sum(attributes)
   

print(attributes_total, attributes)</lang>

Output:

Sample run:

(74, [16, 10, 12, 9, 16, 11])

Python: Nested Comprehensions #1

<lang python>import random random.seed() total = 0 count = 0

while total < 75 or count < 2:

   attributes = [(sum(sorted([random.randint(1, 6) for roll in range(0, 4)])[1:])) for attribute in range(0, 6)]    
  
   for attribute in attributes:
       if attribute >= 15:
           count += 1
  
   total = sum(attributes)
   

print(total, attributes)</lang>

Output:

Sample run:

(77, [17, 8, 15, 13, 12, 12])

Python: Nested Comprehensions #2

With comprehensions for checking candidate values in the while expression. <lang python>import random

def compute():

   values = []
   while (sum(values) < 75                            # Total must be >= 75
          or sum(1 for v in values if v >= 15) < 2):  # Two must be >= 15
       values = [sum(sorted(random.randint(1, 6) for _ in range(4))[1:]) for _ in range(6)]
   return sum(values), values

for i in range(3):

   print(*compute())

</lang>

Output:
81 [12, 17, 9, 9, 17, 17]
75 [16, 7, 13, 12, 15, 12]
81 [15, 11, 15, 16, 10, 14]

REXX

version 1

<lang rexx>/* REXX Generates 4 random, whole values between 1 and 6. Saves the sum of the 3 largest values. Generates a total of 6 values this way. Displays the total, and all 6 values once finished.

  • /

Do try=1 By 1

 ge15=0
 sum=0
 ol=
 Do i=1 To 6
   rl=
   Do j=1 To 4
     rl=rl (random(5)+1)
     End
   rl=wordsort(rl)
   rsum.i=maxsum()
   If rsum.i>=15 Then ge15=ge15+1
   sum=sum+rsum.i
   ol=ol right(rsum.i,2)
   End
 Say ol '->' ge15 sum
 If ge15>=2 & sum>=75 Then Leave
 End

Say try 'iterations' Say ol '=>' sum Exit

maxsum: procedure Expose rl /**********************************************************************

  • Comute the sum of the 3 largest values
                                                                                                                                            • /
 m=0
 Do i=2 To 4
   m=m+word(rl,i)
   End
 Return m

wordsort: Procedure /**********************************************************************

  • Sort the list of words supplied as argument. Return the sorted list
                                                                                                                                            • /
 Parse Arg wl
 wa.=
 wa.0=0
 Do While wl<>
   Parse Var wl w wl
   Do i=1 To wa.0
     If wa.i>w Then Leave
     End
   If i<=wa.0 Then Do
     Do j=wa.0 To i By -1
       ii=j+1
       wa.ii=wa.j
       End
     End
   wa.i=w
   wa.0=wa.0+1
   End
 swl=
 Do i=1 To wa.0
   swl=swl wa.i
   End
 Return strip(swl)</lang>
Output:
I:\>rexx cast
 13 13  8 15 14 11 -> 1 74
 10  9 13  7 15  9 -> 1 63
 15 15 14 13 17 14 -> 3 88
3 iterations
 15 15 14 13 17 14 => 88

version 2

This REXX version doesn't need a sort to compute the sum of the largest three (of four) values. <lang rexx>/*REXX program generates values for six core attributes for a RPG (Role Playing Game).*/

  do  until  m>=2  &  $$>=75;      list=        /*do rolls until requirements are met. */
  $$= 0                                         /*the sum of the best  3 of 4  values. */
  m= 0                                          /*the number of values ≥ 15   (so far).*/
       do 6;                  $= 0              /*6 values (meet criteria); attrib. sum*/
            do d=1  for 4;    @.d= random(1, 6) /*roll four random dice (six sided die)*/
            $= $ + @.d                          /*also obtain their sum  (of die pips).*/
            end   /*d*/                         /* [↓]  use of MIN  BIF avoids sorting.*/
       $= $  -  min(@.1, @.2, @.3, @.4)         /*obtain the sum of the highest 3 rolls*/
       list= list  $                            /*append the above sum  ($)  to a list.*/
       $$= $$ + $                               /*add the  $  sum  to the overall sum. */
       m= m + ($>=15)                           /*get # of rolls that meet the minimum.*/
       end       /*do 6*/                       /* [↑]  gen six core attribute values. */
  end            /*until*/                      /*stick a fork in it,  we're all done. */

say 'The total for ' list " is ──► " $$', ' m " entries are ≥ 15."</lang>

output   when using the default (internal) inputs:
The total for   14 12 15 16 14 15   is ──►  86,  3  entries are ≥ 15.

version 3

A variation of version 2 <lang rexx>/*REXX program generates values for six core attributes for an RPG (Role Playing Game).*/ Do n=1 By 1 until m>=2 & tot>=75;

 slist=
 tot=0
 m=0
 Do 6
   sum=0
   Do d=1 To 4;
     cast.d=random(1,6)
     sum=sum+cast.d
     End
   min=min(cast.1,cast.2,cast.3,cast.4)
   sum=sum-min
   slist=slist sum
   tot=tot+sum
   m=m+(sum>=15)
   end
 Say 'the total for' space(slist) 'is -->' tot', 'm' entries are >= 15.'
 end

Say 'Solution found with' n 'iterations'</lang>

Output:
I:\>rexx rpg
the total for 12 14 14 13 12 9 is --> 74, 0 entries are >= 15.
the total for 15 11 13 14 10 10 is --> 73, 1 entries are >= 15.
the total for 18 12 12 11 16 10 is --> 79, 2 entries are >= 15.
Solution found with 3 iterations

Ring

<lang ring>

  1. Project  : RPG Attributes Generator

load "stdlib.ring" attributestotal = 0 count = 0 while attributestotal < 75 or count < 2

       attributes = [] 
       for attribute = 0 to 6
            rolls = [] 
            largest3 = [] 
            for roll = 0 to 4
                 result = random(5)+1
                 add(rolls,result)
            next 
            sortedrolls = sort(rolls)
            sortedrolls = reverse(sortedrolls)
            for n = 1 to 3
                 add(largest3,sortedrolls[n])
            next
            rollstotal = sum(largest3) 
            if rollstotal >= 15
               count = count + 1
            ok 
            add(attributes,rollstotal)
       next
       attributestotal = sum(attributes)

end showline()

func sum(aList)

      num = 0
      for n = 1 to len(aList)
           num = num + aList[n]
      next
      return num

func showline()

       line = "(" + attributestotal + ", ["
       for n = 1 to len(attributes)
            line = line + attributes[n] + ", "
       next
       line = left(line,len(line)-2)
       line = line + "])"
       see line + nl

</lang> Output:

(95, [14, 11, 14, 13, 16, 11, 16])

Scala

<lang scala> import scala.util.Random Random.setSeed(1)

def rollDice():Int = {

 val v4 = Stream.continually(Random.nextInt(6)+1).take(4)
 v4.sum - v4.min

}

def getAttributes():Seq[Int] = Stream.continually(rollDice()).take(6)

def getCharacter():Seq[Int] = {

 val attrs = getAttributes()
 println("generated => " + attrs.mkString("[",",", "]"))
 (attrs.sum, attrs.filter(_>15).size) match {
   case (a, b)  if (a < 75 || b < 2) => getCharacter
   case _ => attrs
 }

}

println("picked => " + getCharacter.mkString("[", ",", "]")) </lang>

generated => [13,13,12,11,10,14]
generated => [17,12,15,11,9,6]
generated => [16,9,9,11,13,14]
generated => [11,12,10,18,7,17]
picked => [11,12,10,18,7,17]

zkl

<lang zkl>reg attrs=List(), S,N; do{

  attrs.clear();
  do(6){
     abcd:=(4).pump(List,(0).random.fp(1,7));   // list of 4 [1..6] randoms
     attrs.append(abcd.sum(0) - (0).min(abcd)); // sum and substract min
  }

}while((S=attrs.sum(0))<75 or (N=attrs.filter('>=(15)).len())<2); println("Random numbers: %s\nSums to %d, with %d >= 15"

       .fmt(attrs.concat(","),S,N));</lang>
Output:
Random numbers: 15,15,7,17,10,13
Sums to 77 with 3 >= 15