write a loop for yahtzee in matlab

To write a loop for Yahtzee in MATLAB, you can use a combination of for loops and while loops. Here's an example of how you can write a basic loop for Yahtzee:

main.m
% Number of rounds in Yahtzee
numRounds = 13;

% Loop for each round
for round = 1:numRounds
    disp(['Round ' num2str(round)]); % Display current round
    
    % Roll the dice
    dice = randi(6, [1 5]); % Generate random dice values
    
    % Loop for re-rolls
    numRolls = 1;
    while numRolls < 3
        disp(['Roll ' num2str(numRolls) ': ' num2str(dice)]); % Display current dice values
        
        % Prompt user to select dice to re-roll
        reRollDice = input('Select dice to re-roll (1-5), or press Enter to keep: ');
        
        % Update dice values based on user selection
        if ~isempty(reRollDice)
            dice(reRollDice) = randi(6, [1 numel(reRollDice)]);
        else
            break; % Exit the loop if user chooses not to re-roll
        end
        
        numRolls = numRolls + 1; % Increment the number of rolls
    end
    
    disp(['Final dice: ' num2str(dice)]); % Display the final dice values
    disp(' '); % Add a line break
end
990 chars
32 lines

In this code, the number of rounds is set to 13. Each round involves rolling the dice up to three times. The user is prompted to select which dice to re-roll. If the user chooses not to re-roll any dice, the loop breaks and moves to the next round. The final dice values for each round are displayed.

Note: This code uses the randi function to generate random dice values. You can replace it with any desired dice rolling mechanism.

Make sure to adjust the code according to your specific Yahtzee game rules and scoring.

related categories

gistlibby LogSnag