Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.02 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, int pAlreadyHit[8][8]);
  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. void initBoard(int diff);
  37.  
  38.  
  39.  
  40. int main() {
  41. bool gamePlay;
  42. do{
  43. resetInitText();
  44.  
  45. cout << "| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |" << endl;
  46. cout << "| **** *** ***** ***** * ***** *** * * ***** **** |" << endl;
  47. cout << "| | ) | | | | | | ( * | | | | ) |" << endl;
  48. cout << "| **** |***| | | | ***** *** |***| | **** |" << endl;
  49. cout << "| | ) | | | | | | * ) | | | | |" << endl;
  50. cout << "| **** * * * * ***** ***** *** * * ***** * |" << endl;
  51. cout << "| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |" << endl;
  52. cout << "| ___ |" << endl;
  53. cout << "| )__) |" << endl;
  54. cout << "| _____ __________L________ |" << endl;
  55. cout << "| ___ =========/ | / | |" << endl;
  56. cout << "| ===/ | / | |---------------------| |" << endl;
  57. cout << "| --------------------------------o o o o---------------- |" << endl;
  58. cout << "| | / |" << endl;
  59.  
  60.  
  61.  
  62. gotoxy(0, 15);
  63.  
  64. cout << "| |_________________/^\\__/^\\__/^\\_____________________/\\/\\___/ |" << endl;
  65. cout << "| /^\\ /^\\ /\\ /\\/\\ /^\\ |" << endl;
  66. gotoxy(25, 9);
  67. cout << "##";
  68. sleep(1);
  69.  
  70. gotoxy(0, 15);
  71.  
  72. cout << "| |_______________/\\__/^\\__/^\\_____________________/^\\/\\_____/ |" << endl;
  73. cout << "| /\\ /\\ /\\ /^\\/^\\ /^\\ |" << endl;
  74. sleep(1);
  75. gotoxy(24, 8);
  76. cout << "##";
  77. gotoxy(0, 15);
  78.  
  79. cout << "| |_____________/\\__/\\__/\\________/\\_/\\_______/^\\/^\\_________/ |" << endl;
  80. cout << "| /\\ /\\ / \\/ \\ /^\\ /\\ " << endl;
  81. sleep(1);
  82. gotoxy(23, 8);
  83. cout << "###";
  84. gotoxy(0, 15);
  85.  
  86. cout << "| |___________/\\__/\\__/\\_____________________/^\\/^\\__________/ |" << endl;
  87. cout << "| /\\ /^\\/^\\ /^\\ /^\\ " << endl;
  88. sleep(1);
  89. gotoxy(20, 9);
  90. cout << "#####";
  91. gotoxy(0, 15);
  92.  
  93. cout << "| |_________________/\\_____________________/^\\/^\\_________/\\_/ |" << endl;
  94. cout << "| /\\/\\ /^\\ / \\ " << endl;
  95. sleep(1);
  96. gotoxy(17, 8);
  97. cout << "#######";
  98. gotoxy(0, 15);
  99.  
  100. cout << "| |_________________/^\\__/^\\__/^\\_____________________/\\/\\___/ |" << endl;
  101. cout << "| /^\\ /^\\ / \\ /\\/\\ /^\\ |" << endl;
  102. sleep(1);
  103. gotoxy(14, 7);
  104. cout << "###########";
  105. gotoxy(17, 9);
  106. cout << " ";
  107.  
  108. gotoxy(0, 15);
  109.  
  110. cout << "| |_______________/\\__/^\\__/^\\_____________________/^\\/\\_____/ |" << endl;
  111. cout << "| /\\ /\\ /\\ /^\\/^\\ /^\\ |" << endl;
  112. sleep(1);
  113. gotoxy(10, 8);
  114. cout << "#############";
  115. gotoxy(0, 15);
  116.  
  117. cout << "| |_____________/\\__/\\__/\\________/\\_/\\_______/^\\/^\\_________/ |" << endl;
  118. cout << "| /\\ /\\ / \\/ \\ /^\\ /\\ " << endl;
  119. sleep(1);
  120. gotoxy(5, 9);
  121. cout << "################";
  122. gotoxy(19, 7);
  123. cout << "~~~~~~~~~~~~~~~~~";
  124. gotoxy(0, 15);
  125.  
  126. cout << "| |___________/\\__/\\__/\\_____________________/^\\/^\\__________/ |" << endl;
  127. cout << "| /\\ /^\\/^\\ /^\\ /^\\ " << endl;
  128. sleep(1);
  129. gotoxy(2, 8);
  130. cout << "################";
  131. gotoxy(0, 15);
  132.  
  133. cout << "| |_________________/\\_____________________/^\\/^\\_________/\\_/ |" << endl;
  134. cout << "| /\\/\\ /\\/\\ /^\\ / \\ " << endl;
  135. sleep(1);
  136.  
  137. resetInitText();
  138.  
  139.  
  140. int i;
  141. int j;
  142. int diff; //used for board array size. Easy = 6x6, Medium = 8x8, Hard = 10x10//
  143. int multiPlayer;
  144.  
  145. cout << "Enter in 1-Player or 2-Player. (1-Player = 1, 2-Player = 2)" << endl;
  146. cin >> multiPlayer;
  147. resetInitText;
  148.  
  149. string user;
  150. string user2 = "Computer";
  151. cout << "Player 1, please enter name." << endl;
  152. cin.ignore();
  153. getline(cin,user);
  154. cout << "Your name is: " << user << endl;
  155. resetInitText;
  156.  
  157. if (multiPlayer == 2) {
  158. string user;
  159. cout << "Player 2, please enter name." << endl;
  160. getline(cin,user2);
  161. cout << "Your name is: " << user2 << endl;
  162. resetInitText;
  163. }
  164.  
  165. cout << "What difficulty would you like to play on? (Easy = 1, Medium = 2, Hard = 3)" << endl;
  166. cout << " (6-by-6, 8-by-8, 10-by-10)" << endl;
  167.  
  168. cin >> diff;
  169. cout << endl;
  170. int shipsSizes[6] = { 2,3,3,4,5,4 };
  171. vector<vector <int> > playerOne;
  172. vector<vector <int> > playerTwo;
  173. if (diff == 1) {
  174. playerOne.resize(6, vector<int>(6, 0));
  175. playerTwo.resize(6, vector<int>(6, 0));
  176.  
  177. }
  178. else if (diff == 2) {
  179. playerOne.resize(8, vector<int>(8, 0));
  180. playerTwo.resize(8, vector<int>(8, 0));
  181.  
  182. }
  183. else if (diff == 3) {
  184. playerOne.resize(10, vector<int>(10, 0));
  185. playerTwo.resize(10, vector<int>(10, 0));
  186.  
  187. }
  188.  
  189. char letters[10] = { 'A','B','C','D','E','F','G','H','I','J' };
  190.  
  191. //initializes the sizes of the boats, the second one is the computer's boats//
  192.  
  193.  
  194.  
  195.  
  196. string temp[2];
  197.  
  198. /*the calc[4] array holds ship placement and is accessed throughout the game
  199. calc[0] = x placement
  200. calc[0] = y placement
  201. calc[0] = direction
  202. calc[0] = size of ship
  203. */
  204. int calc[4];
  205.  
  206. int game = 17;
  207.  
  208. int loopState = 0;
  209. int tracker = 0;
  210.  
  211. resetInitText();
  212.  
  213. //vvv This for loop couts<< the original empty board vvv//
  214. initBoard(diff);
  215. //^^^ This for loop couts<< the original empty board ^^^//
  216.  
  217.  
  218. tracker = 0;
  219.  
  220. //vvv This for loop places down the player's 1 ships vvv//
  221. for (i = 0; i < (diff + 3); i++) {
  222. cout << user << ", where would you like to place a size [" << shipsSizes[i] << "] ship? (Ex. B7)" << endl;
  223. cin >> temp[0]; //inputs coords for boat placement as a string. Example is "B7", or "C5"
  224. cout << user << ", what direction would you like to place that ship? (N,S,E,W)" << endl;
  225. cin >> temp[1]; //inputs direction for boat placement as a string. Example is "N", or "E"
  226. 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
  227. checker(loopState, calc, playerOne, diff); //this 374-checker() function works with the calc[4] array to check if the boat placement is "allowed"
  228. if (loopState == -1) { //if the placement isn't "allowed", a loopState of -1 is returned and forces the player to choose another placement
  229. i--;
  230. gotoxy(5, 15 + diff*2);
  231.  
  232. cout << endl << "ERROR, cannot put a ship there" << endl;
  233. sleep(3);
  234. loopState = 0;
  235. }
  236. else { //if loopState doesnt come back as -1, then the printer() function is ran
  237. printer(loopState, calc, playerOne, tracker, diff); //the 274-printer() function uses the calc[4] array to "print" the boats onto the board
  238. } //this is only done if the boat placement checked "valid" (see above)
  239. resetText(diff); //this 355-resetText() function resets the text to it's original position, ready for another loop
  240. }
  241. //^^^ This for loop places down the player's 1 ships ^^^//
  242.  
  243. loopState = 0;
  244.  
  245. if (multiPlayer == 1) {
  246. for (i = 0; i < (diff + 3); i++) {
  247.  
  248. srand(time(0));
  249. srand(time(0) * (rand() % 1000) * (rand() % 513) + (i * 6584));
  250.  
  251. calc[0] = rand() % (4 + (2 * diff)); //randomly picks x-coord to place ship
  252. srand(time(0) * (rand() % 1000) + (i * 4584)); //randomizes seed again
  253. calc[1] = rand() % (4 + (2 * diff)); //randomly picks y-coord to place ship
  254. srand(time(NULL) * rand() % 467 + (i * 5584)); //randomizes seed again a second time
  255. calc[2] = rand() % 4; //randomly picks direction to place ship
  256. calc[3] = shipsSizes[2 - i + diff]; //chooses the next size ship to place
  257. //program places them down in opposite order to increase placement speed, hence the [4-i].
  258.  
  259. gotoxy(26,15);
  260. cout << "Placing computer ships.";
  261. gotoxy(26,15);
  262. gotoxy(36,15);
  263. gotoxy(26,15);
  264. cout << "Placing computer ships..";
  265. gotoxy(26,15);
  266. gotoxy(36,15);
  267. gotoxy(26,15);
  268. cout << "Placing computer ships...";
  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.  
  279. printer(1, calc, playerTwo, tracker, diff); //"prints" the computer's placement after it has found a "valid" ship placement
  280.  
  281. }
  282.  
  283. }
  284. }
  285.  
  286.  
  287. else if (multiPlayer == 2) {
  288. resetInitText();
  289. initBoard(diff);
  290. //vvv This for loop places down the player's 2 ships vvv//
  291. for (i = 0; i < (diff + 3); i++) {
  292. cout << user2 << " where would you like to place a size [" << shipsSizes[i] << "] ship? (Ex. B7)" << endl;
  293. cin >> temp[0]; //inputs coords for boat placement as a string. Example is "B7", or "C5"
  294. cout << user2 << " what direction would you like to place that ship? (N,S,E,W)" << endl;
  295. cin >> temp[1]; //inputs direction for boat placement as a string. Example is "N", or "E"
  296. 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
  297. checker(loopState, calc, playerTwo, diff); //this 374-checker() function works with the calc[4] array to check if the boat placement is "allowed"
  298. if (loopState == -1) { //if the placement isn't "allowed", a loopState of -1 is returned and forces the player to choose another placement
  299. i--;
  300. gotoxy(5, 15 + diff*2);
  301. cout << endl << "ERROR, cannot put a ship there" << endl;
  302. sleep(3);
  303. loopState = 0;
  304. }
  305. else {
  306.  
  307.  
  308.  
  309. //if loopState doesnt come back as -1, then the printer() function is ran
  310. printer(2, calc, playerTwo, tracker, diff); //the 274-printer() function uses the calc[4] array to "print" the boats onto the board
  311. } //this is only done if the boat placement checked "valid" (see above)
  312. resetText(diff); //this 355-resetText() function resets the text to it's original position, ready for another loop
  313. }
  314. //^^^ This for loop places down the player's 2 ships ^^^//
  315. resetInitText();
  316. initBoard(diff);
  317. }
  318. resetText(diff);
  319.  
  320.  
  321.  
  322.  
  323. gameLoop(playerOne, playerTwo, diff, multiPlayer, user, user2); //this enter's the function "gameLoop()". see line 413
  324.  
  325.  
  326.  
  327.  
  328.  
  329. sleep(3);
  330. cout << endl;
  331. cout << endl;
  332. cout << "Would you like to play again? (Y/N)" << endl;
  333. string responseG = "y";
  334. cin >> responseG;
  335. if(responseG == "Y" || responseG == "y"){
  336. gamePlay = true;
  337.  
  338. }
  339.  
  340. else {
  341.  
  342. gamePlay = false;
  343. }
  344. }while(gamePlay == true);
  345. return 0;
  346. }
  347.  
  348.  
  349.  
  350. void locationCalc(std::string a, int boats, std::string b, int calc[]) {
  351. int x = 0;
  352. int y = 0;
  353. int size = boats;
  354. int direction;
  355. direction = 5;
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362. if (b == "N" || b == "n") {
  363. direction = 0;
  364. }
  365. else if (b == "E" || b == "e") {
  366. direction = 1;
  367. }
  368. else if (b == "S" || b == "s") {
  369. direction = 2;
  370. }
  371. else if (b == "W" || b == "w") {
  372. direction = 3;
  373. }
  374.  
  375. if (a[0] == 'A' || a[0] == 'a') {
  376. x = 0;
  377. }
  378. else if (a[0] == 'B' || a[0] == 'b') {
  379. x = 1;
  380. }
  381. else if (a[0] == 'C' || a[0] == 'c') {
  382. x = 2;
  383. }
  384. else if (a[0] == 'D' || a[0] == 'd') {
  385. x = 3;
  386. }
  387. else if (a[0] == 'E' || a[0] == 'e') {
  388. x = 4;
  389. }
  390. else if (a[0] == 'F' || a[0] == 'f') {
  391. x = 5;
  392. }
  393. else if (a[0] == 'G' || a[0] == 'g') {
  394. x = 6;
  395. }
  396. else if (a[0] == 'H' || a[0] == 'h') {
  397. x = 7;
  398. }
  399. else if (a[0] == 'I' || a[0] == 'i') {
  400. x = 8;
  401. }
  402. else if (a[0] == 'J' || a[0] == 'j') {
  403. x = 9;
  404. }
  405. else if (a[0] == 'K' || a[0] == 'k') {
  406. x = 10;
  407. }
  408. else if (a[0] == 'L' || a[0] == 'l') {
  409. x = 11;
  410. }
  411.  
  412.  
  413.  
  414. if (a[1] == '2') {
  415. y = 1;
  416. }
  417. else if (a[1] == '3') {
  418. y = 2;
  419. }
  420. else if (a[1] == '4') {
  421. y = 3;
  422. }
  423. else if (a[1] == '5') {
  424. y = 4;
  425. }
  426. else if (a[1] == '6') {
  427. y = 5;
  428. }
  429. else if (a[1] == '7') {
  430. y = 6;
  431. }
  432. else if (a[1] == '8') {
  433. y = 7;
  434. }
  435. else if (a[1] == '9') {
  436. y = 8;
  437. }
  438. else if (a[1] == '1' && a[2] == '0') {
  439. y = 9;
  440. }
  441. else if (a[1] == '1' && a[2] == '1') {
  442. y = 10;
  443. }
  444. else if (a[1] == '1' && a[2] == '2') {
  445. y = 11;
  446. }
  447. else if (a[1] == '1') {
  448. y = 0;
  449. }
  450.  
  451. calc[0] = x;
  452. calc[1] = y;
  453. calc[2] = direction;
  454. calc[3] = size;
  455.  
  456. }
  457. void printer(int player, int array[], vector<vector <int> >& board, int& tracker, int diff) {
  458. int x;
  459. int y;
  460. int i;
  461.  
  462. char box = (char)219;
  463.  
  464. char c;
  465. c = box;
  466. std::string ship(2, c);
  467.  
  468. ship.push_back(c);
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477. if (player == 0) {
  478.  
  479. if (array[2] == 0) {//this places ships if they chose the north direction
  480. for (i = 0; i < array[3]; i++) {//runs a loop that places down all parts of the ship
  481. x = (array[0] * 2) + (18 * player) + player * (diff * 4);
  482. y = (array[1]) - (i); //shifts the placement upwards in the y-direction
  483. gotoxy(x+1, y+1);
  484. cout << "?";
  485. board[array[1] - i][array[0]] = 1;
  486. }
  487. }
  488. else if (array[2] == 1) {//this places ships if they chose the east direction
  489. for (i = 0; i < array[3]; i++) {
  490. x = (array[0] * 2) + (i * 2) + (18 * player) + player * (diff * 4); //shifts the placement to the right in the x-direction
  491. y = (array[1]);
  492. gotoxy(x+1, y+1);
  493. cout << "?";
  494. board[array[1]][array[0] + i] = 1;
  495. }
  496. }
  497. else if (array[2] == 2) {//this places ships if they chose the south direction
  498. for (i = 0; i < array[3]; i++) {
  499. x = (array[0] * 2) + (18 * player) + player * (diff * 4);
  500. y = (array[1]) + (i); //shifts the placement to the down in the y-direction
  501. gotoxy(x+1, y+1);
  502. cout << "?";
  503. board[array[1] + i][array[0]] = 1;
  504. }
  505. }
  506. else if (array[2] == 3) {//this places ships if they chose the west direction
  507. for (i = 0; i < array[3]; i++) {
  508. x = (array[0] * 2) - (i * 2) + (18 * player) + player * (diff * 4); //shifts the placement to the left in the x-direction
  509. y = (array[1]);
  510. gotoxy(x+1, y+1);
  511. cout << "?";
  512. board[array[1]][array[0] - i] = 1;
  513. }
  514. }
  515. }
  516. else if (player == 1) {
  517. if (array[2] == 0) {//this places ships if they chose the north direction
  518. for (i = 0; i < array[3]; i++) {//runs a loop that places down all parts of the ship
  519. x = (array[0] * 2) + (18 * player) + player * (diff * 4);
  520. y = (array[1]) - (i); //shifts the placement upwards in the y-direction
  521. gotoxy(x+1, y+1);
  522. board[array[1] - i][array[0]] = 1;
  523. }
  524. }
  525. else if (array[2] == 1) {//this places ships if they chose the east direction
  526. for (i = 0; i < array[3]; i++) {
  527. x = (array[0] * 2) + (i * 2) + (18 * player) + player * (diff * 4); //shifts the placement to the right in the x-direction
  528. y = (array[1]);
  529. gotoxy(x+1, y+1);
  530. board[array[1]][array[0] + i] = 1;
  531. }
  532. }
  533. else if (array[2] == 2) {//this places ships if they chose the south direction
  534. for (i = 0; i < array[3]; i++) {
  535. x = (array[0] * 2) + (18 * player) + player * (diff * 4);
  536. y = (array[1]) + (i); //shifts the placement to the down in the y-direction
  537. gotoxy(x+1, y+1);
  538. board[array[1] + i][array[0]] = 1;
  539. }
  540. }
  541. else if (array[2] == 3) {//this places ships if they chose the west direction
  542. for (i = 0; i < array[3]; i++) {
  543. x = (array[0] * 2) - (i * 2) + (18 * player) + player * (diff * 4); //shifts the placement to the left in the x-direction
  544. y = (array[1]);
  545. gotoxy(x+1, y+1);
  546. board[array[1]][array[0] - i] = 1;
  547. }
  548. }
  549. }
  550. else if (player == 2) {
  551. if (array[2] == 0) {//this places ships if they chose the north direction
  552. for (i = 0; i < array[3]; i++) {//runs a loop that places down all parts of the ship
  553. x = (array[0] * 2) + (18 * (player - 1)) + (player - 1) * (diff * 4);
  554. y = (array[1]) - (i); //shifts the placement upwards in the y-direction
  555. gotoxy(x+1, y+1);
  556. cout << "?";
  557. board[array[1] - i][array[0]] = 1;
  558. }
  559. }
  560. else if (array[2] == 1) {//this places ships if they chose the east direction
  561. for (i = 0; i < array[3]; i++) {
  562. x = (array[0] * 2) + (i * 2) + (18 * (player - 1) + (player - 1) * (diff * 4)); //shifts the placement to the right in the x-direction
  563. y = (array[1]);
  564. gotoxy(x+1, y+1);
  565. cout << "?";
  566. board[array[1]][array[0] + i] = 1;
  567. }
  568. }
  569. else if (array[2] == 2) {//this places ships if they chose the south direction
  570. for (i = 0; i < array[3]; i++) {
  571. x = (array[0] * 2) + (18 * (player - 1)) + (player - 1) * (diff * 4);
  572. y = (array[1]) + (i); //shifts the placement to the down in the y-direction
  573. gotoxy(x+1, y+1);
  574. cout << "?";
  575. board[array[1] + i][array[0]] = 1;
  576. }
  577. }
  578. else if (array[2] == 3) {//this places ships if they chose the west direction
  579. for (i = 0; i < array[3]; i++) {
  580. x = (array[0] * 2) - (i * 2) + (18 * (player - 1)) + (player - 1) * (diff * 4); //shifts the placement to the left in the x-direction
  581. y = (array[1]);
  582. gotoxy(x+1, y+1);
  583. cout << "?";
  584. board[array[1]][array[0] - i] = 1;
  585. }
  586. }
  587. }
  588.  
  589. }
  590. void resetText(int playerDiff) {
  591. int i;
  592. if (playerDiff == 1) {
  593. for (i = 7; i < 19; i++) {
  594. gotoxy(0+1, i+1);
  595. cout << " ";
  596. }
  597. gotoxy(0+1, 7+1);
  598. }
  599. else if (playerDiff == 2) {
  600. for (i = 9; i < 19; i++) {
  601. gotoxy(0+1, i+1);
  602. cout << " ";
  603. }
  604. gotoxy(0+1, 9+1);
  605. }
  606. else if (playerDiff == 3) {
  607. for (i = 11; i < 19; i++) {
  608. gotoxy(0+1, i+1);
  609. cout << " ";
  610. }
  611. gotoxy(0+1, 11+1);
  612. }
  613. }
  614. void resetInitText() {
  615. int i;
  616. gotoxy(0+1, 0+1);
  617. for (i = 0; i < 20; i++) {
  618. gotoxy(0+1, i+1);
  619. cout << " ";
  620. }
  621. gotoxy(0+1, 0+1);
  622.  
  623. }
  624. void checker(int& player, int array[], vector<vector <int> > board, int playerDiff) {
  625.  
  626. int i;
  627.  
  628.  
  629.  
  630. if (array[2] == 0) {
  631. for (i = 0; i < array[3]; i++) {
  632.  
  633. if (array[1] - i < 0) {
  634.  
  635. player = -1;
  636. }
  637. else if (board[array[1] - i][array[0]] != 0) {
  638. player = -1;
  639. }
  640. }
  641. }
  642. else if (array[2] == 1) {
  643. for (i = 0; i < array[3]; i++) {
  644. if (array[0] + i > 3 + (playerDiff * 2)) {
  645.  
  646. player = -1;
  647. }
  648. else if (board[array[1]][array[0] + i] != 0) {
  649. player = -1;
  650. }
  651. }
  652. }
  653. else if (array[2] == 2) {
  654. for (i = 0; i < array[3]; i++) {
  655. if (array[1] + i > 3 + (playerDiff * 2)) {
  656.  
  657. player = -1;
  658. }
  659. else if (board[array[1] + i][array[0]] != 0) {
  660. player = -1;
  661. }
  662. }
  663. }
  664. else if (array[2] == 3) {
  665. for (i = 0; i < array[3]; i++) {
  666. if (array[0] - i < 0) {
  667.  
  668. player = -1;
  669. }
  670. else if (board[array[1]][array[0] - i] != 0) {
  671. player = -1;
  672. }
  673. }
  674. }
  675. }
  676.  
  677.  
  678.  
  679. void gameLoop(vector<vector<int> > playerOne, vector<vector <int> > playerTwo, int playerDiff, int multiPlayer, string user, string user2) {
  680. string attackCoords;
  681. int coords[4];
  682. int pHitTimes = 0;
  683. int pHit;
  684. int cHitTimes = 0;
  685. int cHit;
  686. int x, y;
  687. bool win = false;
  688. int alreadyHit;
  689. int cAlreadyHit[8][8];
  690. int pAlreadyHit[8][8];
  691. //the code below runs the loop which is the "game". where both players take turns inputting coords.
  692. //the program inputs the user coords, but the computer randomly chooses coords using the 452-computerBrains() function
  693. //the program uses the 460-hitregplayer() and the 483-hitregcomputer() functions to see if it was a hit/miss
  694. //then it uses the 503-hitPrinter() function to display to the board
  695. int goal = 0;
  696.  
  697. if (playerDiff == 1) {
  698. goal = 12;
  699. }
  700. else if (playerDiff == 2) {
  701. goal = 17;
  702. }
  703. else if (playerDiff == 3) {
  704. goal = 21;
  705. }
  706.  
  707.  
  708. do {
  709. resetText(playerDiff);
  710. cout << user << ", enter in the coords you wish to attack. (Ex. B7): ";
  711. cin >> attackCoords;
  712.  
  713. locationCalc(attackCoords, 0, "W", coords);
  714. hitregplayer(coords[1], coords[0], playerTwo, pHitTimes, pHit, pAlreadyHit);
  715. hitPrinter(coords[0], coords[1], 1, pHit, playerDiff);
  716.  
  717. resetText(playerDiff);
  718. alreadyHit = 1;
  719.  
  720.  
  721. if (cHitTimes == goal || pHitTimes == goal) {
  722. win = true;
  723. }
  724. if(win == false){
  725. if (multiPlayer == 1) {
  726. do {
  727. computerBrains(x, y, playerDiff);
  728. hitregcomputer(y, x, playerOne, cHitTimes, cHit, alreadyHit, cAlreadyHit,multiPlayer);
  729. } while (alreadyHit == 1);
  730. hitPrinter(x, y, 0, cHit, playerDiff);
  731. }
  732. else if (multiPlayer == 2) {
  733. cout << user2 << ", enter in the coords you wish to attack. (Ex. B7): ";
  734. cin >> attackCoords;
  735.  
  736. locationCalc(attackCoords, 0, "W", coords);
  737. hitregcomputer(coords[1], coords[0], playerOne, cHitTimes, cHit, alreadyHit, cAlreadyHit,multiPlayer);
  738. hitPrinter(coords[0], coords[1], 0, cHit, playerDiff);
  739. }
  740.  
  741. sleep(2);
  742. }
  743.  
  744. if (cHitTimes == goal || pHitTimes == goal) {
  745. win = true;
  746.  
  747.  
  748. resetText(playerDiff);
  749. }
  750. } while (win == false);
  751.  
  752. if (pHitTimes == goal) {
  753. cout << user << " beat " << user2 << "!";
  754. }
  755. else {
  756. if (multiPlayer == 2){
  757. cout << user2 << " beat " << user << "!";
  758. }
  759. if (multiPlayer == 1){
  760. cout << "Computer won.";
  761. }
  762. sleep(3);
  763. }}
  764.  
  765. void computerBrains(int& x, int& y, int playerDiff) {
  766. if (playerDiff == 1) {
  767. srand(time(0));
  768. x = rand() % 6;
  769. srand(time(0) * rand() % 1000);
  770. y = rand() % 6;
  771. }
  772. else if (playerDiff == 2) {
  773. srand(time(0));
  774. x = rand() % 8;
  775. srand(time(0) * rand() % 1000);
  776. y = rand() % 8;
  777. }
  778. else if (playerDiff == 3) {
  779. srand(time(0));
  780. x = rand() % 10;
  781. srand(time(0) * rand() % 1000);
  782. y = rand() % 10;
  783. }
  784.  
  785. }
  786.  
  787. void hitregplayer(int i, int j, vector<vector <int> > plocation, int& timeshit, int& pHit, int pAlreadyHit[8][8]) { /* where location refers to where the player aims, and computer location*/
  788. int u;
  789. int v;
  790.  
  791.  
  792. if (plocation[i][j] == 1) {//clocation refers == true where ship coordinate is
  793. cout << "Hit" << endl;
  794. //alreadyHit[i][j] = 1; //sets alreadyHit as true
  795. //location[i][j] = 0; //resets player target
  796. if(pAlreadyHit[i][j] != 1){
  797. timeshit++;
  798. }
  799. pHit = 1;
  800. pAlreadyHit[i][j] = 1;
  801. cout << endl;
  802. cout << endl;
  803.  
  804.  
  805. }
  806. else {
  807. cout << "Miss, better grab a popsicle!" << endl;
  808. pHit = 0;
  809.  
  810. }
  811. sleep(2);
  812.  
  813. }
  814.  
  815. 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*/
  816.  
  817.  
  818. if (clocation[i][j] == 1 && cAlreadyHit[i][j] != 1) {//plocation refers == true where ship coordinate is
  819. if (multiplayer == 1) {cout << "Computer attacked at (" << j << ", " << i << ") and hit!." << endl;
  820. //calreadyHit[i][j] = 1; //sets alreadyHit as true
  821. //colocation[i][j] = 0; //resets player target
  822. timeshitc++;
  823. cHit = 1;
  824. alreadyHit = 0;
  825. cAlreadyHit[i][j] = 1;
  826. }
  827.  
  828. if (multiplayer == 2) { cout << "Hit!" << endl;
  829. timeshitc++;
  830. cHit = 1;
  831. alreadyHit = 0;
  832. cAlreadyHit[i][j] = 1; }
  833. }
  834. else if (cAlreadyHit[i][j] != 1) {
  835. if (multiplayer == 1) {cout << "Computer attacked at (" << j + 1 << ", " << i + 1 << ") and missed!." << endl;
  836. cHit = 0;
  837. alreadyHit = 0;
  838. cAlreadyHit[i][j] = 1;
  839. }
  840. if (multiplayer == 2) { cout << "Miss, better grab a popsicle!" << endl;
  841. cHit = 0;
  842. alreadyHit = 0;
  843. cAlreadyHit[i][j] = 1;}
  844. }}
  845.  
  846.  
  847. void hitPrinter(int x, int y, int player, int hit, int diff) {
  848. gotoxy((x * 2 + (player * 18) + player * (diff * 4))+1, y+1);
  849. if (hit == 1) {
  850. cout << "X" ;
  851. }
  852. else {
  853. cout << char(177);
  854. }
  855. gotoxy(0+1, 9+1);
  856. }
  857.  
  858.  
  859. void initBoard(int diff){
  860.  
  861.  
  862.  
  863. vector<vector <char> > playerOne;
  864. vector<vector <char> > playerTwo;
  865. int i;
  866. int j;
  867.  
  868. char letters[10] = { 'A','B','C','D','E','F','G','H','I','J' };
  869.  
  870.  
  871. if (diff == 1) {
  872. playerOne.resize(6, vector<char>(6, '*'));
  873. playerTwo.resize(6, vector<char>(6, '*'));
  874.  
  875. }
  876. else if (diff == 2) {
  877. playerOne.resize(8, vector<char>(8, '*'));
  878. playerTwo.resize(8, vector<char>(8, '*'));
  879.  
  880. }
  881. else if (diff == 3) {
  882. playerOne.resize(10, vector<char>(10, '*'));
  883. playerTwo.resize(10, vector<char>(10, '*'));
  884.  
  885. }
  886.  
  887.  
  888.  
  889. if (diff == 1) {
  890. for (i = 0; i < 6; i++) {
  891.  
  892. for (j = 0; j < 6; j++) {
  893. cout << playerOne[i][j] << " "; //<<< couts player's board
  894. }
  895. cout << (i + 1) << " | " << (i + 1) << " "; //<<< couts spacer and vertical number coords on board
  896. for (j = 0; j < 6; j++) {
  897. cout << playerOne[i][j] << " "; //<<< couts computer's board, but uses same array because its all zeros
  898. }
  899. if (i == 5) {
  900. cout << endl;
  901. }
  902. for (j = 0; j < 6; j++) {
  903. if (i == 5) {
  904. cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
  905. }
  906. if (j == 5 && i == 5) {
  907. cout << " | "; //spacer
  908. }
  909. }
  910.  
  911. for (j = 0; j < 6; j++) {
  912. if (i == 5) {
  913. cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
  914. }
  915. }
  916. cout << endl;
  917. }
  918. }
  919. else if (diff == 2) {
  920. for (i = 0; i < 8; i++) {
  921.  
  922. for (j = 0; j < 8; j++) {
  923. cout << playerOne[i][j] << " "; //<<< couts player's board
  924. }
  925. cout << (i + 1) << " | " << (i + 1) << " "; //<<< couts spacer and vertical number coords on board
  926. for (j = 0; j < 8; j++) {
  927. cout << playerOne[i][j] << " "; //<<< couts computer's board, but uses same array because its all zeros
  928. }
  929. if (i == 7) {
  930. cout << endl;
  931. }
  932. for (j = 0; j < 8; j++) {
  933. if (i == 7) {
  934. cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
  935. }
  936. }
  937. if (i == 7) {
  938. cout << " | "; //spacer
  939. }
  940. for (j = 0; j < 8; j++) {
  941. if (i == 7) {
  942. cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
  943. }
  944. }
  945. cout << endl;
  946. }
  947. }
  948. else if (diff == 3) {
  949. for (i = 0; i < 10; i++) {
  950.  
  951. for (j = 0; j < 10; j++) {
  952. cout << playerOne[i][j] << " "; //<<< couts player's board
  953. }
  954. if (i == 9) {
  955. cout << (i + 1) << " | " << (i + 1) << " ";
  956. }
  957. else {
  958. cout << (i + 1) << " | " << (i + 1) << " "; //<<< couts spacer and vertical number coords on board
  959. }
  960. for (j = 0; j < 10; j++) {
  961. cout << playerOne[i][j] << " "; //<<< couts computer's board, but uses same array because its all zeros
  962. }
  963. if (i == 9) {
  964. cout << endl;
  965. }
  966. for (j = 0; j < 10; j++) {
  967. if (i == 9) {
  968. cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
  969. }
  970. }
  971. if (i == 9) {
  972. cout << " | "; //spacer
  973. }
  974. for (j = 0; j < 10; j++) {
  975. if (i == 9) {
  976. cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
  977. }
  978. }
  979. cout << endl;
  980. }
  981. }
  982.  
  983.  
  984.  
  985. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement