Advertisement
Felanpro

Weird Game

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