Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %% Game: Virtual Craps
- % Plays a game of virtual craps
- clc, clear
- GameEnd = 0;
- RollNum = 1;
- Total = 0;
- fprintf( 'Ready to try your luck at craps? \n')
- while ~GameEnd
- [dice1,dice2,sum] = DiceToss( RollNum);
- if( RollNum == 1)
- switch sum
- case 2
- fprintf( 'You lost. try again?\n');
- GameEnd = 1;
- case 3
- fprintf( 'You lost. try again?\n');
- GameEnd = 1;
- case 7
- fprintf( 'Congrats you won. Go again?\n');
- GameEnd = 1;
- case 11
- fprintf( 'Congrats you won. Go again?\n');
- GameEnd = 1;
- case 12
- fprintf( 'You lost. try again?\n');
- GameEnd = 1;
- end % switch
- Total = sum;
- else
- switch sum
- case Total
- fprintf( 'Congrats you won. Go again?\n');
- GameEnd = 1;
- case 7
- fprintf( 'You lost. Try again?\n');
- GameEnd = 1;
- end % switch
- end % if statement
- RollNum = RollNum + 1;
- end % while loop
- function [dice1,dice2,sum] = DiceToss( rolling )
- fprintf( '\nDice are being thrown... \n \n')
- %{
- Inputs:
- Matlab will generate two random integers with a max of 6
- Outputs:
- Two 'dice' numbers and their sum
- Processing:
- 'rolls' dice for the user which prompts the rest of the code
- %}
- dice1 = randi( 6); %[]number vaule of the first role
- dice2 = randi( 6); %[]number vaule of the second rold
- sum = dice1+dice2; %[]sum of the cdice values
- fprintf( 'Here is roll # %d:\nThe first dice is a %d\nThe second dice is a %d\nThe sum of the dice is %d\n ',rolling, dice1, dice2, sum);
- end %function end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement