Zanuark

Linux compatible battleship

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