Advertisement
MKcrazy8

bruh AVS

Dec 16th, 2022
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <ctime>
  4. #include <thread>
  5. #include <semaphore.h>
  6.  
  7.  
  8. int anch_money;
  9. int taran_money;
  10. int anch_hits;
  11. int taran_hits;
  12. int cost_shot;
  13. int anch[8][8] = {0};
  14. int taran[8][8] = {0};
  15. int num = 0;
  16.  
  17. pthread_mutex_t mutex;
  18.  
  19.  
  20.  
  21.  
  22. /// IS it Nuked?
  23. /// \param name - country
  24. /// \return
  25. bool isEmpty(int name[8][8]) {
  26.     bool isIt = false;
  27.     for (int i = 0; i < 8; ++i) {
  28.         for (int j = 0; j < 8; ++j) {
  29.             if(name[i][j] == 0) {
  30.                 isIt = true;
  31.             }
  32.         }
  33.     }
  34.     return isIt;
  35. }
  36.  
  37. int rndNum() {
  38.     srand(time(NULL)+num);
  39.     int x = rand();
  40.     return x;
  41. }
  42.  
  43. sem_t semaphore;
  44.  
  45. /// Anchuaria spends money.
  46. /// \param param
  47. /// \return
  48. void *makeShotAncuaria(void *param) {
  49.     anch_money -= anch_hits * cost_shot;
  50.     std::cout << "anch made shot: ";
  51.     num++;
  52.     int x = rndNum() % 8;
  53.     num++;
  54.     int y = rndNum() % 8;
  55.     // mutex for value
  56.     pthread_mutex_lock(&mutex);
  57.     taran[x][y] = 1;
  58.     pthread_mutex_unlock(&mutex);
  59.     //output shotted cell
  60.     std::cout << x << " " << y << "\n";
  61.     std::cout << "Block shotted\n";
  62.     //sem_post(&semaphore);
  63.     return nullptr;
  64. }
  65. /// Taran spends money
  66. /// \param param
  67. /// \return
  68. void *makeShottaran(void *param) {
  69.     sem_wait(&semaphore);
  70.     taran_money -= taran_hits * cost_shot;
  71.     //using semamorph
  72.     std::cout << "taran shoot: ";
  73.     num++;
  74.     int x = rndNum() % 8;
  75.     num++;
  76.     int y = rndNum() % 8;
  77.     pthread_mutex_lock(&mutex);
  78.     anch[x][y] = 1;
  79.     pthread_mutex_unlock(&mutex);
  80.     //output shotted cell
  81.     std::cout << x << " " << y << "\n";
  82.     std::cout << "Block shotted\n";
  83.     sem_post(&semaphore);
  84.     return nullptr;
  85. }
  86.  
  87.  
  88.  
  89. void console_war() {
  90.     sem_init(&semaphore, 0, 1);
  91. //вводим количество денег у стран и стоимость одного выстрела
  92.     std::cout << "Enter amount  for each country$$$$$:\n";
  93.     std::cin >> anch_money >> taran_money;
  94.     std::cout << "Enter cost for one bullet:\n";
  95.     std::cin >> cost_shot;
  96.     std::cout << "Anch and Taran countries are fields 8x8. shoots are full randomly";
  97.  
  98.     while ((taran_money >= 0 || anch_money >= 0) && isEmpty(anch) && isEmpty(taran)) {
  99.         std::cout << "Enter number of shots for first and for second country:\n";
  100.         //Frst country
  101.         std::cin >> anch_hits >> taran_hits;
  102.         num = 0;
  103.         //threads
  104.         pthread_t threads_anch[anch_hits];
  105.         pthread_t threads_taran[taran_hits];
  106.         //Anch shots threads
  107.         for (int i = 0; i < anch_hits; i++) {
  108.             num++;
  109.             pthread_create(&threads_anch[i], nullptr, makeShotAncuaria, &i);
  110.             std::this_thread::sleep_for(std::chrono::milliseconds(500));
  111.         }
  112.         // Taran shots threads
  113.         for (int i = 0; i < taran_hits; i++) {
  114.             num++;
  115.             pthread_create(&threads_taran[i], nullptr, makeShottaran, &i);
  116.             std::this_thread::sleep_for(std::chrono::milliseconds(500));
  117.         }
  118.         //Waiting for Anch
  119.         for (int j = 0; j < anch_hits; j++) {
  120.             num++;
  121.             pthread_join(threads_anch[j], nullptr);
  122.         }
  123.         // Waiting for Taran
  124.         for (int j = 0; j < taran_hits; j++) {
  125.             num++;
  126.             pthread_join(threads_taran[j], nullptr);
  127.         }
  128.     }
  129.     //The end statistic
  130.     if (taran_money <= 0) std::cout << "Taran lost all money\n";
  131.     if (anch_money <= 0) std::cout << "Anchuria lost all money\n";
  132.     if (!isEmpty(anch)) std::cout << "Anchuria is Nuked!!! Game over\n";
  133.     if (!isEmpty(taran)) std::cout << "taran is Nuked!!! Game over\n";
  134. }
  135.  
  136.  
  137. int main() {
  138.     console_war();
  139.     return 0;
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement