Advertisement
Guest User

Untitled

a guest
May 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 48.84 KB | None | 0 0
  1. #include <ctime>
  2. #include <stdarg.h>
  3. #include <fstream>
  4. using namespace std;
  5. #include "MyCrt.h"
  6.  
  7. double dTime = 200;
  8.  
  9. class Field;
  10. class Ship;
  11.  
  12. void drawMenu (int a, int b, int c) {
  13.     TextBackground (a);
  14.     GoToXY (5, 17);//play
  15.     cout << "          " << endl;
  16.     GoToXY (5, WhereY());
  17.     cout << "   Play   " << endl;
  18.     GoToXY (5, WhereY());
  19.     cout << "          " << endl;
  20.     TextBackground (b);
  21.     GoToXY (37, 17); //load
  22.     cout << "          " << endl;
  23.     GoToXY (37, WhereY());
  24.     cout << "   Load   " << endl;
  25.     GoToXY (37, WhereY());
  26.     cout << "          " << endl;
  27.     TextBackground (c);
  28.     GoToXY (68, 17); //load
  29.     cout << "          " << endl;
  30.     GoToXY (68, WhereY());
  31.     cout << "   Exit   " << endl;
  32.     GoToXY (68, WhereY());
  33.     cout << "          " << endl;
  34.     TextBackground (0);
  35. }
  36.  
  37. void drawResume (int a, int b, int c) {
  38.     TextBackground (a);
  39.     GoToXY (5, 32);//play 17 was
  40.     cout << "          " << endl;
  41.     GoToXY (5, WhereY());
  42.     cout << "  Resume  " << endl;
  43.     GoToXY (5, WhereY());
  44.     cout << "          " << endl;
  45.     TextBackground (b);
  46.     GoToXY (37, 32); //load
  47.     cout << "          " << endl;
  48.     GoToXY (37, WhereY());
  49.     cout << "   Save   " << endl;
  50.     GoToXY (37, WhereY());
  51.     cout << "          " << endl;
  52.     TextBackground (c);
  53.     GoToXY (68, 32); //load
  54.     cout << "          " << endl;
  55.     GoToXY (68, WhereY());
  56.     cout << "   Exit   " << endl;
  57.     GoToXY (68, WhereY());
  58.     cout << "          " << endl;
  59.     TextBackground (0);
  60. }
  61.  
  62. void resume (int &back_menu) {
  63.     //ClrScr();
  64.     int i;
  65.     drawResume (8, 7, 7);
  66.     int ch;
  67.     int a(8), b(7), c(7), sw;
  68.     while (((ch = _getch()) != 13) && (ch != 27)) {
  69.         if (ch == 75) {//left
  70.             sw = a;
  71.             a = b;
  72.             b = a;
  73.             b = sw;
  74.             b = c;
  75.             c = sw;
  76.             drawResume (a, b, c);
  77.         }
  78.         if (ch == 77) {//right
  79.             sw = b;
  80.             b = c;
  81.             c = sw;
  82.             sw = b;
  83.             b = a;
  84.             a = sw;
  85.             drawResume (a, b, c);
  86.         }
  87.     }
  88.     if ((a == 8) || (ch == 27)) {
  89.         back_menu = 1;
  90.         return;
  91.     }
  92.     if (b == 8) {
  93.         back_menu = 2;
  94.         return;
  95.     }
  96.     if (c == 8) {
  97.         back_menu = 3;
  98.         return;
  99.     }
  100. }
  101.  
  102. int inform () {
  103.     ClrScr();
  104.     int i;
  105.     drawMenu (8, 7, 7);
  106.     int ch;
  107.     int a(8), b(7), c(7), sw;
  108.     while ((ch = _getch()) != 13) {
  109.         if (ch == 75) {//left
  110.             sw = a;
  111.             a = b;
  112.             b = a;
  113.             b = sw;
  114.             b = c;
  115.             c = sw;
  116.             drawMenu (a, b, c);
  117.         }
  118.         if (ch == 77) {//right
  119.             sw = b;
  120.             b = c;
  121.             c = sw;
  122.             sw = b;
  123.             b = a;
  124.             a = sw;
  125.             drawMenu (a, b, c);
  126.         }
  127.     }
  128.     if (a == 8)
  129.         return 0;
  130.     if (b == 8)
  131.         return 1;
  132.     if (c == 8)
  133.         return 2;
  134. }
  135.  
  136. class Field {
  137. public:
  138.     int x1, y1;
  139.    
  140.     void draw (int x, int y) { //рисует сетку
  141.         GoToXY (x, y);
  142.         x1 = x;
  143.         y1 = y;
  144.         for (int i = 0; i < 5; ++i) {
  145.            
  146.             for (int j = 0; j < 5; ++j) {
  147.                 TextBackground (8);
  148.                 cout << "    ";
  149.                 TextBackground (7);
  150.                 cout << "    ";
  151.             }
  152.             cout << endl;
  153.             GoToXY (x, WhereY());
  154.             for (int j = 0; j < 5; ++j) {
  155.                 TextBackground (8);
  156.                 cout << "    ";
  157.                 TextBackground (7);
  158.                 cout << "    ";
  159.             }
  160.             cout << endl;
  161.             GoToXY (x, WhereY());
  162.             for (int j = 0; j < 5; ++j) {
  163.                 TextBackground (7);
  164.                 cout << "    ";
  165.                 TextBackground (8);
  166.                 cout << "    ";
  167.             }
  168.             cout << endl;
  169.             GoToXY (x, WhereY());
  170.             for (int j = 0; j < 5; ++j) {
  171.                 TextBackground (7);
  172.                 cout << "    ";
  173.                 TextBackground (8);
  174.                 cout << "    ";
  175.             }
  176.             cout << endl;
  177.             GoToXY (x, WhereY());
  178.         }
  179.     }
  180.  
  181.     void shipArrange (int a[10][10]) { //расставляет корабли
  182.         TextBackground (1);
  183.         for (int i = 0; i < 10; ++i)
  184.             for (int j = 0; j < 10; ++j)
  185.                 if (a[i][j] == 1) { //корабли при расстановке
  186.                     TextBackground (14); //10 - green, 12 - red
  187.                     GoToXY (x1+4*j, y1+2*i);
  188.                     cout << "    ";
  189.                     GoToXY (x1+4*j, y1+2*i+1);
  190.                     cout << "    ";
  191.                     //TextForeground (15);
  192.                 }
  193.                 else if (a[i][j] == 3) { //корабли
  194.                     TextBackground (10); //10 - green, 12 - red
  195.                     GoToXY (x1+4*j, y1+2*i);
  196.                     cout << "    ";
  197.                     GoToXY (x1+4*j, y1+2*i+1);
  198.                     cout << "    ";
  199.                     //TextBackground (1);
  200.                 }
  201.                 else if (a[i][j] == 2) { //область вокруг кораблей
  202.                     TextBackground (1);
  203.                     GoToXY (x1+4*j, y1+2*i);
  204.                     cout << "    ";
  205.                     GoToXY (x1+4*j, y1+2*i+1);
  206.                     cout << "    ";
  207.                 }
  208.                 else if (a[i][j] == 0) { //пустые клетки
  209.                     if (((i + j) % 2) == 0) {
  210.                         TextBackground (8);
  211.                         GoToXY (x1+4*j, y1+2*i);
  212.                         cout << "    ";
  213.                         GoToXY (x1+4*j, y1+2*i+1);
  214.                         cout << "    ";
  215.                     }
  216.                     else {
  217.                         TextBackground (7);
  218.                         GoToXY (x1+4*j, y1+2*i);
  219.                         cout << "    ";
  220.                         GoToXY (x1+4*j, y1+2*i+1);
  221.                         cout << "    ";
  222.                     }
  223.                 }
  224.         TextBackground (0);
  225.     }
  226.  
  227.     void refresh (int a[10][10]) {
  228.         TextBackground (1);
  229.         for (int i = 0; i < 10; ++i)
  230.             for (int j = 0; j < 10; ++j)
  231.                 if (a[i][j] == 3) { //корабли
  232.                     TextBackground (10); //10 - green, 12 - red
  233.                     GoToXY (x1+4*j, y1+2*i);
  234.                     cout << "    ";
  235.                     GoToXY (x1+4*j, y1+2*i+1);
  236.                     cout << "    ";
  237.                 }
  238.                 else if ((a[i][j] == 0)||(a[i][j] == 2)) { //пустые клетки
  239.                     if (((i + j) % 2) == 0) {
  240.                         TextBackground (8);
  241.                         GoToXY (x1+4*j, y1+2*i);
  242.                         cout << "    ";
  243.                         GoToXY (x1+4*j, y1+2*i+1);
  244.                         cout << "    ";
  245.                     }
  246.                     else {
  247.                         TextBackground (7);
  248.                         GoToXY (x1+4*j, y1+2*i);
  249.                         cout << "    ";
  250.                         GoToXY (x1+4*j, y1+2*i+1);
  251.                         cout << "    ";
  252.                     }
  253.                 }
  254.                 else if (a[i][j] == 1) { //корабли при расстановке
  255.                     TextBackground (14); //10 - green, 12 - red
  256.                     GoToXY (x1+4*j, y1+2*i);
  257.                     cout << "    ";
  258.                     GoToXY (x1+4*j, y1+2*i+1);
  259.                     cout << "    ";
  260.                 }
  261.                 else if (a[i][j] == 6) { //animation
  262.                     TextBackground (15); //10 - green, 12 - red
  263.                     GoToXY (x1+4*j, y1+2*i);
  264.                     cout << "    ";
  265.                     GoToXY (x1+4*j, y1+2*i+1);
  266.                     cout << "    ";
  267.                 }
  268.                 else if (a[i][j] == 4) { //промах
  269.                     TextBackground (1); //10 - green, 12 - red
  270.                     GoToXY (x1+4*j, y1+2*i);
  271.                     cout << "    ";
  272.                     GoToXY (x1+4*j, y1+2*i+1);
  273.                     cout << "    ";
  274.                 }
  275.                 else if (a[i][j] == 5) { //подбитые корабли
  276.                     TextBackground (12); //10 - green, 12 - red
  277.                     GoToXY (x1+4*j, y1+2*i);
  278.                     cout << "    ";
  279.                     GoToXY (x1+4*j, y1+2*i+1);
  280.                     cout << "    ";
  281.                 }
  282.         TextBackground (0);
  283.     }
  284. };
  285.  
  286. class Ship {
  287. public:
  288.    
  289.     int x, y, length, health;
  290.     bool orientation; //true - vert, false - horiz
  291. };
  292.  
  293. void matchArea (int a[10][10], int x, int y, int length, int orientation, int num) {
  294.     if (length == 4) {
  295.                 if (orientation) {
  296.                 if ((x <= 8) && (y <= 8))
  297.                     a[x+1][y+1] = num;
  298.                 if (y <= 8)
  299.                     a[x][y+1] = num;
  300.                 if (x > 0)
  301.                     a[x-1][y] = num;
  302.                 if ((x > 0) && (y > 0))
  303.                     a[x-1][y-1] = num;
  304.                 if ((x <= 8) && (y > 0))
  305.                     a[x+1][y-1] = num;
  306.                 if ((x > 0) && (y <= 8))
  307.                     a[x-1][y+1] = num;
  308.                 if (y > 0)
  309.                     a[x][y-1] = num;
  310.                 if (x <= 5)
  311.                     a[x+4][y] = num;
  312.                 if ((x <= 5) && (y > 0))
  313.                     a[x+4][y-1] = num;
  314.                 if ((x <= 6) && (y > 0))
  315.                     a[x+3][y-1] = num;
  316.                 if ((x <= 6) && (y <= 8))
  317.                     a[x+3][y+1] = num;
  318.                 if ((x <= 5) && (y <= 8))
  319.                     a[x+4][y+1] = num;
  320.                 if ((x <= 7) && (y > 0))
  321.                     a[x+2][y-1] = num;
  322.                 if ((x <= 7) && (y <= 8))
  323.                     a[x+2][y+1] = num;
  324.             }
  325.             else {
  326.                 if (y > 0)//сдева начинаю по часовой
  327.                     a[x][y-1] = num;
  328.                 if ((y > 0) && (x > 0))
  329.                     a[x-1][y-1] = num;
  330.                 if (x > 0)
  331.                     a[x-1][y] = num;
  332.                 if ((y <= 8) && (x > 0))
  333.                     a[x-1][y+1] = num;
  334.                 if ((y <= 7) && (x > 0))
  335.                     a[x-1][y+2] = num;
  336.                 if ((y <= 6) && (x > 0))
  337.                     a[x-1][y+3] = num;
  338.                 if ((y <= 5) && (x > 0))
  339.                     a[x-1][y+4] = num;
  340.                 if (y <= 5)
  341.                     a[x][y+4] = num;
  342.                 if ((y <= 6) && (x <= 8))
  343.                     a[x+1][y+3] = num;
  344.                 if ((y <= 5) && (x <= 8))
  345.                     a[x+1][y+4] = num;
  346.                 if ((y <= 7) && (x <= 8))
  347.                     a[x+1][y+2] = num;
  348.                 if ((y <= 8) && (x <= 8))
  349.                     a[x+1][y+1] = num;
  350.                 if (x <= 8)
  351.                     a[x+1][y] = num;
  352.                 if ((x <= 8) && (y > 0))
  353.                     a[x+1][y-1] = num;
  354.  
  355.             }
  356.             }
  357.             else if (length == 3) {
  358.                 if (orientation) {
  359.                 if ((x <= 8) && (y <= 8))
  360.                     a[x+1][y+1] = num;
  361.                 if (y <= 8)
  362.                     a[x][y+1] = num;
  363.                 if (x > 0)
  364.                     a[x-1][y] = num;
  365.                 if ((x > 0) && (y > 0))
  366.                     a[x-1][y-1] = num;
  367.                 if ((x <= 8) && (y > 0))
  368.                     a[x+1][y-1] = num;
  369.                 if ((x > 0) && (y <= 8))
  370.                     a[x-1][y+1] = num;
  371.                 if (y > 0)
  372.                     a[x][y-1] = num;
  373.                 if (x <= 6)
  374.                     a[x+3][y] = num;
  375.                 if ((x <= 6) && (y > 0))
  376.                     a[x+3][y-1] = num;
  377.                 if ((x <= 6) && (y <= 8))
  378.                     a[x+3][y+1] = num;
  379.                 if ((x <= 7) && (y > 0))
  380.                     a[x+2][y-1] = num;
  381.                 if ((x <= 7) && (y <= 8))
  382.                     a[x+2][y+1] = num;
  383.             }
  384.             else {
  385.                 if (y > 0)//сдева начинаю по часовой
  386.                     a[x][y-1] = num;
  387.                 if ((y > 0) && (x > 0))
  388.                     a[x-1][y-1] = num;
  389.                 if (x > 0)
  390.                     a[x-1][y] = num;
  391.                 if ((y <= 8) && (x > 0))
  392.                     a[x-1][y+1] = num;
  393.                 if ((y <= 7) && (x > 0))
  394.                     a[x-1][y+2] = num;
  395.                 if ((y <= 6) && (x > 0))
  396.                     a[x-1][y+3] = num;
  397.                 if (y <= 6)
  398.                     a[x][y+3] = num;
  399.                 if ((y <= 6) && (x <= 8))
  400.                     a[x+1][y+3] = num;
  401.                 if ((y <= 7) && (x <= 8))
  402.                     a[x+1][y+2] = num;
  403.                 if ((y <= 8) && (x <= 8))
  404.                     a[x+1][y+1] = num;
  405.                 if (x <= 8)
  406.                     a[x+1][y] = num;
  407.                 if ((x <= 8) && (y > 0))
  408.                     a[x+1][y-1] = num;
  409.             }
  410.             }
  411.             else if (length == 2) {
  412.                 if (orientation) {
  413.                 if ((x <= 8) && (y <= 8))
  414.                     a[x+1][y+1] = num;
  415.                 if (y <= 8)
  416.                     a[x][y+1] = num;
  417.                 if (x > 0)
  418.                     a[x-1][y] = num;
  419.                 if ((x > 0) && (y > 0))
  420.                     a[x-1][y-1] = num;
  421.                 if ((x <= 8) && (y > 0))
  422.                     a[x+1][y-1] = num;
  423.                 if ((x > 0) && (y <= 8))
  424.                     a[x-1][y+1] = num;
  425.                 if (y > 0)
  426.                     a[x][y-1] = num;
  427.                 if (x <= 7)
  428.                     a[x+2][y] = num;
  429.                 if ((x <= 7) && (y > 0))
  430.                     a[x+2][y-1] = num;
  431.                 if ((x <= 7) && (y <= 8))
  432.                     a[x+2][y+1] = num;     
  433.             }
  434.             else {
  435.                 if (y > 0)//сдева начинаю по часовой
  436.                     a[x][y-1] = num;
  437.                 if ((y > 0) && (x > 0))
  438.                     a[x-1][y-1] = num;
  439.                 if (x > 0)
  440.                     a[x-1][y] = num;
  441.                 if ((y <= 8) && (x > 0))
  442.                     a[x-1][y+1] = num;
  443.                 if ((y <= 7) && (x > 0))
  444.                     a[x-1][y+2] = num;
  445.                 if (y <= 7)
  446.                     a[x][y+2] = num;
  447.                 if ((y <= 7) && (x <= 8))
  448.                     a[x+1][y+2] = num;
  449.                 if ((y <= 8) && (x <= 8))
  450.                     a[x+1][y+1] = num;
  451.                 if (x <= 8)
  452.                     a[x+1][y] = num;
  453.                 if ((x <= 8) && (y > 0))
  454.                     a[x+1][y-1] = num;
  455.             }
  456.             }  
  457.             else if (length == 1) {
  458.                 if (x <= 8)
  459.                 a[x+1][y] = num;
  460.             if ((x <= 8) && (y <= 8))
  461.                 a[x+1][y+1] = num;
  462.             if (y <= 8)
  463.                 a[x][y+1] = num;
  464.             if (x > 0)
  465.                 a[x-1][y] = num;
  466.             if ((x > 0) && (y > 0))
  467.                 a[x-1][y-1] = num;
  468.             if ((x <= 8) && (y > 0))
  469.                 a[x+1][y-1] = num;
  470.             if ((x > 0) && (y <= 8))
  471.                 a[x-1][y+1] = num;
  472.             if (y > 0)
  473.                 a[x][y-1] = num;
  474.             }
  475. }
  476.  
  477. int findShipP (int x, int y, Ship ships[10]) {//номер корабля отдает
  478.         for (int i = 0; i < 10; ++i) {
  479.             for (int j = 0; j < ships[i].length; ++j) {
  480.                 if (ships[i].orientation) {
  481.                     if ((ships[i].x + j == x) && (ships[i].y == y))
  482.                         return i;
  483.                 }
  484.                 else {
  485.                     if ((ships[i].x == x) && (ships[i].y + j == y))
  486.                         return i;
  487.                 }
  488.             }
  489.         }
  490.     }
  491.  
  492. int minim (int a, int b) {
  493.     if (a < b) {
  494.         return a;
  495.     }
  496.     else
  497.         return b;
  498. }
  499.  
  500. int maxim (int a, int b) {
  501.     if (a > b) {
  502.         return a;
  503.     }
  504.     else
  505.         return b;
  506. }
  507.  
  508. class Counter {
  509. public:
  510.     int ship[4];
  511.  
  512.     Counter () {
  513.         ship[3] = 1;
  514.         ship[2] = 2;
  515.         ship[1] = 3;
  516.         ship[0] = 4;
  517.     }
  518.  
  519.     void draw (int x, int y) {
  520.         TextBackground (0);
  521.         GoToXY (x, y);
  522.         cout << "Enemy's ships amount: " << endl;
  523.         GoToXY (x, WhereY());
  524.         cout << "1's - " << ship[0] << endl;
  525.         GoToXY (x, WhereY());
  526.         cout << "2's - " << ship[1] << endl;
  527.         GoToXY (x, WhereY());
  528.         cout << "3's - " << ship[2] << endl;
  529.         GoToXY (x, WhereY());
  530.         cout << "4's - " << ship[3] << endl;
  531.         GoToXY (x, WhereY());
  532.     }
  533. };
  534.  
  535. class Player;
  536. class Bot {
  537. private:
  538.     bool check_cache (int a[10][10], int length, int x, int y, bool orientation) {
  539.         for (int i = 0; i < length; ++i)
  540.             if (orientation) {
  541.                 if (a[x+i][y] != 0)
  542.                     return true;
  543.             }
  544.             else {
  545.                 if (a[x][y+i] != 0)
  546.                     return true;
  547.             }
  548.         return false;      
  549.     }
  550. public:
  551.     friend Player;
  552.  
  553.     bool current_orientation;
  554.  
  555.     int ships_amount, free_boxes, enemy_ships[4];
  556.  
  557.     Bot () {
  558.         ships_amount = 10;
  559.         free_boxes = 100;
  560.         enemy_ships[0] = 4;
  561.         enemy_ships[1] = 3;
  562.         enemy_ships[2] = 2;
  563.         enemy_ships[3] = 1;
  564.     }
  565.  
  566.     Ship ships[10];
  567.  
  568.     int findShip (int x, int y) {//номер корабля отдает
  569.         for (int i = 0; i < 10; ++i) {
  570.             for (int j = 0; j < ships[i].length; ++j) {
  571.                 if (ships[i].orientation) {
  572.                     if ((ships[i].x + j == x) && (ships[i].y == y))
  573.                         return i;
  574.                 }
  575.                 else {
  576.                     if ((ships[i].x == x) && (ships[i].y + j == y))
  577.                         return i;
  578.                 }
  579.             }
  580.         }
  581.     }
  582.  
  583.     bool check_sides (int x, int y, int b2[10][10]) {
  584.         int sides[2];
  585.         sides[0] = 0;
  586.         sides[1] = 0;
  587.         int n = 0;
  588.         if (enemy_ships[3] != 0)
  589.             n = 3;
  590.         else if (enemy_ships[2] != 0)
  591.             n = 2;
  592.         else if (enemy_ships[1] != 0)
  593.             n = 1;
  594.         for (int i = 1; i <= n; ++i) {
  595.             if ((b2[x-i][y] == 0) && (x - i >= 0))
  596.                 ++sides[0];
  597.             if ((b2[x][y+i] == 0) && (y + i <= 9))
  598.                 ++sides[1];
  599.             if ((b2[x+i][y] == 0) && (x + i <= 9))
  600.                 ++sides[0];
  601.             if ((b2[x][y-i] == 0) && (y - i >= 0))
  602.                 ++sides[1];
  603.         }
  604.         for (int i = 0; i < 2; ++i)
  605.             if (sides[i] >= n)
  606.                 return false;
  607.         return true;
  608.     }
  609.    
  610.     void shipArrange (int a[10][10], Field &f_bot) {
  611.         int c[30];
  612.         srand (time (0));
  613.         for (int i = 0; i < 20; ++i)
  614.             c[i] = rand() % 10;
  615.         for (int i = 20; i < 30; ++i)
  616.             c[i] = rand() % 2;
  617.         for (int i = 9; i >= 0; --i) {
  618.             ships[i].orientation  = c[i+20];
  619.             if (i < 4)
  620.                 ships[i].length = 1;
  621.             else if (i < 7)
  622.                 ships[i].length = 2;
  623.             else if (i < 9)
  624.                 ships[i].length = 3;
  625.             else
  626.                 ships[i].length = 4;
  627.             ships[i].health = ships[i].length;
  628.             ships[i].x = c[i];
  629.             ships[i].y = c[i+10];
  630.             if (ships[i].orientation) {
  631.                 while ((ships[i].x + ships[i].length -1  > 9 ) || (check_cache (a, ships[i].length, ships[i].x, ships[i].y, ships[i].orientation))) {
  632.                         --ships[i].x;
  633.                         if (ships[i].x < 0) {
  634.                             ships[i].x = 9;
  635.                             ++ships[i].y;
  636.                         }
  637.                         if (ships[i].y == 10)
  638.                             ships[i].y = 0;
  639.                 }
  640.             }
  641.             else {
  642.                 while ((ships[i].y  > 10 - ships[i].length) || (check_cache (a, ships[i].length, ships[i].x, ships[i].y, ships[i].orientation))) {
  643.                         --ships[i].y;
  644.                         if (ships[i].y < 0) {
  645.                             ships[i].y = 9;
  646.                             ++ships[i].x;
  647.                         }
  648.                         if (ships[i].x == 10)
  649.                             ships[i].x = 0;
  650.                 }
  651.             }
  652.             int x = ships[i].x;
  653.             int y = ships[i].y;
  654.             if (ships[i].orientation)
  655.                 for (int j = 0; j < ships[i].length; ++j) {
  656.                     a[ships[i].x+j][ships[i].y] = 3;
  657.                 }
  658.             else
  659.                 for (int j = 0; j < ships[i].length; ++j) {
  660.                     a[ships[i].x][ships[i].y+j] = 3;
  661.                 }
  662.             matchArea (a, x, y, ships[i].length, ships[i].orientation, 2);
  663.         }
  664.     }
  665.  
  666.     void attack (int a[10][10], int b2[10][10], Field &f_player, Ship ships[10], int &ships_amount, bool &shot, int trigger[7]) {
  667.         int number = rand() % free_boxes;
  668.         --free_boxes;
  669.         int x = 0, y = 0, count = 0;
  670.         while ((count < number) || (b2[x][y] != 0) || (check_sides(x, y, b2))) {
  671.             ++x;
  672.             if (x > 9) {
  673.                 x = 0;
  674.                 ++y;
  675.             }
  676.             if (y > 9) {
  677.                 y = 0;
  678.                 x = 0;
  679.             }
  680.             if (b2[x][y] == 0)
  681.                 ++count;
  682.         }
  683.         if (a[x][y] == 3) {
  684.             a[x][y] = 6;
  685.             f_player.refresh(a);
  686.             Sleep (dTime);
  687.             a[x][y] = 5;
  688.             b2[x][y] = 5;
  689.             int num = findShipP (x, y, ships);
  690.             --ships[num].health;
  691.             trigger[0] = 1;
  692.             trigger[1] = x;
  693.             trigger[2] = y;
  694.             current_orientation = ships[num].orientation;
  695.             if (ships[num].health == 0) {
  696.                     matchArea (b2, ships[num].x, ships[num].y, ships[num].length, ships[num].orientation, 4);
  697.                     matchArea (a, ships[num].x, ships[num].y, ships[num].length, ships[num].orientation, 4);
  698.                     --ships_amount;
  699.                     trigger[0] = 0;
  700.                     f_player.refresh(a);
  701.             }
  702.             if (ships_amount != 0)
  703.                 shot = true;
  704.             else {
  705.                 shot = false;
  706.                 f_player.refresh(a);
  707.                 return;
  708.             }
  709.         }
  710.         else {
  711.             a[x][y] = 6;
  712.             f_player.refresh(a);
  713.             Sleep (dTime);
  714.             a[x][y] = 4;
  715.             b2[x][y] = 4;
  716.             shot = false;
  717.         }
  718.         f_player.refresh(a);
  719.     }
  720.  
  721.     void attack_w_trigger (int a[10][10], int b2[10][10], Field &f_player, Ship ships[10], int &ships_amount, bool &shot, int trigger[7]) {
  722.         --free_boxes;
  723.         int x = trigger[1], y = trigger[2];
  724.         if (trigger[0] == 1) {//2-x+ палубный
  725.             int posib = -1;
  726.             int places[4];
  727.             if ((x > 0) && (b2[x-1][y] == 0)) {
  728.                 ++posib;
  729.                 places[posib] = 0;
  730.             }
  731.             if ((x < 9) && (b2[x+1][y] == 0)) {
  732.                 ++posib;
  733.                 places[posib] = 1;
  734.             }
  735.             if ((y > 0) && (b2[x][y-1] == 0)) {
  736.                 ++posib;
  737.                 places[posib] = 2;
  738.             }
  739.             if ((y < 9) && (b2[x][y+1] == 0)) {
  740.                 ++posib;
  741.                 places[posib] = 3;
  742.             }
  743.             int number;
  744.             if (posib != 0)
  745.                 number = rand() % posib;
  746.             else
  747.                 number = 0;
  748.             number = places[number];
  749.             if (number == 0) {
  750.                 --x;
  751.             }
  752.             else if (number == 1) {
  753.                 ++x;
  754.             }
  755.             else if (number == 2) {
  756.                 --y;
  757.             }
  758.             else if (number == 3) {
  759.                 ++y;
  760.             }
  761.         }
  762.         else if (trigger[0] == 2) {//3-x+ палубный
  763.             int posib = -1;
  764.             int places[2];
  765.             int x1, y1, x2, y2;
  766.             if (current_orientation) {
  767.                 x1 = minim (trigger[1], trigger[3]);
  768.                 x2 = maxim (trigger[1], trigger[3]);
  769.                 y1 = trigger[2];
  770.                 if ((b2[x1-1][y1] == 0) && (x1 > 0)) {
  771.                     ++posib;
  772.                     places[posib] = 0;
  773.                 }
  774.                 if ((b2[x2+1][y1] == 0) && (x2 < 9)) {
  775.                     ++posib;
  776.                     places[posib] = 1;
  777.                 }
  778.                 int number;
  779.                 if (posib != 0)
  780.                     number = rand() % posib;
  781.                 else
  782.                     number = 0;
  783.                 number = places[number];
  784.                 y = y1;
  785.                 if (number == 0) {
  786.                     x = x1-1;
  787.                 }
  788.                 else {
  789.                     x = x2+1;
  790.                 }
  791.             }
  792.             else {
  793.                 y1 = minim (trigger[2], trigger[4]);
  794.                 y2 = maxim (trigger[2], trigger[4]);
  795.                 x1 = trigger[1];
  796.                 if ((b2[x1][y1-1] == 0) && (y1 > 0)) {
  797.                     ++posib;
  798.                     places[posib] = 0;
  799.                 }
  800.                 if ((b2[x1][y2+1] == 0) && (y2 < 9)) {
  801.                     ++posib;
  802.                     places[posib] = 1;
  803.                 }
  804.                 int number;
  805.                 if (posib != 0)
  806.                     number = rand() % posib;
  807.                 else
  808.                     number = 0;
  809.                 number = places[number];
  810.                 x = x1;
  811.                 if (number == 0) {
  812.                     y = y1-1;
  813.                 }
  814.                 else {
  815.                     y = y2+1;
  816.                 }
  817.             }
  818.         }
  819.         else if (trigger[0] == 3) {//4-x палубный
  820.             int posib = -1;
  821.             int places[2];
  822.             int x1, y1, x2, y2;
  823.             if (current_orientation) {
  824.                 x1 = minim (trigger[1], trigger[3]);
  825.                 x1 = minim (x1, trigger[5]);
  826.                 x2 = maxim (trigger[1], trigger[3]);
  827.                 x2 = maxim (x2, trigger[5]);
  828.                 y1 = trigger[2];
  829.                 if ((b2[x1-1][y1] == 0) && (x1 > 0)) {
  830.                     ++posib;
  831.                     places[posib] = 0;
  832.                 }
  833.                 if ((b2[x2+1][y1] == 0) && (x2 < 9)) {
  834.                     ++posib;
  835.                     places[posib] = 1;
  836.                 }
  837.                 int number;
  838.                 if (posib != 0)
  839.                     number = rand() % posib;
  840.                 else
  841.                     number = 0;
  842.                 number = places[number];
  843.                 y = y1;
  844.                 if (number == 0) {
  845.                     x = x1-1;
  846.                 }
  847.                 else {
  848.                     x = x2+1;
  849.                 }
  850.             }
  851.             else {
  852.                 y1 = minim (trigger[2], trigger[4]);
  853.                 y1 = minim (y1, trigger[6]);
  854.                 y2 = maxim (trigger[2], trigger[4]);
  855.                 y2 = maxim (y2, trigger[6]);
  856.                 x1 = trigger[1];
  857.                 if ((b2[x1][y1-1] == 0) && (y1 > 0)) {
  858.                     ++posib;
  859.                     places[posib] = 0;
  860.                 }
  861.                 if ((b2[x1][y2+1] == 0) && (y2 < 9)) {
  862.                     ++posib;
  863.                     places[posib] = 1;
  864.                 }
  865.                 int number;
  866.                 if (posib != 0)
  867.                     number = rand() % posib;
  868.                 else
  869.                     number = 0;
  870.                 number = places[number];
  871.                 x = x1;
  872.                 if (number == 0) {
  873.                     y = y1-1;
  874.                 }
  875.                 else {
  876.                     y = y2+1;
  877.                 }
  878.             }
  879.         }
  880.         if (a[x][y] == 3) {
  881.             a[x][y] = 6;
  882.             f_player.refresh(a);
  883.             Sleep (dTime);
  884.             a[x][y] = 5;
  885.             b2[x][y] = 5;
  886.             int num = findShipP (x, y, ships);
  887.             --ships[num].health;
  888.             ++trigger[0];
  889.             if (trigger[0] == 2) {
  890.                 trigger[3] = x;
  891.                 trigger[4] = y;
  892.             }
  893.             if (trigger[0] == 3) {
  894.                 trigger[5] = x;
  895.                 trigger[6] = y;
  896.             }
  897.             if (ships[num].health == 0) {
  898.                     matchArea (b2, ships[num].x, ships[num].y, ships[num].length, ships[num].orientation, 4);
  899.                     matchArea (a, ships[num].x, ships[num].y, ships[num].length, ships[num].orientation, 4);
  900.                     --ships_amount;
  901.                     --enemy_ships[ships[num].length-1];
  902.                     trigger[0] = 0;
  903.                     f_player.refresh(a);
  904.             }
  905.             if (ships_amount != 0)
  906.                 shot = true;
  907.             else {
  908.                 shot = false;
  909.                 f_player.refresh(a);
  910.                 return;
  911.             }
  912.         }
  913.         else {
  914.             a[x][y] = 6;
  915.             f_player.refresh(a);
  916.             Sleep (dTime);
  917.             b2[x][y] = 4;
  918.             a[x][y] = 4;
  919.             shot = false;
  920.         }
  921.         f_player.refresh(a);
  922.     }
  923. };
  924.  
  925. class Player {
  926. public:
  927.     int ships_amount;
  928.     Player () {
  929.         ships_amount = 10;
  930.     }
  931.  
  932.     int findShip (int x, int y) {//номер корабля отдает
  933.         for (int i = 0; i < 10; ++i) {
  934.             for (int j = 0; j < ships[i].length; ++j) {
  935.                 if (ships[i].orientation) {
  936.                     if ((ships[i].x + j == x) && (ships[i].y == y))
  937.                         return i;
  938.                 }
  939.                 else {
  940.                     if ((ships[i].x == x) && (ships[i].y + j == y))
  941.                         return i;
  942.                 }
  943.             }
  944.         }
  945.     }
  946.  
  947.     Ship ships[10];
  948.  
  949.     void shipArrange (int a[10][10], Field &f_player) {
  950.         int x, y;
  951.         int c, cache;
  952.         for (int i = 0; i < 4; ++i) {
  953.             x = 0;
  954.             y = 0;
  955.             cache = a[x][y];
  956.             a[x][y] = 1;
  957.             f_player.shipArrange(a);
  958.             while (((c = _getch()) != 13) || (cache != 0))
  959.             {
  960.                 if ((c == 77) && (y <= 8))//right
  961.                 {
  962.                     a[x][y] = cache;
  963.                     y = y + 1;
  964.                     cache = a[x][y];
  965.                     a[x][y] = 1;
  966.                     f_player.shipArrange(a);
  967.                 }
  968.                 if ((c == 75) && (y >= 1))//left
  969.                 {
  970.                     a[x][y] = cache;
  971.                     y = y - 1;
  972.                     cache = a[x][y];
  973.                     a[x][y] = 1;
  974.                     f_player.shipArrange(a);
  975.                 }
  976.                 if ((c == 72) && (x >= 1))//up
  977.                 {
  978.                     a[x][y] = cache;
  979.                     x = x - 1;
  980.                     cache = a[x][y];
  981.                     a[x][y] = 1;
  982.                     f_player.shipArrange(a);
  983.                 }
  984.                 if ((c == 80) && (x <= 8))//down
  985.                 {
  986.                     a[x][y] = cache;
  987.                     x = x + 1;
  988.                     cache = a[x][y];
  989.                     a[x][y] = 1;
  990.                     f_player.shipArrange(a);
  991.                 }
  992.                 if (c == 27) {
  993.                    
  994.                 }
  995.             }
  996.             a[x][y] = 3;
  997.             matchArea (a, x, y, 1, true, 2);
  998.             ships[i].x = x;
  999.             ships[i].y = y;
  1000.             ships[i].length = 1;
  1001.             ships[i].health = 1;
  1002.             ships[i].orientation = true;
  1003.     }
  1004.         int cache2;
  1005.         for (int i = 4; i < 7; ++i) { //расстановка двухалубных
  1006.             ships[i].orientation = true;
  1007.             ships[i].length = 2;
  1008.             ships[i].health = 2;
  1009.             x = 0;
  1010.             y = 0;
  1011.             cache = a[x][y];
  1012.             cache2 = a[x+1][y];
  1013.             a[x][y] = 1;
  1014.             a[x+1][y] = 1;
  1015.             f_player.shipArrange(a);
  1016.             while (((c = _getch()) != 13) || (cache != 0) || (cache2 != 0))
  1017.             {
  1018.                 if (ships[i].orientation) { //вертикальная
  1019.                     if ((c == 77) && (y <= 8))//right
  1020.                     {
  1021.                         a[x][y] = cache;
  1022.                         a[x+1][y] = cache2;
  1023.                         y = y + 1;
  1024.                         cache = a[x][y];
  1025.                         cache2 = a[x+1][y];
  1026.                         a[x][y] = 1;
  1027.                         a[x+1][y] = 1;
  1028.                         f_player.shipArrange(a);
  1029.                     }
  1030.                     if ((c == 75) && (y >= 1))//left
  1031.                     {
  1032.                         a[x][y] = cache;
  1033.                         a[x+1][y] = cache2;
  1034.                         y = y - 1;
  1035.                         cache = a[x][y];
  1036.                         cache2 = a[x+1][y];
  1037.                         a[x][y] = 1;
  1038.                         a[x+1][y] = 1;
  1039.                         f_player.shipArrange(a);
  1040.                     }
  1041.                     if ((c == 72) && (x >= 1))//up
  1042.                     {
  1043.                         a[x][y] = cache;
  1044.                         a[x+1][y] = cache2;
  1045.                         x = x - 1;
  1046.                         cache = a[x][y];
  1047.                         cache2 = a[x+1][y];
  1048.                         a[x][y] = 1;
  1049.                         a[x+1][y] = 1;
  1050.                         f_player.shipArrange(a);
  1051.                     }
  1052.                     if ((c == 80) && (x <= 7))//down
  1053.                     {
  1054.                         a[x][y] = cache;
  1055.                         a[x+1][y] = cache2;
  1056.                         x = x + 1;
  1057.                         cache = a[x][y];
  1058.                         cache2 = a[x+1][y];
  1059.                         a[x][y] = 1;
  1060.                         a[x+1][y] = 1;
  1061.                         f_player.shipArrange(a);
  1062.                     }
  1063.                     if ((c == 43) && (y <= 8)) { //смена ориентации
  1064.                         ships[i].orientation = false;
  1065.                         a[x+1][y] = cache2;
  1066.                         cache2 = a[x][y+1];
  1067.                         a[x][y+1] = 1;
  1068.                         f_player.shipArrange(a);
  1069.                     }
  1070.                 }
  1071.                 else {   //горизонтальная
  1072.                     if ((c == 77) && (y <= 7))//right
  1073.                     {
  1074.                         a[x][y] = cache;
  1075.                         a[x][y+1] = cache2;
  1076.                         y = y + 1;
  1077.                         cache = a[x][y];
  1078.                         cache2 = a[x][y+1];
  1079.                         a[x][y] = 1;
  1080.                         a[x][y+1] = 1;
  1081.                         f_player.shipArrange(a);
  1082.                     }
  1083.                     if ((c == 75) && (y >= 1))//left
  1084.                     {
  1085.                         a[x][y] = cache;
  1086.                         a[x][y+1] = cache2;
  1087.                         y = y - 1;
  1088.                         cache = a[x][y];
  1089.                         cache2 = a[x][y+1];
  1090.                         a[x][y] = 1;
  1091.                         a[x][y+1] = 1;
  1092.                         f_player.shipArrange(a);
  1093.                     }
  1094.                     if ((c == 72) && (x >= 1))//up
  1095.                     {
  1096.                         a[x][y] = cache;
  1097.                         a[x][y+1] = cache2;
  1098.                         x = x - 1;
  1099.                         cache = a[x][y];
  1100.                         cache2 = a[x][y+1];
  1101.                         a[x][y] = 1;
  1102.                         a[x][y+1] = 1;
  1103.                         f_player.shipArrange(a);
  1104.                     }
  1105.                     if ((c == 80) && (x <= 8))//down
  1106.                     {
  1107.                         a[x][y] = cache;
  1108.                         a[x][y+1] = cache2;
  1109.                         x = x + 1;
  1110.                         cache = a[x][y];
  1111.                         cache2 = a[x][y+1];
  1112.                         a[x][y] = 1;
  1113.                         a[x][y+1] = 1;
  1114.                         f_player.shipArrange(a);
  1115.                     }
  1116.                     if ((c == 43) && (x <= 8)) { //смена ориентации
  1117.                         ships[i].orientation = true;
  1118.                         a[x][y+1] = cache2;
  1119.                         cache2 = a[x+1][y];
  1120.                         a[x+1][y] = 1;
  1121.                         f_player.shipArrange(a);
  1122.                     }
  1123.                 }
  1124.             }
  1125.             if (ships[i].orientation) {
  1126.                 a[x][y] = 3;
  1127.                 a[x+1][y] = 3;
  1128.                 if ((x <= 8) && (y <= 8))
  1129.                     a[x+1][y+1] = 2;
  1130.                 if (y <= 8)
  1131.                     a[x][y+1] = 2;
  1132.                 if (x > 0)
  1133.                     a[x-1][y] = 2;
  1134.                 if ((x > 0) && (y > 0))
  1135.                     a[x-1][y-1] = 2;
  1136.                 if ((x <= 8) && (y > 0))
  1137.                     a[x+1][y-1] = 2;
  1138.                 if ((x > 0) && (y <= 8))
  1139.                     a[x-1][y+1] = 2;
  1140.                 if (y > 0)
  1141.                     a[x][y-1] = 2;
  1142.                 if (x <= 7)
  1143.                     a[x+2][y] = 2;
  1144.                 if ((x <= 7) && (y > 0))
  1145.                     a[x+2][y-1] = 2;
  1146.                 if ((x <= 7) && (y <= 8))
  1147.                     a[x+2][y+1] = 2;
  1148.             }
  1149.             else {
  1150.                 a[x][y] = 3;
  1151.                 a[x][y+1] = 3;
  1152.                 if (y > 0)//сдева начинаю по часовой
  1153.                     a[x][y-1] = 2;
  1154.                 if ((y > 0) && (x > 0))
  1155.                     a[x-1][y-1] = 2;
  1156.                 if (x > 0)
  1157.                     a[x-1][y] = 2;
  1158.                 if ((y <= 8) && (x > 0))
  1159.                     a[x-1][y+1] = 2;
  1160.                 if ((y <= 7) && (x > 0))
  1161.                     a[x-1][y+2] = 2;
  1162.                 if (y <= 7)
  1163.                     a[x][y+2] = 2;
  1164.                 if ((y <= 7) && (x <= 8))
  1165.                     a[x+1][y+2] = 2;
  1166.                 if ((y <= 8) && (x <= 8))
  1167.                     a[x+1][y+1] = 2;
  1168.                 if (x <= 8)
  1169.                     a[x+1][y] = 2;
  1170.                 if ((x <= 8) && (y > 0))
  1171.                     a[x+1][y-1] = 2;
  1172.             }
  1173.             ships[i].x = x;
  1174.             ships[i].y = y;
  1175.     }
  1176.     int cache3;
  1177.         for (int i = 7; i < 9; ++i) { //расстановка трехалубных
  1178.             ships[i].orientation = true;
  1179.             ships[i].length = 3;
  1180.             ships[i].health = 3;
  1181.             x = 0;
  1182.             y = 0;
  1183.             cache = a[x][y];
  1184.             cache2 = a[x+1][y];
  1185.             cache3 = a[x+2][y];
  1186.             a[x][y] = 1;
  1187.             a[x+1][y] = 1;
  1188.             a[x+2][y] = 1;
  1189.             f_player.shipArrange(a);
  1190.             while (((c = _getch()) != 13) || (cache != 0) || (cache2 != 0) || (cache3 != 0))
  1191.             {
  1192.                 if (ships[i].orientation) { //вертикальная
  1193.                     if ((c == 77) && (y <= 8))//right
  1194.                     {
  1195.                         a[x][y] = cache;
  1196.                         a[x+1][y] = cache2;
  1197.                         a[x+2][y] = cache3;
  1198.                         y = y + 1;
  1199.                         cache = a[x][y];
  1200.                         cache2 = a[x+1][y];
  1201.                         cache3 = a[x+2][y];
  1202.                         a[x][y] = 1;
  1203.                         a[x+1][y] = 1;
  1204.                         a[x+2][y] = 1;
  1205.                         f_player.shipArrange(a);
  1206.                     }
  1207.                     if ((c == 75) && (y >= 1))//left
  1208.                     {
  1209.                         a[x][y] = cache;
  1210.                         a[x+1][y] = cache2;
  1211.                         a[x+2][y] = cache3;
  1212.                         y = y - 1;
  1213.                         cache = a[x][y];
  1214.                         cache2 = a[x+1][y];
  1215.                         cache3 = a[x+2][y];
  1216.                         a[x][y] = 1;
  1217.                         a[x+1][y] = 1;
  1218.                         a[x+2][y] = 1;
  1219.                         f_player.shipArrange(a);
  1220.                     }
  1221.                     if ((c == 72) && (x >= 1))//up
  1222.                     {
  1223.                         a[x][y] = cache;
  1224.                         a[x+1][y] = cache2;
  1225.                         a[x+2][y] = cache3;
  1226.                         x = x - 1;
  1227.                         cache = a[x][y];
  1228.                         cache2 = a[x+1][y];
  1229.                         cache3 = a[x+2][y];
  1230.                         a[x][y] = 1;
  1231.                         a[x+1][y] = 1;
  1232.                         a[x+2][y] = 1;
  1233.                         f_player.shipArrange(a);
  1234.                     }
  1235.                     if ((c == 80) && (x <= 6))//down
  1236.                     {
  1237.                         a[x][y] = cache;
  1238.                         a[x+1][y] = cache2;
  1239.                         a[x+2][y] = cache3;
  1240.                         x = x + 1;
  1241.                         cache = a[x][y];
  1242.                         cache2 = a[x+1][y];
  1243.                         cache3 = a[x+2][y];
  1244.                         a[x][y] = 1;
  1245.                         a[x+1][y] = 1;
  1246.                         a[x+2][y] = 1;
  1247.                         f_player.shipArrange(a);
  1248.                     }
  1249.                     if ((c == 43) && (y <= 7)) { //смена ориентации
  1250.                         ships[i].orientation = false;
  1251.                         a[x+1][y] = cache2;
  1252.                         a[x+2][y] = cache3;
  1253.                         cache2 = a[x][y+1];
  1254.                         cache3 = a[x][y+2];
  1255.                         a[x][y+1] = 1;
  1256.                         a[x][y+2] = 1;
  1257.                         f_player.shipArrange(a);
  1258.                     }
  1259.                 }
  1260.                 else {   //горизонтальная
  1261.                     if ((c == 77) && (y <= 6))//right
  1262.                     {
  1263.                         a[x][y] = cache;
  1264.                         a[x][y+1] = cache2;
  1265.                         a[x][y+2] = cache3;
  1266.                         y = y + 1;
  1267.                         cache = a[x][y];
  1268.                         cache2 = a[x][y+1];
  1269.                         cache3 = a[x][y+2];
  1270.                         a[x][y] = 1;
  1271.                         a[x][y+1] = 1;
  1272.                         a[x][y+2] = 1;
  1273.                         f_player.shipArrange(a);
  1274.                     }
  1275.                     if ((c == 75) && (y >= 1))//left
  1276.                     {
  1277.                         a[x][y] = cache;
  1278.                         a[x][y+1] = cache2;
  1279.                         a[x][y+2] = cache3;
  1280.                         y = y - 1;
  1281.                         cache = a[x][y];
  1282.                         cache2 = a[x][y+1];
  1283.                         cache3 = a[x][y+2];
  1284.                         a[x][y] = 1;
  1285.                         a[x][y+1] = 1;
  1286.                         a[x][y+2] = 1;
  1287.                         f_player.shipArrange(a);
  1288.                     }
  1289.                     if ((c == 72) && (x >= 1))//up
  1290.                     {
  1291.                         a[x][y] = cache;
  1292.                         a[x][y+1] = cache2;
  1293.                         a[x][y+2] = cache3;
  1294.                         x = x - 1;
  1295.                         cache = a[x][y];
  1296.                         cache2 = a[x][y+1];
  1297.                         cache3 = a[x][y+2];
  1298.                         a[x][y] = 1;
  1299.                         a[x][y+1] = 1;
  1300.                         a[x][y+2] = 1;
  1301.                         f_player.shipArrange(a);
  1302.                     }
  1303.                     if ((c == 80) && (x <= 8))//down
  1304.                     {
  1305.                         a[x][y] = cache;
  1306.                         a[x][y+1] = cache2;
  1307.                         a[x][y+2] = cache3;
  1308.                         x = x + 1;
  1309.                         cache = a[x][y];
  1310.                         cache2 = a[x][y+1];
  1311.                         cache3 = a[x][y+2];
  1312.                         a[x][y] = 1;
  1313.                         a[x][y+1] = 1;
  1314.                         a[x][y+2] = 1;
  1315.                         f_player.shipArrange(a);
  1316.                     }
  1317.                     if ((c == 43) && (x <= 7)) { //смена ориентации
  1318.                         ships[i].orientation = true;
  1319.                         a[x][y+1] = cache2;
  1320.                         a[x][y+2] = cache3;
  1321.                         cache2 = a[x+1][y];
  1322.                         cache3 = a[x+2][y];
  1323.                         a[x+1][y] = 1;
  1324.                         a[x+2][y] = 1;
  1325.                         f_player.shipArrange(a);
  1326.                     }
  1327.                 }
  1328.             }
  1329.             if (ships[i].orientation) {
  1330.                 a[x][y] = 3;
  1331.                 a[x+1][y] = 3;
  1332.                 a[x+2][y] = 3;
  1333.                 if ((x <= 8) && (y <= 8))
  1334.                     a[x+1][y+1] = 2;
  1335.                 if (y <= 8)
  1336.                     a[x][y+1] = 2;
  1337.                 if (x > 0)
  1338.                     a[x-1][y] = 2;
  1339.                 if ((x > 0) && (y > 0))
  1340.                     a[x-1][y-1] = 2;
  1341.                 if ((x <= 8) && (y > 0))
  1342.                     a[x+1][y-1] = 2;
  1343.                 if ((x > 0) && (y <= 8))
  1344.                     a[x-1][y+1] = 2;
  1345.                 if (y > 0)
  1346.                     a[x][y-1] = 2;
  1347.                 if (x <= 6)
  1348.                     a[x+3][y] = 2;
  1349.                 if ((x <= 6) && (y > 0))
  1350.                     a[x+3][y-1] = 2;
  1351.                 if ((x <= 6) && (y <= 8))
  1352.                     a[x+3][y+1] = 2;
  1353.                 if ((x <= 7) && (y > 0))
  1354.                     a[x+2][y-1] = 2;
  1355.                 if ((x <= 7) && (y <= 8))
  1356.                     a[x+2][y+1] = 2;
  1357.             }
  1358.             else {
  1359.                 a[x][y] = 3;
  1360.                 a[x][y+1] = 3;
  1361.                 a[x][y+2] = 3;
  1362.                 if (y > 0)//сдева начинаю по часовой
  1363.                     a[x][y-1] = 2;
  1364.                 if ((y > 0) && (x > 0))
  1365.                     a[x-1][y-1] = 2;
  1366.                 if (x > 0)
  1367.                     a[x-1][y] = 2;
  1368.                 if ((y <= 8) && (x > 0))
  1369.                     a[x-1][y+1] = 2;
  1370.                 if ((y <= 7) && (x > 0))
  1371.                     a[x-1][y+2] = 2;
  1372.                 if ((y <= 6) && (x > 0))
  1373.                     a[x-1][y+3] = 2;
  1374.                 if (y <= 6)
  1375.                     a[x][y+3] = 2;
  1376.                 if ((y <= 6) && (x <= 8))
  1377.                     a[x+1][y+3] = 2;
  1378.                 if ((y <= 7) && (x <= 8))
  1379.                     a[x+1][y+2] = 2;
  1380.                 if ((y <= 8) && (x <= 8))
  1381.                     a[x+1][y+1] = 2;
  1382.                 if (x <= 8)
  1383.                     a[x+1][y] = 2;
  1384.                 if ((x <= 8) && (y > 0))
  1385.                     a[x+1][y-1] = 2;
  1386.             }
  1387.             ships[i].x = x;
  1388.             ships[i].y = y;
  1389.     }
  1390.     int cache4;
  1391.         for (int i = 9; i < 10; ++i) { //расстановка четырехпаалубных
  1392.             ships[i].orientation = true;
  1393.             ships[i].length = 4;
  1394.             ships[i].health = 4;
  1395.             x = 0;
  1396.             y = 0;
  1397.             cache = a[x][y];
  1398.             cache2 = a[x+1][y];
  1399.             cache3 = a[x+2][y];
  1400.             cache4 = a[x+3][y];
  1401.             a[x][y] = 1;
  1402.             a[x+1][y] = 1;
  1403.             a[x+2][y] = 1;
  1404.             a[x+3][y] = 1;
  1405.             f_player.shipArrange(a);
  1406.             while (((c = _getch()) != 13) || (cache != 0) || (cache2 != 0) || (cache3 != 0) || (cache4 != 0))
  1407.             {
  1408.                 if (ships[i].orientation) { //вертикальная
  1409.                     if ((c == 77) && (y <= 8))//right
  1410.                     {
  1411.                         a[x][y] = cache;
  1412.                         a[x+1][y] = cache2;
  1413.                         a[x+2][y] = cache3;
  1414.                         a[x+3][y] = cache4;
  1415.                         y = y + 1;
  1416.                         cache = a[x][y];
  1417.                         cache2 = a[x+1][y];
  1418.                         cache3 = a[x+2][y];
  1419.                         cache4 = a[x+3][y];
  1420.                         a[x][y] = 1;
  1421.                         a[x+1][y] = 1;
  1422.                         a[x+2][y] = 1;
  1423.                         a[x+3][y] = 1;
  1424.                         f_player.shipArrange(a);
  1425.                     }
  1426.                     if ((c == 75) && (y >= 1))//left
  1427.                     {
  1428.                         a[x][y] = cache;
  1429.                         a[x+1][y] = cache2;
  1430.                         a[x+2][y] = cache3;
  1431.                         a[x+3][y] = cache4;
  1432.                         y = y - 1;
  1433.                         cache = a[x][y];
  1434.                         cache2 = a[x+1][y];
  1435.                         cache3 = a[x+2][y];
  1436.                         cache4 = a[x+3][y];
  1437.                         a[x][y] = 1;
  1438.                         a[x+1][y] = 1;
  1439.                         a[x+2][y] = 1;
  1440.                         a[x+3][y] = 1;
  1441.                         f_player.shipArrange(a);
  1442.                     }
  1443.                     if ((c == 72) && (x >= 1))//up
  1444.                     {
  1445.                         a[x][y] = cache;
  1446.                         a[x+1][y] = cache2;
  1447.                         a[x+2][y] = cache3;
  1448.                         a[x+3][y] = cache4;
  1449.                         x = x - 1;
  1450.                         cache = a[x][y];
  1451.                         cache2 = a[x+1][y];
  1452.                         cache3 = a[x+2][y];
  1453.                         cache4 = a[x+3][y];
  1454.                         a[x][y] = 1;
  1455.                         a[x+1][y] = 1;
  1456.                         a[x+2][y] = 1;
  1457.                         a[x+3][y] = 1;
  1458.                         f_player.shipArrange(a);
  1459.                     }
  1460.                     if ((c == 80) && (x <= 5))//down
  1461.                     {
  1462.                         a[x][y] = cache;
  1463.                         a[x+1][y] = cache2;
  1464.                         a[x+2][y] = cache3;
  1465.                         a[x+3][y] = cache4;
  1466.                         x = x + 1;
  1467.                         cache = a[x][y];
  1468.                         cache2 = a[x+1][y];
  1469.                         cache3 = a[x+2][y];
  1470.                         cache4 = a[x+3][y];
  1471.                         a[x][y] = 1;
  1472.                         a[x+1][y] = 1;
  1473.                         a[x+2][y] = 1;
  1474.                         a[x+3][y] = 1;
  1475.                         f_player.shipArrange(a);
  1476.                     }
  1477.                     if ((c == 43) && (y <= 6)) { //смена ориентации
  1478.                         ships[i].orientation = false;
  1479.                         a[x+1][y] = cache2;
  1480.                         a[x+2][y] = cache3;
  1481.                         a[x+3][y] = cache4;
  1482.                         cache2 = a[x][y+1];
  1483.                         cache3 = a[x][y+2];
  1484.                         cache4 = a[x][y+3];
  1485.                         a[x][y+1] = 1;
  1486.                         a[x][y+2] = 1;
  1487.                         a[x][y+3] = 1;
  1488.                         f_player.shipArrange(a);
  1489.                     }
  1490.                 }
  1491.                 else {   //горизонтальная
  1492.                     if ((c == 77) && (y <= 5))//right
  1493.                     {
  1494.                         a[x][y] = cache;
  1495.                         a[x][y+1] = cache2;
  1496.                         a[x][y+2] = cache3;
  1497.                         a[x][y+3] = cache4;
  1498.                         y = y + 1;
  1499.                         cache = a[x][y];
  1500.                         cache2 = a[x][y+1];
  1501.                         cache3 = a[x][y+2];
  1502.                         cache4 = a[x][y+3];
  1503.                         a[x][y] = 1;
  1504.                         a[x][y+1] = 1;
  1505.                         a[x][y+2] = 1;
  1506.                         a[x][y+3] = 1;
  1507.                         f_player.shipArrange(a);
  1508.                     }
  1509.                     if ((c == 75) && (y >= 1))//left
  1510.                     {
  1511.                         a[x][y] = cache;
  1512.                         a[x][y+1] = cache2;
  1513.                         a[x][y+2] = cache3;
  1514.                         a[x][y+3] = cache4;
  1515.                         y = y - 1;
  1516.                         cache = a[x][y];
  1517.                         cache2 = a[x][y+1];
  1518.                         cache3 = a[x][y+2];
  1519.                         cache4 = a[x][y+3];
  1520.                         a[x][y] = 1;
  1521.                         a[x][y+1] = 1;
  1522.                         a[x][y+2] = 1;
  1523.                         a[x][y+3] = 1;
  1524.                         f_player.shipArrange(a);
  1525.                     }
  1526.                     if ((c == 72) && (x >= 1))//up
  1527.                     {
  1528.                         a[x][y] = cache;
  1529.                         a[x][y+1] = cache2;
  1530.                         a[x][y+2] = cache3;
  1531.                         a[x][y+3] = cache4;
  1532.                         x = x - 1;
  1533.                         cache = a[x][y];
  1534.                         cache2 = a[x][y+1];
  1535.                         cache3 = a[x][y+2];
  1536.                         cache4 = a[x][y+3];
  1537.                         a[x][y] = 1;
  1538.                         a[x][y+1] = 1;
  1539.                         a[x][y+2] = 1;
  1540.                         a[x][y+3] = 1;
  1541.                         f_player.shipArrange(a);
  1542.                     }
  1543.                     if ((c == 80) && (x <= 8))//down
  1544.                     {
  1545.                         a[x][y] = cache;
  1546.                         a[x][y+1] = cache2;
  1547.                         a[x][y+2] = cache3;
  1548.                         a[x][y+3] = cache4;
  1549.                         x = x + 1;
  1550.                         cache = a[x][y];
  1551.                         cache2 = a[x][y+1];
  1552.                         cache3 = a[x][y+2];
  1553.                         cache4 = a[x][y+3];
  1554.                         a[x][y] = 1;
  1555.                         a[x][y+1] = 1;
  1556.                         a[x][y+2] = 1;
  1557.                         a[x][y+3] = 1;
  1558.                         f_player.shipArrange(a);
  1559.                     }
  1560.                     if ((c == 43) && (x <= 6)) { //смена ориентации
  1561.                         ships[i].orientation = true;
  1562.                         a[x][y+1] = cache2;
  1563.                         a[x][y+2] = cache3;
  1564.                         a[x][y+3] = cache4;
  1565.                         cache2 = a[x+1][y];
  1566.                         cache3 = a[x+2][y];
  1567.                         cache4 = a[x+3][y];
  1568.                         a[x+1][y] = 1;
  1569.                         a[x+2][y] = 1;
  1570.                         a[x+3][y] = 1;
  1571.                         f_player.shipArrange(a);
  1572.                     }
  1573.                 }
  1574.             }
  1575.             if (ships[i].orientation) {
  1576.                 a[x][y] = 3;
  1577.                 a[x+1][y] = 3;
  1578.                 a[x+2][y] = 3;
  1579.                 a[x+3][y] = 3;
  1580.                 if ((x <= 8) && (y <= 8))
  1581.                     a[x+1][y+1] = 2;
  1582.                 if (y <= 8)
  1583.                     a[x][y+1] = 2;
  1584.                 if (x > 0)
  1585.                     a[x-1][y] = 2;
  1586.                 if ((x > 0) && (y > 0))
  1587.                     a[x-1][y-1] = 2;
  1588.                 if ((x <= 8) && (y > 0))
  1589.                     a[x+1][y-1] = 2;
  1590.                 if ((x > 0) && (y <= 8))
  1591.                     a[x-1][y+1] = 2;
  1592.                 if (y > 0)
  1593.                     a[x][y-1] = 2;
  1594.                 if (x <= 5)
  1595.                     a[x+4][y] = 2;
  1596.                 if ((x <= 5) && (y > 0))
  1597.                     a[x+4][y-1] = 2;
  1598.                 if ((x <= 6) && (y > 0))
  1599.                     a[x+3][y-1] = 2;
  1600.                 if ((x <= 6) && (y <= 8))
  1601.                     a[x+3][y+1] = 2;
  1602.                 if ((x <= 5) && (y <= 8))
  1603.                     a[x+4][y+1] = 2;
  1604.                 if ((x <= 7) && (y > 0))
  1605.                     a[x+2][y-1] = 2;
  1606.                 if ((x <= 7) && (y <= 8))
  1607.                     a[x+2][y+1] = 2;
  1608.             }
  1609.             else {
  1610.                 a[x][y] = 3;
  1611.                 a[x][y+1] = 3;
  1612.                 a[x][y+2] = 3;
  1613.                 a[x][y+3] = 3;
  1614.                 if (y > 0)//сдева начинаю по часовой
  1615.                     a[x][y-1] = 2;
  1616.                 if ((y > 0) && (x > 0))
  1617.                     a[x-1][y-1] = 2;
  1618.                 if (x > 0)
  1619.                     a[x-1][y] = 2;
  1620.                 if ((y <= 8) && (x > 0))
  1621.                     a[x-1][y+1] = 2;
  1622.                 if ((y <= 7) && (x > 0))
  1623.                     a[x-1][y+2] = 2;
  1624.                 if ((y <= 6) && (x > 0))
  1625.                     a[x-1][y+3] = 2;
  1626.                 if ((y <= 5) && (x > 0))
  1627.                     a[x-1][y+4] = 2;
  1628.                 if (y <= 5)
  1629.                     a[x][y+4] = 2;
  1630.                 if ((y <= 6) && (x <= 8))
  1631.                     a[x+1][y+3] = 2;
  1632.                 if ((y <= 5) && (x <= 8))
  1633.                     a[x+1][y+4] = 2;
  1634.                 if ((y <= 7) && (x <= 8))
  1635.                     a[x+1][y+2] = 2;
  1636.                 if ((y <= 8) && (x <= 8))
  1637.                     a[x+1][y+1] = 2;
  1638.                 if (x <= 8)
  1639.                     a[x+1][y] = 2;
  1640.                 if ((x <= 8) && (y > 0))
  1641.                     a[x+1][y-1] = 2;
  1642.             }
  1643.             ships[i].x = x;
  1644.             ships[i].y = y;
  1645.     }
  1646.     }
  1647.  
  1648.     void attack (int b[10][10], int a2[10][10], Field &f_bot, Bot &bot, bool &shot, int &x, int &y, Counter &counter, int &back_menu, Field &f_player, int a[10][10], bool done) {
  1649.         int c, cache, num;
  1650.         f_bot.refresh(a2);
  1651.         if (!done) {
  1652.             GoToXY (3, 1);
  1653.             cout << "Something went wrong, save was not done";
  1654.         }
  1655.         done = true;
  1656.         cache = a2[x][y];
  1657.         a2[x][y] = 1;
  1658.             while (((c = _getch()) != 13) || (cache != 0))
  1659.             {
  1660.                 if ((c == 77) && (y <= 8))//right
  1661.                 {
  1662.                     a2[x][y] = cache;
  1663.                     y = y + 1;
  1664.                     cache = a2[x][y];
  1665.                     a2[x][y] = 1;
  1666.                     f_bot.refresh(a2);
  1667.                 }
  1668.                 else if ((c == 77) && (y > 8))//right!!!
  1669.                 {
  1670.                     a2[x][y] = cache;
  1671.                     y = 0;
  1672.                     cache = a2[x][y];
  1673.                     a2[x][y] = 1;
  1674.                     f_bot.refresh(a2);
  1675.                 }
  1676.                 if ((c == 75) && (y >= 1))//left
  1677.                 {
  1678.                     a2[x][y] = cache;
  1679.                     y = y - 1;
  1680.                     cache = a2[x][y];
  1681.                     a2[x][y] = 1;
  1682.                     f_bot.refresh(a2);
  1683.                 }
  1684.                 else if ((c == 75) && (y < 1))//left!!!
  1685.                 {
  1686.                     a2[x][y] = cache;
  1687.                     y = 9;
  1688.                     cache = a2[x][y];
  1689.                     a2[x][y] = 1;
  1690.                     f_bot.refresh(a2);
  1691.                 }
  1692.                 if ((c == 72) && (x >= 1))//up
  1693.                 {
  1694.                     a2[x][y] = cache;
  1695.                     x = x - 1;
  1696.                     cache = a2[x][y];
  1697.                     a2[x][y] = 1;
  1698.                     f_bot.refresh(a2);
  1699.                 }
  1700.                 else if ((c == 72) && (x < 1))//up!!!
  1701.                 {
  1702.                     a2[x][y] = cache;
  1703.                     x = 9;
  1704.                     cache = a2[x][y];
  1705.                     a2[x][y] = 1;
  1706.                     f_bot.refresh(a2);
  1707.                 }
  1708.                 if ((c == 80) && (x <= 8))//down
  1709.                 {
  1710.                     a2[x][y] = cache;
  1711.                     x = x + 1;
  1712.                     cache = a2[x][y];
  1713.                     a2[x][y] = 1;
  1714.                     f_bot.refresh(a2);
  1715.                 }
  1716.                 else if ((c == 80) && (x > 8))//down
  1717.                 {
  1718.                     a2[x][y] = cache;
  1719.                     x = 0;
  1720.                     cache = a2[x][y];
  1721.                     a2[x][y] = 1;
  1722.                     f_bot.refresh(a2);
  1723.                 }
  1724.                 if (c == 27) {
  1725.                     back_menu = 0;
  1726.                     resume (back_menu);
  1727.                     if ((back_menu == 1) || (back_menu == 2)) {
  1728.                         a2[x][y] = cache;
  1729.                         TextBackground (0);
  1730.                         GoToXY (5, 32);//play 17 was
  1731.                         cout << "          " << endl;
  1732.                         GoToXY (5, WhereY());
  1733.                         cout << "          " << endl;
  1734.                         GoToXY (5, WhereY());
  1735.                         cout << "          " << endl;
  1736.                         GoToXY (37, 32); //load
  1737.                         cout << "          " << endl;
  1738.                         GoToXY (37, WhereY());
  1739.                         cout << "          " << endl;
  1740.                         GoToXY (37, WhereY());
  1741.                         cout << "          " << endl;
  1742.                         GoToXY (68, 32); //load
  1743.                         cout << "          " << endl;
  1744.                         GoToXY (68, WhereY());
  1745.                         cout << "          " << endl;
  1746.                         GoToXY (68, WhereY());
  1747.                         cout << "          " << endl;
  1748.                         return;
  1749.                     }
  1750.                     if (back_menu == 3)
  1751.                         return;
  1752.                 }
  1753.             }
  1754.             if (b[x][y] == 3) {
  1755.                 a2[x][y] = 5;
  1756.                 num = bot.findShip (x, y);
  1757.                 --bot.ships[num].health;
  1758.                 if (bot.ships[num].health == 0) {
  1759.                     matchArea (a2, bot.ships[num].x, bot.ships[num].y, bot.ships[num].length, bot.ships[num].orientation, 4);
  1760.                     --bot.ships_amount;
  1761.                     f_bot.refresh(a2);
  1762.                     --counter.ship[bot.ships[num].length-1];
  1763.                     counter.draw (1, 5);
  1764.                 }
  1765.                 if (bot.ships_amount != 0)
  1766.                     shot = true;
  1767.                 else {
  1768.                     shot = false;
  1769.                     return;
  1770.                 }
  1771.             }
  1772.             else {
  1773.                 a2[x][y] = 4;
  1774.                 shot = false;
  1775.             }
  1776.             f_bot.refresh(a2);
  1777.     }
  1778. };
  1779.  
  1780. void save (int a[10][10], int a2[10][10], int b[10][10], int b2[10][10], int trigger[7], Field f_bot, Field f_player, Player player, Bot bot, Counter counter, int x, int y, bool shot_player, bool shot_bot, int back_menu, bool &done) {
  1781.     ofstream F ("sea_battle3.dat", ios::binary);
  1782.     done = F;
  1783.     if (F) {
  1784.     F.write ((char*)a, sizeof (int) * 100);
  1785.     F.write ((char*)a2, sizeof (int) * 100);
  1786.     F.write ((char*)b, sizeof (int) * 100);
  1787.     F.write ((char*)b2, sizeof (int) * 100);
  1788.     F.write ((char*)trigger, sizeof (int) * 7);
  1789.     F.write ((char*)&f_bot.x1, sizeof (int));
  1790.     F.write ((char*)&f_bot.y1, sizeof (int));
  1791.     F.write ((char*)&f_player.x1, sizeof (int));
  1792.     F.write ((char*)&f_player.y1, sizeof (int));
  1793.     F.write ((char*)&player.ships_amount, sizeof (int));
  1794.     for (int i = 0; i < 10; ++i) {
  1795.         F.write ((char*)&player.ships[i].x, sizeof (int));
  1796.         F.write ((char*)&player.ships[i].y, sizeof (int));
  1797.         F.write ((char*)&player.ships[i].length, sizeof (int));
  1798.         F.write ((char*)&player.ships[i].health, sizeof (int));
  1799.         F.write ((char*)&player.ships[i].orientation, sizeof (bool));
  1800.     }
  1801.     F.write ((char*)&bot.current_orientation, sizeof (bool));
  1802.     F.write ((char*)&bot.ships_amount, sizeof (int));
  1803.     F.write ((char*)&bot.free_boxes, sizeof (int));
  1804.     F.write ((char*)bot.enemy_ships, sizeof (int) * 4);
  1805.     for (int i = 0; i < 10; ++i) {
  1806.         F.write ((char*)&bot.ships[i].x, sizeof (int));
  1807.         F.write ((char*)&bot.ships[i].y, sizeof (int));
  1808.         F.write ((char*)&bot.ships[i].length, sizeof (int));
  1809.         F.write ((char*)&bot.ships[i].health, sizeof (int));
  1810.         F.write ((char*)&bot.ships[i].orientation, sizeof (bool));
  1811.     }
  1812.     F.write ((char*)counter.ship, sizeof (int) * 4);
  1813.     F.write ((char*)&x, sizeof (int));
  1814.     F.write ((char*)&y, sizeof (int));
  1815.     F.write ((char*)&shot_player, sizeof (bool));
  1816.     F.write ((char*)&shot_bot, sizeof (bool));
  1817.     F.write ((char*)&back_menu, sizeof (int));
  1818.     }
  1819.     F.close();
  1820.     return;
  1821. }
  1822.  
  1823. void load (int a[10][10], int a2[10][10], int b[10][10], int b2[10][10], int trigger[7], Field &f_bot, Field &f_player, Player &player, Bot &bot, Counter &counter, int &x, int &y, bool &shot_player, bool &shot_bot, int &back_menu, bool &is_loaded) {
  1824.     ifstream F ("sea_battle3.dat", ios::binary);
  1825.     if (F) {
  1826.     is_loaded = F;
  1827.     int n;
  1828.     bool m;
  1829.     F.read ((char*)a, sizeof (int) * 100);
  1830.     F.read ((char*)a2, sizeof (int) * 100);
  1831.     F.read ((char*)b, sizeof (int) * 100);
  1832.     F.read ((char*)b2, sizeof (int) * 100);
  1833.     F.read ((char*)trigger, sizeof (int) * 7);
  1834.     F.read ((char*)&n, sizeof (int));
  1835.     f_bot.x1 = n;
  1836.     F.read ((char*)&n, sizeof (int));
  1837.     f_bot.y1 = n;
  1838.     F.read ((char*)&n, sizeof (int));
  1839.     f_player.x1 = n;
  1840.     F.read ((char*)&n, sizeof (int));
  1841.     f_player.y1 = n;
  1842.     F.read ((char*)&n, sizeof (int));
  1843.     player.ships_amount = n;
  1844.     for (int i = 0; i < 10; ++i) {
  1845.         F.read ((char*)&n, sizeof (int));
  1846.         player.ships[i].x = n;
  1847.         F.read ((char*)&n, sizeof (int));
  1848.         player.ships[i].y = n;
  1849.         F.read ((char*)&n, sizeof (int));
  1850.         player.ships[i].length = n;
  1851.         F.read ((char*)&n, sizeof (int));
  1852.         player.ships[i].health = n;
  1853.         F.read ((char*)&m, sizeof (bool));
  1854.         player.ships[i].orientation = m;
  1855.     }
  1856.     F.read ((char*)&m, sizeof (bool));
  1857.     bot.current_orientation = m;
  1858.     F.read ((char*)&n, sizeof (int));
  1859.     bot.ships_amount = n;
  1860.     F.read ((char*)&n, sizeof (int));
  1861.     bot.free_boxes = n;
  1862.     F.read ((char*)bot.enemy_ships, sizeof (int) * 4);
  1863.     for (int i = 0; i < 10; ++i) {
  1864.         F.read ((char*)&n, sizeof (int));
  1865.         bot.ships[i].x = n;
  1866.         F.read ((char*)&n, sizeof (int));
  1867.         bot.ships[i].y = n;
  1868.         F.read ((char*)&n, sizeof (int));
  1869.         bot.ships[i].length = n;
  1870.         F.read ((char*)&n, sizeof (int));
  1871.         bot.ships[i].health = n;
  1872.         F.read ((char*)&m, sizeof (bool));
  1873.         bot.ships[i].orientation = m;
  1874.     }
  1875.     F.read ((char*)counter.ship, sizeof (int) * 4);
  1876.     F.read ((char*)&n, sizeof (int));
  1877.     x = n;
  1878.     F.read ((char*)&n, sizeof (int));
  1879.     y = n;
  1880.     F.read ((char*)&m, sizeof (bool));
  1881.     shot_player = m;
  1882.     F.read ((char*)&m, sizeof (bool));
  1883.     shot_bot = m;
  1884.     F.read ((char*)&n, sizeof (int));
  1885.     back_menu = n;
  1886.     F.close();
  1887.     return;
  1888.     }
  1889.     else {
  1890.         is_loaded = false;
  1891.         return;
  1892.     }
  1893. }
  1894.  
  1895. void work()
  1896. {
  1897.     ClrScr();
  1898.     Field f_bot, f_player;
  1899.     Player player;
  1900.     Bot bot;
  1901.     Counter counter;
  1902.     int trigger[7] = {0, 0, 0, 0, 0, 0, 0};
  1903.     int a[10][10] =   {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1904.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1905.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1906.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1907.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1908.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1909.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1910.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1911.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1912.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  1913.                      };
  1914.     int a2[10][10] =  {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1915.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1916.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1917.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1918.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1919.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1920.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1921.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1922.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1923.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  1924.                      };
  1925.     int b[10][10] =   {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1926.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1927.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1928.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1929.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1930.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1931.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1932.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1933.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1934.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  1935.                      };
  1936.     int b2[10][10] =   {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1937.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1938.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1939.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1940.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1941.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1942.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1943.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1944.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  1945.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  1946.                      };
  1947.     f_player.draw (43, 10);
  1948.     counter.draw (1, 5);
  1949.     f_bot.draw (1, 10);
  1950.     bot.shipArrange(b, f_bot);
  1951.     int back_menu(0);
  1952.     player.shipArrange(a, f_player);
  1953.     f_player.refresh(a);
  1954.     bool shot_player, shot_bot;
  1955.     int x(0), y(0);
  1956.     bool done (true);
  1957.     while ((bot.ships_amount != 0) && (player.ships_amount != 0)) {
  1958.         shot_player = true;
  1959.         shot_bot = true;
  1960.         while (shot_player) {
  1961.             back_menu = 0;
  1962.             player.attack (b, a2, f_bot, bot, shot_player, x, y, counter, back_menu, f_player, a, done);
  1963.             if (back_menu == 3)
  1964.                 return;
  1965.             if (back_menu == 2) {
  1966.                 save (a, a2, b, b2, trigger, f_bot, f_player, player, bot, counter, x, y, shot_player, shot_bot, back_menu, done);
  1967.             }
  1968.             back_menu = 0;
  1969.         }
  1970.         if (bot.ships_amount != 0)
  1971.             while (shot_bot) {
  1972.                 if (trigger[0]) {
  1973.                     bot.attack_w_trigger (a, b2, f_player, player.ships, player.ships_amount, shot_bot, trigger);
  1974.                 }
  1975.                 else
  1976.                     bot.attack (a, b2, f_player, player.ships, player.ships_amount, shot_bot, trigger);
  1977.             }
  1978.     }
  1979.     GoToXY (10, 35);
  1980.     if (bot.ships_amount == 0)
  1981.         cout << "You win!!!!!!!!";
  1982.     else
  1983.         cout << "Lost :(";
  1984.     int c = _getch();
  1985. }
  1986.  
  1987. void work_w_load (bool &is_loaded) {
  1988.     ClrScr();
  1989.     Field f_bot, f_player;
  1990.     Player player;
  1991.     Bot bot;
  1992.     Counter counter;
  1993.     int a[10][10], a2[10][10], b[10][10], b2[10][10], trigger[7];
  1994.     int back_menu;
  1995.     bool shot_player, shot_bot;
  1996.     int x, y;
  1997.     bool done (true);
  1998.     load (a, a2, b, b2, trigger, f_bot, f_player, player, bot, counter, x, y, shot_player, shot_bot, back_menu, is_loaded);
  1999.     if (!is_loaded)
  2000.         return;
  2001.     counter.draw (1, 5);
  2002.     f_bot.refresh(a2);
  2003.     f_player.refresh(a);
  2004.     while ((bot.ships_amount != 0) && (player.ships_amount != 0)) {
  2005.         shot_player = true;
  2006.         shot_bot = true;
  2007.         while (shot_player) {
  2008.             back_menu = 0;
  2009.             player.attack (b, a2, f_bot, bot, shot_player, x, y, counter, back_menu, f_player, a, done);
  2010.             if (back_menu == 3)
  2011.                 return;
  2012.             if (back_menu == 2) {
  2013.                 save (a, a2, b, b2, trigger, f_bot, f_player, player, bot, counter, x, y, shot_player, shot_bot, back_menu, done);
  2014.             }
  2015.             back_menu = 0;
  2016.         }
  2017.         if (bot.ships_amount != 0)
  2018.             while (shot_bot) {
  2019.                 if (trigger[0]) {
  2020.                     bot.attack_w_trigger (a, b2, f_player, player.ships, player.ships_amount, shot_bot, trigger);
  2021.                 }
  2022.                 else
  2023.                     bot.attack (a, b2, f_player, player.ships, player.ships_amount, shot_bot, trigger);
  2024.             }
  2025.     }
  2026.     GoToXY (10, 35);
  2027.     if (bot.ships_amount == 0)
  2028.         cout << "You win!!!!!!!!";
  2029.     else
  2030.         cout << "Lost :(";
  2031.     int c = _getch();
  2032. }
  2033.  
  2034. int main()
  2035. {
  2036.     ScrSize (85, 40);
  2037.     ConSize (85, 40);
  2038.     CursorVisible(false);
  2039.     int c;
  2040.     while (true) {
  2041.     if ((c = inform()) == 0)
  2042.         work();
  2043.     else if (c == 2)
  2044.         return 0;
  2045.     else if (c == 1) {
  2046.         bool is_loaded;
  2047.         work_w_load  (is_loaded);
  2048.     }
  2049.     }
  2050. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement