Advertisement
skimono

Untitled

Dec 22nd, 2022
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. vector <string> cort;
  8. int cnt_white = 0, cnt_black = 0;
  9.  
  10. void re_count() {
  11.     for (int i = 1; i <= 8; i++) {
  12.         for (int j = 1; j <= 8; j++) {
  13.             if (cort[i][j] == 'w' || cort[i][j] == 'W') {
  14.                 cnt_white++;
  15.             }
  16.             if (cort[i][j] == 'b' || cort[i][j] == 'B') {
  17.                 cnt_black++;
  18.             }
  19.         }
  20.     }
  21.     for (int j = 1; j <= 8; j++) {
  22.         if (cort[1][j] == 'w') {
  23.             cort[1][j] = 'W';
  24.         }
  25.         if (cort[8][j] == 'b') {
  26.             cort[8][j] = 'B';
  27.         }
  28.     }
  29. }
  30.  
  31. void print() {
  32.     for (int i = 0; i <= 9; i++) {
  33.         for (int j = 0; j <= 9; j++) {
  34.             cout << cort[i][j] << " ";
  35.         }
  36.         cout << endl;
  37.     }
  38. }
  39.  
  40. void gen() {
  41.     int i;
  42.     cort[0][0] = ' ';
  43.     cort[0][9] = ' ';
  44.     cort[9][0] = ' ';
  45.     cort[9][9] = ' ';
  46.     for (i = 1; i <= 8; i++) {
  47.         cort[i][0] = '9' - i;
  48.         cort[i][9] = '9' - i;
  49.         cort[0][i] = 'a' + (i - 1);
  50.         cort[9][i] = 'a' + (i - 1);
  51.     }
  52.     for (i = 2; i <= 8; i += 2) {
  53.         cort[1][i] = 'b';
  54.     }
  55.     for (i = 1; i <= 8; i += 2) {
  56.         cort[2][i] = 'b';
  57.     }
  58.     for (i = 2; i <= 8; i += 2) {
  59.         cort[3][i] = 'b';
  60.     }
  61.     for (i = 1; i <= 8; i += 2) {
  62.         cort[6][i] = 'w';
  63.     }
  64.     for (i = 2; i <= 8; i += 2) {
  65.         cort[7][i] = 'w';
  66.     }
  67.     for (i = 1; i <= 8; i += 2) {
  68.         cort[8][i] = 'w';
  69.     }
  70. }
  71.  
  72. pair <int, int> get(int color) {
  73.     while (true) {
  74.         string action;
  75.         cin >> action;
  76.         //cout << action << endl;
  77.         int x, y;
  78.         while (action.size() != 2 || action[0] < 'a' || action[0] > 'h' || action[1] < '1' || action[1] > '8') {
  79.             cout << "Проверьте правильность ввода!" << endl;
  80.             cin >> action;
  81.         }
  82.         y = action[0] - 'a' + 1;
  83.         x = 8 - (action[1] - '1');
  84.         //cout << x << " " << y << endl;
  85.         if (color == 0) {
  86.             if (cort[x][y] != 'w' && cort[x][y] != 'W') {
  87.                 cout << "Выберите клетку, в которой находится ваша фигура!" << endl;
  88.             }
  89.             else {
  90.                 return { x, y };
  91.             }
  92.         }
  93.         else {
  94.             if (cort[x][y] != 'b' && cort[x][y] != 'B') {
  95.                 cout << "Выберите клетку, в которой находится ваша фигура!" << endl;
  96.             }
  97.             else {
  98.                 return { x, y };
  99.             }
  100.         }
  101.     }
  102. }
  103.  
  104. bool go(int x, int y, int x2, int y2, int color) {
  105.     char start = cort[x][y];
  106.     cort[x][y] = '.';
  107.     bool res = false;
  108.     if (x2 >= x && y2 >= y) {
  109.         while (x != x2 && y != y2) {
  110.             if (color == 0 && cort[x][y] != 'W' && cort[x][y] != 'w' && cort[x][y] != '.') {
  111.                 cort[x][y] = '.';
  112.                 res = true;
  113.             }
  114.             else if (color == 1 && cort[x][y] != 'B' && cort[x][y] != 'b' && cort[x][y] != '.') {
  115.                 cort[x][y] = '.';
  116.                 res = true;
  117.             }
  118.             x++;
  119.             y++;
  120.         }
  121.     }
  122.     else if (x2 >= x && y2 <= y) {
  123.         while (x != x2 && y != y2) {
  124.             if (color == 0 && cort[x][y] != 'W' && cort[x][y] != 'w' && cort[x][y] != '.') {
  125.                 cort[x][y] = '.';
  126.                 res = true;
  127.             }
  128.             else if (color == 1 && cort[x][y] != 'B' && cort[x][y] != 'b' && cort[x][y] != '.') {
  129.                 cort[x][y] = '.';
  130.                 res = true;
  131.             }
  132.             x++;
  133.             y--;
  134.         }
  135.     }
  136.     else if (x2 <= x && y2 >= y) {
  137.         while (x != x2 && y != y2) {
  138.             if (color == 0 && cort[x][y] != 'W' && cort[x][y] != 'w' && cort[x][y] != '.') {
  139.                 cort[x][y] = '.';
  140.                 res = true;
  141.             }
  142.             else if (color == 1 && cort[x][y] != 'B' && cort[x][y] != 'b' && cort[x][y] != '.') {
  143.                 cort[x][y] = '.';
  144.                 res = true;
  145.             }
  146.             x--;
  147.             y++;
  148.         }
  149.     }
  150.     else {
  151.         while (x != x2 && y != y2) {
  152.             if (color == 0 && cort[x][y] != 'W' && cort[x][y] != 'w' && cort[x][y] != '.') {
  153.                 cort[x][y] = '.';
  154.                 res = true;
  155.             }
  156.             else if (color == 1 && cort[x][y] != 'B' && cort[x][y] != 'b' && cort[x][y] != '.') {
  157.                 cort[x][y] = '.';
  158.                 res = true;
  159.             }
  160.             x--;
  161.             y--;
  162.         }
  163.     }
  164.     cort[x2][y2] = start;
  165.     return res;
  166. }
  167.  
  168. bool get2(int color, int x, int y) {
  169.     if (color == 0) {
  170.         bool flag = (cort[x][y] == 'W');
  171.         while (true) {
  172.             string action;
  173.             cin >> action;
  174.             int x2, y2;
  175.             while (action.size() != 2 || action[0] < 'a' || action[0] > 'h' || action[1] < '1' || action[1] > '8') {
  176.                 cout << "Проверьте правильность ввода!" << endl;
  177.                 cin >> action;
  178.             }
  179.             y2 = action[0] - 'a' + 1;
  180.             x2 = 8 - (action[1] - '1');
  181.             if (abs(x2 - x) == abs(y2 - y) && abs(x2 - x) > 0 && cort[x2][y2] == '.') {
  182.                 if (flag) {
  183.                     if (cort[x2][y2] == '.') {
  184.                         return go(x, y, x2, y2, 0);
  185.                     }
  186.                     else {
  187.                         cout << "Вы не можете попасть в эту клетку" << endl;
  188.                     }
  189.                 }
  190.                 else {
  191.                     if (abs(x2 - x) == 1) {
  192.                         if (cort[x2][y2] == '.' && x2 < x) {
  193.                             return go(x, y, x2, y2, 0);
  194.                         }
  195.                         else {
  196.                             cout << "Вы не можете попасть в эту клетку" << endl;
  197.                         }
  198.                     }
  199.                     else if (abs(x2 - x) == 2) {
  200.                         if (x2 >= x && y2 >= y) {
  201.                             if ((cort[x + 1][y + 1] == 'b' || cort[x + 1][y + 1] == 'B') && cort[x2][y2] == '.') {
  202.                                 return go(x, y, x2, y2, 0);
  203.                             }
  204.                             else {
  205.                                 cout << "Вы не можете попасть в эту клетку" << endl;
  206.                             }
  207.                         }
  208.                         else if (x2 >= x && y2 <= y) {
  209.                             if ((cort[x + 1][y - 1] == 'b' || cort[x + 1][y - 1] == 'B') && cort[x2][y2] == '.') {
  210.                                 return go(x, y, x2, y2, 0);
  211.                             }
  212.                             else {
  213.                                 cout << "Вы не можете попасть в эту клетку" << endl;
  214.                             }
  215.                         }
  216.                         else if (x2 <= x && y2 >= y) {
  217.                             if ((cort[x - 1][y + 1] == 'b' || cort[x - 1][y + 1] == 'B') && cort[x2][y2] == '.') {
  218.                                 return go(x, y, x2, y2, 0);
  219.                             }
  220.                             else {
  221.                                 cout << "Вы не можете попасть в эту клетку" << endl;
  222.                             }
  223.                         }
  224.                         else {
  225.                             if ((cort[x - 1][y - 1] == 'b' || cort[x - 1][y - 1] == 'B') && cort[x2][y2] == '.') {
  226.                                 return go(x, y, x2, y2, 0);
  227.                             }
  228.                             else {
  229.                                 cout << "Вы не можете попасть в эту клетку" << endl;
  230.                             }
  231.                         }
  232.                     }
  233.                     else {
  234.                         cout << "Вы не можете попасть в эту клетку" << endl;
  235.                     }
  236.                 }
  237.             }
  238.             else {
  239.                 cout << "Вы не можете попасть в эту клетку" << endl;
  240.             }
  241.         }
  242.     }
  243.     else {
  244.         bool flag = (cort[x][y] == 'B');
  245.         while (true) {
  246.             string action;
  247.             cin >> action;
  248.             int x2, y2;
  249.             while (action.size() != 2 || action[0] < 'a' || action[0] > 'h' || action[1] < '1' || action[1] > '8') {
  250.                 cout << "Проверьте правильность ввода!" << endl;
  251.                 cin >> action;
  252.             }
  253.             y2 = action[0] - 'a' + 1;
  254.             x2 = 8 - (action[1] - '1');
  255.             if (abs(x2 - x) == abs(y2 - y) && abs(x2 - x) > 0 && cort[x2][y2] == '.') {
  256.                 if (flag) {
  257.                     if (cort[x2][y2] == '.') {
  258.                         return go(x, y, x2, y2, 1);
  259.                     }
  260.                     else {
  261.                         cout << "Вы не можете попасть в эту клетку" << endl;
  262.                     }
  263.                 }
  264.                 else {
  265.                     if (abs(x2 - x) == 1) {
  266.                         if (cort[x2][y2] == '.' && x2 > x) {
  267.                             return go(x, y, x2, y2, 1);
  268.                         }
  269.                         else {
  270.                             cout << "Вы не можете попасть в эту клетку" << endl;
  271.                         }
  272.                     }
  273.                     else if (abs(x2 - x) == 2) {
  274.                         if (x2 >= x && y2 >= y) {
  275.                             if ((cort[x + 1][y + 1] == 'w' || cort[x + 1][y + 1] == 'W') && cort[x2][y2] == '.') {
  276.                                 return go(x, y, x2, y2, 1);
  277.                             }
  278.                             else {
  279.                                 cout << "Вы не можете попасть в эту клетку" << endl;
  280.                             }
  281.                         }
  282.                         else if (x2 >= x && y2 <= y) {
  283.                             if ((cort[x + 1][y - 1] == 'w' || cort[x + 1][y - 1] == 'W') && cort[x2][y2] == '.') {
  284.                                 return go(x, y, x2, y2, 1);
  285.                             }
  286.                             else {
  287.                                 cout << "Вы не можете попасть в эту клетку" << endl;
  288.                             }
  289.                         }
  290.                         else if (x2 <= x && y2 >= y) {
  291.                             if ((cort[x - 1][y + 1] == 'w' || cort[x - 1][y + 1] == 'W') && cort[x2][y2] == '.') {
  292.                                 return go(x, y, x2, y2, 1);
  293.                             }
  294.                             else {
  295.                                 cout << "Вы не можете попасть в эту клетку" << endl;
  296.                             }
  297.                         }
  298.                         else {
  299.                             if ((cort[x - 1][y - 1] == 'w' || cort[x - 1][y - 1] == 'W') && cort[x2][y2] == '.') {
  300.                                 return go(x, y, x2, y2, 1);
  301.                             }
  302.                             else {
  303.                                 cout << "Вы не можете попасть в эту клетку" << endl;
  304.                             }
  305.                         }
  306.                     }
  307.                     else {
  308.                         cout << "Вы не можете попасть в эту клетку" << endl;
  309.                     }
  310.                 }
  311.             }
  312.             else {
  313.                 cout << "Вы не можете попасть в эту клетку" << endl;
  314.             }
  315.         }
  316.     }
  317. }
  318.  
  319. int main() {
  320.     setlocale(LC_ALL, "Russian");
  321.     string name1, name2;
  322.     cout << "Введите имя первого игрока" << endl;
  323.     getline(cin, name1);
  324.     cout << "Введите имя второго игрока" << endl;
  325.     getline(cin, name2);
  326.     int i, j, x, y;
  327.     pair <int, int> res;
  328.     long long player = 0;
  329.     cort.resize(10, string("........."));
  330.     gen();
  331.     cout << "Правила:" << endl;
  332.     cout << "Стандартные правила шашек" << endl;
  333.     cout << "w - белая шашка, W - белая дамка" << endl;
  334.     cout << "b - чёрная шашка, B - чёрная дамка" << endl;
  335.     cout << "Во время хода, введите слитно координаты выбранной вами фигуры, принадлежащей вам, напрмер a3" << endl;
  336.     cout << "В следующей строке выведите координату, куда вы хотите переставить, выбранную вами фигуру" << endl;
  337.     print();
  338.     while (true) {
  339.         if (player % 2 == 0) {
  340.             cout << "Ход игрока" << " " << name1 << " " << "(белые)" << endl;
  341.             res = get(0);
  342.             x = res.first;
  343.             y = res.second;
  344.             if (!get2(0, x, y)) player++;
  345.         }
  346.         else {
  347.             cout << "Ход игрока" << " " << name2 << " " << "(чёрные)" << endl;
  348.             res = get(1);
  349.             x = res.first;
  350.             y = res.second;
  351.             if (!get2(1, x, y)) player++;
  352.         }
  353.         re_count();
  354.         print();
  355.         cout << cnt_white << " " << cnt_black << endl;
  356.         if (cnt_black == 0) {
  357.             cout << "Победитель" << " " << name1 << " " << "(белые)" << endl;
  358.             return 0;
  359.         }
  360.         else if (cnt_white == 0) {
  361.             cout << "Победитель" << " " << name2 << " " << "(чёрные)" << endl;
  362.             return 0;
  363.         }
  364.     }
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement