Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.50 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct ListOfRows {
  4.     int SizeOfList = 0;
  5.     struct RowOfSteps {
  6.         int IndexOfRow = 0;
  7.         int SizeOfRow = 0;
  8.         struct Step {
  9.             int x1;
  10.             int y1;
  11.             int x2;
  12.             int y2;
  13.             bool murder;
  14.             Step* pNext;
  15.         };
  16.         RowOfSteps* pNext = nullptr;
  17.         Step* HeadOfRow = nullptr;
  18.         void AddStep(int x1, int y1, int x2, int y2, bool murder) {
  19.             Step* X = new Step;
  20.             X->murder = murder;
  21.             X->pNext = nullptr;
  22.             if (HeadOfRow == nullptr) {
  23.                 HeadOfRow = X;
  24.             }
  25.             else {
  26.                 Step* Last = HeadOfRow;
  27.                 while (Last->pNext) {
  28.                     Last = Last->pNext;
  29.                 }
  30.                 Last->pNext = X;
  31.             }
  32.             X->x1 = x1;
  33.             X->y1 = y1;
  34.             X->x2 = x2;
  35.             X->y2 = y2;
  36.             ++SizeOfRow;
  37.         }
  38.         Step* Get(int index) {
  39.             Step* Current = HeadOfRow;
  40.             int k = 0;
  41.             while (k < index) {
  42.                 Current = Current->pNext;
  43.                 ++k;
  44.             }
  45.             return Current;
  46.         }
  47.         void PrintAllSteps() {
  48.             Step* Current = HeadOfRow;
  49.             while (Current) {
  50.                 if (Current->murder) {
  51.                     std::cout << Current->x1 << ":" << Current->y1 << "->(Murder)" <<//Together
  52.                         Current->x2 << ":" << Current->y2 << "|"; //Together
  53.                 }
  54.                 else {
  55.                     std::cout << Current->x1 << ":" << Current->y1 //Together
  56.                         << "->" << Current->x2 << ":" << Current->y2 << "|"; //Together
  57.                 }
  58.                 Current = Current->pNext;
  59.             }
  60.         }
  61.     };
  62.     RowOfSteps* HeadOfList = nullptr;
  63.     RowOfSteps* TailOfList = nullptr;
  64.     RowOfSteps* GetPtrOfRow(int index) {
  65.         RowOfSteps* Current = HeadOfList;
  66.         int k = 0;
  67.         while (k < index) {
  68.             Current = Current->pNext;
  69.             ++k;
  70.         }
  71.         return Current;
  72.     }
  73.     void AddRow(int index) {
  74.         RowOfSteps* X = new RowOfSteps;
  75.         X->IndexOfRow = index;
  76.         X->pNext = nullptr;
  77.         if (HeadOfList == nullptr) {
  78.             HeadOfList = X;
  79.         }
  80.         else {
  81.             RowOfSteps* Last = HeadOfList;
  82.             while (Last->pNext) {
  83.                 Last = Last->pNext;
  84.             }
  85.             Last->pNext = X;
  86.         }
  87.         TailOfList = X;
  88.         ++SizeOfList;
  89.     }
  90.     void PrintAllRows() {
  91.         RowOfSteps* Current = HeadOfList;
  92.         while (Current) {
  93.             std::cout << "(Row " << Current->IndexOfRow << "): ";
  94.             Current->PrintAllSteps();
  95.             Current = Current->pNext;
  96.         }
  97.     }
  98. };
  99.  
  100. int main() {
  101.     int numberofcells;
  102.     std::cout << "Input size of table: ";
  103.     std::cin >> numberofcells; std::cout << "\n";
  104.     //
  105.     char** mTable = new char*[numberofcells];
  106.     for (int i = 0; i < numberofcells; ++i) {
  107.         mTable[i] = new char[numberofcells];
  108.         for (int j = 0; j < numberofcells; ++j) {
  109.             mTable[i][j] = '-';
  110.         }
  111.     }
  112.     //Создание матрицы^
  113.     int k = 0;
  114.     for (int i = 0; i < numberofcells/2 - 1; ++i) {
  115.         ++k;
  116.         for (int j = 0; j < numberofcells; ++j) {
  117.             if (k % 2 == 0) {
  118.                 mTable[i][j] = 'B';
  119.             }
  120.             ++k;
  121.         }
  122.     }
  123.     //Заполнение матрицы черными шашками^
  124.     k = -1;
  125.     for (int i = numberofcells - 1; i > numberofcells / 2; --i) {
  126.         ++k;
  127.         for (int j = 0; j < numberofcells; ++j) {
  128.             if (k % 2 == 0) {
  129.                 mTable[i][j] = 'W';
  130.             }
  131.             ++k;
  132.         }
  133.     }
  134.     //Заполнение матрицы белыми фигурами^
  135.     for (int i = 0; i < numberofcells; ++i) {
  136.         for (int j = 0; j < numberofcells; ++j) {
  137.             std::cout << mTable[i][j] << " ";
  138.         }
  139.         std::cout << "\n";
  140.     }
  141.     //Вывод матрциы^
  142.     std::cout << "\n";
  143.     ListOfRows ListPlayer1;
  144.     ListOfRows ListPlayer2;
  145.     ListPlayer1.AddRow(1);
  146.     bool playing = true;
  147.     bool whiteturn = true;
  148.     int answer;
  149.     while (playing) {
  150.         std::cout << "Input action ( 1-Step; 2-Exit ): ";
  151.         std::cin >> answer;
  152.         if (answer == 1) {
  153.             int x1, x2, y1, y2;
  154.             std::cout << "Input x1 y1 -> x2 y2 : "; std::cin >> x1 >> y1 >> x2 >> y2; std::cout << "\n";
  155.             bool murder = false;
  156.             //
  157.             if (x1 + 2 == x2 && y1 + 2 == y2) {
  158.                 if (mTable[x1 + 1][y1 + 1] != mTable[x1][x2]) {
  159.                     mTable[x1 + 1][y1 + 1] = '-';
  160.                     murder = true;
  161.                 }
  162.             }
  163.             else if (x1 + 2 == x2 && y1 - 2 == y2) {
  164.                 if (mTable[x1 + 1][y1 - 1] != mTable[x1][x2]) {
  165.                     mTable[x1 + 1][y1 - 1] = '-';
  166.                     murder = true;
  167.                 }
  168.             }
  169.             else if (x1 - 2 == x2 && y1 + 2 == y2) {
  170.                 if (mTable[x1 - 1][y1 + 1] != mTable[x1][x2]) {
  171.                     mTable[x1 - 1][y1 + 1] = '-';
  172.                     murder = true;
  173.                 }
  174.             }
  175.             else if (x1 - 2 == x2 && y1 - 2 == y2) {
  176.                 if (mTable[x1 - 1][y1 - 1] != mTable[x1][x2]) {
  177.                     mTable[x1 - 1][y1 - 1] = '-';
  178.                     murder = true;
  179.                 }
  180.             }
  181.             //Проверка на убийство^
  182.             if (mTable[x1][y1] == 'B' && whiteturn == true) {
  183.                 whiteturn = false;
  184.                 ListPlayer2.AddRow(ListPlayer2.SizeOfList + 1);
  185.                 ListPlayer2.TailOfList->AddStep(x1, y1, x2, y2, murder);
  186.                 mTable[x2][y2] = 'B';
  187.             }
  188.             else if (mTable[x1][y1] == 'W' && whiteturn == false) {
  189.                 whiteturn = true;
  190.                 ListPlayer1.AddRow(ListPlayer1.SizeOfList + 1);
  191.                 ListPlayer1.TailOfList->AddStep(x1, y1, x2, y2, murder);
  192.                 mTable[x2][y2] = 'W';
  193.  
  194.             }
  195.             else if (mTable[x1][y1] == 'B' && whiteturn == false) {
  196.                 ListPlayer2.TailOfList->AddStep(x1, y1, x2, y2, murder);
  197.                 mTable[x2][y2] = 'B';
  198.             }
  199.             else if (mTable[x1][y1] == 'W' && whiteturn == true) {
  200.                 ListPlayer1.TailOfList->AddStep(x1, y1, x2, y2, murder);
  201.                 mTable[x2][y2] = 'W';
  202.             }
  203.             //Запись хода в историю^
  204.             mTable[x1][y1] = '-';
  205.         }
  206.         else {
  207.             playing = false;
  208.         }
  209.         std::cout << "Player1: "; ListPlayer1.PrintAllRows(); std::cout << "\n";
  210.         std::cout << "Player2: "; ListPlayer2.PrintAllRows(); std::cout << "\n";
  211.         //Вывод истории^
  212.         for (int i = 0; i < numberofcells; ++i) {
  213.             for (int j = 0; j < numberofcells; ++j) {
  214.                 std::cout << mTable[i][j] << " ";
  215.             }
  216.             std::cout << "\n";
  217.         }
  218.         //Вывод матрицы^
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement