Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctime>
- #include <stdarg.h>
- #include <fstream>
- using namespace std;
- #include "MyCrt.h"
- double dTime = 200;
- class Field;
- class Ship;
- void drawMenu (int a, int b, int c) {
- TextBackground (a);
- GoToXY (5, 17);//play
- cout << " " << endl;
- GoToXY (5, WhereY());
- cout << " Play " << endl;
- GoToXY (5, WhereY());
- cout << " " << endl;
- TextBackground (b);
- GoToXY (37, 17); //load
- cout << " " << endl;
- GoToXY (37, WhereY());
- cout << " Load " << endl;
- GoToXY (37, WhereY());
- cout << " " << endl;
- TextBackground (c);
- GoToXY (68, 17); //load
- cout << " " << endl;
- GoToXY (68, WhereY());
- cout << " Exit " << endl;
- GoToXY (68, WhereY());
- cout << " " << endl;
- TextBackground (0);
- }
- void drawResume (int a, int b, int c) {
- TextBackground (a);
- GoToXY (5, 32);//play 17 was
- cout << " " << endl;
- GoToXY (5, WhereY());
- cout << " Resume " << endl;
- GoToXY (5, WhereY());
- cout << " " << endl;
- TextBackground (b);
- GoToXY (37, 32); //load
- cout << " " << endl;
- GoToXY (37, WhereY());
- cout << " Save " << endl;
- GoToXY (37, WhereY());
- cout << " " << endl;
- TextBackground (c);
- GoToXY (68, 32); //load
- cout << " " << endl;
- GoToXY (68, WhereY());
- cout << " Exit " << endl;
- GoToXY (68, WhereY());
- cout << " " << endl;
- TextBackground (0);
- }
- void resume (int &back_menu) {
- //ClrScr();
- int i;
- drawResume (8, 7, 7);
- int ch;
- int a(8), b(7), c(7), sw;
- while (((ch = _getch()) != 13) && (ch != 27)) {
- if (ch == 75) {//left
- sw = a;
- a = b;
- b = a;
- b = sw;
- b = c;
- c = sw;
- drawResume (a, b, c);
- }
- if (ch == 77) {//right
- sw = b;
- b = c;
- c = sw;
- sw = b;
- b = a;
- a = sw;
- drawResume (a, b, c);
- }
- }
- if ((a == 8) || (ch == 27)) {
- back_menu = 1;
- return;
- }
- if (b == 8) {
- back_menu = 2;
- return;
- }
- if (c == 8) {
- back_menu = 3;
- return;
- }
- }
- int inform () {
- ClrScr();
- int i;
- drawMenu (8, 7, 7);
- int ch;
- int a(8), b(7), c(7), sw;
- while ((ch = _getch()) != 13) {
- if (ch == 75) {//left
- sw = a;
- a = b;
- b = a;
- b = sw;
- b = c;
- c = sw;
- drawMenu (a, b, c);
- }
- if (ch == 77) {//right
- sw = b;
- b = c;
- c = sw;
- sw = b;
- b = a;
- a = sw;
- drawMenu (a, b, c);
- }
- }
- if (a == 8)
- return 0;
- if (b == 8)
- return 1;
- if (c == 8)
- return 2;
- }
- class Field {
- public:
- int x1, y1;
- void draw (int x, int y) { //рисует сетку
- GoToXY (x, y);
- x1 = x;
- y1 = y;
- for (int i = 0; i < 5; ++i) {
- for (int j = 0; j < 5; ++j) {
- TextBackground (8);
- cout << " ";
- TextBackground (7);
- cout << " ";
- }
- cout << endl;
- GoToXY (x, WhereY());
- for (int j = 0; j < 5; ++j) {
- TextBackground (8);
- cout << " ";
- TextBackground (7);
- cout << " ";
- }
- cout << endl;
- GoToXY (x, WhereY());
- for (int j = 0; j < 5; ++j) {
- TextBackground (7);
- cout << " ";
- TextBackground (8);
- cout << " ";
- }
- cout << endl;
- GoToXY (x, WhereY());
- for (int j = 0; j < 5; ++j) {
- TextBackground (7);
- cout << " ";
- TextBackground (8);
- cout << " ";
- }
- cout << endl;
- GoToXY (x, WhereY());
- }
- }
- void shipArrange (int a[10][10]) { //расставляет корабли
- TextBackground (1);
- for (int i = 0; i < 10; ++i)
- for (int j = 0; j < 10; ++j)
- if (a[i][j] == 1) { //корабли при расстановке
- TextBackground (14); //10 - green, 12 - red
- GoToXY (x1+4*j, y1+2*i);
- cout << " ";
- GoToXY (x1+4*j, y1+2*i+1);
- cout << " ";
- //TextForeground (15);
- }
- else if (a[i][j] == 3) { //корабли
- TextBackground (10); //10 - green, 12 - red
- GoToXY (x1+4*j, y1+2*i);
- cout << " ";
- GoToXY (x1+4*j, y1+2*i+1);
- cout << " ";
- //TextBackground (1);
- }
- else if (a[i][j] == 2) { //область вокруг кораблей
- TextBackground (1);
- GoToXY (x1+4*j, y1+2*i);
- cout << " ";
- GoToXY (x1+4*j, y1+2*i+1);
- cout << " ";
- }
- else if (a[i][j] == 0) { //пустые клетки
- if (((i + j) % 2) == 0) {
- TextBackground (8);
- GoToXY (x1+4*j, y1+2*i);
- cout << " ";
- GoToXY (x1+4*j, y1+2*i+1);
- cout << " ";
- }
- else {
- TextBackground (7);
- GoToXY (x1+4*j, y1+2*i);
- cout << " ";
- GoToXY (x1+4*j, y1+2*i+1);
- cout << " ";
- }
- }
- TextBackground (0);
- }
- void refresh (int a[10][10]) {
- TextBackground (1);
- for (int i = 0; i < 10; ++i)
- for (int j = 0; j < 10; ++j)
- if (a[i][j] == 3) { //корабли
- TextBackground (10); //10 - green, 12 - red
- GoToXY (x1+4*j, y1+2*i);
- cout << " ";
- GoToXY (x1+4*j, y1+2*i+1);
- cout << " ";
- }
- else if ((a[i][j] == 0)||(a[i][j] == 2)) { //пустые клетки
- if (((i + j) % 2) == 0) {
- TextBackground (8);
- GoToXY (x1+4*j, y1+2*i);
- cout << " ";
- GoToXY (x1+4*j, y1+2*i+1);
- cout << " ";
- }
- else {
- TextBackground (7);
- GoToXY (x1+4*j, y1+2*i);
- cout << " ";
- GoToXY (x1+4*j, y1+2*i+1);
- cout << " ";
- }
- }
- else if (a[i][j] == 1) { //корабли при расстановке
- TextBackground (14); //10 - green, 12 - red
- GoToXY (x1+4*j, y1+2*i);
- cout << " ";
- GoToXY (x1+4*j, y1+2*i+1);
- cout << " ";
- }
- else if (a[i][j] == 6) { //animation
- TextBackground (15); //10 - green, 12 - red
- GoToXY (x1+4*j, y1+2*i);
- cout << " ";
- GoToXY (x1+4*j, y1+2*i+1);
- cout << " ";
- }
- else if (a[i][j] == 4) { //промах
- TextBackground (1); //10 - green, 12 - red
- GoToXY (x1+4*j, y1+2*i);
- cout << " ";
- GoToXY (x1+4*j, y1+2*i+1);
- cout << " ";
- }
- else if (a[i][j] == 5) { //подбитые корабли
- TextBackground (12); //10 - green, 12 - red
- GoToXY (x1+4*j, y1+2*i);
- cout << " ";
- GoToXY (x1+4*j, y1+2*i+1);
- cout << " ";
- }
- TextBackground (0);
- }
- };
- class Ship {
- public:
- int x, y, length, health;
- bool orientation; //true - vert, false - horiz
- };
- void matchArea (int a[10][10], int x, int y, int length, int orientation, int num) {
- if (length == 4) {
- if (orientation) {
- if ((x <= 8) && (y <= 8))
- a[x+1][y+1] = num;
- if (y <= 8)
- a[x][y+1] = num;
- if (x > 0)
- a[x-1][y] = num;
- if ((x > 0) && (y > 0))
- a[x-1][y-1] = num;
- if ((x <= 8) && (y > 0))
- a[x+1][y-1] = num;
- if ((x > 0) && (y <= 8))
- a[x-1][y+1] = num;
- if (y > 0)
- a[x][y-1] = num;
- if (x <= 5)
- a[x+4][y] = num;
- if ((x <= 5) && (y > 0))
- a[x+4][y-1] = num;
- if ((x <= 6) && (y > 0))
- a[x+3][y-1] = num;
- if ((x <= 6) && (y <= 8))
- a[x+3][y+1] = num;
- if ((x <= 5) && (y <= 8))
- a[x+4][y+1] = num;
- if ((x <= 7) && (y > 0))
- a[x+2][y-1] = num;
- if ((x <= 7) && (y <= 8))
- a[x+2][y+1] = num;
- }
- else {
- if (y > 0)//сдева начинаю по часовой
- a[x][y-1] = num;
- if ((y > 0) && (x > 0))
- a[x-1][y-1] = num;
- if (x > 0)
- a[x-1][y] = num;
- if ((y <= 8) && (x > 0))
- a[x-1][y+1] = num;
- if ((y <= 7) && (x > 0))
- a[x-1][y+2] = num;
- if ((y <= 6) && (x > 0))
- a[x-1][y+3] = num;
- if ((y <= 5) && (x > 0))
- a[x-1][y+4] = num;
- if (y <= 5)
- a[x][y+4] = num;
- if ((y <= 6) && (x <= 8))
- a[x+1][y+3] = num;
- if ((y <= 5) && (x <= 8))
- a[x+1][y+4] = num;
- if ((y <= 7) && (x <= 8))
- a[x+1][y+2] = num;
- if ((y <= 8) && (x <= 8))
- a[x+1][y+1] = num;
- if (x <= 8)
- a[x+1][y] = num;
- if ((x <= 8) && (y > 0))
- a[x+1][y-1] = num;
- }
- }
- else if (length == 3) {
- if (orientation) {
- if ((x <= 8) && (y <= 8))
- a[x+1][y+1] = num;
- if (y <= 8)
- a[x][y+1] = num;
- if (x > 0)
- a[x-1][y] = num;
- if ((x > 0) && (y > 0))
- a[x-1][y-1] = num;
- if ((x <= 8) && (y > 0))
- a[x+1][y-1] = num;
- if ((x > 0) && (y <= 8))
- a[x-1][y+1] = num;
- if (y > 0)
- a[x][y-1] = num;
- if (x <= 6)
- a[x+3][y] = num;
- if ((x <= 6) && (y > 0))
- a[x+3][y-1] = num;
- if ((x <= 6) && (y <= 8))
- a[x+3][y+1] = num;
- if ((x <= 7) && (y > 0))
- a[x+2][y-1] = num;
- if ((x <= 7) && (y <= 8))
- a[x+2][y+1] = num;
- }
- else {
- if (y > 0)//сдева начинаю по часовой
- a[x][y-1] = num;
- if ((y > 0) && (x > 0))
- a[x-1][y-1] = num;
- if (x > 0)
- a[x-1][y] = num;
- if ((y <= 8) && (x > 0))
- a[x-1][y+1] = num;
- if ((y <= 7) && (x > 0))
- a[x-1][y+2] = num;
- if ((y <= 6) && (x > 0))
- a[x-1][y+3] = num;
- if (y <= 6)
- a[x][y+3] = num;
- if ((y <= 6) && (x <= 8))
- a[x+1][y+3] = num;
- if ((y <= 7) && (x <= 8))
- a[x+1][y+2] = num;
- if ((y <= 8) && (x <= 8))
- a[x+1][y+1] = num;
- if (x <= 8)
- a[x+1][y] = num;
- if ((x <= 8) && (y > 0))
- a[x+1][y-1] = num;
- }
- }
- else if (length == 2) {
- if (orientation) {
- if ((x <= 8) && (y <= 8))
- a[x+1][y+1] = num;
- if (y <= 8)
- a[x][y+1] = num;
- if (x > 0)
- a[x-1][y] = num;
- if ((x > 0) && (y > 0))
- a[x-1][y-1] = num;
- if ((x <= 8) && (y > 0))
- a[x+1][y-1] = num;
- if ((x > 0) && (y <= 8))
- a[x-1][y+1] = num;
- if (y > 0)
- a[x][y-1] = num;
- if (x <= 7)
- a[x+2][y] = num;
- if ((x <= 7) && (y > 0))
- a[x+2][y-1] = num;
- if ((x <= 7) && (y <= 8))
- a[x+2][y+1] = num;
- }
- else {
- if (y > 0)//сдева начинаю по часовой
- a[x][y-1] = num;
- if ((y > 0) && (x > 0))
- a[x-1][y-1] = num;
- if (x > 0)
- a[x-1][y] = num;
- if ((y <= 8) && (x > 0))
- a[x-1][y+1] = num;
- if ((y <= 7) && (x > 0))
- a[x-1][y+2] = num;
- if (y <= 7)
- a[x][y+2] = num;
- if ((y <= 7) && (x <= 8))
- a[x+1][y+2] = num;
- if ((y <= 8) && (x <= 8))
- a[x+1][y+1] = num;
- if (x <= 8)
- a[x+1][y] = num;
- if ((x <= 8) && (y > 0))
- a[x+1][y-1] = num;
- }
- }
- else if (length == 1) {
- if (x <= 8)
- a[x+1][y] = num;
- if ((x <= 8) && (y <= 8))
- a[x+1][y+1] = num;
- if (y <= 8)
- a[x][y+1] = num;
- if (x > 0)
- a[x-1][y] = num;
- if ((x > 0) && (y > 0))
- a[x-1][y-1] = num;
- if ((x <= 8) && (y > 0))
- a[x+1][y-1] = num;
- if ((x > 0) && (y <= 8))
- a[x-1][y+1] = num;
- if (y > 0)
- a[x][y-1] = num;
- }
- }
- int findShipP (int x, int y, Ship ships[10]) {//номер корабля отдает
- for (int i = 0; i < 10; ++i) {
- for (int j = 0; j < ships[i].length; ++j) {
- if (ships[i].orientation) {
- if ((ships[i].x + j == x) && (ships[i].y == y))
- return i;
- }
- else {
- if ((ships[i].x == x) && (ships[i].y + j == y))
- return i;
- }
- }
- }
- }
- int minim (int a, int b) {
- if (a < b) {
- return a;
- }
- else
- return b;
- }
- int maxim (int a, int b) {
- if (a > b) {
- return a;
- }
- else
- return b;
- }
- class Counter {
- public:
- int ship[4];
- Counter () {
- ship[3] = 1;
- ship[2] = 2;
- ship[1] = 3;
- ship[0] = 4;
- }
- void draw (int x, int y) {
- TextBackground (0);
- GoToXY (x, y);
- cout << "Enemy's ships amount: " << endl;
- GoToXY (x, WhereY());
- cout << "1's - " << ship[0] << endl;
- GoToXY (x, WhereY());
- cout << "2's - " << ship[1] << endl;
- GoToXY (x, WhereY());
- cout << "3's - " << ship[2] << endl;
- GoToXY (x, WhereY());
- cout << "4's - " << ship[3] << endl;
- GoToXY (x, WhereY());
- }
- };
- class Player;
- class Bot {
- private:
- bool check_cache (int a[10][10], int length, int x, int y, bool orientation) {
- for (int i = 0; i < length; ++i)
- if (orientation) {
- if (a[x+i][y] != 0)
- return true;
- }
- else {
- if (a[x][y+i] != 0)
- return true;
- }
- return false;
- }
- public:
- friend Player;
- bool current_orientation;
- int ships_amount, free_boxes, enemy_ships[4];
- Bot () {
- ships_amount = 10;
- free_boxes = 100;
- enemy_ships[0] = 4;
- enemy_ships[1] = 3;
- enemy_ships[2] = 2;
- enemy_ships[3] = 1;
- }
- Ship ships[10];
- int findShip (int x, int y) {//номер корабля отдает
- for (int i = 0; i < 10; ++i) {
- for (int j = 0; j < ships[i].length; ++j) {
- if (ships[i].orientation) {
- if ((ships[i].x + j == x) && (ships[i].y == y))
- return i;
- }
- else {
- if ((ships[i].x == x) && (ships[i].y + j == y))
- return i;
- }
- }
- }
- }
- bool check_sides (int x, int y, int b2[10][10]) {
- int sides[2];
- sides[0] = 0;
- sides[1] = 0;
- int n = 0;
- if (enemy_ships[3] != 0)
- n = 3;
- else if (enemy_ships[2] != 0)
- n = 2;
- else if (enemy_ships[1] != 0)
- n = 1;
- for (int i = 1; i <= n; ++i) {
- if ((b2[x-i][y] == 0) && (x - i >= 0))
- ++sides[0];
- if ((b2[x][y+i] == 0) && (y + i <= 9))
- ++sides[1];
- if ((b2[x+i][y] == 0) && (x + i <= 9))
- ++sides[0];
- if ((b2[x][y-i] == 0) && (y - i >= 0))
- ++sides[1];
- }
- for (int i = 0; i < 2; ++i)
- if (sides[i] >= n)
- return false;
- return true;
- }
- void shipArrange (int a[10][10], Field &f_bot) {
- int c[30];
- srand (time (0));
- for (int i = 0; i < 20; ++i)
- c[i] = rand() % 10;
- for (int i = 20; i < 30; ++i)
- c[i] = rand() % 2;
- for (int i = 9; i >= 0; --i) {
- ships[i].orientation = c[i+20];
- if (i < 4)
- ships[i].length = 1;
- else if (i < 7)
- ships[i].length = 2;
- else if (i < 9)
- ships[i].length = 3;
- else
- ships[i].length = 4;
- ships[i].health = ships[i].length;
- ships[i].x = c[i];
- ships[i].y = c[i+10];
- if (ships[i].orientation) {
- while ((ships[i].x + ships[i].length -1 > 9 ) || (check_cache (a, ships[i].length, ships[i].x, ships[i].y, ships[i].orientation))) {
- --ships[i].x;
- if (ships[i].x < 0) {
- ships[i].x = 9;
- ++ships[i].y;
- }
- if (ships[i].y == 10)
- ships[i].y = 0;
- }
- }
- else {
- while ((ships[i].y > 10 - ships[i].length) || (check_cache (a, ships[i].length, ships[i].x, ships[i].y, ships[i].orientation))) {
- --ships[i].y;
- if (ships[i].y < 0) {
- ships[i].y = 9;
- ++ships[i].x;
- }
- if (ships[i].x == 10)
- ships[i].x = 0;
- }
- }
- int x = ships[i].x;
- int y = ships[i].y;
- if (ships[i].orientation)
- for (int j = 0; j < ships[i].length; ++j) {
- a[ships[i].x+j][ships[i].y] = 3;
- }
- else
- for (int j = 0; j < ships[i].length; ++j) {
- a[ships[i].x][ships[i].y+j] = 3;
- }
- matchArea (a, x, y, ships[i].length, ships[i].orientation, 2);
- }
- }
- void attack (int a[10][10], int b2[10][10], Field &f_player, Ship ships[10], int &ships_amount, bool &shot, int trigger[7]) {
- int number = rand() % free_boxes;
- --free_boxes;
- int x = 0, y = 0, count = 0;
- while ((count < number) || (b2[x][y] != 0) || (check_sides(x, y, b2))) {
- ++x;
- if (x > 9) {
- x = 0;
- ++y;
- }
- if (y > 9) {
- y = 0;
- x = 0;
- }
- if (b2[x][y] == 0)
- ++count;
- }
- if (a[x][y] == 3) {
- a[x][y] = 6;
- f_player.refresh(a);
- Sleep (dTime);
- a[x][y] = 5;
- b2[x][y] = 5;
- int num = findShipP (x, y, ships);
- --ships[num].health;
- trigger[0] = 1;
- trigger[1] = x;
- trigger[2] = y;
- current_orientation = ships[num].orientation;
- if (ships[num].health == 0) {
- matchArea (b2, ships[num].x, ships[num].y, ships[num].length, ships[num].orientation, 4);
- matchArea (a, ships[num].x, ships[num].y, ships[num].length, ships[num].orientation, 4);
- --ships_amount;
- trigger[0] = 0;
- f_player.refresh(a);
- }
- if (ships_amount != 0)
- shot = true;
- else {
- shot = false;
- f_player.refresh(a);
- return;
- }
- }
- else {
- a[x][y] = 6;
- f_player.refresh(a);
- Sleep (dTime);
- a[x][y] = 4;
- b2[x][y] = 4;
- shot = false;
- }
- f_player.refresh(a);
- }
- 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]) {
- --free_boxes;
- int x = trigger[1], y = trigger[2];
- if (trigger[0] == 1) {//2-x+ палубный
- int posib = -1;
- int places[4];
- if ((x > 0) && (b2[x-1][y] == 0)) {
- ++posib;
- places[posib] = 0;
- }
- if ((x < 9) && (b2[x+1][y] == 0)) {
- ++posib;
- places[posib] = 1;
- }
- if ((y > 0) && (b2[x][y-1] == 0)) {
- ++posib;
- places[posib] = 2;
- }
- if ((y < 9) && (b2[x][y+1] == 0)) {
- ++posib;
- places[posib] = 3;
- }
- int number;
- if (posib != 0)
- number = rand() % posib;
- else
- number = 0;
- number = places[number];
- if (number == 0) {
- --x;
- }
- else if (number == 1) {
- ++x;
- }
- else if (number == 2) {
- --y;
- }
- else if (number == 3) {
- ++y;
- }
- }
- else if (trigger[0] == 2) {//3-x+ палубный
- int posib = -1;
- int places[2];
- int x1, y1, x2, y2;
- if (current_orientation) {
- x1 = minim (trigger[1], trigger[3]);
- x2 = maxim (trigger[1], trigger[3]);
- y1 = trigger[2];
- if ((b2[x1-1][y1] == 0) && (x1 > 0)) {
- ++posib;
- places[posib] = 0;
- }
- if ((b2[x2+1][y1] == 0) && (x2 < 9)) {
- ++posib;
- places[posib] = 1;
- }
- int number;
- if (posib != 0)
- number = rand() % posib;
- else
- number = 0;
- number = places[number];
- y = y1;
- if (number == 0) {
- x = x1-1;
- }
- else {
- x = x2+1;
- }
- }
- else {
- y1 = minim (trigger[2], trigger[4]);
- y2 = maxim (trigger[2], trigger[4]);
- x1 = trigger[1];
- if ((b2[x1][y1-1] == 0) && (y1 > 0)) {
- ++posib;
- places[posib] = 0;
- }
- if ((b2[x1][y2+1] == 0) && (y2 < 9)) {
- ++posib;
- places[posib] = 1;
- }
- int number;
- if (posib != 0)
- number = rand() % posib;
- else
- number = 0;
- number = places[number];
- x = x1;
- if (number == 0) {
- y = y1-1;
- }
- else {
- y = y2+1;
- }
- }
- }
- else if (trigger[0] == 3) {//4-x палубный
- int posib = -1;
- int places[2];
- int x1, y1, x2, y2;
- if (current_orientation) {
- x1 = minim (trigger[1], trigger[3]);
- x1 = minim (x1, trigger[5]);
- x2 = maxim (trigger[1], trigger[3]);
- x2 = maxim (x2, trigger[5]);
- y1 = trigger[2];
- if ((b2[x1-1][y1] == 0) && (x1 > 0)) {
- ++posib;
- places[posib] = 0;
- }
- if ((b2[x2+1][y1] == 0) && (x2 < 9)) {
- ++posib;
- places[posib] = 1;
- }
- int number;
- if (posib != 0)
- number = rand() % posib;
- else
- number = 0;
- number = places[number];
- y = y1;
- if (number == 0) {
- x = x1-1;
- }
- else {
- x = x2+1;
- }
- }
- else {
- y1 = minim (trigger[2], trigger[4]);
- y1 = minim (y1, trigger[6]);
- y2 = maxim (trigger[2], trigger[4]);
- y2 = maxim (y2, trigger[6]);
- x1 = trigger[1];
- if ((b2[x1][y1-1] == 0) && (y1 > 0)) {
- ++posib;
- places[posib] = 0;
- }
- if ((b2[x1][y2+1] == 0) && (y2 < 9)) {
- ++posib;
- places[posib] = 1;
- }
- int number;
- if (posib != 0)
- number = rand() % posib;
- else
- number = 0;
- number = places[number];
- x = x1;
- if (number == 0) {
- y = y1-1;
- }
- else {
- y = y2+1;
- }
- }
- }
- if (a[x][y] == 3) {
- a[x][y] = 6;
- f_player.refresh(a);
- Sleep (dTime);
- a[x][y] = 5;
- b2[x][y] = 5;
- int num = findShipP (x, y, ships);
- --ships[num].health;
- ++trigger[0];
- if (trigger[0] == 2) {
- trigger[3] = x;
- trigger[4] = y;
- }
- if (trigger[0] == 3) {
- trigger[5] = x;
- trigger[6] = y;
- }
- if (ships[num].health == 0) {
- matchArea (b2, ships[num].x, ships[num].y, ships[num].length, ships[num].orientation, 4);
- matchArea (a, ships[num].x, ships[num].y, ships[num].length, ships[num].orientation, 4);
- --ships_amount;
- --enemy_ships[ships[num].length-1];
- trigger[0] = 0;
- f_player.refresh(a);
- }
- if (ships_amount != 0)
- shot = true;
- else {
- shot = false;
- f_player.refresh(a);
- return;
- }
- }
- else {
- a[x][y] = 6;
- f_player.refresh(a);
- Sleep (dTime);
- b2[x][y] = 4;
- a[x][y] = 4;
- shot = false;
- }
- f_player.refresh(a);
- }
- };
- class Player {
- public:
- int ships_amount;
- Player () {
- ships_amount = 10;
- }
- int findShip (int x, int y) {//номер корабля отдает
- for (int i = 0; i < 10; ++i) {
- for (int j = 0; j < ships[i].length; ++j) {
- if (ships[i].orientation) {
- if ((ships[i].x + j == x) && (ships[i].y == y))
- return i;
- }
- else {
- if ((ships[i].x == x) && (ships[i].y + j == y))
- return i;
- }
- }
- }
- }
- Ship ships[10];
- void shipArrange (int a[10][10], Field &f_player) {
- int x, y;
- int c, cache;
- for (int i = 0; i < 4; ++i) {
- x = 0;
- y = 0;
- cache = a[x][y];
- a[x][y] = 1;
- f_player.shipArrange(a);
- while (((c = _getch()) != 13) || (cache != 0))
- {
- if ((c == 77) && (y <= 8))//right
- {
- a[x][y] = cache;
- y = y + 1;
- cache = a[x][y];
- a[x][y] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 75) && (y >= 1))//left
- {
- a[x][y] = cache;
- y = y - 1;
- cache = a[x][y];
- a[x][y] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 72) && (x >= 1))//up
- {
- a[x][y] = cache;
- x = x - 1;
- cache = a[x][y];
- a[x][y] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 80) && (x <= 8))//down
- {
- a[x][y] = cache;
- x = x + 1;
- cache = a[x][y];
- a[x][y] = 1;
- f_player.shipArrange(a);
- }
- if (c == 27) {
- }
- }
- a[x][y] = 3;
- matchArea (a, x, y, 1, true, 2);
- ships[i].x = x;
- ships[i].y = y;
- ships[i].length = 1;
- ships[i].health = 1;
- ships[i].orientation = true;
- }
- int cache2;
- for (int i = 4; i < 7; ++i) { //расстановка двухалубных
- ships[i].orientation = true;
- ships[i].length = 2;
- ships[i].health = 2;
- x = 0;
- y = 0;
- cache = a[x][y];
- cache2 = a[x+1][y];
- a[x][y] = 1;
- a[x+1][y] = 1;
- f_player.shipArrange(a);
- while (((c = _getch()) != 13) || (cache != 0) || (cache2 != 0))
- {
- if (ships[i].orientation) { //вертикальная
- if ((c == 77) && (y <= 8))//right
- {
- a[x][y] = cache;
- a[x+1][y] = cache2;
- y = y + 1;
- cache = a[x][y];
- cache2 = a[x+1][y];
- a[x][y] = 1;
- a[x+1][y] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 75) && (y >= 1))//left
- {
- a[x][y] = cache;
- a[x+1][y] = cache2;
- y = y - 1;
- cache = a[x][y];
- cache2 = a[x+1][y];
- a[x][y] = 1;
- a[x+1][y] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 72) && (x >= 1))//up
- {
- a[x][y] = cache;
- a[x+1][y] = cache2;
- x = x - 1;
- cache = a[x][y];
- cache2 = a[x+1][y];
- a[x][y] = 1;
- a[x+1][y] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 80) && (x <= 7))//down
- {
- a[x][y] = cache;
- a[x+1][y] = cache2;
- x = x + 1;
- cache = a[x][y];
- cache2 = a[x+1][y];
- a[x][y] = 1;
- a[x+1][y] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 43) && (y <= 8)) { //смена ориентации
- ships[i].orientation = false;
- a[x+1][y] = cache2;
- cache2 = a[x][y+1];
- a[x][y+1] = 1;
- f_player.shipArrange(a);
- }
- }
- else { //горизонтальная
- if ((c == 77) && (y <= 7))//right
- {
- a[x][y] = cache;
- a[x][y+1] = cache2;
- y = y + 1;
- cache = a[x][y];
- cache2 = a[x][y+1];
- a[x][y] = 1;
- a[x][y+1] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 75) && (y >= 1))//left
- {
- a[x][y] = cache;
- a[x][y+1] = cache2;
- y = y - 1;
- cache = a[x][y];
- cache2 = a[x][y+1];
- a[x][y] = 1;
- a[x][y+1] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 72) && (x >= 1))//up
- {
- a[x][y] = cache;
- a[x][y+1] = cache2;
- x = x - 1;
- cache = a[x][y];
- cache2 = a[x][y+1];
- a[x][y] = 1;
- a[x][y+1] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 80) && (x <= 8))//down
- {
- a[x][y] = cache;
- a[x][y+1] = cache2;
- x = x + 1;
- cache = a[x][y];
- cache2 = a[x][y+1];
- a[x][y] = 1;
- a[x][y+1] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 43) && (x <= 8)) { //смена ориентации
- ships[i].orientation = true;
- a[x][y+1] = cache2;
- cache2 = a[x+1][y];
- a[x+1][y] = 1;
- f_player.shipArrange(a);
- }
- }
- }
- if (ships[i].orientation) {
- a[x][y] = 3;
- a[x+1][y] = 3;
- if ((x <= 8) && (y <= 8))
- a[x+1][y+1] = 2;
- if (y <= 8)
- a[x][y+1] = 2;
- if (x > 0)
- a[x-1][y] = 2;
- if ((x > 0) && (y > 0))
- a[x-1][y-1] = 2;
- if ((x <= 8) && (y > 0))
- a[x+1][y-1] = 2;
- if ((x > 0) && (y <= 8))
- a[x-1][y+1] = 2;
- if (y > 0)
- a[x][y-1] = 2;
- if (x <= 7)
- a[x+2][y] = 2;
- if ((x <= 7) && (y > 0))
- a[x+2][y-1] = 2;
- if ((x <= 7) && (y <= 8))
- a[x+2][y+1] = 2;
- }
- else {
- a[x][y] = 3;
- a[x][y+1] = 3;
- if (y > 0)//сдева начинаю по часовой
- a[x][y-1] = 2;
- if ((y > 0) && (x > 0))
- a[x-1][y-1] = 2;
- if (x > 0)
- a[x-1][y] = 2;
- if ((y <= 8) && (x > 0))
- a[x-1][y+1] = 2;
- if ((y <= 7) && (x > 0))
- a[x-1][y+2] = 2;
- if (y <= 7)
- a[x][y+2] = 2;
- if ((y <= 7) && (x <= 8))
- a[x+1][y+2] = 2;
- if ((y <= 8) && (x <= 8))
- a[x+1][y+1] = 2;
- if (x <= 8)
- a[x+1][y] = 2;
- if ((x <= 8) && (y > 0))
- a[x+1][y-1] = 2;
- }
- ships[i].x = x;
- ships[i].y = y;
- }
- int cache3;
- for (int i = 7; i < 9; ++i) { //расстановка трехалубных
- ships[i].orientation = true;
- ships[i].length = 3;
- ships[i].health = 3;
- x = 0;
- y = 0;
- cache = a[x][y];
- cache2 = a[x+1][y];
- cache3 = a[x+2][y];
- a[x][y] = 1;
- a[x+1][y] = 1;
- a[x+2][y] = 1;
- f_player.shipArrange(a);
- while (((c = _getch()) != 13) || (cache != 0) || (cache2 != 0) || (cache3 != 0))
- {
- if (ships[i].orientation) { //вертикальная
- if ((c == 77) && (y <= 8))//right
- {
- a[x][y] = cache;
- a[x+1][y] = cache2;
- a[x+2][y] = cache3;
- y = y + 1;
- cache = a[x][y];
- cache2 = a[x+1][y];
- cache3 = a[x+2][y];
- a[x][y] = 1;
- a[x+1][y] = 1;
- a[x+2][y] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 75) && (y >= 1))//left
- {
- a[x][y] = cache;
- a[x+1][y] = cache2;
- a[x+2][y] = cache3;
- y = y - 1;
- cache = a[x][y];
- cache2 = a[x+1][y];
- cache3 = a[x+2][y];
- a[x][y] = 1;
- a[x+1][y] = 1;
- a[x+2][y] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 72) && (x >= 1))//up
- {
- a[x][y] = cache;
- a[x+1][y] = cache2;
- a[x+2][y] = cache3;
- x = x - 1;
- cache = a[x][y];
- cache2 = a[x+1][y];
- cache3 = a[x+2][y];
- a[x][y] = 1;
- a[x+1][y] = 1;
- a[x+2][y] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 80) && (x <= 6))//down
- {
- a[x][y] = cache;
- a[x+1][y] = cache2;
- a[x+2][y] = cache3;
- x = x + 1;
- cache = a[x][y];
- cache2 = a[x+1][y];
- cache3 = a[x+2][y];
- a[x][y] = 1;
- a[x+1][y] = 1;
- a[x+2][y] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 43) && (y <= 7)) { //смена ориентации
- ships[i].orientation = false;
- a[x+1][y] = cache2;
- a[x+2][y] = cache3;
- cache2 = a[x][y+1];
- cache3 = a[x][y+2];
- a[x][y+1] = 1;
- a[x][y+2] = 1;
- f_player.shipArrange(a);
- }
- }
- else { //горизонтальная
- if ((c == 77) && (y <= 6))//right
- {
- a[x][y] = cache;
- a[x][y+1] = cache2;
- a[x][y+2] = cache3;
- y = y + 1;
- cache = a[x][y];
- cache2 = a[x][y+1];
- cache3 = a[x][y+2];
- a[x][y] = 1;
- a[x][y+1] = 1;
- a[x][y+2] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 75) && (y >= 1))//left
- {
- a[x][y] = cache;
- a[x][y+1] = cache2;
- a[x][y+2] = cache3;
- y = y - 1;
- cache = a[x][y];
- cache2 = a[x][y+1];
- cache3 = a[x][y+2];
- a[x][y] = 1;
- a[x][y+1] = 1;
- a[x][y+2] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 72) && (x >= 1))//up
- {
- a[x][y] = cache;
- a[x][y+1] = cache2;
- a[x][y+2] = cache3;
- x = x - 1;
- cache = a[x][y];
- cache2 = a[x][y+1];
- cache3 = a[x][y+2];
- a[x][y] = 1;
- a[x][y+1] = 1;
- a[x][y+2] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 80) && (x <= 8))//down
- {
- a[x][y] = cache;
- a[x][y+1] = cache2;
- a[x][y+2] = cache3;
- x = x + 1;
- cache = a[x][y];
- cache2 = a[x][y+1];
- cache3 = a[x][y+2];
- a[x][y] = 1;
- a[x][y+1] = 1;
- a[x][y+2] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 43) && (x <= 7)) { //смена ориентации
- ships[i].orientation = true;
- a[x][y+1] = cache2;
- a[x][y+2] = cache3;
- cache2 = a[x+1][y];
- cache3 = a[x+2][y];
- a[x+1][y] = 1;
- a[x+2][y] = 1;
- f_player.shipArrange(a);
- }
- }
- }
- if (ships[i].orientation) {
- a[x][y] = 3;
- a[x+1][y] = 3;
- a[x+2][y] = 3;
- if ((x <= 8) && (y <= 8))
- a[x+1][y+1] = 2;
- if (y <= 8)
- a[x][y+1] = 2;
- if (x > 0)
- a[x-1][y] = 2;
- if ((x > 0) && (y > 0))
- a[x-1][y-1] = 2;
- if ((x <= 8) && (y > 0))
- a[x+1][y-1] = 2;
- if ((x > 0) && (y <= 8))
- a[x-1][y+1] = 2;
- if (y > 0)
- a[x][y-1] = 2;
- if (x <= 6)
- a[x+3][y] = 2;
- if ((x <= 6) && (y > 0))
- a[x+3][y-1] = 2;
- if ((x <= 6) && (y <= 8))
- a[x+3][y+1] = 2;
- if ((x <= 7) && (y > 0))
- a[x+2][y-1] = 2;
- if ((x <= 7) && (y <= 8))
- a[x+2][y+1] = 2;
- }
- else {
- a[x][y] = 3;
- a[x][y+1] = 3;
- a[x][y+2] = 3;
- if (y > 0)//сдева начинаю по часовой
- a[x][y-1] = 2;
- if ((y > 0) && (x > 0))
- a[x-1][y-1] = 2;
- if (x > 0)
- a[x-1][y] = 2;
- if ((y <= 8) && (x > 0))
- a[x-1][y+1] = 2;
- if ((y <= 7) && (x > 0))
- a[x-1][y+2] = 2;
- if ((y <= 6) && (x > 0))
- a[x-1][y+3] = 2;
- if (y <= 6)
- a[x][y+3] = 2;
- if ((y <= 6) && (x <= 8))
- a[x+1][y+3] = 2;
- if ((y <= 7) && (x <= 8))
- a[x+1][y+2] = 2;
- if ((y <= 8) && (x <= 8))
- a[x+1][y+1] = 2;
- if (x <= 8)
- a[x+1][y] = 2;
- if ((x <= 8) && (y > 0))
- a[x+1][y-1] = 2;
- }
- ships[i].x = x;
- ships[i].y = y;
- }
- int cache4;
- for (int i = 9; i < 10; ++i) { //расстановка четырехпаалубных
- ships[i].orientation = true;
- ships[i].length = 4;
- ships[i].health = 4;
- x = 0;
- y = 0;
- cache = a[x][y];
- cache2 = a[x+1][y];
- cache3 = a[x+2][y];
- cache4 = a[x+3][y];
- a[x][y] = 1;
- a[x+1][y] = 1;
- a[x+2][y] = 1;
- a[x+3][y] = 1;
- f_player.shipArrange(a);
- while (((c = _getch()) != 13) || (cache != 0) || (cache2 != 0) || (cache3 != 0) || (cache4 != 0))
- {
- if (ships[i].orientation) { //вертикальная
- if ((c == 77) && (y <= 8))//right
- {
- a[x][y] = cache;
- a[x+1][y] = cache2;
- a[x+2][y] = cache3;
- a[x+3][y] = cache4;
- y = y + 1;
- cache = a[x][y];
- cache2 = a[x+1][y];
- cache3 = a[x+2][y];
- cache4 = a[x+3][y];
- a[x][y] = 1;
- a[x+1][y] = 1;
- a[x+2][y] = 1;
- a[x+3][y] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 75) && (y >= 1))//left
- {
- a[x][y] = cache;
- a[x+1][y] = cache2;
- a[x+2][y] = cache3;
- a[x+3][y] = cache4;
- y = y - 1;
- cache = a[x][y];
- cache2 = a[x+1][y];
- cache3 = a[x+2][y];
- cache4 = a[x+3][y];
- a[x][y] = 1;
- a[x+1][y] = 1;
- a[x+2][y] = 1;
- a[x+3][y] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 72) && (x >= 1))//up
- {
- a[x][y] = cache;
- a[x+1][y] = cache2;
- a[x+2][y] = cache3;
- a[x+3][y] = cache4;
- x = x - 1;
- cache = a[x][y];
- cache2 = a[x+1][y];
- cache3 = a[x+2][y];
- cache4 = a[x+3][y];
- a[x][y] = 1;
- a[x+1][y] = 1;
- a[x+2][y] = 1;
- a[x+3][y] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 80) && (x <= 5))//down
- {
- a[x][y] = cache;
- a[x+1][y] = cache2;
- a[x+2][y] = cache3;
- a[x+3][y] = cache4;
- x = x + 1;
- cache = a[x][y];
- cache2 = a[x+1][y];
- cache3 = a[x+2][y];
- cache4 = a[x+3][y];
- a[x][y] = 1;
- a[x+1][y] = 1;
- a[x+2][y] = 1;
- a[x+3][y] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 43) && (y <= 6)) { //смена ориентации
- ships[i].orientation = false;
- a[x+1][y] = cache2;
- a[x+2][y] = cache3;
- a[x+3][y] = cache4;
- cache2 = a[x][y+1];
- cache3 = a[x][y+2];
- cache4 = a[x][y+3];
- a[x][y+1] = 1;
- a[x][y+2] = 1;
- a[x][y+3] = 1;
- f_player.shipArrange(a);
- }
- }
- else { //горизонтальная
- if ((c == 77) && (y <= 5))//right
- {
- a[x][y] = cache;
- a[x][y+1] = cache2;
- a[x][y+2] = cache3;
- a[x][y+3] = cache4;
- y = y + 1;
- cache = a[x][y];
- cache2 = a[x][y+1];
- cache3 = a[x][y+2];
- cache4 = a[x][y+3];
- a[x][y] = 1;
- a[x][y+1] = 1;
- a[x][y+2] = 1;
- a[x][y+3] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 75) && (y >= 1))//left
- {
- a[x][y] = cache;
- a[x][y+1] = cache2;
- a[x][y+2] = cache3;
- a[x][y+3] = cache4;
- y = y - 1;
- cache = a[x][y];
- cache2 = a[x][y+1];
- cache3 = a[x][y+2];
- cache4 = a[x][y+3];
- a[x][y] = 1;
- a[x][y+1] = 1;
- a[x][y+2] = 1;
- a[x][y+3] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 72) && (x >= 1))//up
- {
- a[x][y] = cache;
- a[x][y+1] = cache2;
- a[x][y+2] = cache3;
- a[x][y+3] = cache4;
- x = x - 1;
- cache = a[x][y];
- cache2 = a[x][y+1];
- cache3 = a[x][y+2];
- cache4 = a[x][y+3];
- a[x][y] = 1;
- a[x][y+1] = 1;
- a[x][y+2] = 1;
- a[x][y+3] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 80) && (x <= 8))//down
- {
- a[x][y] = cache;
- a[x][y+1] = cache2;
- a[x][y+2] = cache3;
- a[x][y+3] = cache4;
- x = x + 1;
- cache = a[x][y];
- cache2 = a[x][y+1];
- cache3 = a[x][y+2];
- cache4 = a[x][y+3];
- a[x][y] = 1;
- a[x][y+1] = 1;
- a[x][y+2] = 1;
- a[x][y+3] = 1;
- f_player.shipArrange(a);
- }
- if ((c == 43) && (x <= 6)) { //смена ориентации
- ships[i].orientation = true;
- a[x][y+1] = cache2;
- a[x][y+2] = cache3;
- a[x][y+3] = cache4;
- cache2 = a[x+1][y];
- cache3 = a[x+2][y];
- cache4 = a[x+3][y];
- a[x+1][y] = 1;
- a[x+2][y] = 1;
- a[x+3][y] = 1;
- f_player.shipArrange(a);
- }
- }
- }
- if (ships[i].orientation) {
- a[x][y] = 3;
- a[x+1][y] = 3;
- a[x+2][y] = 3;
- a[x+3][y] = 3;
- if ((x <= 8) && (y <= 8))
- a[x+1][y+1] = 2;
- if (y <= 8)
- a[x][y+1] = 2;
- if (x > 0)
- a[x-1][y] = 2;
- if ((x > 0) && (y > 0))
- a[x-1][y-1] = 2;
- if ((x <= 8) && (y > 0))
- a[x+1][y-1] = 2;
- if ((x > 0) && (y <= 8))
- a[x-1][y+1] = 2;
- if (y > 0)
- a[x][y-1] = 2;
- if (x <= 5)
- a[x+4][y] = 2;
- if ((x <= 5) && (y > 0))
- a[x+4][y-1] = 2;
- if ((x <= 6) && (y > 0))
- a[x+3][y-1] = 2;
- if ((x <= 6) && (y <= 8))
- a[x+3][y+1] = 2;
- if ((x <= 5) && (y <= 8))
- a[x+4][y+1] = 2;
- if ((x <= 7) && (y > 0))
- a[x+2][y-1] = 2;
- if ((x <= 7) && (y <= 8))
- a[x+2][y+1] = 2;
- }
- else {
- a[x][y] = 3;
- a[x][y+1] = 3;
- a[x][y+2] = 3;
- a[x][y+3] = 3;
- if (y > 0)//сдева начинаю по часовой
- a[x][y-1] = 2;
- if ((y > 0) && (x > 0))
- a[x-1][y-1] = 2;
- if (x > 0)
- a[x-1][y] = 2;
- if ((y <= 8) && (x > 0))
- a[x-1][y+1] = 2;
- if ((y <= 7) && (x > 0))
- a[x-1][y+2] = 2;
- if ((y <= 6) && (x > 0))
- a[x-1][y+3] = 2;
- if ((y <= 5) && (x > 0))
- a[x-1][y+4] = 2;
- if (y <= 5)
- a[x][y+4] = 2;
- if ((y <= 6) && (x <= 8))
- a[x+1][y+3] = 2;
- if ((y <= 5) && (x <= 8))
- a[x+1][y+4] = 2;
- if ((y <= 7) && (x <= 8))
- a[x+1][y+2] = 2;
- if ((y <= 8) && (x <= 8))
- a[x+1][y+1] = 2;
- if (x <= 8)
- a[x+1][y] = 2;
- if ((x <= 8) && (y > 0))
- a[x+1][y-1] = 2;
- }
- ships[i].x = x;
- ships[i].y = y;
- }
- }
- 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) {
- int c, cache, num;
- f_bot.refresh(a2);
- if (!done) {
- GoToXY (3, 1);
- cout << "Something went wrong, save was not done";
- }
- done = true;
- cache = a2[x][y];
- a2[x][y] = 1;
- while (((c = _getch()) != 13) || (cache != 0))
- {
- if ((c == 77) && (y <= 8))//right
- {
- a2[x][y] = cache;
- y = y + 1;
- cache = a2[x][y];
- a2[x][y] = 1;
- f_bot.refresh(a2);
- }
- else if ((c == 77) && (y > 8))//right!!!
- {
- a2[x][y] = cache;
- y = 0;
- cache = a2[x][y];
- a2[x][y] = 1;
- f_bot.refresh(a2);
- }
- if ((c == 75) && (y >= 1))//left
- {
- a2[x][y] = cache;
- y = y - 1;
- cache = a2[x][y];
- a2[x][y] = 1;
- f_bot.refresh(a2);
- }
- else if ((c == 75) && (y < 1))//left!!!
- {
- a2[x][y] = cache;
- y = 9;
- cache = a2[x][y];
- a2[x][y] = 1;
- f_bot.refresh(a2);
- }
- if ((c == 72) && (x >= 1))//up
- {
- a2[x][y] = cache;
- x = x - 1;
- cache = a2[x][y];
- a2[x][y] = 1;
- f_bot.refresh(a2);
- }
- else if ((c == 72) && (x < 1))//up!!!
- {
- a2[x][y] = cache;
- x = 9;
- cache = a2[x][y];
- a2[x][y] = 1;
- f_bot.refresh(a2);
- }
- if ((c == 80) && (x <= 8))//down
- {
- a2[x][y] = cache;
- x = x + 1;
- cache = a2[x][y];
- a2[x][y] = 1;
- f_bot.refresh(a2);
- }
- else if ((c == 80) && (x > 8))//down
- {
- a2[x][y] = cache;
- x = 0;
- cache = a2[x][y];
- a2[x][y] = 1;
- f_bot.refresh(a2);
- }
- if (c == 27) {
- back_menu = 0;
- resume (back_menu);
- if ((back_menu == 1) || (back_menu == 2)) {
- a2[x][y] = cache;
- TextBackground (0);
- GoToXY (5, 32);//play 17 was
- cout << " " << endl;
- GoToXY (5, WhereY());
- cout << " " << endl;
- GoToXY (5, WhereY());
- cout << " " << endl;
- GoToXY (37, 32); //load
- cout << " " << endl;
- GoToXY (37, WhereY());
- cout << " " << endl;
- GoToXY (37, WhereY());
- cout << " " << endl;
- GoToXY (68, 32); //load
- cout << " " << endl;
- GoToXY (68, WhereY());
- cout << " " << endl;
- GoToXY (68, WhereY());
- cout << " " << endl;
- return;
- }
- if (back_menu == 3)
- return;
- }
- }
- if (b[x][y] == 3) {
- a2[x][y] = 5;
- num = bot.findShip (x, y);
- --bot.ships[num].health;
- if (bot.ships[num].health == 0) {
- matchArea (a2, bot.ships[num].x, bot.ships[num].y, bot.ships[num].length, bot.ships[num].orientation, 4);
- --bot.ships_amount;
- f_bot.refresh(a2);
- --counter.ship[bot.ships[num].length-1];
- counter.draw (1, 5);
- }
- if (bot.ships_amount != 0)
- shot = true;
- else {
- shot = false;
- return;
- }
- }
- else {
- a2[x][y] = 4;
- shot = false;
- }
- f_bot.refresh(a2);
- }
- };
- 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) {
- ofstream F ("sea_battle3.dat", ios::binary);
- done = F;
- if (F) {
- F.write ((char*)a, sizeof (int) * 100);
- F.write ((char*)a2, sizeof (int) * 100);
- F.write ((char*)b, sizeof (int) * 100);
- F.write ((char*)b2, sizeof (int) * 100);
- F.write ((char*)trigger, sizeof (int) * 7);
- F.write ((char*)&f_bot.x1, sizeof (int));
- F.write ((char*)&f_bot.y1, sizeof (int));
- F.write ((char*)&f_player.x1, sizeof (int));
- F.write ((char*)&f_player.y1, sizeof (int));
- F.write ((char*)&player.ships_amount, sizeof (int));
- for (int i = 0; i < 10; ++i) {
- F.write ((char*)&player.ships[i].x, sizeof (int));
- F.write ((char*)&player.ships[i].y, sizeof (int));
- F.write ((char*)&player.ships[i].length, sizeof (int));
- F.write ((char*)&player.ships[i].health, sizeof (int));
- F.write ((char*)&player.ships[i].orientation, sizeof (bool));
- }
- F.write ((char*)&bot.current_orientation, sizeof (bool));
- F.write ((char*)&bot.ships_amount, sizeof (int));
- F.write ((char*)&bot.free_boxes, sizeof (int));
- F.write ((char*)bot.enemy_ships, sizeof (int) * 4);
- for (int i = 0; i < 10; ++i) {
- F.write ((char*)&bot.ships[i].x, sizeof (int));
- F.write ((char*)&bot.ships[i].y, sizeof (int));
- F.write ((char*)&bot.ships[i].length, sizeof (int));
- F.write ((char*)&bot.ships[i].health, sizeof (int));
- F.write ((char*)&bot.ships[i].orientation, sizeof (bool));
- }
- F.write ((char*)counter.ship, sizeof (int) * 4);
- F.write ((char*)&x, sizeof (int));
- F.write ((char*)&y, sizeof (int));
- F.write ((char*)&shot_player, sizeof (bool));
- F.write ((char*)&shot_bot, sizeof (bool));
- F.write ((char*)&back_menu, sizeof (int));
- }
- F.close();
- return;
- }
- 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) {
- ifstream F ("sea_battle3.dat", ios::binary);
- if (F) {
- is_loaded = F;
- int n;
- bool m;
- F.read ((char*)a, sizeof (int) * 100);
- F.read ((char*)a2, sizeof (int) * 100);
- F.read ((char*)b, sizeof (int) * 100);
- F.read ((char*)b2, sizeof (int) * 100);
- F.read ((char*)trigger, sizeof (int) * 7);
- F.read ((char*)&n, sizeof (int));
- f_bot.x1 = n;
- F.read ((char*)&n, sizeof (int));
- f_bot.y1 = n;
- F.read ((char*)&n, sizeof (int));
- f_player.x1 = n;
- F.read ((char*)&n, sizeof (int));
- f_player.y1 = n;
- F.read ((char*)&n, sizeof (int));
- player.ships_amount = n;
- for (int i = 0; i < 10; ++i) {
- F.read ((char*)&n, sizeof (int));
- player.ships[i].x = n;
- F.read ((char*)&n, sizeof (int));
- player.ships[i].y = n;
- F.read ((char*)&n, sizeof (int));
- player.ships[i].length = n;
- F.read ((char*)&n, sizeof (int));
- player.ships[i].health = n;
- F.read ((char*)&m, sizeof (bool));
- player.ships[i].orientation = m;
- }
- F.read ((char*)&m, sizeof (bool));
- bot.current_orientation = m;
- F.read ((char*)&n, sizeof (int));
- bot.ships_amount = n;
- F.read ((char*)&n, sizeof (int));
- bot.free_boxes = n;
- F.read ((char*)bot.enemy_ships, sizeof (int) * 4);
- for (int i = 0; i < 10; ++i) {
- F.read ((char*)&n, sizeof (int));
- bot.ships[i].x = n;
- F.read ((char*)&n, sizeof (int));
- bot.ships[i].y = n;
- F.read ((char*)&n, sizeof (int));
- bot.ships[i].length = n;
- F.read ((char*)&n, sizeof (int));
- bot.ships[i].health = n;
- F.read ((char*)&m, sizeof (bool));
- bot.ships[i].orientation = m;
- }
- F.read ((char*)counter.ship, sizeof (int) * 4);
- F.read ((char*)&n, sizeof (int));
- x = n;
- F.read ((char*)&n, sizeof (int));
- y = n;
- F.read ((char*)&m, sizeof (bool));
- shot_player = m;
- F.read ((char*)&m, sizeof (bool));
- shot_bot = m;
- F.read ((char*)&n, sizeof (int));
- back_menu = n;
- F.close();
- return;
- }
- else {
- is_loaded = false;
- return;
- }
- }
- void work()
- {
- ClrScr();
- Field f_bot, f_player;
- Player player;
- Bot bot;
- Counter counter;
- int trigger[7] = {0, 0, 0, 0, 0, 0, 0};
- int a[10][10] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
- };
- int a2[10][10] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
- };
- int b[10][10] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
- };
- int b2[10][10] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
- };
- f_player.draw (43, 10);
- counter.draw (1, 5);
- f_bot.draw (1, 10);
- bot.shipArrange(b, f_bot);
- int back_menu(0);
- player.shipArrange(a, f_player);
- f_player.refresh(a);
- bool shot_player, shot_bot;
- int x(0), y(0);
- bool done (true);
- while ((bot.ships_amount != 0) && (player.ships_amount != 0)) {
- shot_player = true;
- shot_bot = true;
- while (shot_player) {
- back_menu = 0;
- player.attack (b, a2, f_bot, bot, shot_player, x, y, counter, back_menu, f_player, a, done);
- if (back_menu == 3)
- return;
- if (back_menu == 2) {
- save (a, a2, b, b2, trigger, f_bot, f_player, player, bot, counter, x, y, shot_player, shot_bot, back_menu, done);
- }
- back_menu = 0;
- }
- if (bot.ships_amount != 0)
- while (shot_bot) {
- if (trigger[0]) {
- bot.attack_w_trigger (a, b2, f_player, player.ships, player.ships_amount, shot_bot, trigger);
- }
- else
- bot.attack (a, b2, f_player, player.ships, player.ships_amount, shot_bot, trigger);
- }
- }
- GoToXY (10, 35);
- if (bot.ships_amount == 0)
- cout << "You win!!!!!!!!";
- else
- cout << "Lost :(";
- int c = _getch();
- }
- void work_w_load (bool &is_loaded) {
- ClrScr();
- Field f_bot, f_player;
- Player player;
- Bot bot;
- Counter counter;
- int a[10][10], a2[10][10], b[10][10], b2[10][10], trigger[7];
- int back_menu;
- bool shot_player, shot_bot;
- int x, y;
- bool done (true);
- load (a, a2, b, b2, trigger, f_bot, f_player, player, bot, counter, x, y, shot_player, shot_bot, back_menu, is_loaded);
- if (!is_loaded)
- return;
- counter.draw (1, 5);
- f_bot.refresh(a2);
- f_player.refresh(a);
- while ((bot.ships_amount != 0) && (player.ships_amount != 0)) {
- shot_player = true;
- shot_bot = true;
- while (shot_player) {
- back_menu = 0;
- player.attack (b, a2, f_bot, bot, shot_player, x, y, counter, back_menu, f_player, a, done);
- if (back_menu == 3)
- return;
- if (back_menu == 2) {
- save (a, a2, b, b2, trigger, f_bot, f_player, player, bot, counter, x, y, shot_player, shot_bot, back_menu, done);
- }
- back_menu = 0;
- }
- if (bot.ships_amount != 0)
- while (shot_bot) {
- if (trigger[0]) {
- bot.attack_w_trigger (a, b2, f_player, player.ships, player.ships_amount, shot_bot, trigger);
- }
- else
- bot.attack (a, b2, f_player, player.ships, player.ships_amount, shot_bot, trigger);
- }
- }
- GoToXY (10, 35);
- if (bot.ships_amount == 0)
- cout << "You win!!!!!!!!";
- else
- cout << "Lost :(";
- int c = _getch();
- }
- int main()
- {
- ScrSize (85, 40);
- ConSize (85, 40);
- CursorVisible(false);
- int c;
- while (true) {
- if ((c = inform()) == 0)
- work();
- else if (c == 2)
- return 0;
- else if (c == 1) {
- bool is_loaded;
- work_w_load (is_loaded);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement