Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <string>
- #include <iomanip>
- #include <ctime>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <windows.h>
- using namespace std;
- class zaidejas {
- private:
- int carrier, battleship, cruiser, submarine, destroyer;
- string laivoPav, statomasLaivas;
- int statomoLaivoIlgis;
- string direction;
- string startingXY;
- int startingX, startingY;
- int k;
- int coordX, coordY;
- public:
- zaidejas() :startingX(0), startingY(0){}
- zaidejas(int a, int b) :startingX(a), startingY(b) {}
- zaidejas(const zaidejas&);
- ~zaidejas(){}
- void pradinisZaidejoUzpildymas() {
- for (int i = 0; i <= 10; i++) {
- for (int j = 0; j <= 10; j++) {
- zaidejoLenta[i][j] = "[ ]";
- }
- }
- }
- virtual void zaidejoLentaIsspausdinti() {
- cout << " A B C D E F G H I J " << endl;
- for (int i = 1; i <= 10; i++) {
- cout << fixed << setw(2) << i << " ";
- for (int j = 1; j <= 10; j++) {
- cout << zaidejoLenta[i][j];
- }
- cout << endl;
- }
- }
- virtual void laivuPriskyrimas() {
- carrier = 1;
- battleship = 1;
- cruiser = 1;
- submarine = 1;
- destroyer = 1;
- }
- void operator ++() {
- pradinisZaidejoUzpildymas();
- laivuPriskyrimas();
- zaidejoLentaIsspausdinti();
- while (arGalimaPildyti()) {
- turimiLaivai();
- tikrinamaArGalima();
- laivoKryptis();
- userBattleCoordinates();
- userBattleShipFill();
- system("cls");
- zaidejoLentaIsspausdinti();
- }
- pravalyti();
- system("cls");
- zaidejoLentaIsspausdinti();
- }
- bool arGalimaPildyti() {
- if (carrier + battleship + cruiser + submarine + destroyer > 0)
- return true;
- else return false;
- }
- void turimiLaivai() {
- cout << "TURIMI LAIVAI:" << endl;
- if (carrier == 1) cout << "Carrier. Laivo ilgis: 5" << endl;
- if (battleship == 1) cout << "Battleship. Laivo ilgis: 4" << endl;
- if (cruiser == 1) cout << "Cruiser. Laivo ilgis: 3" << endl;
- if (submarine == 1) cout << "Submarine. Laivo ilgis: 3" << endl;
- if (destroyer == 1) cout << "Destroyer. Laivo ilgis: 2" << endl;
- }
- void tikrinamaArGalima() {
- cout << "Kuri laiva norite pastatyti? (pvz.:'Carrier') ";
- cin >> laivoPav;
- if (laivoPav == "Carrier") {
- if (carrier == 0) {
- cout << "Laivas jau yra pastatytas. Pabandykite dar karta." << endl;
- tikrinamaArGalima();
- }
- else {
- carrier--;
- statomasLaivas = "Carrier";
- statomoLaivoIlgis = 5;
- }
- }
- else if (laivoPav == "Battleship") {
- if (battleship == 0) {
- cout << "Laivas jau yra pastatytas. Pabandykite dar karta." << endl;
- tikrinamaArGalima();
- }
- else {
- battleship--;
- statomasLaivas = "Battleship";
- statomoLaivoIlgis = 4;
- }
- }
- else if (laivoPav == "Cruiser") {
- if (cruiser == 0) {
- cout << "Laivas jau yra pastatytas. Pabandykite dar karta." << endl;
- tikrinamaArGalima();
- }
- else {
- cruiser--;
- statomasLaivas = "Cruiser";
- statomoLaivoIlgis = 3;
- }
- }
- else if (laivoPav == "Submarine") {
- if (submarine == 0) {
- cout << "Laivas jau yra pastatytas. Pabandykite dar karta." << endl;
- tikrinamaArGalima();
- }
- else {
- submarine--;
- statomasLaivas = "Submarine";
- statomoLaivoIlgis = 3;
- }
- }
- else if (laivoPav == "Destroyer") {
- if (destroyer == 0) {
- cout << "Laivas jau yra pastatytas. Pabandykite dar karta." << endl;
- tikrinamaArGalima();
- }
- else {
- destroyer--;
- statomasLaivas = "Destroyer";
- statomoLaivoIlgis = 2;
- }
- }
- else {
- cout << "Neteisingai ivestas pavadinimas. Pabandykite dar karta." << endl;
- tikrinamaArGalima();
- }
- }
- void laivoKryptis() {
- cout << "Kokia kryptimi norite pastatyti laiva? ('v' - vertikaliai, 'h' - horizontaliai) ";
- cin >> direction;
- if (direction != "v" && direction != "h") {
- cout << "Kryptis ivesta neteisingai. Pabandykite dar karta." << endl;
- laivoKryptis();
- }
- }
- void userBattleCoordinates() {
- cout << "Iveskite pradine koordinate (pvz. '1A'): ";
- cin >> startingXY;
- if (startingXY.size() > 3) {
- cout << "Ivyko klaida. Pabandykite dar karta." << endl;
- userBattleCoordinates();
- }
- if (startingXY[0] == '1') {
- if (startingXY[1] == '0') {
- startingX = 10;
- }
- else startingX = 1;
- }
- else if (startingXY[0] == '2') startingX = 2;
- else if (startingXY[0] == '3') startingX = 3;
- else if (startingXY[0] == '4') startingX = 4;
- else if (startingXY[0] == '5') startingX = 5;
- else if (startingXY[0] == '6') startingX = 6;
- else if (startingXY[0] == '7') startingX = 7;
- else if (startingXY[0] == '8') startingX = 8;
- else if (startingXY[0] == '9') startingX = 9;
- else {
- cout << "Ivyko klaida. Pabandykite dar karta." << endl;
- userBattleCoordinates();
- }
- int index;
- if (startingX == 10) index = 2;
- else index = 1;
- if (startingXY[index] == 'A') startingY = 1;
- else if (startingXY[index] == 'B') startingY = 2;
- else if (startingXY[index] == 'C') startingY = 3;
- else if (startingXY[index] == 'D') startingY = 4;
- else if (startingXY[index] == 'E') startingY = 5;
- else if (startingXY[index] == 'F') startingY = 6;
- else if (startingXY[index] == 'G') startingY = 7;
- else if (startingXY[index] == 'H') startingY = 8;
- else if (startingXY[index] == 'I') startingY = 9;
- else if (startingXY[index] == 'J') startingY = 10;
- else {
- cout << "Ivyko klaida. Pabandykite dar karta." << endl;
- userBattleCoordinates();
- }
- // tikrinama ar koordinates kertasi ar ne
- if (direction == "h") {
- for (int i = 0; i < statomoLaivoIlgis; i++) {
- if (zaidejoLenta[startingX][startingY + i] == "[X]" || zaidejoLenta[startingX][startingY + i] == "[.]") {
- cout << "Laivo statymas negalimas. Jis kertasi su kitu laivu erdve." << endl;
- userBattleCoordinates();
- }
- }
- if (startingY + statomoLaivoIlgis - 1 > 10) {
- cout << "Laivo statymas negalimas. Jo ilgis virsija zaidimu lenta." << endl;
- userBattleCoordinates();
- }
- }
- else if (direction == "v") {
- for (int i = 0; i < statomoLaivoIlgis; i++) {
- if (zaidejoLenta[startingX + i][startingY] == "[X]" || zaidejoLenta[startingX + i][startingY] == "[.]") {
- cout << "Laivo statymas negalimas. Jis kertasi su kitu laivu erdve." << endl;
- userBattleCoordinates();
- }
- }
- if (startingX + statomoLaivoIlgis - 1 > 10) {
- cout << "Laivo statymas negalimas. Jo ilgis virsija zaidimu lenta." << endl;
- userBattleCoordinates();
- }
- }
- }
- void userBattleShipFill() {
- k = 0;
- if (direction == "h") {
- zaidejoLenta[startingX][startingY - 1] = "[.]";
- zaidejoLenta[startingX][startingY + statomoLaivoIlgis] = "[.]";
- for (int i = 0; i < statomoLaivoIlgis; i++) {
- coordX = startingX;
- coordY = startingY + i;
- zaidejoLenta[startingX][startingY + i] = "[X]";
- shipCoordinatesForEach();
- }
- for (int i = 0; i < statomoLaivoIlgis + 2; i++) {
- zaidejoLenta[startingX - 1][startingY - 1 + i] = "[.]";
- }
- for (int i = 0; i < statomoLaivoIlgis + 2; i++) {
- zaidejoLenta[startingX + 1][startingY - 1 + i] = "[.]";
- }
- }
- else if (direction == "v") {
- zaidejoLenta[startingX - 1][startingY] = "[.]";
- zaidejoLenta[startingX + statomoLaivoIlgis][startingY] = "[.]";
- for (int i = 0; i < statomoLaivoIlgis; i++) {
- coordX = startingX + i;
- coordY = startingY;
- zaidejoLenta[startingX + i][startingY] = "[X]";
- shipCoordinatesForEach();
- }
- for (int i = 0; i < statomoLaivoIlgis + 2; i++) {
- zaidejoLenta[startingX - 1 + i][startingY - 1] = "[.]";
- }
- for (int i = 0; i < statomoLaivoIlgis + 2; i++) {
- zaidejoLenta[startingX - 1 + i][startingY + 1] = "[.]";
- }
- }
- }
- void shipCoordinatesForEach() {
- if (statomasLaivas == "Carrier") {
- userCarrierX[k] = coordX;
- userCarrierY[k] = coordY;
- k++;
- }
- else if (statomasLaivas == "Battleship") {
- userBattleshipX[k] = coordX;
- userBattleshipY[k] = coordY;
- k++;
- }
- else if (statomasLaivas == "Cruiser") {
- userCruiserX[k] = coordX;
- userCruiserY[k] = coordY;
- k++;
- }
- else if (statomasLaivas == "Submarine") {
- userSubmarineX[k] = coordX;
- userSubmarineY[k] = coordY;
- k++;
- }
- else if (statomasLaivas == "Destroyer") {
- userDestroyerX[k] = coordX;
- userDestroyerY[k] = coordY;
- k++;
- }
- }
- void pravalyti() {
- for (int i = 0; i <= 10; i++) {
- for (int j = 0; j <= 10; j++) {
- if (zaidejoLenta[i][j] == "[.]") zaidejoLenta[i][j] = "[ ]";
- }
- }
- }
- protected:
- string zaidejoLenta[12][12];
- int userCarrierX[5], userBattleshipX[4], userCruiserX[3], userSubmarineX[3], userDestroyerX[2];
- int userCarrierY[5], userBattleshipY[4], userCruiserY[3], userSubmarineY[3], userDestroyerY[2];
- };
- class kompiuteris {
- private:
- int k;
- int shipLenght;
- string shipName;
- int cpuDir, startingX, startingY, coordX, coordY;
- public:
- kompiuteris() :k(0), shipLenght(0) {}
- kompiuteris(int a, int b) :k(a), shipLenght(b) {}
- void cpuPradinisUzpildymas() {
- for (int i = 1; i <= 10; i++) {
- for (int j = 1; j <= 10; j++) {
- cpuBoard[i][j] = "[ ]";
- }
- }
- }
- void cpuBattleFillMain() {
- cpuPradinisUzpildymas();
- cpuBattleCarrierFill();
- cpuBattleBattleshipFill();
- cpuBattleCruiserFill();
- cpuBattleSubmarineFill();
- cpuBattleDestroyerFill();
- pravalyti();
- cpuBoardShow();
- }
- void cpuBattleCarrierFill() {
- k = 0;
- shipLenght = 5;
- shipName = "Carrier";
- cpuDir = rand() % 2 + 1;
- cpuBattleShipPlacement();
- }
- void cpuBattleBattleshipFill() {
- k = 0;
- shipLenght = 4;
- shipName = "Battleship";
- cpuDir = rand() % 2 + 1;
- cpuBattleShipPlacement();
- }
- void cpuBattleCruiserFill() {
- k = 0;
- shipLenght = 3;
- shipName = "Cruiser";
- cpuDir = rand() % 2 + 1;
- cpuBattleShipPlacement();
- }
- void cpuBattleSubmarineFill() {
- k = 0;
- shipLenght = 3;
- shipName = "Submarine";
- cpuDir = rand() % 2 + 1;
- cpuBattleShipPlacement();
- }
- void cpuBattleDestroyerFill() {
- k = 0;
- shipLenght = 2;
- shipName = "Destroyer";
- cpuDir = rand() % 2 + 1;
- cpuBattleShipPlacement();
- }
- void cpuBattleShipPlacement() {
- bool arKertasi = false;
- startingX = rand() % 10 + 1;
- startingY = rand() % 10 + 1;
- if (cpuDir == 1) {
- cpuBattleShipCoordinatesHorizontal();
- for (int i = 0; i < shipLenght; i++) {
- cpuBoard[startingX][startingY + i] = "[X]";
- coordX = startingX;
- coordY = startingY + i;
- cpuShipCoordinatesForEach();
- }
- cpuBoard[startingX][startingY - 1] = "[.]";
- cpuBoard[startingX][startingY + shipLenght] = "[.]";
- for (int i = 0; i < shipLenght + 2; i++) {
- cpuBoard[startingX - 1][startingY - 1 + i] = "[.]";
- }
- for (int i = 0; i < shipLenght + 2; i++) {
- cpuBoard[startingX + 1][startingY - 1 + i] = "[.]";
- }
- }
- else {
- cpuBattleShipCoordinatesVertical();
- for (int i = 0; i < shipLenght; i++) {
- cpuBoard[startingX + i][startingY] = "[X]";
- coordX = startingX + i;
- coordY = startingY;
- cpuShipCoordinatesForEach();
- }
- cpuBoard[startingX - 1][startingY] = "[.]";
- cpuBoard[startingX + shipLenght][startingY] = "[.]";
- for (int i = 0; i < shipLenght + 2; i++) {
- cpuBoard[startingX - 1 + i][startingY - 1] = "[.]";
- }
- for (int i = 0; i < shipLenght + 2; i++) {
- cpuBoard[startingX - 1 + i][startingY + 1] = "[.]";
- }
- }
- }
- void cpuShipCoordinatesForEach() {
- if (shipName == "Carrier") {
- cpuCarrierX[k] = coordX;
- cpuCarrierY[k] = coordY;
- k++;
- }
- if (shipName == "Battleship") {
- cpuBattleshipX[k] = coordX;
- cpuBattleshipY[k] = coordY;
- k++;
- }
- if (shipName == "Cruiser") {
- cpuCruiserX[k] = coordX;
- cpuCruiserY[k] = coordY;
- k++;
- }
- if (shipName == "Submarine") {
- cpuSubmarineX[k] = coordX;
- cpuSubmarineY[k] = coordY;
- k++;
- }
- if (shipName == "Destroyer") {
- cpuDestroyerX[k] = coordX;
- cpuDestroyerY[k] = coordY;
- k++;
- }
- }
- void cpuBattleShipCoordinatesVertical() {
- startingX = rand() % 10 + 1;
- startingY = rand() % 10 + 1;
- bool arKertasi = false;
- for (int i = 0; i < shipLenght; i++) {
- if (cpuBoard[startingX + i][startingY] == "[.]") {
- arKertasi = true;
- }
- }
- for (int i = 0; i < shipLenght; i++) {
- if (cpuBoard[startingX + i][startingY] == "[X]") {
- arKertasi = true;
- }
- }
- if (startingX + shipLenght - 1 > 10) arKertasi = true;
- if (arKertasi == true) cpuBattleShipCoordinatesVertical();
- }
- void cpuBattleShipCoordinatesHorizontal() {
- startingX = rand() % 10 + 1;
- startingY = rand() % 10 + 1;
- bool arKertasi = false;
- for (int i = 0; i < shipLenght; i++) {
- if (cpuBoard[startingX][startingY + i] == "[.]") {
- arKertasi = true;
- }
- }
- for (int i = 0; i < shipLenght; i++) {
- if (cpuBoard[startingX][startingY + i] == "[X]") {
- arKertasi = true;
- }
- }
- if (startingY + shipLenght - 1 > 10) arKertasi = true;
- if (arKertasi == true) cpuBattleShipCoordinatesHorizontal();
- }
- void pravalyti() {
- for (int i = 1; i <= 10; i++) {
- for (int j = 1; j <= 10; j++) {
- if (cpuBoard[i][j] == "[.]") cpuBoard[i][j] = "[ ]";
- }
- }
- }
- void cpuBoardShow() {
- cout << " A B C D E F G H I J " << endl;
- for (int i = 1; i <= 10; i++) {
- cout << fixed << setw(2) << i << " ";
- for (int j = 1; j <= 10; j++) {
- cout << cpuBoard[i][j];
- }
- cout << endl;
- }
- }
- protected:
- string cpuBoard[12][12];
- int cpuCarrierX[5], cpuBattleshipX[4], cpuCruiserX[3], cpuSubmarineX[3], cpuDestroyerX[2];
- int cpuCarrierY[5], cpuBattleshipY[4], cpuCruiserY[3], cpuSubmarineY[3], cpuDestroyerY[2];
- };
- class LaivuMusis : public zaidejas, public kompiuteris {
- private:
- string startingXY;
- int startingX, startingY;
- string cpuLentaVisible[12][12];
- int zaidejoKoordinates, cpuKoordinates;
- int possibleAttackXY[200];
- int attackIndex, x_coord, y_coord;
- int atakosRezimas;
- int reikalingosKoordinatesX[5], reikalingosKoordinatesY[5];
- int reikalingasIlgis;
- string reikalingaKryptis;
- string kryptys[4];
- int pradineX, pradineY;
- int laivokiekis, laivokiekismetodas;
- public:
- LaivuMusis() :laivokiekis(0), laivokiekismetodas(0) {}
- LaivuMusis(int a, int b) :laivokiekis(a), laivokiekismetodas(b) {}
- bool zaidejoIrCpuKoordinates() {
- zaidejoKoordinates = 0;
- cpuKoordinates = 0;
- for (int i = 1; i <= 10; i++) {
- for (int j = 1; j <= 10; j++) {
- if (zaidejoLenta[i][j] == "[X]") zaidejoKoordinates++;
- if (cpuBoard[i][j] == "[X]") cpuKoordinates++;
- }
- }
- if (zaidejoKoordinates > 0 && cpuKoordinates > 0) return true;
- else return false;
- }
- void arZaidejasLaimejo() {
- if (zaidejoKoordinates == 0 && cpuKoordinates > 0) {
- cout << "Jus pralaimejote." << endl;
- system("pause");
- }
- else if (cpuKoordinates == 0 && zaidejoKoordinates > 0) {
- cout << "Jus laimejote. Sveikiname!" << endl;
- system("pause");
- }
- else {
- cout << "Kazkaip gavosi kazkas netaip..." << endl;
- system("pause");
- }
- }
- void uzpildytiCpuVisible() {
- for (int i = 1; i <= 10; i++) {
- for (int j = 1; j <= 10; j++) {
- cpuLentaVisible[i][j] = "[ ]";
- }
- }
- }
- void zaidejoAtakuojamaKoordinate() {
- cout << "Iveskite atakuojama koordinate (pvz. '1A'): ";
- cin >> startingXY;
- if (startingXY[0] == '1') {
- if (startingXY[1] == '0') {
- startingX = 10;
- }
- else startingX = 1;
- }
- else if (startingXY[0] == '2') startingX = 2;
- else if (startingXY[0] == '3') startingX = 3;
- else if (startingXY[0] == '4') startingX = 4;
- else if (startingXY[0] == '5') startingX = 5;
- else if (startingXY[0] == '6') startingX = 6;
- else if (startingXY[0] == '7') startingX = 7;
- else if (startingXY[0] == '8') startingX = 8;
- else if (startingXY[0] == '9') startingX = 9;
- else {
- cout << "Ivyko klaida. Pabandykite dar karta." << endl;
- zaidejoAtakuojamaKoordinate();
- }
- int index;
- if (startingX == 10) index = 2;
- else index = 1;
- if (startingXY[index] == 'A') startingY = 1;
- else if (startingXY[index] == 'B') startingY = 2;
- else if (startingXY[index] == 'C') startingY = 3;
- else if (startingXY[index] == 'D') startingY = 4;
- else if (startingXY[index] == 'E') startingY = 5;
- else if (startingXY[index] == 'F') startingY = 6;
- else if (startingXY[index] == 'G') startingY = 7;
- else if (startingXY[index] == 'H') startingY = 8;
- else if (startingXY[index] == 'I') startingY = 9;
- else if (startingXY[index] == 'J') startingY = 10;
- else {
- cout << "Ivyko klaida. Pabandykite dar karta." << endl;
- zaidejoAtakuojamaKoordinate();
- }
- if (cpuBoard[startingX][startingY] == "[!]") {
- cout << "Ivyko klaida. Pabandykite dar karta." << endl;
- if (zaidejoIrCpuKoordinates()) zaidejoAtakuojamaKoordinate();
- }
- else if (cpuBoard[startingX][startingY] == "[X]") {
- if (laivokiekismetodas == 0) {
- laivokiekis = kuriLaivaAtakuojaZaidejas();
- laivokiekismetodas = 1;
- }
- cpuLentaVisible[startingX][startingY] = "[!]";
- cpuBoard[startingX][startingY] = "[!]";
- system("cls");
- visibleOutput();
- cout << "Jus pataikete!" << endl;
- if (laivokiekismetodas == 1) {
- laivokiekis--;
- if (laivokiekis == 0) {
- cout << "Laivas nuskandintas!" << endl;
- laivokiekismetodas = 0;
- }
- }
- if (zaidejoIrCpuKoordinates()) zaidejoAtakuojamaKoordinate();
- }
- else {
- cpuLentaVisible[startingX][startingY] = "[.]";
- system("cls");
- visibleOutput();
- cout << "Jus nepataikete. Dabar kompiuterio eile." << endl;
- }
- }
- int kuriLaivaAtakuojaZaidejas() {
- for (int i = 0; i < 5; i++) {
- if (startingX == cpuCarrierX[i] && startingY == cpuCarrierY[i]) {
- return 5;
- }
- }
- for (int i = 0; i < 4; i++) {
- if (startingX == cpuBattleshipX[i] && startingY == cpuBattleshipY[i]) {
- return 4;
- }
- }
- for (int i = 0; i < 3; i++) {
- if (startingX == cpuCruiserX[i] && startingY == cpuCruiserY[i]) {
- return 3;
- }
- }
- for (int i = 0; i < 3; i++) {
- if (startingX == cpuSubmarineX[i] && startingY == cpuSubmarineY[i]) {
- return 3;
- }
- }
- for (int i = 0; i < 2; i++) {
- if (startingX == cpuDestroyerX[i] && startingY == cpuDestroyerY[i]) {
- return 2;
- }
- }
- }
- void visibleOutput() {
- cout << " JUSU LENTA:" << endl;
- cout << " A B C D E F G H I J " << endl;
- for (int i = 1; i <= 10; i++) {
- cout << fixed << setw(2) << i << " ";
- for (int j = 1; j <= 10; j++) {
- cout << zaidejoLenta[i][j];
- }
- cout << endl;
- }
- cout << endl;
- cout << " KOMPIUTERIO LENTA:" << endl;
- cout << " A B C D E F G H I J " << endl;
- for (int i = 1; i <= 10; i++) {
- cout << fixed << setw(2) << i << " ";
- for (int j = 1; j <= 10; j++) {
- cout << cpuLentaVisible[i][j];
- }
- cout << endl;
- }
- }
- void kurisZaidejoLaivas() {
- for (int i = 0; i < 5; i++) {
- if (userCarrierX[i] == x_coord && userCarrierY[i] == y_coord) {
- for (int i = 0; i < 5; i++) {
- reikalingosKoordinatesX[i] = userCarrierX[i];
- reikalingosKoordinatesY[i] = userCarrierY[i];
- reikalingasIlgis = 5;
- }
- }
- }
- for (int i = 0; i < 4; i++) {
- if (userBattleshipX[i] == x_coord && userBattleshipY[i] == y_coord) {
- for (int i = 0; i < 4; i++) {
- reikalingosKoordinatesX[i] = userBattleshipX[i];
- reikalingosKoordinatesY[i] = userBattleshipY[i];
- reikalingasIlgis = 4;
- }
- break;
- }
- }
- for (int i = 0; i < 3; i++) {
- if (userCruiserX[i] == x_coord && userCruiserY[i] == y_coord) {
- for (int i = 0; i < 3; i++) {
- reikalingosKoordinatesX[i] = userCruiserX[i];
- reikalingosKoordinatesY[i] = userCruiserY[i];
- reikalingasIlgis = 3;
- }
- break;
- }
- }
- for (int i = 0; i < 3; i++) {
- if (userSubmarineX[i] == x_coord && userSubmarineY[i] == y_coord) {
- for (int i = 0; i < 3; i++) {
- reikalingosKoordinatesX[i] = userSubmarineX[i];
- reikalingosKoordinatesY[i] = userSubmarineY[i];
- reikalingasIlgis = 3;
- }
- break;
- }
- }
- for (int i = 0; i < 2; i++) {
- if (userDestroyerX[i] == x_coord && userDestroyerY[i] == y_coord) {
- for (int i = 0; i < 2; i++) {
- reikalingosKoordinatesX[i] = userDestroyerX[i];
- reikalingosKoordinatesY[i] = userDestroyerY[i];
- reikalingasIlgis = 2;
- }
- break;
- }
- }
- }
- bool reikalinguKoordinaciuSkaicius() {
- int k = 0;
- for (int i = 0; i < reikalingasIlgis; i++) {
- if (reikalingosKoordinatesX[i] == 0 && reikalingosKoordinatesY[i] == 0) {
- k++;
- }
- }
- if (k == reikalingasIlgis) return true;
- else return false;
- }
- void pradinesReikalingosKoordinates() {
- for (int i = 0; i < 5; i++) {
- reikalingosKoordinatesX[i] = 0;
- reikalingosKoordinatesX[i] = 0;
- }
- }
- void galimosAtakos() {
- int f = 0;
- for (int i = 11; i <= 110; i++) {
- possibleAttackXY[f] = i;
- f++;
- }
- }
- void randomAttack() {
- attackIndex = rand() % 100;
- if (possibleAttackXY[attackIndex] != 0) {
- if (possibleAttackXY[attackIndex] % 10 == 0) {
- x_coord = (possibleAttackXY[attackIndex] / 10) - 1;
- y_coord = 10;
- }
- else {
- x_coord = possibleAttackXY[attackIndex] / 10;
- y_coord = possibleAttackXY[attackIndex] % 10;
- }
- possibleAttackXY[attackIndex] = 0;
- }
- else {
- randomAttack();
- }
- }
- void kompiuterioAtaka() {
- bool laivoNeraDabar = reikalinguKoordinaciuSkaicius();
- if (laivoNeraDabar) atakosRezimas = 0;
- if (atakosRezimas == 0) {
- nulinisAtakosRezimas();
- }
- else if (atakosRezimas == 1) {
- pirmasAtakosRezimas();
- }
- else if (atakosRezimas == 2) {
- antrasAtakosRezimas();
- }
- }
- void nulinisAtakosRezimas() {
- randomAttack();
- if (zaidejoLenta[x_coord][y_coord] == "[.]" || zaidejoLenta[x_coord][y_coord] == "[!]") nulinisAtakosRezimas();
- else {
- if (zaidejoLenta[x_coord][y_coord] == "[X]") {
- if (atakosRezimas == 0) {
- kryptys[0] = "kaire";
- kryptys[1] = "desine";
- kryptys[2] = "virsus";
- kryptys[3] = "apacia";
- }
- zaidejoLenta[x_coord][y_coord] = "[!]";
- Sleep(1000);
- system("cls");
- visibleOutput();
- pradineX = x_coord;
- pradineY = y_coord;
- kurisZaidejoLaivas();
- salinamaKoordinatePataikytaReikalinga();
- atakosRezimas = 1;
- kompiuterioAtaka();
- }
- else {
- zaidejoLenta[x_coord][y_coord] = "[.]";
- }
- }
- }
- void pirmasAtakosRezimas() {
- int kryptiesIndex = rand() % 4;
- if (kryptys[kryptiesIndex] != "netinka") {
- if (kryptys[kryptiesIndex] == "kaire") {
- if (zaidejoLenta[x_coord - 1][y_coord] == "[.]" || zaidejoLenta[x_coord - 1][y_coord] == "[!]") {
- kryptys[kryptiesIndex] = "netinka";
- pirmasAtakosRezimas();
- }
- if (x_coord - 1 < 1) {
- kryptys[kryptiesIndex] = "netinka";
- pirmasAtakosRezimas();
- }
- if (zaidejoLenta[x_coord - 1][y_coord] == "[X]") {
- reikalingaKryptis = "kaire";
- x_coord = x_coord - 1;
- zaidejoLenta[x_coord][y_coord] = "[!]";
- Sleep(1000);
- system("cls");
- visibleOutput();
- salinamaKoordinatePataikytaReikalinga();
- atakosRezimas = 2;
- kompiuterioAtaka();
- }
- else {
- atakosRezimas = 1;
- kryptys[kryptiesIndex] = "netinka";
- zaidejoLenta[x_coord - 1][y_coord] = "[.]";
- }
- }
- else if (kryptys[kryptiesIndex] == "desine") {
- if (zaidejoLenta[x_coord + 1][y_coord] == "[.]" || zaidejoLenta[x_coord + 1][y_coord] == "[!]") {
- kryptys[kryptiesIndex] = "netinka";
- pirmasAtakosRezimas();
- }
- if (x_coord + 1 > 10) {
- kryptys[kryptiesIndex] = "netinka";
- pirmasAtakosRezimas();
- }
- if (zaidejoLenta[x_coord + 1][y_coord] == "[X]") {
- reikalingaKryptis = "desine";
- x_coord = x_coord + 1;
- zaidejoLenta[x_coord][y_coord] = "[!]";
- system("cls");
- visibleOutput();
- salinamaKoordinatePataikytaReikalinga();
- atakosRezimas = 2;
- kompiuterioAtaka();
- }
- else {
- atakosRezimas = 1;
- kryptys[kryptiesIndex] = "netinka";
- zaidejoLenta[x_coord + 1][y_coord] = "[.]";
- }
- }
- else if (kryptys[kryptiesIndex] == "virsus") {
- if (zaidejoLenta[x_coord][y_coord - 1] == "[.]" || zaidejoLenta[x_coord][y_coord - 1] == "[!]") {
- kryptys[kryptiesIndex] = "netinka";
- pirmasAtakosRezimas();
- }
- if (y_coord - 1 < 1) {
- kryptys[kryptiesIndex] = "netinka";
- pirmasAtakosRezimas();
- }
- if (zaidejoLenta[x_coord][y_coord - 1] == "[X]") {
- reikalingaKryptis = "virsus";
- y_coord = y_coord - 1;
- zaidejoLenta[x_coord][y_coord] = "[!]";
- system("cls");
- visibleOutput();
- salinamaKoordinatePataikytaReikalinga();
- atakosRezimas = 2;
- kompiuterioAtaka();
- }
- else {
- atakosRezimas = 1;
- kryptys[kryptiesIndex] = "netinka";
- zaidejoLenta[x_coord][y_coord - 1] = "[.]";
- }
- }
- else if (kryptys[kryptiesIndex] == "apacia") {
- if (zaidejoLenta[x_coord][y_coord + 1] == "[.]" || zaidejoLenta[x_coord][y_coord + 1] == "[!]") {
- kryptys[kryptiesIndex] = "netinka";
- pirmasAtakosRezimas();
- }
- if (y_coord + 1 > 10) {
- kryptys[kryptiesIndex] = "netinka";
- pirmasAtakosRezimas();
- }
- if (zaidejoLenta[x_coord][y_coord + 1] == "[X]") {
- reikalingaKryptis = "apacia";
- y_coord = y_coord + 1;
- zaidejoLenta[x_coord][y_coord] = "[!]";
- system("cls");
- visibleOutput();
- salinamaKoordinatePataikytaReikalinga();
- atakosRezimas = 2;
- kompiuterioAtaka();
- }
- else {
- atakosRezimas = 1;
- kryptys[kryptiesIndex] = "netinka";
- zaidejoLenta[x_coord][y_coord + 1] = "[.]";
- }
- }
- }
- else {
- pirmasAtakosRezimas();
- }
- }
- void antrasAtakosRezimas() {
- bool arJauSunaikintas = reikalinguKoordinaciuSkaicius();
- if (arJauSunaikintas) {
- atakosRezimas = 0;
- kompiuterioAtaka();
- }
- else {
- if (reikalingaKryptis == "kaire") {
- if (x_coord - 1 < 1) {
- x_coord = pradineX;
- y_coord = pradineY;
- atakosRezimas = 2;
- reikalingaKryptis = "desine";
- antrasAtakosRezimas();
- }
- if (zaidejoLenta[x_coord - 1][y_coord] == "[.]" || zaidejoLenta[x_coord - 1][y_coord] == "[!]") {
- x_coord = pradineX;
- y_coord = pradineY;
- atakosRezimas = 2;
- reikalingaKryptis = "desine";
- antrasAtakosRezimas();
- }
- if (zaidejoLenta[x_coord - 1][y_coord] == "[X]") {
- x_coord = x_coord - 1;
- zaidejoLenta[x_coord][y_coord] = "[!]";
- Sleep(1000);
- system("cls");
- visibleOutput();
- salinamaKoordinatePataikytaReikalinga();
- atakosRezimas = 2;
- antrasAtakosRezimas();
- }
- else {
- zaidejoLenta[x_coord - 1][y_coord] = "[.]";
- x_coord = pradineX;
- y_coord = pradineY;
- atakosRezimas = 2;
- reikalingaKryptis = "desine";
- }
- }
- else if (reikalingaKryptis == "desine") {
- if (x_coord + 1 > 10) {
- x_coord = pradineX;
- y_coord = pradineY;
- atakosRezimas = 2;
- reikalingaKryptis = "kaire";
- antrasAtakosRezimas();
- }
- if (zaidejoLenta[x_coord + 1][y_coord] == "[.]" || zaidejoLenta[x_coord + 1][y_coord] == "[!]") {
- x_coord = pradineX;
- y_coord = pradineY;
- atakosRezimas = 2;
- reikalingaKryptis = "kaire";
- antrasAtakosRezimas();
- }
- if (zaidejoLenta[x_coord + 1][y_coord] == "[X]") {
- x_coord = x_coord + 1;
- zaidejoLenta[x_coord][y_coord] = "[!]";
- Sleep(1000);
- system("cls");
- visibleOutput();
- salinamaKoordinatePataikytaReikalinga();
- atakosRezimas = 2;
- antrasAtakosRezimas();
- }
- else {
- zaidejoLenta[x_coord + 1][y_coord] = "[.]";
- x_coord = pradineX;
- y_coord = pradineY;
- atakosRezimas = 2;
- reikalingaKryptis = "kaire";
- }
- }
- else if (reikalingaKryptis == "virsus") {
- if (y_coord - 1 < 1) {
- x_coord = pradineX;
- y_coord = pradineY;
- atakosRezimas = 2;
- reikalingaKryptis = "apacia";
- antrasAtakosRezimas();
- }
- if (zaidejoLenta[x_coord][y_coord - 1] == "[.]" || zaidejoLenta[x_coord][y_coord - 1] == "[!]") {
- x_coord = pradineX;
- y_coord = pradineY;
- atakosRezimas = 2;
- reikalingaKryptis = "apacia";
- antrasAtakosRezimas();
- }
- if (zaidejoLenta[x_coord][y_coord - 1] == "[X]") {
- y_coord = y_coord - 1;
- zaidejoLenta[x_coord][y_coord] = "[!]";
- Sleep(1000);
- system("cls");
- visibleOutput();
- salinamaKoordinatePataikytaReikalinga();
- atakosRezimas = 2;
- antrasAtakosRezimas();
- }
- else {
- zaidejoLenta[x_coord][y_coord - 1] = "[.]";
- x_coord = pradineX;
- y_coord = pradineY;
- atakosRezimas = 2;
- reikalingaKryptis = "apacia";
- }
- }
- else if (reikalingaKryptis == "apacia") {
- if (y_coord + 1 > 10) {
- reikalingaKryptis = "virsus";
- antrasAtakosRezimas();
- }
- if (zaidejoLenta[x_coord][y_coord + 1] == "[.]" || zaidejoLenta[x_coord][y_coord + 1] == "[!]") {
- x_coord = pradineX;
- y_coord = pradineY;
- atakosRezimas = 2;
- reikalingaKryptis = "virsus";
- antrasAtakosRezimas();
- }
- if (zaidejoLenta[x_coord][y_coord + 1] == "[X]") {
- y_coord = y_coord + 1;
- zaidejoLenta[x_coord][y_coord] = "[!]";
- Sleep(1000);
- system("cls");
- visibleOutput();
- salinamaKoordinatePataikytaReikalinga();
- atakosRezimas = 2;
- antrasAtakosRezimas();
- }
- else {
- zaidejoLenta[x_coord][y_coord + 1] = "[.]";
- x_coord = pradineX;
- y_coord = pradineY;
- atakosRezimas = 2;
- reikalingaKryptis = "virsus";
- }
- }
- }
- }
- void salinamaKoordinatePataikytaReikalinga() {
- for (int i = 0; i < reikalingasIlgis; i++) {
- if (reikalingosKoordinatesX[i] == x_coord && reikalingosKoordinatesY[i] == y_coord) {
- reikalingosKoordinatesX[i] = 0;
- reikalingosKoordinatesY[i] = 0;
- break;
- }
- }
- }
- void pilnasZaidimas() {
- laivokiekismetodas = 0;
- bool vykdyti;
- atakosRezimas = 0;
- uzpildytiCpuVisible();
- galimosAtakos();
- pradinesReikalingosKoordinates();
- vykdyti = zaidejoIrCpuKoordinates();
- system("cls");
- visibleOutput();
- while (vykdyti) {
- zaidejoAtakuojamaKoordinate();
- // atakos kryptys
- kompiuterioAtaka();
- Sleep(1000);
- system("cls");
- visibleOutput();
- vykdyti = zaidejoIrCpuKoordinates();
- }
- arZaidejasLaimejo();
- }
- };
- int main()
- {
- srand(time(NULL));
- LaivuMusis zaidimas;
- ++zaidimas;
- zaidimas.cpuBattleFillMain();
- zaidimas.pilnasZaidimas();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement