Advertisement
Zanuark

battleship with names

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