Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test the length of all the characters in a list and adjust the location of Output( based on the length?

I wrote a program to roll stats for D&D 5e characters. It rolls 4d6 and drops the lowest and adds that value to a list. It repeats this for a total of 6 times and then outputs the result to the screen. I want to be able to center the output so it looks nice and remove the {} brackets around the list as well as the commas between numbers. The problem I'm running into with this is I can't just output a blank space to all the spots because everything after the second character is variable in what it is.

Here's the code I'm using.

0→dim(∟STATS
For(I,1,6,1
0→dim(∟ROLLS
randInt(1,6,4)→∟ROLLS
1+sum(not(cumSum(∟ROLLS=min(∟ROLLS))))→X
∟ROLLS(X)-min(∟ROLLS)→∟ROLLS(X)
sum(∟ROLLS)→∟STATS(1+dim(∟STATS
End
ClrHome
While not(getKey
Output(5,4,∟STATS
End
ClrHome

An example output looks like this:

___{8,10,9,10,9,15}_______| 

Another one might look like this

___{13,9,14,11,9,10}______|

(| represents the end of the screen, _ represents spaces but shown for width reasons)

Notice it always starts in the one spot but the rest os in varying locations.

In case it matters for the sake of screen size this is the TI-84 Plus CE

like image 845
Himitsu_no_Yami Avatar asked Sep 16 '19 23:09

Himitsu_no_Yami


Video Answer


1 Answers

Strings are your friend!

I spoke with a friend on this and she gave me the idea to store the list elements into a string and then output the string instead of the list. The trick was figuring out how to store the numbers in a string. I found the toString( function which helped with that.

Here's the working code snippet.

toString(∟STATS(1))→Str0
For(I,2,6,1
Str0+" "+toString(∟STATS(I))→Str0
End

That gave me the string of numbers and spaces formatted nicely. My last issue was how to center it where again my friend came to my rescue and suggested getting the length of the string /2 as the column coordinate. I tried using that and was a bit off center so I subtracted 2 to get a column. The way I did this is in the code below.

ClrHome
While not(getKey
Output(5,(int(length(Str0)/2-2)),Str0
End

So the full working code is

0→dim(∟STATS
For(I,1,6,1
0→dim(∟ROLLS
randInt(1,6,4)→∟ROLLS
1+sum(not(cumSum(∟ROLLS=min(∟ROLLS))))→X
∟ROLLS(X)-min(∟ROLLS)→∟ROLLS(X)
sum(∟ROLLS)→∟STATS(1+dim(∟STATS
End
toString(∟STATS(1))→Str0
For(I,2,6,1
Str0+" "+toString(∟STATS(I))→Str0
End
ClrHome
While not(getKey
Output(5,(int(length(Str0)/2-2)),Str0
End
ClrHome
SetUpEditor
like image 60
Himitsu_no_Yami Avatar answered Sep 30 '22 11:09

Himitsu_no_Yami