Advertisement
Felanpro

Weird Game using threads

Feb 19th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <thread>
  6. #include <Windows.h>
  7.  
  8. using namespace std;
  9.  
  10. char matrix[10][10];
  11. int moves;
  12.  
  13. void initialize() //Assign values to the play board (matrix[][])
  14. {
  15.     for(int d = 0; d < 10; d++)
  16.     {
  17.         for(int q = 0; q < 10; q++)
  18.         {
  19.             matrix[d][q] = '*';
  20.         }
  21.     }
  22.     matrix[0][0] = 'X';
  23. }
  24.  
  25.  
  26. void draw() //Draw the play board
  27. {
  28.     for(int x = 0; x < 10; x++)
  29.     {
  30.         for(int z = 0; z < 10; z++)
  31.         {
  32.             cout << matrix[x][z] << " ";
  33.         }
  34.         cout << endl;
  35.     }
  36. }
  37.  
  38. void spawnEnemy() //Spawn the enemy O
  39. {
  40.     int spawnEnemy_correct = 0;
  41.  
  42.     while(spawnEnemy_correct < 3)
  43.     {
  44.         srand(time(0));
  45.         int random_num_one = (rand() % 10);
  46.         int random_num_two = (rand() % 10);
  47.  
  48.         if(matrix[random_num_one][random_num_two] != 'X')
  49.         {
  50.             matrix[random_num_one][random_num_two] = 'O';
  51.             spawnEnemy_correct++;
  52.         }
  53.         else if(random_num_one == 9 && random_num_two == 9)
  54.             ;
  55.         else
  56.             ;
  57.     }
  58. }
  59.  
  60. int stop_program = 0; //Stops the game and prints out the results
  61. int dead = 0; //Decides wether the player died or not.
  62.  
  63. void input() //Let the user input
  64. {
  65.     //77 = right arrow
  66.     //72 = up arrow
  67.     //80 down arrow
  68.     //75 = left arrow
  69.     char variable;
  70.     int c = 0;
  71.     int m = 0;
  72.     int breaking = 0;
  73.     int break_out = 0;
  74.  
  75.     //Calculate what column and row X is at
  76.     for(c = 0; c < 10; c++)
  77.     {
  78.         for(m = 0; m < 10; m++)
  79.         {
  80.             if(matrix[c][m] == 'X')
  81.             {
  82.                 breaking = 1;
  83.                 break;
  84.             }
  85.         }
  86.         if(breaking == 1)
  87.             break;
  88.     }
  89.  
  90.     //Scan input and execute movement if it is possible
  91.     while(break_out == 0)
  92.     {
  93.         variable = getch();
  94.  
  95.         if(variable == 77)
  96.         {
  97.             if((m + 1) > 9)
  98.             {
  99.                 ;
  100.             }
  101.             else if(matrix[c][m + 1] == 'O')
  102.             {
  103.                 break_out = 1;
  104.                 stop_program = 1;
  105.                 dead = 1;
  106.             }
  107.             else
  108.             {
  109.                 matrix[c][m] = '*';
  110.                 matrix[c][m + 1] = 'X';
  111.                 break_out = 1;
  112.             }
  113.         }
  114.         else if(variable == 72)
  115.         {
  116.             if((c - 1) < 0)
  117.             {
  118.                 ;
  119.             }
  120.             else if(matrix[c - 1][m] == 'O')
  121.             {
  122.                 break_out = 1;
  123.                 stop_program = 1;
  124.                 dead = 1;
  125.             }
  126.             else
  127.             {
  128.                 matrix[c][m] = '*';
  129.                 matrix[c - 1][m] = 'X';
  130.                 break_out = 1;
  131.             }
  132.         }
  133.         else if(variable == 75)
  134.         {
  135.             if(m - 1 < 0)
  136.             {
  137.                 ;
  138.             }
  139.             else if(matrix[c][m - 1] == 'O')
  140.             {
  141.                 break_out = 1;
  142.                 stop_program = 1;
  143.                 dead = 1;
  144.             }
  145.             else
  146.             {
  147.                 matrix[c][m] = '*';
  148.                 matrix[c][m - 1] = 'X';
  149.                 break_out = 1;
  150.             }
  151.         }
  152.         else if(variable == 80)
  153.         {
  154.             if((c + 1) > 9)
  155.             {
  156.                 ;
  157.             }
  158.             else if(matrix[c + 1][m] == 'O')
  159.             {
  160.                 break_out = 1;
  161.                 stop_program = 1;
  162.                 dead = 1;
  163.             }
  164.             else
  165.             {
  166.                 matrix[c][m] = '*';
  167.                 matrix[c + 1][m] = 'X';
  168.                 break_out = 1;
  169.             }
  170.         }
  171.  
  172.         if(matrix[9][9] == 'X')
  173.         {
  174.             stop_program = 1;
  175.             break_out = 1;
  176.         }
  177.     }
  178. }
  179.  
  180. int counter_variable = 0;
  181.  
  182. int counting_function()
  183. {
  184.     counter_variable = 0;
  185.     while(stop_program == 0)
  186.     {
  187.         Sleep(1000);
  188.         counter_variable++;
  189.     }
  190. }
  191.  
  192. int main()
  193. {
  194.     initialize();
  195.     thread t1(counting_function);
  196.     while(stop_program == 0)
  197.     {
  198.         spawnEnemy();
  199.         draw();
  200.         input();
  201.         moves++;
  202.         system("cls");
  203.     }
  204.     t1.join();
  205.  
  206.     if(dead == 1)
  207.     {
  208.         cout << "You died!" << endl;
  209.         cout << "Number of moves: " << moves << endl;
  210.         cout << "Number of seconds: " << counter_variable << endl;
  211.     }
  212.     else
  213.     {
  214.         cout << "You made it to the other side!" << endl;
  215.         cout << "Number of moves: " << moves << endl;
  216.         cout << "Number of seconds: " << counter_variable << endl;
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement