Jump to content

Photo

Dice Formatting Woes with BASIC 7

BASIC Programming

5 replies to this topic

#1 DocFlareon OFFLINE  

DocFlareon

    Space Invader

  • 34 posts

Posted Fri Jun 9, 2017 6:51 AM

I'm building an RPG Assistant app for various systems, and I've come up on a stumbling block in Commodore BASIC v7.0 when it comes to formatting the output for rolling percentile dice in the form of 2 d10 (high die, low die). I can't figure out how to format the output the way I want it without slowing the system to a crawl via nine extra IF/THEN statements. What I want to show are dice rolls of 01-00(printed as 100). What I get instead is 0 1-0 0(printed as 100). The workaround(not using nine extra IF/THEN) that I've found has the unwanted behavior of not displaying the high die if it equals zero.

rem d100
dim va$(2):rem string array for output
x=0:rem high die
y=0:rem low die
x=int(rnd(0)*10):y=int(rnd(0)*10):rem rolling the dice
va$(0)=str$(x):va$(1)=str$(y):rem putting the dice rolls into a string array for output
vb$=str$((x*10)+y):rem workaround string
if x=0 and y=0 then print 100:else print va$(0)(1):rem print vb$



#2 carlsson OFFLINE  

carlsson

    Metagalactic Mule

  • 8,843 posts
  • Location:Västerås, Sweden

Posted Fri Jun 9, 2017 7:02 AM

Are you going to display the dice rolls separately somewhere else? Otherwise you could add the two rolls at once.

 

This code will display the range 01-100 without leading spaces:

VA$=MID$(STR$(X),2)+MID$(STR$(Y),2):IF VA$="00" THEN VA$="1"+VA$



#3 DocFlareon OFFLINE  

DocFlareon

    Space Invader

  • Topic Starter
  • 34 posts

Posted Fri Jun 9, 2017 7:30 AM

Are you going to display the dice rolls separately somewhere else? Otherwise you could add the two rolls at once.

 

This code will display the range 01-100 without leading spaces:

VA$=MID$(STR$(X),2)+MID$(STR$(Y),2):IF VA$="00" THEN VA$="1"+VA$

As the project extends along, I'll be printing die rolls where appropriate on character sheets, but for now I'm just getting the dice-rolling module working for output on the console.

 

That being said, your line of code worked rather well.  Thank you!



#4 carlsson OFFLINE  

carlsson

    Metagalactic Mule

  • 8,843 posts
  • Location:Västerås, Sweden

Posted Fri Jun 9, 2017 8:04 AM

Since you happen to know that each RND statement will generate a number 0-9, possibly you could use RIGHT$(STR$(X),1) instead of MID$. I don't know if it is any quicker though, and the MID$ variant is more universal as it just skips the leading space generated by STR$ (and PRINT, I think it uses part of the same routine).



#5 DocFlareon OFFLINE  

DocFlareon

    Space Invader

  • Topic Starter
  • 34 posts

Posted Fri Jun 9, 2017 8:58 AM

I'll tinker around a bit later on. I have it working nicely on Commodore BASIC v7 and Amiga( and HiSoft) BASIC. Now to get it to behave in Atari BASIC rev.C Atari BASIC's odd-ball string handling might cause issues.



#6 carlsson OFFLINE  

carlsson

    Metagalactic Mule

  • 8,843 posts
  • Location:Västerås, Sweden

Posted Fri Jun 9, 2017 1:06 PM

I can assert you that Atari BASIC doesn't seem to add leading spaces to numbers when printed or converted to strings.







Also tagged with one or more of these keywords: BASIC Programming

0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users