Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <clocale>
  3. #include <windows.h>
  4.  
  5. using  namespace std;
  6.  
  7. class Field {
  8. public:
  9.     Field(int **game_field_,int n) {
  10.         game_field = new int* [n];
  11.         for (int i=0;i<n;i++)
  12.             game_field[i] = new int[n];
  13.         for (int i=0;i<n;i++)
  14.             for (int j=0;j<n;j++)
  15.                 game_field[i][j] = game_field_[i][j];
  16.         this->n = n;
  17.     };
  18.  
  19.     virtual ~Field() {
  20.         for (int i=0;i<n;i++)
  21.             delete game_field[i];
  22.         delete [] game_field;
  23.         n = 0;
  24.     };
  25.  
  26.     bool can_move(int number) {
  27.         int i=0,j=0;
  28.         int i1=-1,j1=-1;
  29.         bool flag = false;
  30.         for (i=0;i<n;i++) {
  31.             for (j=0; j < n; j++) {
  32.                 if (game_field[i][j] == number) {
  33.                     if (i > 0 && game_field[i-1][j] == number) {
  34.                         i1 = i-1;
  35.                         j1 = j;
  36.                     }
  37.                     else if (i < (n-1) && game_field[i+1][j] == number) {
  38.                         i1 = i+1;
  39.                         j1 = j;
  40.                     }
  41.                     else if (j > 0 && game_field[i][j-1] == number) {
  42.                         i1 = i;
  43.                         j1 = j+1;
  44.                     }
  45.                     else if (j < (n-1) && game_field[i][j+1] == number) {
  46.                         i1 = i;
  47.                         j1 = j+1;
  48.                     }
  49.                     flag = true;
  50.                     break;
  51.                 }
  52.             }
  53.             if (flag)
  54.                 break;
  55.         }
  56.         if (i1 == -1 && j1 == -1)
  57.             return  i > 0 && game_field[i-1][j] == -1 || i < (n-1) && game_field[i+1][j] == -1 ||
  58.                     j > 0 && game_field[i][j-1] == -1 || j < (n-1) && game_field[i][j+1] == -1;
  59.         else {
  60.             if (j1 == j) {
  61.                 return (i1 > 0 && game_field[i1 - 1][j1] == -1) ||
  62.                        i1 < (n-1) && game_field[i1 + 1][j1] == -1 ||
  63.                        j > 0 && (game_field[i][j - 1] == -1 && game_field[i1][j - 1] == -1) ||
  64.                        j < (n-1)  && (game_field[i][j + 1] == -1 && game_field[i1][j + 1] == -1);
  65.             }
  66.             else {
  67.                 return (j1 > 0 && game_field[i][j1-1] == -1) ||
  68.                        j1 < (n-1) && game_field[i][j1+1] == -1 ||
  69.                        i > 0 && (game_field[i-1][j] == -1 && game_field[i-1][j1] == -1) ||
  70.                        i < (n-1) && (game_field[i+1][j] == -1 && game_field[i+1][j1] == -1);
  71.             }
  72.         }
  73.  
  74.     }
  75.     void print() {
  76.         for (int i = 0;i<n;i++) {
  77.             for (int j = 0; j < n; j++) {
  78.                 if (game_field[i][j] == -1)
  79.                     cout << " |   ";
  80.                 else if (game_field[i][j] < 10)
  81.                     cout << " |  " << game_field[i][j];
  82.                 else
  83.                     cout << " | " << game_field[i][j];
  84.             }
  85.             cout << " |" << endl;
  86.         }
  87.     }
  88.  
  89. private:
  90.     int **game_field;
  91.     int n;
  92. };
  93.  
  94. int main() {
  95.     int n;
  96.     setlocale(LC_ALL,"Rus");
  97.     cout << "Please, input dimension of matrix" << endl;
  98.     cin >> n;
  99.     if (n < 0) {
  100.         cout << "Error in input number (dimension matrix)" << endl;
  101.         return 1;
  102.     }
  103.     cout << "Please, input matrix of game field" << endl;
  104.     int **field = new int* [n];
  105.     for (int i=0;i<n;i++)
  106.         field[i] = new int[n];
  107.     for (int i=0;i<n;i++)
  108.         for (int j=0;j<n;j++)
  109.             cin >> field[i][j];
  110.     Field game(field,n);
  111.     game.print();
  112.     cout << "Please, input number, which you find" << endl;
  113.     int number;
  114.     cin >> number;
  115.     // пустая клетка - -1
  116.     if (number > n*n || number < -1 || number == 0) {
  117.         cout << "Error in input number (doesn't exist)" << endl;
  118.         return 1;
  119.     }
  120.     if (game.can_move(number))
  121.         cout << "You can move!" << endl;
  122.     else
  123.         cout << "You can't move!" << endl;
  124.     for (int i=0;i<n;i++)
  125.         delete field[i];
  126.     delete [] field;
  127.     system("pause");
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement