RPG attributes generator: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Kotlin)
Line 165: Line 165:
for roll in range(0, 4):
for roll in range(0, 4):
rolls.append(random.randint(1, 6))
result = random.randint(1, 6)
rolls.append(result)
sorted_rolls = sorted(rolls)
sorted_rolls = sorted(rolls)

Revision as of 14:09, 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

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

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