Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <stdio.h>
- #include <time.h>
- #include <thread>
- #include <Windows.h>
- using namespace std;
- char matrix[10][10];
- int moves;
- void initialize() //Assign values to the play board (matrix[][])
- {
- for(int d = 0; d < 10; d++)
- {
- for(int q = 0; q < 10; q++)
- {
- matrix[d][q] = '*';
- }
- }
- matrix[0][0] = 'X';
- }
- void draw() //Draw the play board
- {
- for(int x = 0; x < 10; x++)
- {
- for(int z = 0; z < 10; z++)
- {
- cout << matrix[x][z] << " ";
- }
- cout << endl;
- }
- }
- void spawnEnemy() //Spawn the enemy O
- {
- int spawnEnemy_correct = 0;
- while(spawnEnemy_correct < 3)
- {
- srand(time(0));
- int random_num_one = (rand() % 10);
- int random_num_two = (rand() % 10);
- if(matrix[random_num_one][random_num_two] != 'X')
- {
- matrix[random_num_one][random_num_two] = 'O';
- spawnEnemy_correct++;
- }
- else if(random_num_one == 9 && random_num_two == 9)
- ;
- else
- ;
- }
- }
- int stop_program = 0; //Stops the game and prints out the results
- int dead = 0; //Decides wether the player died or not.
- void input() //Let the user input
- {
- //77 = right arrow
- //72 = up arrow
- //80 down arrow
- //75 = left arrow
- char variable;
- int c = 0;
- int m = 0;
- int breaking = 0;
- int break_out = 0;
- //Calculate what column and row X is at
- for(c = 0; c < 10; c++)
- {
- for(m = 0; m < 10; m++)
- {
- if(matrix[c][m] == 'X')
- {
- breaking = 1;
- break;
- }
- }
- if(breaking == 1)
- break;
- }
- //Scan input and execute movement if it is possible
- while(break_out == 0)
- {
- variable = getch();
- if(variable == 77)
- {
- if((m + 1) > 9)
- {
- ;
- }
- else if(matrix[c][m + 1] == 'O')
- {
- break_out = 1;
- stop_program = 1;
- dead = 1;
- }
- else
- {
- matrix[c][m] = '*';
- matrix[c][m + 1] = 'X';
- break_out = 1;
- }
- }
- else if(variable == 72)
- {
- if((c - 1) < 0)
- {
- ;
- }
- else if(matrix[c - 1][m] == 'O')
- {
- break_out = 1;
- stop_program = 1;
- dead = 1;
- }
- else
- {
- matrix[c][m] = '*';
- matrix[c - 1][m] = 'X';
- break_out = 1;
- }
- }
- else if(variable == 75)
- {
- if(m - 1 < 0)
- {
- ;
- }
- else if(matrix[c][m - 1] == 'O')
- {
- break_out = 1;
- stop_program = 1;
- dead = 1;
- }
- else
- {
- matrix[c][m] = '*';
- matrix[c][m - 1] = 'X';
- break_out = 1;
- }
- }
- else if(variable == 80)
- {
- if((c + 1) > 9)
- {
- ;
- }
- else if(matrix[c + 1][m] == 'O')
- {
- break_out = 1;
- stop_program = 1;
- dead = 1;
- }
- else
- {
- matrix[c][m] = '*';
- matrix[c + 1][m] = 'X';
- break_out = 1;
- }
- }
- if(matrix[9][9] == 'X')
- {
- stop_program = 1;
- break_out = 1;
- }
- }
- }
- int counter_variable = 0;
- int counting_function()
- {
- counter_variable = 0;
- while(stop_program == 0)
- {
- Sleep(1000);
- counter_variable++;
- }
- }
- int main()
- {
- initialize();
- thread t1(counting_function);
- while(stop_program == 0)
- {
- spawnEnemy();
- draw();
- input();
- moves++;
- system("cls");
- }
- t1.join();
- if(dead == 1)
- {
- cout << "You died!" << endl;
- cout << "Number of moves: " << moves << endl;
- cout << "Number of seconds: " << counter_variable << endl;
- }
- else
- {
- cout << "You made it to the other side!" << endl;
- cout << "Number of moves: " << moves << endl;
- cout << "Number of seconds: " << counter_variable << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement