OK, so I'm trying my best to make a dice simulator for a role playing game I'm going to be hosting, which will plug into various other time-saving programs later on. This is the basics:
n number of six-sided dice are rolled and the results are added together. If all the dice except one (at most) are ones, that counts as a "perfect" hit. (If only one die is rolled, either a one, two or three counts as a perfect hit.) If two or more dice are sixes, that counts as a "fumble".
Now, I'm kinda new to programming, and have only taken a smaller course in it, and I hold no illusions to my version of the simulator being the best there is, or even just GOOD! If you have a better way of doing it, I don't mind you telling me =P
Here's the code for the dice function:
function [DieResult Special] = t6(n)DieResult = 0;NumberOnes = 0;NumberTwos = 0;NumberThrees = 0;NumberSixes = 0;for k=1:n % Rolls the dice
DieRoll = floor(rand()*6) + 1; if DieRoll == 1 NumberOnes = NumberOnes + 1; elseif DieRoll == 2 NumberTwos = NumberTwos + 1; elseif DieRoll == 3 NumberThrees = NumberThrees + 1; elseif DieRoll == 6 NumberSixes = NumberSixes + 1; end DieResult = DieResult + DieRoll;endif NumberOnes >= n - 1 && n >= 2 % Check fumble / perfect hit
Special = 1;elseif NumberOnes == 1 || NumberTwos == 1 || NumberThrees == 1 && n == 1 Special = 1;elseif NumberSixes >= 2 Special = 2;else Special = 0;end
Now, I seem to be getting far too many perfect hits. For example, I can get a result of 8 (perfect) when only rolling two dice (n=2), which should not be possible. I've tried reading through all the code in search of what I've done wrong, but I can't find it!
Thanks in advance, any help is greatly appreciated!
Best Answer