Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <time.h>
- #include <cstdio>
- #include <conio.h>
- #include <cstdlib>
- using namespace std;
- const int sizeOfFirstField = 6, sizeOfSecondField = 6;
- int valueFirstField = 0, valueSecondField = 0;
- int spentFirstField = 0, spentSecondField = 0;
- int firstField[sizeOfFirstField][sizeOfFirstField], secondField[sizeOfSecondField][sizeOfSecondField];
- HANDLE hMutex;
- int makeShot(int x, int y, int inField) {
- int hit;
- if(inField == 1) {
- hit = firstField[x][y];
- firstField[x][y] = 0;
- }else if(inField == 2) {
- hit = secondField[-x][-y];
- secondField[-x][-y] = 0;
- }
- if(hit) {
- cout << "HIT!";
- }
- else {
- cout << "Missed:(";
- }
- cout << endl;
- return hit;
- }
- int randByMouse(int l, int r) {
- while(true) {
- POINT pt;
- GetCursorPos(&pt);
- Sleep(100);
- POINT pt2;
- GetCursorPos(&pt2);
- if(pt.x != pt2.x || pt.y != pt2.y) {
- srand((pt2.x - pt.x)*(pt2.x - pt.x) + (pt2.y - pt.y)*(pt2.y - pt.y));
- break;
- }
- }
- int rnd = rand()%(r - l + 1) + l;
- cout << rnd << endl;
- return rnd;
- }
- DWORD WINAPI func(PVOID param) {
- int field = *((int*)param);
- while(true) {
- WaitForSingleObject(hMutex, INFINITE);
- if(field == 1) {
- if(spentFirstField > valueSecondField || !valueSecondField) {
- cout << "Right is end\n";
- break;
- }
- spentFirstField++;
- valueSecondField -= makeShot(-randByMouse(0, sizeOfSecondField-1),
- -randByMouse(0, sizeOfSecondField-1),
- 2);
- cout << "Second field: " << endl;
- for(int i = 0; i < sizeOfSecondField; i++) {
- for(int j = 0; j < sizeOfSecondField; j++) {
- cout << secondField[i][j];
- }
- cout << endl;
- }
- cout << endl;
- }else if (field == 2){
- if(spentSecondField > valueFirstField || !valueFirstField) {
- cout << "Left is end\n";
- break;
- }
- spentSecondField++;
- valueFirstField -= makeShot(randByMouse(0, sizeOfFirstField-1),
- randByMouse(0, sizeOfFirstField-1),
- 1);
- cout << "First field: " << endl;
- for(int i = 0; i < sizeOfFirstField; i++) {
- for(int j = 0; j < sizeOfFirstField; j++) {
- cout << firstField[i][j];
- }
- cout << endl;
- }
- cout << endl;
- }
- ReleaseMutex(hMutex);
- }
- return 0;
- }
- void inputFields() {
- cout << "Enter first field:" << endl;
- for(int i = 0; i < sizeOfFirstField; i++) {
- for(int j = 0; j < sizeOfFirstField; j++) {
- cin >> firstField[i][j];
- valueFirstField += firstField[i][j];
- }
- }
- cout << "Enter second field:" << endl;
- for(int i = 0; i < sizeOfSecondField; i++) {
- for(int j = 0; j < sizeOfSecondField; j++) {
- cin >> secondField[i][j];
- valueSecondField += secondField[i][j];
- }
- }
- }
- int main()
- {
- DWORD idThread[2], threadResult[2];
- HANDLE thread[2];
- int field[2] = {1, 2};
- hMutex = CreateMutex(NULL, false, "MyMutex");
- inputFields();
- cout << "Move your mouse please :)" << endl;
- thread[0] = CreateThread(NULL, 0, func, &field[0], 0, &idThread[0]);
- thread[1] = CreateThread(NULL, 0, func, &field[1], 0, &idThread[1]);
- (void) WaitForMultipleObjects(2, thread, TRUE, INFINITE);
- (void) GetExitCodeThread(thread[0], &threadResult[0]);
- (void) GetExitCodeThread(thread[1], &threadResult[1]);
- CloseHandle(hMutex);
- return 0;
- }
- /*
- 0 0 0 0 0 0
- 5 5 5 0 0 0
- 0 0 0 0 2 0
- 0 3 3 0 2 0
- 0 0 3 0 0 0
- 0 0 0 0 0 0
- 2 0 8 8 0 0
- 2 0 8 0 5 0
- 2 0 0 5 0 0
- 2 0 5 0 3 3
- 2 0 0 0 0 0
- 2 2 2 2 2 2
- 0 0 0 0 0 0
- 5123 54124 524124 0 0 0
- 0 0 0 0 2124124 0
- 0 3124124 3124124 0 212412 0
- 0 0 3124124 0 0 0
- 0 0 0 0 0 0
- 0 0 0 0 0 0
- 5123123 53 5123123 0 0 0
- 0 0 0 0 2123123 0
- 0 3123123 3123123 0 212123 0
- 0 0 3123123 0 0 0
- 0 0 0 0 0 0
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement