Effacer les filtres
Effacer les filtres

Help with speciality dice simulator

2 vues (au cours des 30 derniers jours)
Erik
Erik le 14 Avr 2011
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;
end
if 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!

Réponse acceptée

Matt Fig
Matt Fig le 14 Avr 2011
From what I can understand, your code could become:
function [DieResult Special] = t6(n)
D = floor(rand(1,n)*6) + 1;
DieResult = sum(D);
Special = 0; % For now...
if sum(D==1)>=(n-1) || (n==1 && D<=3)
disp('A perfect hit')
Special = 1;
elseif sum(D==6)>=2
disp('A fumble')
Special = 2;
end
  1 commentaire
Erik
Erik le 15 Avr 2011
Thanks a lot! That sure was a much simpler program, good thing you people actually know this stuff ^^

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Just for fun dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by