Advertisement

A little code problem I can't figure out.

Started by March 13, 2000 11:46 AM
3 comments, last by reaptide 24 years, 6 months ago
Hello everyone, I''m in the process of creating a character generator for an AD&D style game. For the character''s ability rolls, I''m using the code listed below. An example of the problem I''m having is that if you called MultiDiceRoll(3, 6) you would normally recieve scores between 3 and 18. For some reason the maximum score I get seems to be 12. The code is in Visual Basic. Think any of you could shed some light on what''s going on? I''m pulling my hair out over it, and can''t seem to figure it out! -- Begin Code ---------------------------------------------- Public Function DiceRoll(NumberOfSides As Integer) '' Rolls a dice with any number of sides. '' Declare a variable to hold the results of the dice roll. Dim RESULTS As Integer '' Generate a random number between 1 and NumberOfSides. Randomize RESULTS = Int((NumberOfSides * Rnd) + 1) '' Return results. DiceRoll = RESULTS End Function ------------------------------------------------------------ Public Function MultiDiceRoll(NumberOfDice As Integer, NumberOfSides As Integer) '' Rolls any number of dice chosen. Uses DiceRoll(). '' Declare an array to hold the dice roll results. Dim RESULTS(100) As Integer '' Declare a variable to keep count of the number of dice '' that have been rolled. Dim DICECOUNT As Integer '' Roll the required number of dice and store the results '' in RESULTS. DICECOUNT = 0 For DICECOUNT = 0 To NumberOfDice RESULTS(DICECOUNT) = DiceRoll(NumberOfSides) DICECOUNT = DICECOUNT + 1 Next '' Reset the counter. DICECOUNT = 0 '' Declare a variable to hold the dice total. Dim TOTAL As Integer '' Total the results of each dice roll. For DICECOUNT = 0 To NumberOfDice TOTAL = TOTAL + RESULTS(DICECOUNT) DICECOUNT = DICECOUNT + 1 Next '' Return TOTAL. MultiDiceRoll = TOTAL End Function -- End Code ------------------------------------------------ Thanks for any help, Reaptide
Your for loop will automatically increment your loop counter, don''t increment it yourself inside the for loop.
That is: get rid of the DICECOUNT = DICECOUNT + 1 line inside the for loop.
Advertisement
Oh crap, I can''t believe I did that.

Guess I was thinking about a C While Loop.

Thank you very much for your help, it is much appriciated.
Oops, I made a terrible mistake so I removed my answer.

SiCrane spotted the real problem though.

Edited by - Spellbound on 3/13/00 12:39:45 PM
Here you go:

function dice(a, b, c)
'' this returns adb+c (e.g. 1d4+1 for a magic missile
'' or 3d6+0 for a DEX score

total = 0
for x = 1 to a
total = total + Int((b * Rnd) + 1)
next

dice = total + c
end function

This topic is closed to new replies.

Advertisement