Advertisement
Felanpro

Time wasted

Feb 22nd, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.63 KB | None | 0 0
  1. /* ---Weird Game v.1--- */
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include <thread>
  7. #include <Windows.h>
  8.  
  9. using namespace std;
  10.  
  11. char matrix[10][10];
  12. int moves;
  13.  
  14. void initialize() //Assign values to the play board (matrix[][])
  15. {
  16.     for(int d = 0; d < 10; d++)
  17.     {
  18.         for(int q = 0; q < 10; q++)
  19.         {
  20.             matrix[d][q] = '*';
  21.         }
  22.     }
  23.     matrix[0][0] = 'X';
  24. }
  25.  
  26.  
  27. void draw() //Draw the play board
  28. {
  29.     for(int x = 0; x < 10; x++)
  30.     {
  31.         for(int z = 0; z < 10; z++)
  32.         {
  33.             cout << matrix[x][z] << " ";
  34.         }
  35.         cout << endl;
  36.     }
  37. }
  38.  
  39. void spawnEnemy() //Spawn the enemy O
  40. {
  41.     int spawnEnemy_correct = 0;
  42.  
  43.     while(spawnEnemy_correct != 1)
  44.     {
  45.         srand(time(0));
  46.         int random_num_one = (rand() % 10);
  47.         int random_num_two = (rand() % 10);
  48.  
  49.         int random_num_three = (rand() % 10);
  50.         int random_num_four = (rand() % 10);
  51.  
  52.         if((matrix[random_num_one][random_num_two] != 'X') && (matrix[random_num_three][random_num_four] != 'X'))
  53.         {
  54.             if((random_num_one == 9 && random_num_two == 9) || (random_num_three == 9 && random_num_four == 9))
  55.             {
  56.                 ;
  57.             }
  58.             else
  59.             {
  60.                 matrix[random_num_one][random_num_two] = 'O';
  61.                 matrix[random_num_three][random_num_four] = 'O';
  62.                 spawnEnemy_correct++;
  63.             }
  64.         }
  65.         else
  66.             ;
  67.     }
  68.  
  69.     int row;
  70.     int ax = 0;
  71.     int count_o;
  72.     int yy = 1;
  73.     int number_of_scans;
  74.     int y = 0;
  75.     //matrix[row][ax] = 'S';
  76.  
  77.     while(1) //Unnecessary while-loop
  78.     {
  79.         //Check the first diagonals if a diagonal is covering the goal
  80.         for(y = 0; y < 9; y++)
  81.         {
  82.             count_o = 0;
  83.             number_of_scans = 0;
  84.             for(row = 1 + y, ax = 0; (ax != yy + y) && (row != 0); ax++, row--) //Scan through the first diagonals
  85.             {
  86.                 if(matrix[row][ax] == 'O')
  87.                 {
  88.                     count_o++;
  89.                 }
  90.                 number_of_scans++;
  91.             }
  92.             if(count_o == number_of_scans)
  93.             {
  94.                 //matrix[row][ax] = '*';
  95.                 for(int xx = y + 1, zz = 0; (xx != 0) && (zz != number_of_scans); xx--, zz++)
  96.                 {
  97.                     matrix[xx][zz] = '*';
  98.                 }
  99.                 break;
  100.             }
  101.         }
  102.         //Check verticles if one is covering the goal
  103.         for(y = 1; y < 9; y++)
  104.         {
  105.             count_o = 0;
  106.             number_of_scans = 0;
  107.             for(yy = 1; yy < 9; yy++)
  108.             {
  109.                 if(matrix[y][yy] == 'O')
  110.                 {
  111.                     count_o++;
  112.                 }
  113.                 number_of_scans++; //y = row, yy = column
  114.             }
  115.             if(count_o == number_of_scans)
  116.             {
  117.                 while(y != 0)
  118.                 {
  119.                     matrix[y][yy] = '*';
  120.                     y--;
  121.                 }
  122.             }
  123.         }
  124.  
  125.         break;
  126.  
  127.     }
  128.  
  129. }
  130.  
  131. int stop_program = 0; //Stops the game and prints out the results
  132. int dead = 0; //Decides wether the player died or not.
  133.  
  134. void input() //Let the user input
  135. {
  136.     //77 = right arrow
  137.     //72 = up arrow
  138.     //80 down arrow
  139.     //75 = left arrow
  140.     char variable;
  141.     int c = 0;
  142.     int m = 0;
  143.     int breaking = 0;
  144.     int break_out = 0;
  145.  
  146.     //Calculate what column and row X is at
  147.     for(c = 0; c < 10; c++)
  148.     {
  149.         for(m = 0; m < 10; m++)
  150.         {
  151.             if(matrix[c][m] == 'X')
  152.             {
  153.                 breaking = 1;
  154.                 break;
  155.             }
  156.         }
  157.         if(breaking == 1)
  158.             break;
  159.     }
  160.  
  161.     //Scan input and execute movement if it is possible
  162.     while(break_out == 0)
  163.     {
  164.         variable = getch();
  165.  
  166.         if(variable == 77)
  167.         {
  168.             if((m + 1) > 9)
  169.             {
  170.                 ;
  171.             }
  172.             else if(matrix[c][m + 1] == 'O')
  173.             {
  174.                 break_out = 1;
  175.                 stop_program = 1;
  176.                 dead = 1;
  177.             }
  178.             else
  179.             {
  180.                 matrix[c][m] = '*';
  181.                 matrix[c][m + 1] = 'X';
  182.                 break_out = 1;
  183.             }
  184.         }
  185.         else if(variable == 72)
  186.         {
  187.             if((c - 1) < 0)
  188.             {
  189.                 ;
  190.             }
  191.             else if(matrix[c - 1][m] == 'O')
  192.             {
  193.                 break_out = 1;
  194.                 stop_program = 1;
  195.                 dead = 1;
  196.             }
  197.             else
  198.             {
  199.                 matrix[c][m] = '*';
  200.                 matrix[c - 1][m] = 'X';
  201.                 break_out = 1;
  202.             }
  203.         }
  204.         else if(variable == 75)
  205.         {
  206.             if(m - 1 < 0)
  207.             {
  208.                 ;
  209.             }
  210.             else if(matrix[c][m - 1] == 'O')
  211.             {
  212.                 break_out = 1;
  213.                 stop_program = 1;
  214.                 dead = 1;
  215.             }
  216.             else
  217.             {
  218.                 matrix[c][m] = '*';
  219.                 matrix[c][m - 1] = 'X';
  220.                 break_out = 1;
  221.             }
  222.         }
  223.         else if(variable == 80)
  224.         {
  225.             if((c + 1) > 9)
  226.             {
  227.                 ;
  228.             }
  229.             else if(matrix[c + 1][m] == 'O')
  230.             {
  231.                 break_out = 1;
  232.                 stop_program = 1;
  233.                 dead = 1;
  234.             }
  235.             else
  236.             {
  237.                 matrix[c][m] = '*';
  238.                 matrix[c + 1][m] = 'X';
  239.                 break_out = 1;
  240.             }
  241.         }
  242.  
  243.         if(matrix[9][9] == 'X')
  244.         {
  245.             stop_program = 1;
  246.             break_out = 1;
  247.         }
  248.     }
  249. }
  250.  
  251. int counter_variable = 0;
  252.  
  253. void counting_function()
  254. {
  255.     counter_variable = 0;
  256.     while(stop_program == 0)
  257.     {
  258.         Sleep(1000);
  259.         counter_variable++;
  260.     }
  261. }
  262.  
  263. int main()
  264. {
  265.     initialize();
  266.     thread t1(counting_function);
  267.     while(stop_program == 0)
  268.     {
  269.         spawnEnemy();
  270.         draw();
  271.         input();
  272.         moves++;
  273.         system("cls");
  274.     }
  275.     t1.join();
  276.  
  277.     if(dead == 1)
  278.     {
  279.         cout << "You died!" << endl;
  280.         cout << "Number of moves: " << moves << endl;
  281.         cout << "Number of seconds: " << counter_variable << endl;
  282.     }
  283.     else
  284.     {
  285.         cout << "You made it to the other side!" << endl;
  286.         cout << "Number of moves: " << moves << endl;
  287.         cout << "Number of seconds: " << counter_variable << endl;
  288.     }
  289.     cin >> stop_program;
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement