NotAlpha102

Game of dice 2

Jun 7th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /*To generate random numbers from 1 to 6*/
  4. int roll_dice()
  5. {
  6.     srand(time(NULL));
  7.     int a, b;
  8.     a=rand()%7;
  9.     b=rand()%7;
  10.     return a+b;
  11. }
  12. /* pre_count holds the value from the previous roll. If there is no previous
  13.  roll or game starting from new, the default value of pre_count=0.*/
  14. int play_game(int pre_count, int wins, int loses)
  15. {
  16.     int dice=roll_dice();
  17.     /* The condition of win, either score 7 on the first run (when pre_count==0), score
  18.      11 or score the same value as in the previous run (pre_count==dice)*/
  19.     if(pre_count == dice || (pre_count == 0 && dice == 7) || dice == 11)
  20.     {
  21.         printf("\nYou win");
  22.         wins++;
  23.         char inp;
  24.         printf("\nplay again? ");
  25.         scanf("%c", &inp);
  26.         /*If you play again, call the same function again and pass the pre_count as 0 (since
  27.          you are starting a new game), and wins and loses as they are to make sure consistent
  28.          counts of wins and loses throughout the entire play.*/
  29.         if (toupper(inp)=='Y')
  30.         {
  31.             return play_game(0, wins, loses);
  32.         }
  33.         else
  34.         {
  35.             printf("\nNumber of wins: %d", wins);
  36.             printf("\nNumber of loses: %d", loses);
  37.             exit(0);
  38.  
  39.         }
  40.     }
  41.     /* The condition of losing the game. Either score 6 or 12
  42.      on the first run or score 7 on any run (when pre_count!=0)*/
  43.     else if((pre_count == 0 && dice == 6) || (pre_count == 0 && dice == 12) || (pre_count != 0 && dice == 7))
  44.     {
  45.         printf("\nYou lose");
  46.         loses++;
  47.         char inp;
  48.         printf("\nplay again? ");
  49.         scanf("%c", &inp);
  50.         if (toupper(inp)=='Y')
  51.         {
  52.             return play_game(0, wins, loses);
  53.         }
  54.         else
  55.         {
  56.             printf("\nNumber of wins: %d", wins);
  57.             printf("\nNumber of loses: %d", loses);
  58.             exit(0);
  59.         }
  60.  
  61.     }
  62.     else
  63.         /* Otherwise, take the dice value as score, which will be the pre_count
  64.         on the next value.*/
  65.     {
  66.         printf("\nYour score: %d", dice);
  67.         char inp;
  68.         /* After showing the score, prompt user to hit enter to roll the dice.
  69.         Then the game is played again (by recalling play_game()) and the dice
  70.         value becomes the pre_count on the next dice roll.*/
  71.         scanf("%c", &inp);
  72.         if(inp==10)
  73.                 return play_game(dice, wins, loses);
  74.  
  75.         }
  76.     }
  77.  
  78. main()
  79. {
  80.     printf("Press enter to roll dice: ");
  81.     while(1)
  82.     {
  83.         char ch;
  84.         scanf("%c", &ch);
  85.         if(ch==10)
  86.         /* The game is started with pre_count=0, wins=0, loses=0*/
  87.             play_game(0,0,0);
  88.     }
  89.  
  90. }
Add Comment
Please, Sign In to add comment