home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / homonlib.zip / PARSEDIE.BAS < prev    next >
BASIC Source File  |  1995-04-13  |  3KB  |  86 lines

  1. DEFINT A-Z
  2.  
  3. DECLARE FUNCTION ParseDie$ (die$)
  4.  
  5.  
  6. 'External procedures:
  7.  
  8. DECLARE FUNCTION Dice (n, d, a)
  9. DECLARE FUNCTION Istr$ (i)
  10.  
  11. FUNCTION ParseDie$ (die$)
  12. '****************************************************************************
  13. 'This function takes a string in "standard gaming notation" and returns a
  14. ' string of the stated dice roll's value.
  15. '
  16. 'If the die$ string begins with an asterisk (*), ParseDie$() will interpret
  17. ' it as a constant (non-random value) and will return whatever follows the
  18. ' asterisk.
  19. '
  20. 'If an illegal character is found in the string, the return value will begin
  21. ' with an at-sign (@) followed by the string position of the offending
  22. ' character.  The following characters are recognized by ParseDie$():
  23. '
  24. '                  *0123456789dD+- (spaces are ignored)
  25. '
  26. 'Remember that the Dice() function accepts only integer arguments and returns
  27. ' and integer.  If the result of the die roll (or any of its parts) exceeds
  28. ' the limits of an integer variable (-32,768 to 32,767) a run-time error will
  29. ' occur.  If you need more, just rewrite these functions using long integers.
  30. '
  31. '    Examples: ParseDie$("3d6")   -->  "3" to "18"
  32. '              ParseDie$("1d4+1") -->  "2" to "5"
  33. '              ParseDie$("*15")   -->  "15"
  34. '              ParseDie$("2d3.1") -->  "@4"  (Illegal character in 4th pos.)
  35. '              ParseDie$("")      -->  "0"
  36. '              ParseDie$("  ")    -->  "1" to "4"  (Defaults to 1d4)
  37. '              ParseDie$("2d50000")    Crash!
  38. '
  39. '****************************************************************************
  40.  
  41. IF die$ = "" THEN                                 'Blank?
  42.      ParseDie$ = "0"
  43.      EXIT FUNCTION
  44. END IF
  45.  
  46. x = INSTR(die$, "*")                              'Constant?
  47. IF x > 0 THEN
  48.      ParseDie$ = MID$(die$, x + 1)
  49.      EXIT FUNCTION
  50. END IF
  51.  
  52. n$ = ""        'Separate the string into the Number of rolls,
  53. d$ = ""        'the number of sides on the Die,
  54. a$ = ""        'and the amount to Add to each roll.
  55.  
  56. nda = 1        'Keep track of which one we're currently building.
  57.  
  58. FOR x = 1 TO LEN(die$)
  59.      x$ = MID$(die$, x, 1)
  60.      SELECT CASE ASC(x$)
  61.           CASE 48 TO 57                           '0-9
  62.                IF nda = 1 THEN n$ = n$ + x$
  63.                IF nda = 2 THEN d$ = d$ + x$
  64.                IF nda = 3 THEN a$ = a$ + x$
  65.           CASE 68, 100                            'd/D
  66.                nda = 2
  67.           CASE 43, 45                             '+/-
  68.                nda = 3
  69.                a$ = x$
  70.           CASE 32                                 'space
  71.                'Ignore it
  72.           CASE ELSE                               'Invalid character. Return
  73.                ParseDie$ = "@" + Istr$(x)         'the location of the
  74.                EXIT FUNCTION                      'offending character.
  75.      END SELECT
  76. NEXT x
  77.  
  78. IF LEN(n$) THEN n = VAL(n$) ELSE n = 1            'Default to 1d4+0
  79. IF LEN(d$) THEN d = VAL(d$) ELSE d = 4
  80. IF LEN(a$) THEN a = VAL(a$) ELSE a = 0
  81.  
  82. ParseDie$ = Istr$(Dice(n, d, a))                  'Get the value.
  83.  
  84. END FUNCTION
  85.  
  86.