Jump to content

Need help with Javascript random stat generator (for RPG's)

SgtBot

Hi, I've been learning a lot of javascript recently and I began programming a script that would emulate how your starting stats are rolled in Dungeons and Dragons. I've never actually played d&d, but from what my roommates were telling me, your starting stats are acquired by rolling a 6 sided dice 4 times (or rolling 4 six side dice all at once) and then taking the highest three numbers and adding them up together. I programmed that like this:

function diceRoll(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
   }

   var rollSet1 = new Array();
    rollSet1[0] = diceRoll(1, 6);
    rollSet1[1] = diceRoll(1, 6);
    rollSet1[2] = diceRoll(1, 6);
    rollSet1[3] = diceRoll(1, 6);

This simulated rolling the 4 dice.

var set1Smol = (Math.min(...rollSet1));

This set the variable: "set1Smol" equal to the smallest number out of the 4 numbers in the array.

var stat1 = (rollSet1[0] + rollSet1[1] + rollSet1[2] + rollSet1[3] - set1Smol);

And this set the variable: "stat1" equal to the sum of all 4 numbers in the array - the smallest number in the array.

I did this for each of the 7 rollSets and put the "stat(x)" variables into a separate array, and then used the Math.min operator to find the smallest number out of all 7 stats.

var totalSetSum = new Array();
    totalSetSum[0] = stat1;
    totalSetSum[1] = stat2;
    totalSetSum[2] = stat3;
    totalSetSum[3] = stat4;
    totalSetSum[4] = stat5;
    totalSetSum[5] = stat6;
    totalSetSum[6] = stat7;

   var lowestStat = (Math.min(...totalSetSum));

Now the one thing I couldn't figure out was how to get rid of the smallest number in the totalSetSum array. Right now I have it just printing all 7 stats and then telling you which one is the smallest, but what I would like to do is create a character stat object with 6 properties that are equal to the 6 highest out of the 7 stats. Does anybody know how I would go about programming that? Any help would be appreciated! Thanks!

 

Here is a link to my github where I have the full javascript program located.

Link to comment
Share on other sites

Link to post
Share on other sites

Pseudo code (not really javascript, just translate it over):

var index = 0;
for (i = 0; i < totalSumSet; i++)
	if (totalSumSet[i] < totalSumSet[index])
    	index = i;
totalSumSet.Remove(i);

 

ORANGE SCREEN WINDOWS 10 VALUE OVER TIME - PC VS MAC

Spoiler

i5 7600k @ 5.0 GHz xD

Corsair H60 with Noctua NF-F12 iPPC-3000 PWM

MSI Z270-A Pro Motherboard

EVGA 1050 Ti SC

16 GB Corsair DDR4 @ 2400 MHz

500 GB Sandisk 950 PRO - Windows 10, Elementary OS, Zorin OS

500 GB Sandisk 850 PRO

1 TB WD Blue

Corsair CX750

1 x Corsair AF120 Quiet Red Led

Rosewell Tyrfing Case

Spoiler

EliteBook 8570w
i7 3720QM @ 2.6 GHz
Quadro K1000M
24 GB DDR3 @ 1600 MHz
250 GB SanDisk 850 EVO - Elementary OS, Windows 10, Debian

Spoiler

i5 3470 @ 3.2 GHz
EVGA 750 Ti SC
8 GB DDR3 @ 1333 MHz
240 GB SanDisk - Windows 10, Linux Mint

 

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, TurbulentWinds said:

Pseudo code (not really javascript, just translate it over):


var index = 0;
for (i = 0; i < totalSumSet; i++)
	if (totalSumSet[i] < totalSumSet[index])
    	index = i;
totalSumSet.Remove(i);

 

In JavaScript you use splice.

 

totalSumSet.splice(i, 1)

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

 

That being said your code would remove everything smaller that the first item.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

13 hours ago, SgtBot said:

Hi, I've been learning a lot of javascript recently and I began programming a script that would emulate how your starting stats are rolled in Dungeons and Dragons. I've never actually played d&d, but from what my roommates were telling me, your starting stats are acquired by rolling a 6 sided dice 4 times (or rolling 4 six side dice all at once) and then taking the highest three numbers and adding them up together. I programmed that like this:


function diceRoll(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
   }

   var rollSet1 = new Array();
    rollSet1[0] = diceRoll(1, 6);
    rollSet1[1] = diceRoll(1, 6);
    rollSet1[2] = diceRoll(1, 6);
    rollSet1[3] = diceRoll(1, 6);

This simulated rolling the 4 dice.


var set1Smol = (Math.min(...rollSet1));

This set the variable: "set1Smol" equal to the smallest number out of the 4 numbers in the array.


var stat1 = (rollSet1[0] + rollSet1[1] + rollSet1[2] + rollSet1[3] - set1Smol);

And this set the variable: "stat1" equal to the sum of all 4 numbers in the array - the smallest number in the array.

I did this for each of the 7 rollSets and put the "stat(x)" variables into a separate array, and then used the Math.min operator to find the smallest number out of all 7 stats.


var totalSetSum = new Array();
    totalSetSum[0] = stat1;
    totalSetSum[1] = stat2;
    totalSetSum[2] = stat3;
    totalSetSum[3] = stat4;
    totalSetSum[4] = stat5;
    totalSetSum[5] = stat6;
    totalSetSum[6] = stat7;

   var lowestStat = (Math.min(...totalSetSum));

Now the one thing I couldn't figure out was how to get rid of the smallest number in the totalSetSum array. Right now I have it just printing all 7 stats and then telling you which one is the smallest, but what I would like to do is create a character stat object with 6 properties that are equal to the 6 highest out of the 7 stats. Does anybody know how I would go about programming that? Any help would be appreciated! Thanks!

 

Here is a link to my github where I have the full javascript program located.

I created a fiddle for you. https://jsfiddle.net/ej8bjpmv/

Open the console in your browser and press run.

CPU: i7-12700KF Grill Plate Edition // MOBO: Asus Z690-PLUS WIFI D4 // RAM: 16GB G.Skill Trident Z 3200MHz CL14 

GPU: MSI GTX 1080 FE // PSU: Corsair RM750i // CASE: Thermaltake Core X71 // BOOT: Samsung Evo 960 500GB

STORAGE: WD PC SN530 512GB + Samsung Evo 860 500GB // COOLING: Full custom loop // DISPLAY: LG 34UC89G-B

Link to comment
Share on other sites

Link to post
Share on other sites

14 hours ago, vorticalbox said:

In JavaScript you use splice.

 

totalSumSet.splice(i, 1)

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

 

That being said your code would remove everything smaller that the first item.

How so? It only removes one item. You seem not to have seen that it is pseudo code and not js that I wrote. It's not meant to be taken literally as js and just copy-pasted. The translation process from concept to code helps newer people learn by doing. 

 

var index = 0;

for (i = 0; i < totalSumSet; i++)
{
		if (totalSumSet[i] < totalSumSet[index]) 
        {
        	  index = i;
        }
}

totalSumSet.Remove(i);

Pseudo code with brackets. Only removes the smallest item.

ORANGE SCREEN WINDOWS 10 VALUE OVER TIME - PC VS MAC

Spoiler

i5 7600k @ 5.0 GHz xD

Corsair H60 with Noctua NF-F12 iPPC-3000 PWM

MSI Z270-A Pro Motherboard

EVGA 1050 Ti SC

16 GB Corsair DDR4 @ 2400 MHz

500 GB Sandisk 950 PRO - Windows 10, Elementary OS, Zorin OS

500 GB Sandisk 850 PRO

1 TB WD Blue

Corsair CX750

1 x Corsair AF120 Quiet Red Led

Rosewell Tyrfing Case

Spoiler

EliteBook 8570w
i7 3720QM @ 2.6 GHz
Quadro K1000M
24 GB DDR3 @ 1600 MHz
250 GB SanDisk 850 EVO - Elementary OS, Windows 10, Debian

Spoiler

i5 3470 @ 3.2 GHz
EVGA 750 Ti SC
8 GB DDR3 @ 1333 MHz
240 GB SanDisk - Windows 10, Linux Mint

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×