Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ---Weird Game v.1--- */
- #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 != 1)
- {
- srand(time(0));
- int random_num_one = (rand() % 10);
- int random_num_two = (rand() % 10);
- int random_num_three = (rand() % 10);
- int random_num_four = (rand() % 10);
- if((matrix[random_num_one][random_num_two] != 'X') && (matrix[random_num_three][random_num_four] != 'X'))
- {
- if((random_num_one == 9 && random_num_two == 9) || (random_num_three == 9 && random_num_four == 9))
- {
- ;
- }
- else
- {
- matrix[random_num_one][random_num_two] = 'O';
- matrix[random_num_three][random_num_four] = 'O';
- spawnEnemy_correct++;
- }
- }
- else
- ;
- }
- int row;
- int ax = 0;
- int count_o;
- int yy = 1;
- int number_of_scans;
- int y = 0;
- //matrix[row][ax] = 'S';
- while(1) //Unnecessary while-loop
- {
- //Check the first diagonals if a diagonal is covering the goal
- for(y = 0; y < 9; y++)
- {
- count_o = 0;
- number_of_scans = 0;
- for(row = 1 + y, ax = 0; (ax != yy + y) && (row != 0); ax++, row--) //Scan through the first diagonals
- {
- if(matrix[row][ax] == 'O')
- {
- count_o++;
- }
- number_of_scans++;
- }
- if(count_o == number_of_scans)
- {
- //matrix[row][ax] = '*';
- for(int xx = y + 1, zz = 0; (xx != 0) && (zz != number_of_scans); xx--, zz++)
- {
- matrix[xx][zz] = '*';
- }
- break;
- }
- }
- //Check verticles if one is covering the goal
- for(y = 1; y < 9; y++)
- {
- count_o = 0;
- number_of_scans = 0;
- for(yy = 1; yy < 9; yy++)
- {
- if(matrix[y][yy] == 'O')
- {
- count_o++;
- }
- number_of_scans++; //y = row, yy = column
- }
- if(count_o == number_of_scans)
- {
- while(y != 0)
- {
- matrix[y][yy] = '*';
- y--;
- }
- }
- }
- break;
- }
- }
- 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;
- void 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;
- }
- cin >> stop_program;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement