Zanuark

another battleship version

Nov 17th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <string>
  4. #include <cmath>
  5. #include <cstdlib>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <vector>
  10. #include <time.h>
  11. using namespace std;
  12.  
  13.  
  14. /*void gotoxy(int x, int y)
  15. {
  16. COORD p = { static_cast<int>(x+1), static_cast<int>(y+1) };
  17. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), p);
  18. }
  19. */
  20.  
  21. void gotoxy(int x,int y)
  22. {
  23. printf("%c[%d;%df",0x1B,y,x);
  24. }
  25.  
  26.  
  27. void resetInitText();
  28. void locationCalc(std::string a, int boats, std::string b, int calc[]);
  29. void printer(int player, int array[], vector<vector <int> >& board, int& tracker, int diff);
  30. void checker(int& player, int array[], vector<vector <int> > board, int playerDiff);
  31. void resetText(int playerDiff);
  32. void hitregplayer(int i, int j, vector<vector <int> > plocation, int& timeshit, int& pHit);
  33. void hitregcomputer(int i, int j, vector<vector <int> > clocation, int& timeshitc, int& cHit, int& alreadyHit, int cAlreadyHit[8][8]);
  34. void computerBrains(int& x, int& y, int playerDiff);
  35. void hitPrinter(int x, int y, int player, int hit, int diff);
  36. void gameLoop(vector<vector<int> > playerOne, vector<vector <int> > playerTwo, int playerDiff, int multiPlayer);
  37.  
  38.  
  39. int main()
  40. {
  41.  
  42.  
  43. cout << "| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |" << endl;
  44. cout << "| **** *** ***** ***** * ***** *** * * ***** **** |" << endl;
  45. cout << "| | ) | | | | | | ( * | | | | ) |" << endl;
  46. cout << "| **** |***| | | | ***** *** |***| | **** |" << endl;
  47. cout << "| | ) | | | | | | * ) | | | | |" << endl;
  48. cout << "| **** * * * * ***** ***** *** * * ***** * |" << endl;
  49. cout << "| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |" << endl;
  50. cout << "| ___ |" << endl;
  51. cout << "| )__) |" << endl;
  52. cout << "| _____ __________L________ |" << endl;
  53. cout << "| ___ =========/ | / | |" << endl;
  54. cout << "| ===/ | / | |---------------------| |" << endl;
  55. cout << "| --------------------------------o o o o---------------- |" << endl;
  56. cout << "| | / |" << endl;
  57. cout << "| |__________________________________________________________/ |" << endl;
  58.  
  59. cout << endl;
  60. sleep(5);
  61. resetInitText();
  62.  
  63.  
  64. int i;
  65. int j;
  66. int diff; //used for board array size. Easy = 6x6, Medium = 8x8, Hard = 10x10//
  67. int multiPlayer;
  68.  
  69. cout << "Enter in 1-Player or 2-Player. (1-Player = 1, 2-Player = 2)";
  70. cin >> multiPlayer;
  71. resetInitText;
  72.  
  73.  
  74. cout << "What difficulty would you like to play on? (Easy = 1, Medium = 2, Hard = 3)" << endl;
  75. cin >> diff;
  76. int shipsSizes[6] = { 2,3,3,4,5,4 };
  77. vector<vector <int> > playerOne;
  78. vector<vector <int> > playerTwo;
  79. if (diff == 1) {
  80. playerOne.resize(6, vector<int>(6, 0));
  81. playerTwo.resize(6, vector<int>(6, 0));
  82.  
  83. }
  84. else if (diff == 2) {
  85. playerOne.resize(8, vector<int>(8, 0));
  86. playerTwo.resize(8, vector<int>(8, 0));
  87.  
  88. }
  89. else if (diff == 3) {
  90. playerOne.resize(10, vector<int>(10, 0));
  91. playerTwo.resize(10, vector<int>(10, 0));
  92.  
  93. }
  94.  
  95. char letters[10] = { 'A','B','C','D','E','F','G','H','I','J' };
  96.  
  97. //initializes the sizes of the boats, the second one is the computer's boats//
  98.  
  99.  
  100.  
  101.  
  102. string temp[2];
  103.  
  104. /*the calc[4] array holds ship placement and is accessed throughout the game
  105. calc[0] = x placement
  106. calc[0] = y placement
  107. calc[0] = direction
  108. calc[0] = size of ship
  109. */
  110. int calc[4];
  111.  
  112. int game = 17;
  113.  
  114. int loopState = 0;
  115. int tracker = 0;
  116.  
  117. resetInitText();
  118.  
  119. //vvv This for loop couts<< the original empty board vvv//
  120. if (diff == 1) {
  121. for (i = 0; i < 6; i++) {
  122.  
  123. for (j = 0; j < 6; j++) {
  124. cout << playerOne[i][j] << " "; //<<< couts player's board
  125. }
  126. cout << (i + 1) << " | " << (i + 1) << " "; //<<< couts spacer and vertical number coords on board
  127. for (j = 0; j < 6; j++) {
  128. cout << playerOne[i][j] << " "; //<<< couts computer's board, but uses same array because its all zeros
  129. }
  130. if (i == 5) {
  131. cout << endl;
  132. }
  133. for (j = 0; j < 6; j++) {
  134. if (i == 5) {
  135. cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
  136. }
  137. if (j == 5 && i == 5) {
  138. cout << " | "; //spacer
  139. }
  140. }
  141.  
  142. for (j = 0; j < 6; j++) {
  143. if (i == 5) {
  144. cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
  145. }
  146. }
  147. cout << endl;
  148. }
  149. }
  150. else if (diff == 2) {
  151. for (i = 0; i < 8; i++) {
  152.  
  153. for (j = 0; j < 8; j++) {
  154. cout << playerOne[i][j] << " "; //<<< couts player's board
  155. }
  156. cout << (i + 1) << " | " << (i + 1) << " "; //<<< couts spacer and vertical number coords on board
  157. for (j = 0; j < 8; j++) {
  158. cout << playerOne[i][j] << " "; //<<< couts computer's board, but uses same array because its all zeros
  159. }
  160. if (i == 7) {
  161. cout << endl;
  162. }
  163. for (j = 0; j < 8; j++) {
  164. if (i == 7) {
  165. cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
  166. }
  167. }
  168. if (i == 7) {
  169. cout << " | "; //spacer
  170. }
  171. for (j = 0; j < 8; j++) {
  172. if (i == 7) {
  173. cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
  174. }
  175. }
  176. cout << endl;
  177. }
  178. }
  179. else if (diff == 3) {
  180. for (i = 0; i < 10; i++) {
  181.  
  182. for (j = 0; j < 10; j++) {
  183. cout << playerOne[i][j] << " "; //<<< couts player's board
  184. }
  185. if (i == 9) {
  186. cout << (i + 1) << " | " << (i + 1) << " ";
  187. }
  188. else {
  189. cout << (i + 1) << " | " << (i + 1) << " "; //<<< couts spacer and vertical number coords on board
  190. }
  191. for (j = 0; j < 10; j++) {
  192. cout << playerOne[i][j] << " "; //<<< couts computer's board, but uses same array because its all zeros
  193. }
  194. if (i == 9) {
  195. cout << endl;
  196. }
  197. for (j = 0; j < 10; j++) {
  198. if (i == 9) {
  199. cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
  200. }
  201. }
  202. if (i == 9) {
  203. cout << " | "; //spacer
  204. }
  205. for (j = 0; j < 10; j++) {
  206. if (i == 9) {
  207. cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
  208. }
  209. }
  210. cout << endl;
  211. }
  212. }
  213. //^^^ This for loop couts<< the original empty board ^^^//
  214.  
  215.  
  216. tracker = 0;
  217.  
  218. //vvv This for loop places down the player's 1 ships vvv//
  219. for (i = 0; i < (diff + 3); i++) {
  220. cout << "Player 1. Where would you like to place a size [" << shipsSizes[i] << "] ship? (Ex. B7)" << endl;
  221. cin >> temp[0]; //inputs coords for boat placement as a string. Example is "B7", or "C5"
  222. cout << "Player 1. What direction would you like to place that ship? (N,S,E,W)" << endl;
  223. cin >> temp[1]; //inputs direction for boat placement as a string. Example is "N", or "E"
  224. locationCalc(temp[0], shipsSizes[i], temp[1], calc); //this function takes the user inputs, and puts them all into the calc[4] array as integer values
  225. checker(loopState, calc, playerOne, diff); //this 374-checker() function works with the calc[4] array to check if the boat placement is "allowed"
  226. if (loopState == -1) { //if the placement isn't "allowed", a loopState of -1 is returned and forces the player to choose another placement
  227. i--;
  228. cout << "ERROR, Player 1 cannot put a ship there";
  229. sleep(3000);
  230. loopState = 0;
  231. }
  232. else { //if loopState doesnt come back as -1, then the printer() function is ran
  233. printer(loopState, calc, playerOne, tracker, diff); //the 274-printer() function uses the calc[4] array to "print" the boats onto the board
  234. } //this is only done if the boat placement checked "valid" (see above)
  235. resetText(diff); //this 355-resetText() function resets the text to it's original position, ready for another loop
  236. }
  237. //^^^ This for loop places down the player's 1 ships ^^^//
  238.  
  239. loopState = 0;
  240.  
  241. if (multiPlayer == 1) {
  242. for (i = 0; i < (diff + 3); i++) {
  243.  
  244. srand(time(0));
  245. srand(time(0) * (rand() % 1000) * (rand() % 513) + (i * 6584));
  246.  
  247. calc[0] = rand() % (4 + (2 * diff)); //randomly picks x-coord to place ship
  248. srand(time(0) * (rand() % 1000) + (i * 4584)); //randomizes seed again
  249. calc[1] = rand() % (4 + (2 * diff)); //randomly picks y-coord to place ship
  250. srand(time(NULL) * rand() % 467 + (i * 5584)); //randomizes seed again a second time
  251. calc[2] = rand() % 4; //randomly picks direction to place ship
  252. calc[3] = shipsSizes[2 - i + diff]; //chooses the next size ship to place
  253. //program places them down in opposite order to increase placement speed, hence the [4-i].
  254.  
  255.  
  256. checker(loopState, calc, playerTwo, diff);//checks that the boat placement
  257. if (loopState == -1) { //same process as above, if the placement comes back invalid, then the computer goes back through the randomizing loop
  258.  
  259. i--; //i-- <<<<this causes the loop to run forever, or until the computer chooses a "valid" placement
  260. loopState = 0;
  261.  
  262. }
  263.  
  264. else if (loopState == 0) {
  265. printer(1, calc, playerTwo, tracker, diff); //"prints" the computer's placement after it has found a "valid" ship placement
  266. sleep(1500);
  267. }
  268.  
  269. }
  270. }
  271. else if (multiPlayer == 2) {
  272. //vvv This for loop places down the player's 2 ships vvv//
  273. for (i = 0; i < (diff + 3); i++) {
  274. cout << "Player 2. Where would you like to place a size [" << shipsSizes[i] << "] ship? (Ex. B7)" << endl;
  275. cin >> temp[0]; //inputs coords for boat placement as a string. Example is "B7", or "C5"
  276. cout << "Player 2. What direction would you like to place that ship? (N,S,E,W)" << endl;
  277. cin >> temp[1]; //inputs direction for boat placement as a string. Example is "N", or "E"
  278. locationCalc(temp[0], shipsSizes[i], temp[1], calc); //this function takes the user inputs, and puts them all into the calc[4] array as integer values
  279. checker(loopState, calc, playerTwo, diff); //this 374-checker() function works with the calc[4] array to check if the boat placement is "allowed"
  280. if (loopState == -1) { //if the placement isn't "allowed", a loopState of -1 is returned and forces the player to choose another placement
  281. i--;
  282. cout << "ERROR, Player 2 cannot put a ship there";
  283. sleep(3000);
  284. loopState = 0;
  285. }
  286. else { //if loopState doesnt come back as -1, then the printer() function is ran
  287. printer(1, calc, playerTwo, tracker, diff); //the 274-printer() function uses the calc[4] array to "print" the boats onto the board
  288. } //this is only done if the boat placement checked "valid" (see above)
  289. resetText(diff); //this 355-resetText() function resets the text to it's original position, ready for another loop
  290. }
  291. //^^^ This for loop places down the player's 2 ships ^^^//
  292. }
  293. resetText(diff);
  294.  
  295.  
  296.  
  297.  
  298. gameLoop(playerOne, playerTwo, diff, multiPlayer); //this enter's the function "gameLoop()". see line 413
  299.  
  300. return 0;
  301. }
  302.  
  303.  
  304.  
  305. void locationCalc(std::string a, int boats, std::string b, int calc[]) {
  306. int x = 0;
  307. int y = 0;
  308. int size = boats;
  309. int direction;
  310. direction = 5;
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317. if (b == "N" || b == "n") {
  318. direction = 0;
  319. }
  320. else if (b == "E" || b == "e") {
  321. direction = 1;
  322. }
  323. else if (b == "S" || b == "s") {
  324. direction = 2;
  325. }
  326. else if (b == "W" || b == "w") {
  327. direction = 3;
  328. }
  329.  
  330. if (a[0] == 'A' || a[0] == 'a') {
  331. x = 0;
  332. }
  333. else if (a[0] == 'B' || a[0] == 'b') {
  334. x = 1;
  335. }
  336. else if (a[0] == 'C' || a[0] == 'c') {
  337. x = 2;
  338. }
  339. else if (a[0] == 'D' || a[0] == 'd') {
  340. x = 3;
  341. }
  342. else if (a[0] == 'E' || a[0] == 'e') {
  343. x = 4;
  344. }
  345. else if (a[0] == 'F' || a[0] == 'f') {
  346. x = 5;
  347. }
  348. else if (a[0] == 'G' || a[0] == 'g') {
  349. x = 6;
  350. }
  351. else if (a[0] == 'H' || a[0] == 'h') {
  352. x = 7;
  353. }
  354. else if (a[0] == 'I' || a[0] == 'i') {
  355. x = 8;
  356. }
  357. else if (a[0] == 'J' || a[0] == 'j') {
  358. x = 9;
  359. }
  360. else if (a[0] == 'K' || a[0] == 'k') {
  361. x = 10;
  362. }
  363. else if (a[0] == 'L' || a[0] == 'l') {
  364. x = 11;
  365. }
  366.  
  367.  
  368.  
  369. if (a[1] == '2') {
  370. y = 1;
  371. }
  372. else if (a[1] == '3') {
  373. y = 2;
  374. }
  375. else if (a[1] == '4') {
  376. y = 3;
  377. }
  378. else if (a[1] == '5') {
  379. y = 4;
  380. }
  381. else if (a[1] == '6') {
  382. y = 5;
  383. }
  384. else if (a[1] == '7') {
  385. y = 6;
  386. }
  387. else if (a[1] == '8') {
  388. y = 7;
  389. }
  390. else if (a[1] == '9') {
  391. y = 8;
  392. }
  393. else if (a[1] == '1' && a[2] == '0') {
  394. y = 9;
  395. }
  396. else if (a[1] == '1' && a[2] == '1') {
  397. y = 10;
  398. }
  399. else if (a[1] == '1' && a[2] == '2') {
  400. y = 11;
  401. }
  402. else if (a[1] == '1') {
  403. y = 0;
  404. }
  405.  
  406. calc[0] = x;
  407. calc[1] = y;
  408. calc[2] = direction;
  409. calc[3] = size;
  410.  
  411. }
  412. void printer(int player, int array[], vector<vector <int> >& board, int& tracker, int diff) {
  413. int x;
  414. int y;
  415. int i;
  416.  
  417. char box = (char)219;
  418.  
  419. char c;
  420. c = box;
  421. std::string ship(2, c);
  422.  
  423. ship.push_back(c);
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432. if (player == 0 || player == 1) {
  433.  
  434. if (array[2] == 0) {//this places ships if they chose the north direction
  435. for (i = 0; i < array[3]; i++) {//runs a loop that places down all parts of the ship
  436. x = (array[0] * 2) + (18 * player) + player * (diff * 4);
  437. y = (array[1]) - (i); //shifts the placement upwards in the y-direction
  438. gotoxy(x+1, y+1);
  439. cout << 1;
  440. board[array[1] - i][array[0]] = 1;
  441. }
  442. }
  443. else if (array[2] == 1) {//this places ships if they chose the east direction
  444. for (i = 0; i < array[3]; i++) {
  445. x = (array[0] * 2) + (i * 2) + (18 * player) + player * (diff * 4); //shifts the placement to the right in the x-direction
  446. y = (array[1]);
  447. gotoxy(x+1, y+1);
  448. cout << 1;
  449. board[array[1]][array[0] + i] = 1;
  450. }
  451. }
  452. else if (array[2] == 2) {//this places ships if they chose the south direction
  453. for (i = 0; i < array[3]; i++) {
  454. x = (array[0] * 2) + (18 * player) + player * (diff * 4);
  455. y = (array[1]) + (i); //shifts the placement to the down in the y-direction
  456. gotoxy(x+1, y+1);
  457. cout << 1;
  458. board[array[1] + i][array[0]] = 1;
  459. }
  460. }
  461. else if (array[2] == 3) {//this places ships if they chose the west direction
  462. for (i = 0; i < array[3]; i++) {
  463. x = (array[0] * 2) - (i * 2) + (18 * player) + player * (diff * 4); //shifts the placement to the left in the x-direction
  464. y = (array[1]);
  465. gotoxy(x+1, y+1);
  466. cout << 1;
  467. board[array[1]][array[0] - i] = 1;
  468. }
  469. }
  470. }
  471.  
  472. }
  473. void resetText(int playerDiff) {
  474. int i;
  475. if (playerDiff == 1) {
  476. for (i = 7; i < 15; i++) {
  477. gotoxy(0+1, i+1);
  478. cout << " ";
  479. }
  480. gotoxy(0+1, 7+1);
  481. }
  482. else if (playerDiff == 2) {
  483. for (i = 9; i < 15; i++) {
  484. gotoxy(0+1, i+1);
  485. cout << " ";
  486. }
  487. gotoxy(0+1, 9+1);
  488. }
  489. else if (playerDiff == 3) {
  490. for (i = 11; i < 15; i++) {
  491. gotoxy(0+1, i+1);
  492. cout << " ";
  493. }
  494. gotoxy(0+1, 11+1);
  495. }
  496. }
  497. void resetInitText() {
  498. int i;
  499. gotoxy(0+1, 0+1);
  500. for (i = 0; i < 20; i++) {
  501. gotoxy(0+1, i+1);
  502. cout << " ";
  503. }
  504. gotoxy(0+1, 0+1);
  505.  
  506. }
  507. void checker(int& player, int array[], vector<vector <int> > board, int playerDiff) {
  508.  
  509. int i;
  510.  
  511.  
  512.  
  513. if (array[2] == 0) {
  514. for (i = 0; i < array[3]; i++) {
  515.  
  516. if (array[1] - i < 0) {
  517.  
  518. player = -1;
  519. }
  520. else if (board[array[1] - i][array[0]] != 0) {
  521. player = -1;
  522. }
  523. }
  524. }
  525. else if (array[2] == 1) {
  526. for (i = 0; i < array[3]; i++) {
  527. if (array[0] + i > 3 + (playerDiff * 2)) {
  528.  
  529. player = -1;
  530. }
  531. else if (board[array[1]][array[0] + i] != 0) {
  532. player = -1;
  533. }
  534. }
  535. }
  536. else if (array[2] == 2) {
  537. for (i = 0; i < array[3]; i++) {
  538. if (array[1] + i > 3 + (playerDiff *2)) {
  539.  
  540. player = -1;
  541. }
  542. else if (board[array[1] + i][array[0]] != 0) {
  543. player = -1;
  544. }
  545. }
  546. }
  547. else if (array[2] == 3) {
  548. for (i = 0; i < array[3]; i++) {
  549. if (array[0] - i < 0) {
  550.  
  551. player = -1;
  552. }
  553. else if (board[array[1]][array[0] - i] != 0) {
  554. player = -1;
  555. }
  556. }
  557. }
  558. }
  559.  
  560.  
  561.  
  562. void gameLoop(vector<vector<int> > playerOne, vector<vector <int> > playerTwo, int playerDiff, int multiPlayer) {
  563. string attackCoords;
  564. int coords[4];
  565. int pHitTimes = 0;
  566. int pHit;
  567. int cHitTimes = 0;
  568. int cHit;
  569. int x, y;
  570. bool win = false;
  571. int alreadyHit;
  572. int cAlreadyHit[8][8];
  573. //the code below runs the loop which is the "game". where both players take turns inputting coords.
  574. //the program inputs the user coords, but the computer randomly chooses coords using the 452-computerBrains() function
  575. //the program uses the 460-hitregplayer() and the 483-hitregcomputer() functions to see if it was a hit/miss
  576. //then it uses the 503-hitPrinter() function to display to the board
  577. int goal = 0;
  578.  
  579. if (playerDiff == 1) {
  580. goal = 12;
  581. }
  582. else if (playerDiff == 2) {
  583. goal = 17;
  584. }
  585. else if (playerDiff == 3) {
  586. goal = 21;
  587. }
  588.  
  589.  
  590. do {
  591. cout << "Player 1. Enter in the coords you wish to attack. (Ex. B7): ";
  592. cin >> attackCoords;
  593.  
  594. locationCalc(attackCoords, 0, "W", coords);
  595. hitregplayer(coords[1], coords[0], playerTwo, pHitTimes, pHit);
  596. hitPrinter(coords[0], coords[1], 1, pHit, playerDiff);
  597. sleep(200);
  598. resetText(playerDiff);
  599. alreadyHit = 1;
  600. if (multiPlayer == 1) {
  601. do {
  602. computerBrains(x, y, playerDiff);
  603. hitregcomputer(y, x, playerOne, cHitTimes, cHit, alreadyHit, cAlreadyHit);
  604. } while (alreadyHit == 1);
  605. hitPrinter(x, y, 0, cHit, playerDiff);
  606. }
  607. else if (multiPlayer == 2) {
  608. cout << "Player 2. Enter in the coords you wish to attack. (Ex. B7): ";
  609. cin >> attackCoords;
  610.  
  611. locationCalc(attackCoords, 0, "W", coords);
  612. hitregcomputer(coords[1], coords[0], playerOne, cHitTimes, cHit, alreadyHit, cAlreadyHit);
  613. hitPrinter(coords[0], coords[1], 0, cHit, playerDiff);
  614. }
  615.  
  616. sleep(200);
  617.  
  618. if (cHitTimes == goal || pHitTimes == goal) {
  619. win = true;
  620. }
  621.  
  622. resetText(playerDiff);
  623. } while (win == false);
  624.  
  625. if (pHitTimes == goal) {
  626. cout << "Player 1 won!";
  627. }
  628. else {
  629. cout << "Player 2 won!";
  630. }
  631. sleep(30000000);
  632. }
  633.  
  634. void computerBrains(int& x, int& y, int playerDiff) {
  635. if (playerDiff == 1) {
  636. srand(time(0));
  637. x = rand() % 6;
  638. srand(time(0) * rand() % 1000);
  639. y = rand() % 6;
  640. }
  641. else if (playerDiff == 2) {
  642. srand(time(0));
  643. x = rand() % 8;
  644. srand(time(0) * rand() % 1000);
  645. y = rand() % 8;
  646. }
  647. else if (playerDiff == 3) {
  648. srand(time(0));
  649. x = rand() % 10;
  650. srand(time(0) * rand() % 1000);
  651. y = rand() % 10;
  652. }
  653.  
  654. }
  655.  
  656. void hitregplayer(int i, int j, vector<vector <int> > plocation, int& timeshit, int& pHit) { /* where location refers to where the player aims, and computer location*/
  657. int u;
  658. int v;
  659.  
  660.  
  661. if (plocation[i][j] == 1) {//clocation refers == true where ship coordinate is
  662. cout << "Hit" << endl;
  663. //alreadyHit[i][j] = 1; //sets alreadyHit as true
  664. //location[i][j] = 0; //resets player target
  665. timeshit++;
  666. pHit = 1;
  667. cout << endl;
  668. cout << endl;
  669.  
  670.  
  671. }
  672. else {
  673. cout << "Miss" << endl;
  674. pHit = 0;
  675.  
  676. }
  677.  
  678. }
  679.  
  680. void hitregcomputer(int i, int j, vector<vector <int> > clocation, int& timeshitc, int& cHit, int& alreadyHit, int cAlreadyHit[8][8]) { /* where colocation refers to where the computer aims, and player location*/
  681.  
  682. if (clocation[i][j] == 1 && cAlreadyHit[i][j] != 1) {//plocation refers == true where ship coordinate is
  683. cout << "Computer attacked at (" << j << ", " << i << ") and hit!." << endl;
  684. //calreadyHit[i][j] = 1; //sets alreadyHit as true
  685. //colocation[i][j] = 0; //resets player target
  686. timeshitc++;
  687. cHit = 1;
  688. alreadyHit = 0;
  689. cAlreadyHit[i][j] = 1;
  690. }
  691. else if (cAlreadyHit[i][j] != 1) {
  692. cout << "Computer attacked at (" << j + 1 << ", " << i + 1 << ") and missed!." << endl;
  693. cHit = 0;
  694. alreadyHit = 0;
  695. cAlreadyHit[i][j] = 1;
  696. }
  697.  
  698. }
  699.  
  700. void hitPrinter(int x, int y, int player, int hit, int diff) {
  701. gotoxy((x * 2 + (player * 18) + player * (diff * 4))+1, y+1);
  702. if (hit == 1) {
  703. cout << "X";
  704. }
  705. else {
  706. cout << "~";
  707. }
  708. gotoxy(0+1, 9+1);
  709. }
Advertisement
Add Comment
Please, Sign In to add comment