RPG attributes generator: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Python}}: Added nested list comprehension solution. Fixed 'while and' to 'while or')
(Added sample outputs for Python)
Line 27: Line 27:
* The total of all 6 values must be at least 75.
* The total of all 6 values must be at least 75.
* At least 2 of the values must be 15 or more.
* At least 2 of the values must be 15 or more.
<p></p>

=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<lang go>package main
Line 112: Line 112:
print(attributes_total, attributes)</lang>
print(attributes_total, attributes)</lang>

{{out}}
Sample run:
<pre>(74, [16, 10, 12, 9, 16, 11])</pre>

===Python: Nested List Comprehensions===
===Python: Nested List Comprehensions===
<lang python>import random
<lang python>import random
Line 128: Line 133:
print(total, attributes)</lang>
print(total, attributes)</lang>

{{out}}
Sample run:
<pre>(77, [17, 8, 15, 13, 12, 12])</pre>

Revision as of 13:49, 19 July 2018

RPG attributes generator is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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.

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 := 0; i < 6; i++ {
           var numbers [4]int
           for j := 0; j < 4; j++ {
               numbers[j] = 1 + r.Intn(6)
           }
           sort.Ints(numbers[:])
           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
       }
       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

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):
           rolls.append(random.randint(1, 6))
       
       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 List Comprehensions

<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])