Advertisement
hiddenGem

Game: Virtual Craps

Jun 29th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.79 KB | None | 0 0
  1. %% Game: Virtual Craps
  2. % Plays a game of virtual craps
  3.  
  4. clc, clear
  5.  
  6.  
  7. GameEnd = 0;
  8. RollNum = 1;
  9. Total = 0;
  10. fprintf( 'Ready to try your luck at craps? \n')
  11.  
  12. while ~GameEnd
  13. [dice1,dice2,sum] = DiceToss( RollNum);
  14.     if( RollNum == 1)
  15.         switch   sum
  16.            case 2
  17.              fprintf( 'You lost. try again?\n');
  18.              GameEnd = 1;
  19.            case 3
  20.              fprintf( 'You lost. try again?\n');
  21.              GameEnd = 1;
  22.            case 7
  23.               fprintf( 'Congrats you won. Go again?\n');
  24.               GameEnd = 1;
  25.            case 11
  26.               fprintf( 'Congrats you won. Go again?\n');
  27.               GameEnd = 1;
  28.            case 12
  29.               fprintf( 'You lost. try again?\n');
  30.               GameEnd = 1;
  31.           end             % switch
  32.  
  33. Total = sum;
  34.  
  35.     else
  36.         switch  sum
  37.             case Total
  38.                 fprintf( 'Congrats you won. Go again?\n');
  39.                 GameEnd = 1;
  40.             case 7
  41.                  fprintf( 'You lost. Try again?\n');
  42.                  GameEnd = 1;
  43.         end         % switch    
  44.     end          % if statement
  45. RollNum = RollNum + 1;
  46. end         % while loop
  47.  
  48.  
  49. function [dice1,dice2,sum] = DiceToss( rolling )
  50. fprintf( '\nDice are being thrown... \n \n')
  51. %{
  52. Inputs:
  53.     Matlab will generate two random integers with a max of 6
  54. Outputs:
  55.     Two 'dice' numbers and their sum
  56. Processing:
  57.     'rolls' dice for the user which prompts the rest of the code
  58. %}
  59.  
  60. dice1 = randi( 6);      %[]number vaule of the first role
  61. dice2 = randi( 6);      %[]number vaule of the second rold
  62. sum = dice1+dice2;      %[]sum of the cdice values
  63. 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);
  64. end  %function end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement