Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <clocale>
- #include <windows.h>
- using namespace std;
- class Field {
- public:
- Field(int **game_field_,int n) {
- game_field = new int* [n];
- for (int i=0;i<n;i++)
- game_field[i] = new int[n];
- for (int i=0;i<n;i++)
- for (int j=0;j<n;j++)
- game_field[i][j] = game_field_[i][j];
- this->n = n;
- };
- virtual ~Field() {
- for (int i=0;i<n;i++)
- delete game_field[i];
- delete [] game_field;
- n = 0;
- };
- bool can_move(int number) {
- int i=0,j=0;
- int i1=-1,j1=-1;
- bool flag = false;
- for (i=0;i<n;i++) {
- for (j=0; j < n; j++) {
- if (game_field[i][j] == number) {
- if (i > 0 && game_field[i-1][j] == number) {
- i1 = i-1;
- j1 = j;
- }
- else if (i < (n-1) && game_field[i+1][j] == number) {
- i1 = i+1;
- j1 = j;
- }
- else if (j > 0 && game_field[i][j-1] == number) {
- i1 = i;
- j1 = j+1;
- }
- else if (j < (n-1) && game_field[i][j+1] == number) {
- i1 = i;
- j1 = j+1;
- }
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- if (i1 == -1 && j1 == -1)
- return i > 0 && game_field[i-1][j] == -1 || i < (n-1) && game_field[i+1][j] == -1 ||
- j > 0 && game_field[i][j-1] == -1 || j < (n-1) && game_field[i][j+1] == -1;
- else {
- if (j1 == j) {
- return (i1 > 0 && game_field[i1 - 1][j1] == -1) ||
- i1 < (n-1) && game_field[i1 + 1][j1] == -1 ||
- j > 0 && (game_field[i][j - 1] == -1 && game_field[i1][j - 1] == -1) ||
- j < (n-1) && (game_field[i][j + 1] == -1 && game_field[i1][j + 1] == -1);
- }
- else {
- return (j1 > 0 && game_field[i][j1-1] == -1) ||
- j1 < (n-1) && game_field[i][j1+1] == -1 ||
- i > 0 && (game_field[i-1][j] == -1 && game_field[i-1][j1] == -1) ||
- i < (n-1) && (game_field[i+1][j] == -1 && game_field[i+1][j1] == -1);
- }
- }
- }
- void print() {
- for (int i = 0;i<n;i++) {
- for (int j = 0; j < n; j++) {
- if (game_field[i][j] == -1)
- cout << " | ";
- else if (game_field[i][j] < 10)
- cout << " | " << game_field[i][j];
- else
- cout << " | " << game_field[i][j];
- }
- cout << " |" << endl;
- }
- }
- private:
- int **game_field;
- int n;
- };
- int main() {
- int n;
- setlocale(LC_ALL,"Rus");
- cout << "Please, input dimension of matrix" << endl;
- cin >> n;
- if (n < 0) {
- cout << "Error in input number (dimension matrix)" << endl;
- return 1;
- }
- cout << "Please, input matrix of game field" << endl;
- int **field = new int* [n];
- for (int i=0;i<n;i++)
- field[i] = new int[n];
- for (int i=0;i<n;i++)
- for (int j=0;j<n;j++)
- cin >> field[i][j];
- Field game(field,n);
- game.print();
- cout << "Please, input number, which you find" << endl;
- int number;
- cin >> number;
- // пустая клетка - -1
- if (number > n*n || number < -1 || number == 0) {
- cout << "Error in input number (doesn't exist)" << endl;
- return 1;
- }
- if (game.can_move(number))
- cout << "You can move!" << endl;
- else
- cout << "You can't move!" << endl;
- for (int i=0;i<n;i++)
- delete field[i];
- delete [] field;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement