5 person die roller
Mostrar comentarios más antiguos
I am trying to build a die roller, for a groupe of people, that says which of 5 people has gotten the 6. Right now the die roller tells how many rolls where made. I want it to tell which of the five persons (who roll out of turn) made the roll. So if the roll number (called i) is greater then 5 (number of players) we need to subtract 5 until it is. And then display which person made the roll and how many total rolls were made.
Code die roller:
i=0; % number of throws
while 1
i = i + 1;
dieRoll = randi(6); % gives a random number 1-6
if dieRoll < 6;
continue % continue if it's not a 6
else
break % break if it is
end
end
disp(i) %display number of throws
I have then tried to implicate an other if else statement, but out of luck:
if i > 5
while i > 5
i=i-5;
end
else
i=i
end
Respuesta aceptada
Más respuestas (3)
Mads Emil Engmarksgaard
el 5 de En. de 2017
Image Analyst
el 5 de En. de 2017
So complicated. Why not simply make and check all the rolls at once:
% Define parameters.
numRolls = 100
numPeople = 5;
% Make all the rolls in one call to randi.
rolls = randi(6, numRolls, numPeople)
% Done. Now report who got a 6 at each roll
for roll = 1 : numRolls
whoGot6 = find(rolls(roll, :) == 6);
fprintf('\nIn roll #%d, these users got a 6: ', roll);
fprintf('%d ', whoGot6);
end
Jos (10584)
el 5 de En. de 2017
A trick is to start counting from 0 and use the function REM or MOD to determine the index
numRolls = 1 ;
while (randi(6) ~= 6)
% loop until a roll was a 6
numRolls = numRolls + 1 ;
end
WhoRolled6 = rem(numRolls-1, 5) + 1 % subtract 1 to count from 0
Categorías
Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!