avr39ripe

cppTheDiceGame

Mar 23rd, 2021 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. int main()
  5. {
  6.     const int div{ 2 }; // count of players to calculate turn number from i
  7.     int drop{ 0 };
  8.     int playerSum{ 0 };
  9.     int computerSum{ 0 };
  10.     char space{ ' ' };
  11.     const int minValue{ 1 };
  12.     const int maxValue{ 6 };
  13.     bool comp{ false }; // who roll the dice: true - comp, false - human :)
  14.     srand(time(0));
  15.  
  16.     std::cout << "\t\t\"The Dice\"\n\nOne by one we will roll the dice,\none, who will have the biggest sum\nin three rolls will win!!!\n\t\tGood luck!!!\n\n";
  17.  
  18.     for (int i{ 1 }; i <= 6; comp = !comp, ++i)
  19.     {
  20.         std::cout << (comp ? "My " : "You ") << "roll No." << ((i / div) + (i % div)) << "\nPress \"" << (comp ? 'c' : 'd') << "\" to roll the dice ---> ";
  21.         std::cin >> space;
  22.  
  23.         while (space != (comp ? 'c' : 'd'))
  24.         {
  25.             std::cout << "Oops, wrong key! Try again ---> ";
  26.             std::cin >> space;
  27.         }
  28.  
  29.         for (int i{ 0 }; i < 2; ++i)
  30.         {
  31.             drop = rand() % maxValue + minValue;
  32.             std::cout << "* * * * *\n";
  33.             if (drop == 1) { std::cout << "*       *\n*   #   *\n*       *\n"; }
  34.             else if (drop == 2) { std::cout << "*  #    *\n*       *\n*    #  *\n"; }
  35.             else if (drop == 3) { std::cout << "*  #    *\n*   #   *\n*    #  *\n"; }
  36.             else if (drop == 4) { std::cout << "* #   # *\n*       *\n* #   # *\n"; }
  37.             else if (drop == 5) { std::cout << "* #   # *\n*   #   *\n* #   # *\n"; }
  38.             else if (drop == 6) { std::cout << "* #   # *\n* #   # *\n* #   # *\n"; }
  39.             std::cout << "* * * * *\n\n";
  40.             comp ? computerSum += drop : playerSum += drop;
  41.         }
  42.  
  43.         std::cout << (comp ? "My " : "You ") << "score is " << (comp ? computerSum : playerSum) << "\n\n";
  44.  
  45.     }
  46.  
  47.     if (playerSum > computerSum)
  48.     {
  49.         std::cout << "Congratulations!!! You are the winner!! You have " << playerSum << " while I have just " << computerSum << "\n\n";
  50.     }
  51.     else if (playerSum < computerSum)
  52.     {
  53.         std::cout << "I'm sorry =( I am the winner!! You have just " << playerSum << " while I have " << computerSum << "\n\n";
  54.     }
  55.     else
  56.     {
  57.         std::cout << "Today it's draw!! Congrats! We both have " << playerSum << "\n\n";
  58.     }
  59.  
  60.     return 0;
  61. }
Add Comment
Please, Sign In to add comment