To create a while loop to roll new dice in Yahtzee, you'll need to use some basic Matlab functions. Here's an example code snippet that rolls five dice until the user is satisfied with the result:
main.m610 chars19 lines
In this code snippet, the while
loop runs indefinitely until the user decides to keep the current roll (by pressing Enter without entering any dice numbers). Inside the loop, the randi
function is used to generate a random roll of five dice, and the disp
function displays the current roll to the user. Then, the input
function prompts the user to enter a list of dice indexes to re-roll. The if
statement checks if the user wants to keep the current roll or re-roll some of the dice. If the user wants to re-roll some of the dice, the str2num
function is used to convert the input string into a list of indexes, and the randi
function is used again to generate new random values for the selected dice. Finally, the loop continues until the user decides to keep the current roll.
gistlibby LogSnag