Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Dragutin Marjanovic
- Write program for modeling action of Real Madrid C.F.
- Team has 11 players. Behave of players is given with function 'player'.
- Player passes the ball to each other which is represented with the class 'Ball'.
- 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.
- After 12 passes, Real Madrid scores and program ends.
- If during the pass player scores, method 'pass_the_ball' returns jersey number.
- After a goal is scored, for all other players method returns 0.
- If goal isn't scored it returns -1.
- Real Madrid never loses posseion of ball. */
- #include <iostream>
- #include <ctime>
- #include <thread>
- #include <mutex>
- #include <condition_variable>
- #include <vector>
- using namespace std;
- #define NULL 0
- #define PLAYERS 11
- int my_random(int low, int high) {
- srand(time(NULL));
- return rand() % (high - low + 1) + low;
- }
- vector<int> player_numbers = {1, 2, 4, 7, 8, 10, 11, 12, 15, 19, 23};
- vector<string> player_names = {
- "Iker Casillas",
- "Raphael Varane",
- "Sergio Ramos (C)",
- "Cristiano Ronaldo",
- "Toni Kroos",
- "James Rodriguez",
- "Gareth Bale",
- "Marcelo Vieira",
- "Daniel Carvajal",
- "Luka Modric",
- "Alarcon Isco"
- };
- class Ball {
- private:
- int number_of_passes;
- bool free;
- bool goal;
- mutex m;
- condition_variable pass;
- public:
- Ball(): number_of_passes(0), free(true), goal(false) {}
- int pass_the_ball(int number) {
- unique_lock<mutex> l(m);
- if (!free && !goal) {
- pass.wait(l);
- }
- if (goal) return 0;
- free = false;
- l.unlock();
- this_thread::sleep_for(chrono::seconds(my_random(1, 3)));
- l.lock();
- if (my_random(1, 3) > 1) {
- number_of_passes++;
- }
- int next_number;
- do {
- next_number = my_random(1, 11);
- }
- while (number == next_number);
- free = true;
- if (number_of_passes == 12) {
- goal = true;
- pass.notify_all();
- return next_number;
- }
- else {
- pass.notify_one();
- return -1;
- }
- }
- };
- mutex term_m;
- void player(Ball &ball, int number) {
- {
- unique_lock<mutex> l(term_m);
- cout << player_numbers[number] << " " << player_names[number] << " went into stadium." << endl;
- }
- int scorer = -1;
- do {
- scorer = ball.pass_the_ball(number);
- {
- unique_lock<mutex> l(term_m);
- cout << player_numbers[number] << " " << player_names[number] << " passes the ball." << endl;
- }
- this_thread::sleep_for(chrono::seconds(1));
- if (scorer > 0) {
- unique_lock<mutex> l(term_m);
- cout << endl << "GOOOOAAAL! " << player_names[scorer] << endl;
- }
- } while (scorer == -1);
- }
- int main() {
- Ball ball;
- thread* threads[PLAYERS];
- for (unsigned i = 0; i < PLAYERS; ++i) {
- threads[i] = new thread(player, ref(ball), i);
- }
- for (unsigned i = 0; i < PLAYERS; ++i) {
- threads[i]->join();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement