Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

reaptide

A little code problem I can't figure out.

This topic is 6736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!