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

Published on 22 June 2024 08:30 PM

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

Function roll_skip_lowest:

  • Generates a random roll of dice dice with sides sides.
  • Collects the valid die values into an array.
  • Removes the minimum value from the array.
  • Returns the sum of the remaining values.

Function rollRPGtoon:

  • Initializes an array attributes to store 6 character attributes.
  • attsum and gte15 are counters to track the sum and number of attributes that are greater than or equal to 15.

Main Loop:

  • A loop continues rolling attributes until the following conditions are met:
    • The sum of all attributes (attsum) is at least 75.
    • At least two attributes are greater than or equal to 15 (gte15 is at least 2).

Attribute Rolling:

  • For each attribute:
    • Calls roll_skip_lowest to generate a roll and remove the lowest value.
    • Stores the roll value in the attributes array.

Post-Roll Checks:

  • Calculates attsum and gte15 based on the rolled attributes.

Output:

  • Prints the rolled attributes, their sum, and the number of attributes that are greater than or equal to 15.

Usage:

  • The function rollRPGtoon is called three times to roll a new character each time.

Source code in the julia programming language

roll_skip_lowest(dice, sides) = (r = rand(collect(1:sides), dice); sum(r) - minimum(r))

function rollRPGtoon()
    attributes = zeros(Int, 6)
    attsum = 0
    gte15 = 0

    while attsum < 75 || gte15 < 2
        for i in 1:6
            attributes[i] = roll_skip_lowest(4, 6)
        end
        attsum = sum(attributes)
        gte15 = mapreduce(x -> x >= 15, +, attributes)
    end

    println("New RPG character roll: $attributes. Sum is $attsum, and $gte15 are >= 15.")
end

rollRPGtoon()
rollRPGtoon()
rollRPGtoon()


  

You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the C# programming language
You may also check:How to resolve the algorithm LU decomposition step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Vector products step by step in the Forth programming language
You may also check:How to resolve the algorithm Polymorphic copy step by step in the C# programming language
You may also check:How to resolve the algorithm Leap year step by step in the Kotlin programming language