NotAlpha102

Game of dice

Jun 7th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 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(1);
  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.     printf("%d", dice);
  18.     /* The condition of win, either score 7 on the first run (when pre_count==0), score
  19.      11 or score the same value as in the previous run (pre_count==dice)*/
  20.     if(pre_count == dice || (pre_count == 0 && dice == 7) || dice == 11)
  21.     {
  22.         printf("You win\n");
  23.         wins++;
  24.         char inp;
  25.         printf("\nplay again? ");
  26.         scanf("%c", &inp);
  27.         /*If you play again, call the same function again and pass the pre_count as 0 (since
  28.          you are starting a new game), and wins and loses as they are to make sure consistent
  29.          counts of wins and loses throughout the entire play.*/
  30.         if (toupper(inp)=='Y')
  31.         {
  32.             return play_game(0, wins, loses);
  33.         }
  34.         else
  35.         {
  36.             printf("Number of wins: %d\n", wins);
  37.             printf("Number of loses: %d\n", loses);
  38.             return 0;
  39.  
  40.         }
  41.     }
  42.     /* The condition of losing the game. Either score 6 or 12
  43.      on the first run or score 7 on any run (when pre_count!=0)*/
  44.     else if((pre_count == 0 && dice == 6) || (pre_count == 0 && dice == 12) || (pre_count != 0 && dice == 7))
  45.     {
  46.         printf("You lose\n");
  47.         loses++;
  48.         char inp;
  49.         printf("\nplay again? ");
  50.         scanf("%c", &inp);
  51.         if (toupper(inp)=='Y')
  52.         {
  53.             return play_game(0, wins, loses);
  54.         }
  55.         else
  56.         {
  57.             printf("Number of wins: %d\n", wins);
  58.             printf("Number of loses: %d\n", loses);
  59.             return 0;
  60.         }
  61.  
  62.     }
  63.     else
  64.         /* Otherwise, take the dice value as score, which will be the pre_count
  65.         on the next value.*/
  66.     {
  67.         printf("Your score: %d", &dice);
  68.         char inp;
  69.         /* After showing the score, prompt user to hit enter to roll the dice.
  70.         Then the game is played again (by recalling play_game()) and the dice
  71.         value becomes the pre_count on the next dice roll.*/
  72.         scanf("%c", &inp);
  73.         if(inp==10)
  74.                 return play_game(dice, wins, loses);
  75.  
  76.         }
  77.     }
  78.  
  79. main()
  80. {
  81.     printf("Press enter to roll dice: ");
  82.     while(1)
  83.     {
  84.         char ch;
  85.         scanf("%c", &ch);
  86.         if(ch==10)
  87.         /* The game is started with pre_count=0, wins=0, loses=0*/
  88.             play_game(0,0,0);
  89.     }
  90.  
  91. }
Add Comment
Please, Sign In to add comment