Advertisement
Felanpro

Game with mutex

Mar 8th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #define WIN32_LEAN_AND_MEAN
  4. #include <Windows.h>
  5. #include <conio.h> //Gives us infamous getch()
  6. #include <time.h>
  7. #include <thread>
  8. #include <mutex>
  9.  
  10. using namespace std;
  11.  
  12. mutex muty; //Creating the mutex thing
  13.  
  14. char sheet[10][10];
  15. int ending = 0;
  16.  
  17. void cls()
  18. {
  19.     // Get the Win32 handle representing standard output.
  20.     // This generally only has to be done once, so we make it static.
  21.     static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  22.  
  23.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  24.     COORD topLeft = { 0, 0 };
  25.  
  26.     // std::cout uses a buffer to batch writes to the underlying console.
  27.     // We need to flush that to the console because we're circumventing
  28.     // std::cout entirely; after we clear the console, we don't want
  29.     // stale buffered text to randomly be written out.
  30.     std::cout.flush();
  31.  
  32.     // Figure out the current width and height of the console window
  33.     if (!GetConsoleScreenBufferInfo(hOut, &csbi)) {
  34.         // TODO: Handle failure!
  35.         abort();
  36.     }
  37.     DWORD length = csbi.dwSize.X * csbi.dwSize.Y;
  38.  
  39.     DWORD written;
  40.  
  41.     // Flood-fill the console with spaces to clear it
  42.     FillConsoleOutputCharacter(hOut, TEXT(' '), length, topLeft, &written);
  43.  
  44.     // Reset the attributes of every character to the default.
  45.     // This clears all background colour formatting, if any.
  46.     FillConsoleOutputAttribute(hOut, csbi.wAttributes, length, topLeft, &written);
  47.  
  48.     // Move the cursor back to the top left for the next sequence of writes
  49.     SetConsoleCursorPosition(hOut, topLeft);
  50. }
  51.  
  52. void setCursorPosition(int x, int y)
  53. {
  54.     static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  55.     std::cout.flush();
  56.     COORD coord = { (SHORT)x, (SHORT)y };
  57.     SetConsoleCursorPosition(hOut, coord);
  58. }
  59.  
  60. int c;
  61. int p;
  62.  
  63. void initialize()
  64. {
  65.     for(int x = 0; x <10; x++)
  66.     {
  67.         for(int y = 0; y < 10; y++)
  68.         {
  69.             sheet[x][y] = '*';
  70.         }
  71.     }
  72.  
  73.     sheet[5][5] = 'X'; //This is where the player starts
  74. }
  75.  
  76. void draw()
  77. {
  78.     for(int xx = 0; xx < 10; xx++)
  79.     {
  80.         for(int yy = 0; yy < 10; yy++)
  81.         {
  82.             cout << sheet[xx][yy];
  83.         }
  84.         cout << endl;
  85.     }
  86. }
  87.  
  88. void input_and_update()
  89. {
  90.     //Get row and column (position) of player
  91.     int breaking = 0;
  92.     for(c = 0; c < 10; c++)
  93.     {
  94.         for(p = 0; p < 10; p++)
  95.         {
  96.             if(sheet[c][p] == 'X')
  97.             {
  98.                 breaking = 1;
  99.                 break;
  100.             }
  101.         }
  102.         if(breaking == 1)
  103.             break;
  104.     }
  105.  
  106.     breaking = 0;
  107.  
  108.     char variable;
  109.  
  110.     //77 = right arrow
  111.     //72 = up arrow
  112.     //80 down arrow
  113.     //75 = left arrow
  114.     // c = row, p = column
  115.     while(breaking == 0)
  116.     {
  117.         variable = getch();
  118.  
  119.         if(variable == 75)
  120.         {
  121.             if(p - 1 < 0)
  122.             {
  123.                 ;
  124.             }
  125.             else if(sheet[c][p - 1] == 'O')
  126.             {
  127.                 ending = 1;
  128.                 breaking = 1;
  129.             }
  130.             else
  131.             {
  132.                 sheet[c][p] = '*';
  133.                 setCursorPosition(p, c);
  134.                 cout << "*";
  135.                 sheet[c][p - 1] = 'X';
  136.                 setCursorPosition((p - 1), c);
  137.                 cout << "X";
  138.                 setCursorPosition(0, 11);
  139.                 breaking = 1;
  140.             }
  141.         }
  142.         else if(variable == 72)
  143.         {
  144.             if(c - 1 < 0)
  145.             {
  146.                 ;
  147.             }
  148.             else if(sheet[c - 1][p] == 'O')
  149.             {
  150.                 ending = 1;
  151.                 breaking = 1;
  152.             }
  153.             else
  154.             {
  155.                 sheet[c][p] = '*';
  156.                 setCursorPosition(p, c);
  157.                 cout << "*";
  158.                 sheet[c - 1][p] = 'X';
  159.                 setCursorPosition(p, c - 1);
  160.                 cout << "X";
  161.                 setCursorPosition(0, 11);
  162.                 breaking = 1;
  163.             }
  164.         }
  165.         else if(variable == 77)
  166.         {
  167.             if(p + 1 > 9)
  168.             {
  169.                 ;
  170.             }
  171.             else if(sheet[c][p + 1] == 'O')
  172.             {
  173.                 ending = 1;
  174.                 breaking = 1;
  175.             }
  176.             else
  177.             {
  178.                 sheet[c][p] = '*';
  179.                 setCursorPosition(p, c);
  180.                 cout << "*";
  181.                 sheet[c][p + 1] = 'X';
  182.                 setCursorPosition((p + 1), c);
  183.                 cout << "X";
  184.                 setCursorPosition(0, 11);
  185.                 breaking = 1;
  186.             }
  187.         }
  188.         else if(variable == 80)
  189.         {
  190.             if(c + 1 > 9)
  191.             {
  192.                 ;
  193.             }
  194.             else if(sheet[c + 1][p] == 'O')
  195.             {
  196.                 ending = 1;
  197.                 breaking = 1;
  198.             }
  199.             else
  200.             {
  201.                 sheet[c][p] = '*';
  202.                 setCursorPosition(p, c);
  203.                 cout << "*";
  204.                 sheet[c + 1][p] = 'X';
  205.                 setCursorPosition(p, c + 1);
  206.                 cout << "X";
  207.                 setCursorPosition(0, 11);
  208.                 breaking = 1;
  209.             }
  210.         }
  211.     }
  212. }
  213.  
  214. int timer = -1;
  215.  
  216. void spawnEnemy()
  217. {
  218.     /*
  219.     Because we spawn an enemy every second we can implement a clock
  220.     into the function which essentially adds 1 to a variable for every second that
  221.     passes. This variable can in this way be used as a clock which we print out at
  222.     the end of the game as an award system.
  223.     */
  224.     muty.lock();
  225.     int break_loop = 1;
  226.  
  227.     while(break_loop == 1)
  228.     {
  229.         timer++;
  230.         srand(time(NULL));
  231.         int random_number_one = rand() % 10;
  232.         int random_number_two = rand() % 10;
  233.  
  234.         if(random_number_one == 5 && random_number_two == 5)
  235.         {
  236.             ;
  237.         }
  238.         else
  239.         {
  240.             sheet[random_number_one][random_number_two] = 'O';
  241.             setCursorPosition(random_number_two, random_number_one);
  242.             cout << "O";
  243.             setCursorPosition(0, 11);
  244.             break_loop = 1;
  245.         }
  246.         if(ending == 1)
  247.         {
  248.             break_loop = 0;
  249.         }
  250.         Sleep(1000);
  251.     }
  252.     muty.unlock();
  253. }
  254.  
  255. int main()
  256. {
  257.     initialize(); // Create play sheet
  258.     draw();
  259.  
  260.     thread t1(spawnEnemy);
  261.  
  262.     while(ending == 0)
  263.     {
  264.         input_and_update();
  265.     }
  266.  
  267.     t1.join();
  268.     setCursorPosition(0, 0);
  269.     cls();
  270.  
  271.     cout << "You survived " << timer << " seconds!" << endl;
  272.     int pause_variable;
  273.     cin >> pause_variable;
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement