Advertisement
Guest User

Untitled

a guest
May 21st, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.08 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <iomanip>
  5. #include <ctime>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include <windows.h>
  10.  
  11. using namespace std;
  12.  
  13. class zaidejas {
  14. private:
  15. int carrier, battleship, cruiser, submarine, destroyer;
  16. string laivoPav, statomasLaivas;
  17. int statomoLaivoIlgis;
  18. string direction;
  19. string startingXY;
  20. int startingX, startingY;
  21. int k;
  22. int coordX, coordY;
  23. public:
  24. zaidejas() :startingX(0), startingY(0){}
  25. zaidejas(int a, int b) :startingX(a), startingY(b) {}
  26. zaidejas(const zaidejas&);
  27. ~zaidejas(){}
  28. void pradinisZaidejoUzpildymas() {
  29. for (int i = 0; i <= 10; i++) {
  30. for (int j = 0; j <= 10; j++) {
  31. zaidejoLenta[i][j] = "[ ]";
  32. }
  33. }
  34. }
  35. virtual void zaidejoLentaIsspausdinti() {
  36. cout << " A B C D E F G H I J " << endl;
  37. for (int i = 1; i <= 10; i++) {
  38. cout << fixed << setw(2) << i << " ";
  39. for (int j = 1; j <= 10; j++) {
  40. cout << zaidejoLenta[i][j];
  41. }
  42. cout << endl;
  43. }
  44. }
  45. virtual void laivuPriskyrimas() {
  46. carrier = 1;
  47. battleship = 1;
  48. cruiser = 1;
  49. submarine = 1;
  50. destroyer = 1;
  51. }
  52. void operator ++() {
  53. pradinisZaidejoUzpildymas();
  54. laivuPriskyrimas();
  55. zaidejoLentaIsspausdinti();
  56. while (arGalimaPildyti()) {
  57. turimiLaivai();
  58. tikrinamaArGalima();
  59. laivoKryptis();
  60. userBattleCoordinates();
  61. userBattleShipFill();
  62. system("cls");
  63. zaidejoLentaIsspausdinti();
  64. }
  65. pravalyti();
  66. system("cls");
  67. zaidejoLentaIsspausdinti();
  68. }
  69. bool arGalimaPildyti() {
  70. if (carrier + battleship + cruiser + submarine + destroyer > 0)
  71. return true;
  72. else return false;
  73. }
  74. void turimiLaivai() {
  75. cout << "TURIMI LAIVAI:" << endl;
  76. if (carrier == 1) cout << "Carrier. Laivo ilgis: 5" << endl;
  77. if (battleship == 1) cout << "Battleship. Laivo ilgis: 4" << endl;
  78. if (cruiser == 1) cout << "Cruiser. Laivo ilgis: 3" << endl;
  79. if (submarine == 1) cout << "Submarine. Laivo ilgis: 3" << endl;
  80. if (destroyer == 1) cout << "Destroyer. Laivo ilgis: 2" << endl;
  81. }
  82. void tikrinamaArGalima() {
  83. cout << "Kuri laiva norite pastatyti? (pvz.:'Carrier') ";
  84. cin >> laivoPav;
  85. if (laivoPav == "Carrier") {
  86. if (carrier == 0) {
  87. cout << "Laivas jau yra pastatytas. Pabandykite dar karta." << endl;
  88. tikrinamaArGalima();
  89. }
  90. else {
  91. carrier--;
  92. statomasLaivas = "Carrier";
  93. statomoLaivoIlgis = 5;
  94. }
  95. }
  96. else if (laivoPav == "Battleship") {
  97. if (battleship == 0) {
  98. cout << "Laivas jau yra pastatytas. Pabandykite dar karta." << endl;
  99. tikrinamaArGalima();
  100. }
  101. else {
  102. battleship--;
  103. statomasLaivas = "Battleship";
  104. statomoLaivoIlgis = 4;
  105. }
  106. }
  107. else if (laivoPav == "Cruiser") {
  108. if (cruiser == 0) {
  109. cout << "Laivas jau yra pastatytas. Pabandykite dar karta." << endl;
  110. tikrinamaArGalima();
  111. }
  112. else {
  113. cruiser--;
  114. statomasLaivas = "Cruiser";
  115. statomoLaivoIlgis = 3;
  116. }
  117. }
  118. else if (laivoPav == "Submarine") {
  119. if (submarine == 0) {
  120. cout << "Laivas jau yra pastatytas. Pabandykite dar karta." << endl;
  121. tikrinamaArGalima();
  122. }
  123. else {
  124. submarine--;
  125. statomasLaivas = "Submarine";
  126. statomoLaivoIlgis = 3;
  127. }
  128. }
  129. else if (laivoPav == "Destroyer") {
  130. if (destroyer == 0) {
  131. cout << "Laivas jau yra pastatytas. Pabandykite dar karta." << endl;
  132. tikrinamaArGalima();
  133. }
  134. else {
  135. destroyer--;
  136. statomasLaivas = "Destroyer";
  137. statomoLaivoIlgis = 2;
  138. }
  139. }
  140. else {
  141. cout << "Neteisingai ivestas pavadinimas. Pabandykite dar karta." << endl;
  142. tikrinamaArGalima();
  143. }
  144. }
  145. void laivoKryptis() {
  146. cout << "Kokia kryptimi norite pastatyti laiva? ('v' - vertikaliai, 'h' - horizontaliai) ";
  147. cin >> direction;
  148. if (direction != "v" && direction != "h") {
  149. cout << "Kryptis ivesta neteisingai. Pabandykite dar karta." << endl;
  150. laivoKryptis();
  151. }
  152. }
  153. void userBattleCoordinates() {
  154. cout << "Iveskite pradine koordinate (pvz. '1A'): ";
  155. cin >> startingXY;
  156. if (startingXY.size() > 3) {
  157. cout << "Ivyko klaida. Pabandykite dar karta." << endl;
  158. userBattleCoordinates();
  159. }
  160. if (startingXY[0] == '1') {
  161. if (startingXY[1] == '0') {
  162. startingX = 10;
  163. }
  164. else startingX = 1;
  165. }
  166. else if (startingXY[0] == '2') startingX = 2;
  167. else if (startingXY[0] == '3') startingX = 3;
  168. else if (startingXY[0] == '4') startingX = 4;
  169. else if (startingXY[0] == '5') startingX = 5;
  170. else if (startingXY[0] == '6') startingX = 6;
  171. else if (startingXY[0] == '7') startingX = 7;
  172. else if (startingXY[0] == '8') startingX = 8;
  173. else if (startingXY[0] == '9') startingX = 9;
  174. else {
  175. cout << "Ivyko klaida. Pabandykite dar karta." << endl;
  176. userBattleCoordinates();
  177. }
  178. int index;
  179. if (startingX == 10) index = 2;
  180. else index = 1;
  181. if (startingXY[index] == 'A') startingY = 1;
  182. else if (startingXY[index] == 'B') startingY = 2;
  183. else if (startingXY[index] == 'C') startingY = 3;
  184. else if (startingXY[index] == 'D') startingY = 4;
  185. else if (startingXY[index] == 'E') startingY = 5;
  186. else if (startingXY[index] == 'F') startingY = 6;
  187. else if (startingXY[index] == 'G') startingY = 7;
  188. else if (startingXY[index] == 'H') startingY = 8;
  189. else if (startingXY[index] == 'I') startingY = 9;
  190. else if (startingXY[index] == 'J') startingY = 10;
  191. else {
  192. cout << "Ivyko klaida. Pabandykite dar karta." << endl;
  193. userBattleCoordinates();
  194. }
  195.  
  196. // tikrinama ar koordinates kertasi ar ne
  197. if (direction == "h") {
  198. for (int i = 0; i < statomoLaivoIlgis; i++) {
  199. if (zaidejoLenta[startingX][startingY + i] == "[X]" || zaidejoLenta[startingX][startingY + i] == "[.]") {
  200. cout << "Laivo statymas negalimas. Jis kertasi su kitu laivu erdve." << endl;
  201. userBattleCoordinates();
  202. }
  203. }
  204. if (startingY + statomoLaivoIlgis - 1 > 10) {
  205. cout << "Laivo statymas negalimas. Jo ilgis virsija zaidimu lenta." << endl;
  206. userBattleCoordinates();
  207. }
  208. }
  209. else if (direction == "v") {
  210. for (int i = 0; i < statomoLaivoIlgis; i++) {
  211. if (zaidejoLenta[startingX + i][startingY] == "[X]" || zaidejoLenta[startingX + i][startingY] == "[.]") {
  212. cout << "Laivo statymas negalimas. Jis kertasi su kitu laivu erdve." << endl;
  213. userBattleCoordinates();
  214. }
  215. }
  216. if (startingX + statomoLaivoIlgis - 1 > 10) {
  217. cout << "Laivo statymas negalimas. Jo ilgis virsija zaidimu lenta." << endl;
  218. userBattleCoordinates();
  219. }
  220. }
  221. }
  222. void userBattleShipFill() {
  223. k = 0;
  224. if (direction == "h") {
  225. zaidejoLenta[startingX][startingY - 1] = "[.]";
  226. zaidejoLenta[startingX][startingY + statomoLaivoIlgis] = "[.]";
  227. for (int i = 0; i < statomoLaivoIlgis; i++) {
  228. coordX = startingX;
  229. coordY = startingY + i;
  230. zaidejoLenta[startingX][startingY + i] = "[X]";
  231. shipCoordinatesForEach();
  232. }
  233. for (int i = 0; i < statomoLaivoIlgis + 2; i++) {
  234. zaidejoLenta[startingX - 1][startingY - 1 + i] = "[.]";
  235. }
  236. for (int i = 0; i < statomoLaivoIlgis + 2; i++) {
  237. zaidejoLenta[startingX + 1][startingY - 1 + i] = "[.]";
  238. }
  239. }
  240. else if (direction == "v") {
  241. zaidejoLenta[startingX - 1][startingY] = "[.]";
  242. zaidejoLenta[startingX + statomoLaivoIlgis][startingY] = "[.]";
  243. for (int i = 0; i < statomoLaivoIlgis; i++) {
  244. coordX = startingX + i;
  245. coordY = startingY;
  246. zaidejoLenta[startingX + i][startingY] = "[X]";
  247. shipCoordinatesForEach();
  248. }
  249. for (int i = 0; i < statomoLaivoIlgis + 2; i++) {
  250. zaidejoLenta[startingX - 1 + i][startingY - 1] = "[.]";
  251. }
  252. for (int i = 0; i < statomoLaivoIlgis + 2; i++) {
  253. zaidejoLenta[startingX - 1 + i][startingY + 1] = "[.]";
  254. }
  255. }
  256. }
  257. void shipCoordinatesForEach() {
  258. if (statomasLaivas == "Carrier") {
  259. userCarrierX[k] = coordX;
  260. userCarrierY[k] = coordY;
  261. k++;
  262. }
  263. else if (statomasLaivas == "Battleship") {
  264. userBattleshipX[k] = coordX;
  265. userBattleshipY[k] = coordY;
  266. k++;
  267. }
  268. else if (statomasLaivas == "Cruiser") {
  269. userCruiserX[k] = coordX;
  270. userCruiserY[k] = coordY;
  271. k++;
  272. }
  273. else if (statomasLaivas == "Submarine") {
  274. userSubmarineX[k] = coordX;
  275. userSubmarineY[k] = coordY;
  276. k++;
  277. }
  278. else if (statomasLaivas == "Destroyer") {
  279. userDestroyerX[k] = coordX;
  280. userDestroyerY[k] = coordY;
  281. k++;
  282. }
  283. }
  284. void pravalyti() {
  285. for (int i = 0; i <= 10; i++) {
  286. for (int j = 0; j <= 10; j++) {
  287. if (zaidejoLenta[i][j] == "[.]") zaidejoLenta[i][j] = "[ ]";
  288. }
  289. }
  290. }
  291. protected:
  292. string zaidejoLenta[12][12];
  293. int userCarrierX[5], userBattleshipX[4], userCruiserX[3], userSubmarineX[3], userDestroyerX[2];
  294. int userCarrierY[5], userBattleshipY[4], userCruiserY[3], userSubmarineY[3], userDestroyerY[2];
  295. };
  296. class kompiuteris {
  297. private:
  298. int k;
  299. int shipLenght;
  300. string shipName;
  301. int cpuDir, startingX, startingY, coordX, coordY;
  302. public:
  303. kompiuteris() :k(0), shipLenght(0) {}
  304. kompiuteris(int a, int b) :k(a), shipLenght(b) {}
  305. void cpuPradinisUzpildymas() {
  306. for (int i = 1; i <= 10; i++) {
  307. for (int j = 1; j <= 10; j++) {
  308. cpuBoard[i][j] = "[ ]";
  309. }
  310. }
  311. }
  312. void cpuBattleFillMain() {
  313. cpuPradinisUzpildymas();
  314. cpuBattleCarrierFill();
  315. cpuBattleBattleshipFill();
  316. cpuBattleCruiserFill();
  317. cpuBattleSubmarineFill();
  318. cpuBattleDestroyerFill();
  319. pravalyti();
  320. cpuBoardShow();
  321. }
  322. void cpuBattleCarrierFill() {
  323. k = 0;
  324. shipLenght = 5;
  325. shipName = "Carrier";
  326. cpuDir = rand() % 2 + 1;
  327. cpuBattleShipPlacement();
  328. }
  329. void cpuBattleBattleshipFill() {
  330. k = 0;
  331. shipLenght = 4;
  332. shipName = "Battleship";
  333. cpuDir = rand() % 2 + 1;
  334. cpuBattleShipPlacement();
  335. }
  336. void cpuBattleCruiserFill() {
  337. k = 0;
  338. shipLenght = 3;
  339. shipName = "Cruiser";
  340. cpuDir = rand() % 2 + 1;
  341. cpuBattleShipPlacement();
  342. }
  343. void cpuBattleSubmarineFill() {
  344. k = 0;
  345. shipLenght = 3;
  346. shipName = "Submarine";
  347. cpuDir = rand() % 2 + 1;
  348. cpuBattleShipPlacement();
  349. }
  350. void cpuBattleDestroyerFill() {
  351. k = 0;
  352. shipLenght = 2;
  353. shipName = "Destroyer";
  354. cpuDir = rand() % 2 + 1;
  355. cpuBattleShipPlacement();
  356. }
  357. void cpuBattleShipPlacement() {
  358. bool arKertasi = false;
  359. startingX = rand() % 10 + 1;
  360. startingY = rand() % 10 + 1;
  361. if (cpuDir == 1) {
  362. cpuBattleShipCoordinatesHorizontal();
  363. for (int i = 0; i < shipLenght; i++) {
  364. cpuBoard[startingX][startingY + i] = "[X]";
  365. coordX = startingX;
  366. coordY = startingY + i;
  367. cpuShipCoordinatesForEach();
  368. }
  369. cpuBoard[startingX][startingY - 1] = "[.]";
  370. cpuBoard[startingX][startingY + shipLenght] = "[.]";
  371. for (int i = 0; i < shipLenght + 2; i++) {
  372. cpuBoard[startingX - 1][startingY - 1 + i] = "[.]";
  373. }
  374. for (int i = 0; i < shipLenght + 2; i++) {
  375. cpuBoard[startingX + 1][startingY - 1 + i] = "[.]";
  376. }
  377. }
  378. else {
  379. cpuBattleShipCoordinatesVertical();
  380. for (int i = 0; i < shipLenght; i++) {
  381. cpuBoard[startingX + i][startingY] = "[X]";
  382. coordX = startingX + i;
  383. coordY = startingY;
  384. cpuShipCoordinatesForEach();
  385. }
  386. cpuBoard[startingX - 1][startingY] = "[.]";
  387. cpuBoard[startingX + shipLenght][startingY] = "[.]";
  388. for (int i = 0; i < shipLenght + 2; i++) {
  389. cpuBoard[startingX - 1 + i][startingY - 1] = "[.]";
  390. }
  391. for (int i = 0; i < shipLenght + 2; i++) {
  392. cpuBoard[startingX - 1 + i][startingY + 1] = "[.]";
  393. }
  394. }
  395. }
  396. void cpuShipCoordinatesForEach() {
  397. if (shipName == "Carrier") {
  398. cpuCarrierX[k] = coordX;
  399. cpuCarrierY[k] = coordY;
  400. k++;
  401. }
  402. if (shipName == "Battleship") {
  403. cpuBattleshipX[k] = coordX;
  404. cpuBattleshipY[k] = coordY;
  405. k++;
  406. }
  407. if (shipName == "Cruiser") {
  408. cpuCruiserX[k] = coordX;
  409. cpuCruiserY[k] = coordY;
  410. k++;
  411. }
  412. if (shipName == "Submarine") {
  413. cpuSubmarineX[k] = coordX;
  414. cpuSubmarineY[k] = coordY;
  415. k++;
  416. }
  417. if (shipName == "Destroyer") {
  418. cpuDestroyerX[k] = coordX;
  419. cpuDestroyerY[k] = coordY;
  420. k++;
  421. }
  422. }
  423. void cpuBattleShipCoordinatesVertical() {
  424. startingX = rand() % 10 + 1;
  425. startingY = rand() % 10 + 1;
  426. bool arKertasi = false;
  427. for (int i = 0; i < shipLenght; i++) {
  428. if (cpuBoard[startingX + i][startingY] == "[.]") {
  429. arKertasi = true;
  430. }
  431. }
  432. for (int i = 0; i < shipLenght; i++) {
  433. if (cpuBoard[startingX + i][startingY] == "[X]") {
  434. arKertasi = true;
  435. }
  436. }
  437. if (startingX + shipLenght - 1 > 10) arKertasi = true;
  438.  
  439. if (arKertasi == true) cpuBattleShipCoordinatesVertical();
  440. }
  441. void cpuBattleShipCoordinatesHorizontal() {
  442. startingX = rand() % 10 + 1;
  443. startingY = rand() % 10 + 1;
  444. bool arKertasi = false;
  445. for (int i = 0; i < shipLenght; i++) {
  446. if (cpuBoard[startingX][startingY + i] == "[.]") {
  447. arKertasi = true;
  448. }
  449. }
  450. for (int i = 0; i < shipLenght; i++) {
  451. if (cpuBoard[startingX][startingY + i] == "[X]") {
  452. arKertasi = true;
  453. }
  454. }
  455. if (startingY + shipLenght - 1 > 10) arKertasi = true;
  456.  
  457. if (arKertasi == true) cpuBattleShipCoordinatesHorizontal();
  458. }
  459. void pravalyti() {
  460. for (int i = 1; i <= 10; i++) {
  461. for (int j = 1; j <= 10; j++) {
  462. if (cpuBoard[i][j] == "[.]") cpuBoard[i][j] = "[ ]";
  463. }
  464. }
  465. }
  466. void cpuBoardShow() {
  467. cout << " A B C D E F G H I J " << endl;
  468. for (int i = 1; i <= 10; i++) {
  469. cout << fixed << setw(2) << i << " ";
  470. for (int j = 1; j <= 10; j++) {
  471. cout << cpuBoard[i][j];
  472. }
  473. cout << endl;
  474. }
  475. }
  476. protected:
  477. string cpuBoard[12][12];
  478. int cpuCarrierX[5], cpuBattleshipX[4], cpuCruiserX[3], cpuSubmarineX[3], cpuDestroyerX[2];
  479. int cpuCarrierY[5], cpuBattleshipY[4], cpuCruiserY[3], cpuSubmarineY[3], cpuDestroyerY[2];
  480. };
  481. class LaivuMusis : public zaidejas, public kompiuteris {
  482. private:
  483. string startingXY;
  484. int startingX, startingY;
  485. string cpuLentaVisible[12][12];
  486. int zaidejoKoordinates, cpuKoordinates;
  487. int possibleAttackXY[200];
  488. int attackIndex, x_coord, y_coord;
  489. int atakosRezimas;
  490. int reikalingosKoordinatesX[5], reikalingosKoordinatesY[5];
  491. int reikalingasIlgis;
  492. string reikalingaKryptis;
  493. string kryptys[4];
  494. int pradineX, pradineY;
  495. int laivokiekis, laivokiekismetodas;
  496. public:
  497. LaivuMusis() :laivokiekis(0), laivokiekismetodas(0) {}
  498. LaivuMusis(int a, int b) :laivokiekis(a), laivokiekismetodas(b) {}
  499. bool zaidejoIrCpuKoordinates() {
  500. zaidejoKoordinates = 0;
  501. cpuKoordinates = 0;
  502. for (int i = 1; i <= 10; i++) {
  503. for (int j = 1; j <= 10; j++) {
  504. if (zaidejoLenta[i][j] == "[X]") zaidejoKoordinates++;
  505. if (cpuBoard[i][j] == "[X]") cpuKoordinates++;
  506. }
  507. }
  508. if (zaidejoKoordinates > 0 && cpuKoordinates > 0) return true;
  509. else return false;
  510. }
  511. void arZaidejasLaimejo() {
  512. if (zaidejoKoordinates == 0 && cpuKoordinates > 0) {
  513. cout << "Jus pralaimejote." << endl;
  514. system("pause");
  515. }
  516. else if (cpuKoordinates == 0 && zaidejoKoordinates > 0) {
  517. cout << "Jus laimejote. Sveikiname!" << endl;
  518. system("pause");
  519. }
  520. else {
  521. cout << "Kazkaip gavosi kazkas netaip..." << endl;
  522. system("pause");
  523. }
  524. }
  525. void uzpildytiCpuVisible() {
  526. for (int i = 1; i <= 10; i++) {
  527. for (int j = 1; j <= 10; j++) {
  528. cpuLentaVisible[i][j] = "[ ]";
  529. }
  530. }
  531. }
  532. void zaidejoAtakuojamaKoordinate() {
  533. cout << "Iveskite atakuojama koordinate (pvz. '1A'): ";
  534. cin >> startingXY;
  535. if (startingXY[0] == '1') {
  536. if (startingXY[1] == '0') {
  537. startingX = 10;
  538. }
  539. else startingX = 1;
  540. }
  541. else if (startingXY[0] == '2') startingX = 2;
  542. else if (startingXY[0] == '3') startingX = 3;
  543. else if (startingXY[0] == '4') startingX = 4;
  544. else if (startingXY[0] == '5') startingX = 5;
  545. else if (startingXY[0] == '6') startingX = 6;
  546. else if (startingXY[0] == '7') startingX = 7;
  547. else if (startingXY[0] == '8') startingX = 8;
  548. else if (startingXY[0] == '9') startingX = 9;
  549. else {
  550. cout << "Ivyko klaida. Pabandykite dar karta." << endl;
  551. zaidejoAtakuojamaKoordinate();
  552. }
  553. int index;
  554. if (startingX == 10) index = 2;
  555. else index = 1;
  556. if (startingXY[index] == 'A') startingY = 1;
  557. else if (startingXY[index] == 'B') startingY = 2;
  558. else if (startingXY[index] == 'C') startingY = 3;
  559. else if (startingXY[index] == 'D') startingY = 4;
  560. else if (startingXY[index] == 'E') startingY = 5;
  561. else if (startingXY[index] == 'F') startingY = 6;
  562. else if (startingXY[index] == 'G') startingY = 7;
  563. else if (startingXY[index] == 'H') startingY = 8;
  564. else if (startingXY[index] == 'I') startingY = 9;
  565. else if (startingXY[index] == 'J') startingY = 10;
  566. else {
  567. cout << "Ivyko klaida. Pabandykite dar karta." << endl;
  568. zaidejoAtakuojamaKoordinate();
  569. }
  570.  
  571. if (cpuBoard[startingX][startingY] == "[!]") {
  572. cout << "Ivyko klaida. Pabandykite dar karta." << endl;
  573. if (zaidejoIrCpuKoordinates()) zaidejoAtakuojamaKoordinate();
  574. }
  575. else if (cpuBoard[startingX][startingY] == "[X]") {
  576. if (laivokiekismetodas == 0) {
  577. laivokiekis = kuriLaivaAtakuojaZaidejas();
  578. laivokiekismetodas = 1;
  579. }
  580. cpuLentaVisible[startingX][startingY] = "[!]";
  581. cpuBoard[startingX][startingY] = "[!]";
  582. system("cls");
  583. visibleOutput();
  584. cout << "Jus pataikete!" << endl;
  585. if (laivokiekismetodas == 1) {
  586. laivokiekis--;
  587. if (laivokiekis == 0) {
  588. cout << "Laivas nuskandintas!" << endl;
  589. laivokiekismetodas = 0;
  590. }
  591. }
  592. if (zaidejoIrCpuKoordinates()) zaidejoAtakuojamaKoordinate();
  593. }
  594. else {
  595. cpuLentaVisible[startingX][startingY] = "[.]";
  596. system("cls");
  597. visibleOutput();
  598. cout << "Jus nepataikete. Dabar kompiuterio eile." << endl;
  599. }
  600. }
  601.  
  602. int kuriLaivaAtakuojaZaidejas() {
  603. for (int i = 0; i < 5; i++) {
  604. if (startingX == cpuCarrierX[i] && startingY == cpuCarrierY[i]) {
  605. return 5;
  606. }
  607. }
  608. for (int i = 0; i < 4; i++) {
  609. if (startingX == cpuBattleshipX[i] && startingY == cpuBattleshipY[i]) {
  610. return 4;
  611. }
  612. }
  613. for (int i = 0; i < 3; i++) {
  614. if (startingX == cpuCruiserX[i] && startingY == cpuCruiserY[i]) {
  615. return 3;
  616. }
  617. }
  618. for (int i = 0; i < 3; i++) {
  619. if (startingX == cpuSubmarineX[i] && startingY == cpuSubmarineY[i]) {
  620. return 3;
  621. }
  622. }
  623. for (int i = 0; i < 2; i++) {
  624. if (startingX == cpuDestroyerX[i] && startingY == cpuDestroyerY[i]) {
  625. return 2;
  626. }
  627. }
  628. }
  629.  
  630. void visibleOutput() {
  631. cout << " JUSU LENTA:" << endl;
  632. cout << " A B C D E F G H I J " << endl;
  633. for (int i = 1; i <= 10; i++) {
  634. cout << fixed << setw(2) << i << " ";
  635. for (int j = 1; j <= 10; j++) {
  636. cout << zaidejoLenta[i][j];
  637. }
  638. cout << endl;
  639. }
  640. cout << endl;
  641. cout << " KOMPIUTERIO LENTA:" << endl;
  642. cout << " A B C D E F G H I J " << endl;
  643. for (int i = 1; i <= 10; i++) {
  644. cout << fixed << setw(2) << i << " ";
  645. for (int j = 1; j <= 10; j++) {
  646. cout << cpuLentaVisible[i][j];
  647. }
  648. cout << endl;
  649. }
  650. }
  651. void kurisZaidejoLaivas() {
  652. for (int i = 0; i < 5; i++) {
  653. if (userCarrierX[i] == x_coord && userCarrierY[i] == y_coord) {
  654. for (int i = 0; i < 5; i++) {
  655. reikalingosKoordinatesX[i] = userCarrierX[i];
  656. reikalingosKoordinatesY[i] = userCarrierY[i];
  657. reikalingasIlgis = 5;
  658. }
  659. }
  660. }
  661. for (int i = 0; i < 4; i++) {
  662. if (userBattleshipX[i] == x_coord && userBattleshipY[i] == y_coord) {
  663. for (int i = 0; i < 4; i++) {
  664. reikalingosKoordinatesX[i] = userBattleshipX[i];
  665. reikalingosKoordinatesY[i] = userBattleshipY[i];
  666. reikalingasIlgis = 4;
  667. }
  668. break;
  669. }
  670. }
  671. for (int i = 0; i < 3; i++) {
  672. if (userCruiserX[i] == x_coord && userCruiserY[i] == y_coord) {
  673. for (int i = 0; i < 3; i++) {
  674. reikalingosKoordinatesX[i] = userCruiserX[i];
  675. reikalingosKoordinatesY[i] = userCruiserY[i];
  676. reikalingasIlgis = 3;
  677. }
  678. break;
  679. }
  680. }
  681. for (int i = 0; i < 3; i++) {
  682. if (userSubmarineX[i] == x_coord && userSubmarineY[i] == y_coord) {
  683. for (int i = 0; i < 3; i++) {
  684. reikalingosKoordinatesX[i] = userSubmarineX[i];
  685. reikalingosKoordinatesY[i] = userSubmarineY[i];
  686. reikalingasIlgis = 3;
  687. }
  688. break;
  689. }
  690. }
  691. for (int i = 0; i < 2; i++) {
  692. if (userDestroyerX[i] == x_coord && userDestroyerY[i] == y_coord) {
  693. for (int i = 0; i < 2; i++) {
  694. reikalingosKoordinatesX[i] = userDestroyerX[i];
  695. reikalingosKoordinatesY[i] = userDestroyerY[i];
  696. reikalingasIlgis = 2;
  697. }
  698. break;
  699. }
  700. }
  701. }
  702. bool reikalinguKoordinaciuSkaicius() {
  703. int k = 0;
  704. for (int i = 0; i < reikalingasIlgis; i++) {
  705. if (reikalingosKoordinatesX[i] == 0 && reikalingosKoordinatesY[i] == 0) {
  706. k++;
  707. }
  708. }
  709. if (k == reikalingasIlgis) return true;
  710. else return false;
  711. }
  712. void pradinesReikalingosKoordinates() {
  713. for (int i = 0; i < 5; i++) {
  714. reikalingosKoordinatesX[i] = 0;
  715. reikalingosKoordinatesX[i] = 0;
  716. }
  717. }
  718. void galimosAtakos() {
  719. int f = 0;
  720. for (int i = 11; i <= 110; i++) {
  721. possibleAttackXY[f] = i;
  722. f++;
  723. }
  724. }
  725. void randomAttack() {
  726. attackIndex = rand() % 100;
  727. if (possibleAttackXY[attackIndex] != 0) {
  728. if (possibleAttackXY[attackIndex] % 10 == 0) {
  729. x_coord = (possibleAttackXY[attackIndex] / 10) - 1;
  730. y_coord = 10;
  731. }
  732. else {
  733. x_coord = possibleAttackXY[attackIndex] / 10;
  734. y_coord = possibleAttackXY[attackIndex] % 10;
  735. }
  736. possibleAttackXY[attackIndex] = 0;
  737. }
  738. else {
  739. randomAttack();
  740. }
  741. }
  742. void kompiuterioAtaka() {
  743. bool laivoNeraDabar = reikalinguKoordinaciuSkaicius();
  744. if (laivoNeraDabar) atakosRezimas = 0;
  745. if (atakosRezimas == 0) {
  746. nulinisAtakosRezimas();
  747. }
  748. else if (atakosRezimas == 1) {
  749. pirmasAtakosRezimas();
  750. }
  751. else if (atakosRezimas == 2) {
  752. antrasAtakosRezimas();
  753. }
  754.  
  755. }
  756. void nulinisAtakosRezimas() {
  757. randomAttack();
  758. if (zaidejoLenta[x_coord][y_coord] == "[.]" || zaidejoLenta[x_coord][y_coord] == "[!]") nulinisAtakosRezimas();
  759. else {
  760. if (zaidejoLenta[x_coord][y_coord] == "[X]") {
  761. if (atakosRezimas == 0) {
  762. kryptys[0] = "kaire";
  763. kryptys[1] = "desine";
  764. kryptys[2] = "virsus";
  765. kryptys[3] = "apacia";
  766. }
  767. zaidejoLenta[x_coord][y_coord] = "[!]";
  768. Sleep(1000);
  769. system("cls");
  770. visibleOutput();
  771. pradineX = x_coord;
  772. pradineY = y_coord;
  773. kurisZaidejoLaivas();
  774. salinamaKoordinatePataikytaReikalinga();
  775. atakosRezimas = 1;
  776. kompiuterioAtaka();
  777. }
  778. else {
  779. zaidejoLenta[x_coord][y_coord] = "[.]";
  780. }
  781. }
  782. }
  783. void pirmasAtakosRezimas() {
  784. int kryptiesIndex = rand() % 4;
  785. if (kryptys[kryptiesIndex] != "netinka") {
  786. if (kryptys[kryptiesIndex] == "kaire") {
  787. if (zaidejoLenta[x_coord - 1][y_coord] == "[.]" || zaidejoLenta[x_coord - 1][y_coord] == "[!]") {
  788. kryptys[kryptiesIndex] = "netinka";
  789. pirmasAtakosRezimas();
  790. }
  791. if (x_coord - 1 < 1) {
  792. kryptys[kryptiesIndex] = "netinka";
  793. pirmasAtakosRezimas();
  794. }
  795.  
  796. if (zaidejoLenta[x_coord - 1][y_coord] == "[X]") {
  797. reikalingaKryptis = "kaire";
  798. x_coord = x_coord - 1;
  799. zaidejoLenta[x_coord][y_coord] = "[!]";
  800. Sleep(1000);
  801. system("cls");
  802. visibleOutput();
  803. salinamaKoordinatePataikytaReikalinga();
  804. atakosRezimas = 2;
  805. kompiuterioAtaka();
  806. }
  807. else {
  808. atakosRezimas = 1;
  809. kryptys[kryptiesIndex] = "netinka";
  810. zaidejoLenta[x_coord - 1][y_coord] = "[.]";
  811. }
  812. }
  813. else if (kryptys[kryptiesIndex] == "desine") {
  814. if (zaidejoLenta[x_coord + 1][y_coord] == "[.]" || zaidejoLenta[x_coord + 1][y_coord] == "[!]") {
  815. kryptys[kryptiesIndex] = "netinka";
  816. pirmasAtakosRezimas();
  817. }
  818. if (x_coord + 1 > 10) {
  819. kryptys[kryptiesIndex] = "netinka";
  820. pirmasAtakosRezimas();
  821. }
  822.  
  823. if (zaidejoLenta[x_coord + 1][y_coord] == "[X]") {
  824. reikalingaKryptis = "desine";
  825. x_coord = x_coord + 1;
  826. zaidejoLenta[x_coord][y_coord] = "[!]";
  827. system("cls");
  828. visibleOutput();
  829. salinamaKoordinatePataikytaReikalinga();
  830. atakosRezimas = 2;
  831. kompiuterioAtaka();
  832. }
  833. else {
  834. atakosRezimas = 1;
  835. kryptys[kryptiesIndex] = "netinka";
  836. zaidejoLenta[x_coord + 1][y_coord] = "[.]";
  837. }
  838.  
  839. }
  840. else if (kryptys[kryptiesIndex] == "virsus") {
  841. if (zaidejoLenta[x_coord][y_coord - 1] == "[.]" || zaidejoLenta[x_coord][y_coord - 1] == "[!]") {
  842. kryptys[kryptiesIndex] = "netinka";
  843. pirmasAtakosRezimas();
  844. }
  845. if (y_coord - 1 < 1) {
  846. kryptys[kryptiesIndex] = "netinka";
  847. pirmasAtakosRezimas();
  848. }
  849.  
  850. if (zaidejoLenta[x_coord][y_coord - 1] == "[X]") {
  851. reikalingaKryptis = "virsus";
  852. y_coord = y_coord - 1;
  853. zaidejoLenta[x_coord][y_coord] = "[!]";
  854. system("cls");
  855. visibleOutput();
  856. salinamaKoordinatePataikytaReikalinga();
  857. atakosRezimas = 2;
  858. kompiuterioAtaka();
  859. }
  860. else {
  861. atakosRezimas = 1;
  862. kryptys[kryptiesIndex] = "netinka";
  863. zaidejoLenta[x_coord][y_coord - 1] = "[.]";
  864. }
  865.  
  866.  
  867. }
  868. else if (kryptys[kryptiesIndex] == "apacia") {
  869. if (zaidejoLenta[x_coord][y_coord + 1] == "[.]" || zaidejoLenta[x_coord][y_coord + 1] == "[!]") {
  870. kryptys[kryptiesIndex] = "netinka";
  871. pirmasAtakosRezimas();
  872. }
  873. if (y_coord + 1 > 10) {
  874. kryptys[kryptiesIndex] = "netinka";
  875. pirmasAtakosRezimas();
  876. }
  877.  
  878. if (zaidejoLenta[x_coord][y_coord + 1] == "[X]") {
  879. reikalingaKryptis = "apacia";
  880. y_coord = y_coord + 1;
  881. zaidejoLenta[x_coord][y_coord] = "[!]";
  882. system("cls");
  883. visibleOutput();
  884. salinamaKoordinatePataikytaReikalinga();
  885. atakosRezimas = 2;
  886. kompiuterioAtaka();
  887. }
  888. else {
  889. atakosRezimas = 1;
  890. kryptys[kryptiesIndex] = "netinka";
  891. zaidejoLenta[x_coord][y_coord + 1] = "[.]";
  892. }
  893. }
  894. }
  895. else {
  896. pirmasAtakosRezimas();
  897. }
  898. }
  899. void antrasAtakosRezimas() {
  900. bool arJauSunaikintas = reikalinguKoordinaciuSkaicius();
  901. if (arJauSunaikintas) {
  902. atakosRezimas = 0;
  903. kompiuterioAtaka();
  904. }
  905. else {
  906. if (reikalingaKryptis == "kaire") {
  907. if (x_coord - 1 < 1) {
  908. x_coord = pradineX;
  909. y_coord = pradineY;
  910. atakosRezimas = 2;
  911. reikalingaKryptis = "desine";
  912. antrasAtakosRezimas();
  913. }
  914. if (zaidejoLenta[x_coord - 1][y_coord] == "[.]" || zaidejoLenta[x_coord - 1][y_coord] == "[!]") {
  915. x_coord = pradineX;
  916. y_coord = pradineY;
  917. atakosRezimas = 2;
  918. reikalingaKryptis = "desine";
  919. antrasAtakosRezimas();
  920. }
  921. if (zaidejoLenta[x_coord - 1][y_coord] == "[X]") {
  922. x_coord = x_coord - 1;
  923. zaidejoLenta[x_coord][y_coord] = "[!]";
  924. Sleep(1000);
  925. system("cls");
  926. visibleOutput();
  927. salinamaKoordinatePataikytaReikalinga();
  928. atakosRezimas = 2;
  929. antrasAtakosRezimas();
  930. }
  931. else {
  932. zaidejoLenta[x_coord - 1][y_coord] = "[.]";
  933. x_coord = pradineX;
  934. y_coord = pradineY;
  935. atakosRezimas = 2;
  936. reikalingaKryptis = "desine";
  937. }
  938. }
  939. else if (reikalingaKryptis == "desine") {
  940. if (x_coord + 1 > 10) {
  941. x_coord = pradineX;
  942. y_coord = pradineY;
  943. atakosRezimas = 2;
  944. reikalingaKryptis = "kaire";
  945. antrasAtakosRezimas();
  946. }
  947. if (zaidejoLenta[x_coord + 1][y_coord] == "[.]" || zaidejoLenta[x_coord + 1][y_coord] == "[!]") {
  948. x_coord = pradineX;
  949. y_coord = pradineY;
  950. atakosRezimas = 2;
  951. reikalingaKryptis = "kaire";
  952. antrasAtakosRezimas();
  953. }
  954. if (zaidejoLenta[x_coord + 1][y_coord] == "[X]") {
  955. x_coord = x_coord + 1;
  956. zaidejoLenta[x_coord][y_coord] = "[!]";
  957. Sleep(1000);
  958. system("cls");
  959. visibleOutput();
  960. salinamaKoordinatePataikytaReikalinga();
  961. atakosRezimas = 2;
  962. antrasAtakosRezimas();
  963. }
  964. else {
  965. zaidejoLenta[x_coord + 1][y_coord] = "[.]";
  966. x_coord = pradineX;
  967. y_coord = pradineY;
  968. atakosRezimas = 2;
  969. reikalingaKryptis = "kaire";
  970. }
  971. }
  972. else if (reikalingaKryptis == "virsus") {
  973. if (y_coord - 1 < 1) {
  974. x_coord = pradineX;
  975. y_coord = pradineY;
  976. atakosRezimas = 2;
  977. reikalingaKryptis = "apacia";
  978. antrasAtakosRezimas();
  979. }
  980. if (zaidejoLenta[x_coord][y_coord - 1] == "[.]" || zaidejoLenta[x_coord][y_coord - 1] == "[!]") {
  981. x_coord = pradineX;
  982. y_coord = pradineY;
  983. atakosRezimas = 2;
  984. reikalingaKryptis = "apacia";
  985. antrasAtakosRezimas();
  986. }
  987. if (zaidejoLenta[x_coord][y_coord - 1] == "[X]") {
  988. y_coord = y_coord - 1;
  989. zaidejoLenta[x_coord][y_coord] = "[!]";
  990. Sleep(1000);
  991. system("cls");
  992. visibleOutput();
  993. salinamaKoordinatePataikytaReikalinga();
  994. atakosRezimas = 2;
  995. antrasAtakosRezimas();
  996. }
  997. else {
  998. zaidejoLenta[x_coord][y_coord - 1] = "[.]";
  999. x_coord = pradineX;
  1000. y_coord = pradineY;
  1001. atakosRezimas = 2;
  1002. reikalingaKryptis = "apacia";
  1003. }
  1004.  
  1005. }
  1006. else if (reikalingaKryptis == "apacia") {
  1007. if (y_coord + 1 > 10) {
  1008. reikalingaKryptis = "virsus";
  1009. antrasAtakosRezimas();
  1010. }
  1011. if (zaidejoLenta[x_coord][y_coord + 1] == "[.]" || zaidejoLenta[x_coord][y_coord + 1] == "[!]") {
  1012. x_coord = pradineX;
  1013. y_coord = pradineY;
  1014. atakosRezimas = 2;
  1015. reikalingaKryptis = "virsus";
  1016. antrasAtakosRezimas();
  1017. }
  1018. if (zaidejoLenta[x_coord][y_coord + 1] == "[X]") {
  1019. y_coord = y_coord + 1;
  1020. zaidejoLenta[x_coord][y_coord] = "[!]";
  1021. Sleep(1000);
  1022. system("cls");
  1023. visibleOutput();
  1024. salinamaKoordinatePataikytaReikalinga();
  1025. atakosRezimas = 2;
  1026. antrasAtakosRezimas();
  1027. }
  1028. else {
  1029. zaidejoLenta[x_coord][y_coord + 1] = "[.]";
  1030. x_coord = pradineX;
  1031. y_coord = pradineY;
  1032. atakosRezimas = 2;
  1033. reikalingaKryptis = "virsus";
  1034. }
  1035. }
  1036. }
  1037. }
  1038. void salinamaKoordinatePataikytaReikalinga() {
  1039. for (int i = 0; i < reikalingasIlgis; i++) {
  1040. if (reikalingosKoordinatesX[i] == x_coord && reikalingosKoordinatesY[i] == y_coord) {
  1041. reikalingosKoordinatesX[i] = 0;
  1042. reikalingosKoordinatesY[i] = 0;
  1043. break;
  1044. }
  1045. }
  1046. }
  1047. void pilnasZaidimas() {
  1048. laivokiekismetodas = 0;
  1049. bool vykdyti;
  1050. atakosRezimas = 0;
  1051. uzpildytiCpuVisible();
  1052. galimosAtakos();
  1053. pradinesReikalingosKoordinates();
  1054. vykdyti = zaidejoIrCpuKoordinates();
  1055. system("cls");
  1056. visibleOutput();
  1057. while (vykdyti) {
  1058. zaidejoAtakuojamaKoordinate();
  1059. // atakos kryptys
  1060. kompiuterioAtaka();
  1061. Sleep(1000);
  1062. system("cls");
  1063. visibleOutput();
  1064. vykdyti = zaidejoIrCpuKoordinates();
  1065. }
  1066. arZaidejasLaimejo();
  1067. }
  1068. };
  1069.  
  1070.  
  1071.  
  1072. int main()
  1073. {
  1074. srand(time(NULL));
  1075. LaivuMusis zaidimas;
  1076. ++zaidimas;
  1077. zaidimas.cpuBattleFillMain();
  1078. zaidimas.pilnasZaidimas();
  1079. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement