Advertisement
DMG

thread::Football

DMG
May 2nd, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. /*
  2.     Author: Dragutin Marjanovic
  3.  
  4. Write program for modeling action of Real Madrid C.F.
  5. Team has 11 players. Behave of players is given with function 'player'.
  6. Player passes the ball to each other which is represented with the class 'Ball'.
  7.  
  8. Player has to wait to get ball. When he gets ball, he keeps it random time (between 1 and 3 seconds) and then he gives the ball to another player. Ball can go forward or backward. 2/3 of balls go forward.
  9.  
  10. After 12 passes, Real Madrid scores and program ends.
  11. If during the pass player scores, method 'pass_the_ball' returns jersey number.
  12. After a goal is scored, for all other players method returns 0.
  13. If goal isn't scored it returns -1.
  14.  
  15. Real Madrid never loses posseion of ball. */
  16.  
  17. #include <iostream>
  18. #include <ctime>
  19.  
  20. #include <thread>
  21. #include <mutex>
  22. #include <condition_variable>
  23. #include <vector>
  24.  
  25. using namespace std;
  26.  
  27. #define     NULL    0
  28. #define     PLAYERS 11
  29.  
  30.  
  31. int my_random(int low, int high) {
  32.    srand(time(NULL));
  33.    return rand() % (high - low + 1) + low;
  34. }
  35.  
  36. vector<int> player_numbers = {1, 2, 4, 7, 8, 10, 11, 12, 15, 19, 23};
  37.  
  38. vector<string> player_names = {
  39.     "Iker Casillas",
  40.     "Raphael Varane",
  41.     "Sergio Ramos (C)",
  42.     "Cristiano Ronaldo",
  43.     "Toni Kroos",
  44.     "James Rodriguez",
  45.     "Gareth Bale",
  46.     "Marcelo Vieira",
  47.     "Daniel Carvajal",
  48.     "Luka Modric",
  49.     "Alarcon Isco"
  50. };
  51.  
  52. class Ball {
  53. private:
  54.     int number_of_passes;
  55.     bool free;
  56.     bool goal;
  57.  
  58.     mutex m;
  59.     condition_variable pass;
  60.  
  61. public:
  62.     Ball(): number_of_passes(0), free(true), goal(false) {}
  63.  
  64.     int pass_the_ball(int number) {
  65.         unique_lock<mutex> l(m);
  66.  
  67.         if (!free && !goal) {
  68.             pass.wait(l);
  69.         }
  70.  
  71.         if (goal) return 0;
  72.  
  73.         free = false;
  74.  
  75.         l.unlock();
  76.         this_thread::sleep_for(chrono::seconds(my_random(1, 3)));
  77.         l.lock();
  78.  
  79.         if (my_random(1, 3) > 1) {
  80.             number_of_passes++;
  81.         }
  82.  
  83.         int next_number;
  84.         do {
  85.             next_number = my_random(1, 11);
  86.         }
  87.         while (number == next_number);
  88.  
  89.         free = true;
  90.  
  91.         if (number_of_passes == 12) {
  92.             goal = true;
  93.             pass.notify_all();
  94.             return next_number;
  95.         }
  96.         else {
  97.             pass.notify_one();
  98.             return -1;
  99.         }
  100.     }
  101. };
  102.  
  103. mutex term_m;
  104. void player(Ball &ball, int number) {
  105.     {
  106.         unique_lock<mutex> l(term_m);
  107.         cout << player_numbers[number] << " " << player_names[number] << " went into stadium." << endl;
  108.     }
  109.     int scorer = -1;
  110.     do {
  111.         scorer = ball.pass_the_ball(number);
  112.         {
  113.             unique_lock<mutex> l(term_m);
  114.             cout << player_numbers[number] << " " << player_names[number] << " passes the ball." << endl;
  115.         }
  116.         this_thread::sleep_for(chrono::seconds(1));
  117.  
  118.         if (scorer > 0) {
  119.             unique_lock<mutex> l(term_m);
  120.             cout << endl << "GOOOOAAAL! " << player_names[scorer] << endl;
  121.         }
  122.     } while (scorer == -1);
  123. }
  124.  
  125. int main() {
  126.  
  127.     Ball ball;
  128.     thread* threads[PLAYERS];
  129.  
  130.     for (unsigned i = 0; i < PLAYERS; ++i) {
  131.         threads[i] = new thread(player, ref(ball), i);
  132.     }
  133.  
  134.     for (unsigned i = 0; i < PLAYERS; ++i) {
  135.         threads[i]->join();
  136.     }
  137.  
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement