Advertisement
MeehoweCK

Untitled

Nov 2nd, 2023
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. // main.cpp
  2. #include "VolleyballSet.h"
  3.  
  4. int main() {
  5.     std::string vTeam1{ "Polska" }, vTeam2{ "Brazylia" };
  6.     VolleyballSet volleyballSet{ vTeam1, vTeam2 };
  7.     volleyballSet.play();
  8.     return 0;
  9. }
  10.  
  11. // VolleyballSet.h
  12. #pragma once
  13. #include <iostream>
  14.  
  15. class VolleyballSet {
  16.     std::string mTeam1;
  17.     std::string mTeam2;
  18.     short int mPointsRequired{ 25 };
  19.     short int mPointsTeam1{ 0 };
  20.     short int mPointsTeam2{ 0 };
  21.     bool continueSet() const;
  22.     void givePoint();
  23.     void printResult() const;
  24.     void printStatus() const;
  25. public:
  26.     VolleyballSet(const std::string& aTeam1, const std::string& aTeam2) : mTeam1(aTeam1), mTeam2(aTeam2) {}
  27.     void play();
  28. };
  29.  
  30. // VolleyballSet.cpp
  31. #include "VolleyballSet.h"
  32. #include <conio.h>
  33. #include <cstdlib>
  34. #include <ctime>
  35.  
  36. void VolleyballSet::play() {
  37.     do {
  38.         printStatus();
  39.         _getch();
  40.         givePoint();
  41.     } while (continueSet());
  42.     printResult();
  43. }
  44.  
  45. bool VolleyballSet::continueSet() const {
  46.     if (mPointsTeam1 >= mPointsRequired) {
  47.         if (mPointsTeam1 - mPointsTeam2 > 1) {
  48.             return false;
  49.         }
  50.     }
  51.     if (mPointsTeam2 >= mPointsRequired) {
  52.         if (mPointsTeam2 - mPointsTeam1 > 1) {
  53.             return false;
  54.         }
  55.     }
  56.     return true;
  57. }
  58.  
  59. void VolleyballSet::givePoint() {
  60.     srand(time(nullptr));
  61.     rand() % 2 == 0 ? ++mPointsTeam1 : ++mPointsTeam2;
  62. }
  63.  
  64. void VolleyballSet::printResult() const {
  65.     if (mPointsTeam1 > mPointsTeam2) {
  66.         std::cout << "Druzyna " << mTeam1 << " wygrywa set " << mPointsTeam1 << ':' << mPointsTeam2 << std::endl;
  67.     }
  68.     else {
  69.         std::cout << "Druzyna " << mTeam2 << " wygrywa set " << mPointsTeam2 << ':' << mPointsTeam1 << std::endl;
  70.     }
  71. }
  72.  
  73. void VolleyballSet::printStatus() const {
  74.     std::cout << mTeam1 << '\t' << mPointsTeam1 << ':' << mPointsTeam2 << '\t' << mTeam2 << std::endl;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement