필터 지우기
필터 지우기

Help with speciality dice simulator

조회 수: 2 (최근 30일)
Erik
Erik 2011년 4월 14일
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!

채택된 답변

Matt Fig
Matt Fig 2011년 4월 14일
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
Erik
Erik 2011년 4월 15일
Thanks a lot! That sure was a much simpler program, good thing you people actually know this stuff ^^

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by