Advertisement
Guest User

lab3 v2.0

a guest
Mar 24th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <time.h>
  4. #include <cstdio>
  5. #include <conio.h>
  6. #include <cstdlib>
  7.  
  8. using namespace std;
  9.  
  10. const int sizeOfFirstField = 6, sizeOfSecondField = 6;
  11. int valueFirstField = 0, valueSecondField = 0;
  12. int spentFirstField = 0, spentSecondField = 0;
  13. int firstField[sizeOfFirstField][sizeOfFirstField], secondField[sizeOfSecondField][sizeOfSecondField];
  14. HANDLE hMutex;
  15.  
  16. int makeShot(int x, int y, int inField) {
  17.     int hit;
  18.     if(inField == 1) {
  19.         hit = firstField[x][y];
  20.         firstField[x][y] = 0;
  21.     }else if(inField == 2) {
  22.         hit = secondField[-x][-y];
  23.         secondField[-x][-y] = 0;
  24.     }
  25.     if(hit) {
  26.         cout << "HIT!";
  27.     }
  28.     else {
  29.         cout << "Missed:(";
  30.     }
  31.     cout << endl;
  32.     return hit;
  33. }
  34.  
  35. int randByMouse(int l, int r) {
  36.     while(true) {
  37.         POINT pt;
  38.         GetCursorPos(&pt);
  39.         Sleep(100);
  40.         POINT pt2;
  41.         GetCursorPos(&pt2);
  42.         if(pt.x != pt2.x || pt.y != pt2.y) {
  43.             srand((pt2.x - pt.x)*(pt2.x - pt.x) + (pt2.y - pt.y)*(pt2.y - pt.y));
  44.             break;
  45.         }
  46.     }
  47.     int rnd = rand()%(r - l + 1) + l;
  48.     cout << rnd << endl;
  49.     return rnd;
  50. }
  51.  
  52. DWORD WINAPI func(PVOID param) {
  53.     int field = *((int*)param);
  54.     while(true) {
  55.         WaitForSingleObject(hMutex, INFINITE);
  56.         if(field == 1) {
  57.             if(spentFirstField > valueSecondField || !valueSecondField) {
  58.                 cout << "Right is end\n";
  59.                 break;
  60.             }
  61.             spentFirstField++;
  62.             valueSecondField -= makeShot(-randByMouse(0, sizeOfSecondField-1),
  63.                                          -randByMouse(0, sizeOfSecondField-1),
  64.                                          2);
  65.             cout << "Second field: " << endl;
  66.             for(int i = 0; i < sizeOfSecondField; i++) {
  67.                 for(int j = 0; j < sizeOfSecondField; j++) {
  68.                     cout << secondField[i][j];
  69.                 }
  70.                 cout << endl;
  71.             }
  72.             cout << endl;
  73.         }else if (field == 2){
  74.             if(spentSecondField > valueFirstField || !valueFirstField) {
  75.                 cout << "Left is end\n";
  76.                 break;
  77.             }
  78.             spentSecondField++;
  79.             valueFirstField -= makeShot(randByMouse(0, sizeOfFirstField-1),
  80.                                          randByMouse(0, sizeOfFirstField-1),
  81.                                          1);
  82.             cout << "First field: " << endl;
  83.             for(int i = 0; i < sizeOfFirstField; i++) {
  84.                 for(int j = 0; j < sizeOfFirstField; j++) {
  85.                     cout << firstField[i][j];
  86.                 }
  87.                 cout << endl;
  88.             }
  89.             cout << endl;
  90.         }
  91.         ReleaseMutex(hMutex);
  92.     }
  93.     return 0;
  94. }
  95.  
  96. void inputFields() {
  97.     cout << "Enter first field:" << endl;
  98.     for(int i = 0; i < sizeOfFirstField; i++) {
  99.         for(int j = 0; j < sizeOfFirstField; j++) {
  100.             cin >> firstField[i][j];
  101.             valueFirstField += firstField[i][j];
  102.         }
  103.     }
  104.     cout << "Enter second field:" << endl;
  105.     for(int i = 0; i < sizeOfSecondField; i++) {
  106.         for(int j = 0; j < sizeOfSecondField; j++) {
  107.             cin >> secondField[i][j];
  108.             valueSecondField += secondField[i][j];
  109.         }
  110.     }
  111. }
  112.  
  113. int main()
  114. {
  115.     DWORD idThread[2], threadResult[2];
  116.     HANDLE thread[2];
  117.     int field[2] = {1, 2};
  118.  
  119.     hMutex = CreateMutex(NULL, false, "MyMutex");
  120.  
  121.     inputFields();
  122.     cout << "Move your mouse please :)" << endl;
  123.  
  124.     thread[0] = CreateThread(NULL, 0, func, &field[0], 0, &idThread[0]);
  125.     thread[1] = CreateThread(NULL, 0, func, &field[1], 0, &idThread[1]);
  126.  
  127.     (void) WaitForMultipleObjects(2, thread, TRUE, INFINITE);
  128.     (void) GetExitCodeThread(thread[0], &threadResult[0]);
  129.     (void) GetExitCodeThread(thread[1], &threadResult[1]);
  130.     CloseHandle(hMutex);
  131.     return 0;
  132. }
  133.  
  134. /*
  135. 0 0 0 0 0 0
  136. 5 5 5 0 0 0
  137. 0 0 0 0 2 0
  138. 0 3 3 0 2 0
  139. 0 0 3 0 0 0
  140. 0 0 0 0 0 0
  141.  
  142. 2 0 8 8 0 0
  143. 2 0 8 0 5 0
  144. 2 0 0 5 0 0
  145. 2 0 5 0 3 3
  146. 2 0 0 0 0 0
  147. 2 2 2 2 2 2
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155. 0 0 0 0 0 0
  156. 5123 54124 524124 0 0 0
  157. 0 0 0 0 2124124 0
  158. 0 3124124 3124124 0 212412 0
  159. 0 0 3124124 0 0 0
  160. 0 0 0 0 0 0
  161.  
  162. 0 0 0 0 0 0
  163. 5123123 53 5123123 0 0 0
  164. 0 0 0 0 2123123 0
  165. 0 3123123 3123123 0 212123 0
  166. 0 0 3123123 0 0 0
  167. 0 0 0 0 0 0
  168.  
  169. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement