Advertisement
Guest User

Untitled

a guest
Sep 30th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. void p1deployer(int p1field, int spelplan1[], string playername[], int x, int numberOfShips, int inmv);
  7. void p2deployer(int p2field, int spelplan2[], string playername[], int x, int numberOfShips, int inmv);
  8. void battle(int player1ships, int player2ships, string playername[], int bomb, int p1hits, int p2hits, int spelplan1[], int spelplan2[]);
  9.  
  10. int main(){
  11. const int p1field = 15;
  12. const int p2field = 15;
  13. const int players = 2;
  14. int spelplan1[p1field] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  15. int spelplan2[p2field] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  16. string playername[players];
  17.  
  18. int numberOfShips;
  19. int inmv=0;
  20. int x=0;
  21. int bomb=0;
  22. int player1ships;
  23. int player2ships;
  24. int p1hits = 0;
  25. int p2hits = 0;
  26.  
  27.  
  28.  
  29. cout << "Welcome to a game of battleship!" << endl;
  30. cout<<"How many battleships will you be using for this conquest? "<<endl <<"(max 15 ships each) NUMBER OF SHIPS: ";
  31. cin >> numberOfShips;
  32.  
  33. if (numberOfShips>15)
  34. {
  35. cout << "Please, remember that 15 the maximum amount of ships:" << endl;
  36. cin >> numberOfShips;
  37. }
  38. if (numberOfShips <= 0)
  39. {
  40. cout << "Please enter a positive value:" << endl;
  41. cin >> numberOfShips;
  42.  
  43. }
  44.  
  45. player1ships = numberOfShips;
  46. player2ships = numberOfShips;
  47.  
  48. cin.ignore();
  49. cout << numberOfShips << " ships?. Very good.. " << endl;
  50.  
  51.  
  52. cout<<"Please enter your name Player One: ";
  53. getline(cin, playername[0]);
  54.  
  55.  
  56.  
  57.  
  58. cout << "Please enter your name Player Two: ";
  59. getline(cin, playername[1]);
  60.  
  61.  
  62. system("cls");
  63. cout << playername[1] << ", look away! It's time for " << playername[0] << " to deploy his/her forces!" << endl;
  64. system("pause");
  65. system("cls");
  66.  
  67. p1deployer(p1field , spelplan1, playername, x, numberOfShips, inmv);
  68.  
  69. system("pause");
  70. system("cls");
  71.  
  72.  
  73. cout << endl << "Now, press enter to let " << playername[1] << " enter his/her positions.";
  74.  
  75. system("pause");
  76. system("cls");
  77.  
  78. p2deployer( p2field, spelplan2, playername, x, numberOfShips, inmv);
  79.  
  80. system("pause");
  81. system("cls");
  82. cout << "Very good!, now the time has come for battle! "<<endl
  83. << playername[0] << ", you go first!" << endl;
  84.  
  85.  
  86. battle(player1ships,player2ships, playername, bomb, p1hits, p2hits, spelplan1, spelplan2);
  87.  
  88. system ("pause");
  89. return 0;
  90.  
  91. }
  92.  
  93. void p1deployer(int p1field, int spelplan1[], string playername[1], int x, int numberOfShips, int inmv){
  94.  
  95.  
  96.  
  97.  
  98. cout << playername[0] << " please station your naval forces." << endl;
  99.  
  100. for (x = 0; x < numberOfShips; x++){
  101.  
  102.  
  103. cout << "Avaliable areas: ";
  104. if (spelplan1[0] == 0){
  105. cout << "0" << " ";
  106. }
  107. if (spelplan1[1] == 0){
  108. cout << "1" << " ";
  109. }
  110. if (spelplan1[2] == 0){
  111. cout << "2" << " ";
  112. }
  113.  
  114. if (spelplan1[3] == 0){
  115. cout << "3" << " ";
  116. }
  117. if (spelplan1[4] == 0){
  118. cout << "4" << " ";
  119. }
  120. if (spelplan1[5] == 0){
  121. cout << "5" << " ";
  122. }
  123. if (spelplan1[6] == 0){
  124. cout << "6" << " ";
  125. }
  126. if (spelplan1[7] == 0){
  127. cout << "7" << " ";
  128. }
  129. if (spelplan1[8] == 0){
  130. cout << "8" << " ";
  131. }if (spelplan1[9] == 0){
  132. cout << "9" << " ";
  133. }if (spelplan1[10] == 0){
  134. cout << "10" << " ";
  135. }if (spelplan1[11] == 0){
  136. cout << "11" << " ";
  137. }if (spelplan1[12] == 0){
  138. cout << "12" << " ";
  139. }if (spelplan1[13] == 0){
  140. cout << "13" << " ";
  141. }if (spelplan1[14] == 0){
  142. cout << "14" << " ";
  143. }
  144.  
  145. cout << endl << "Please enter an area to occupy: ";
  146. cin >> inmv;
  147.  
  148. if ((inmv != 0) && (inmv != 1) && (inmv != 2) && (inmv != 3) && (inmv != 4) && (inmv != 5) && (inmv != 6) && (inmv != 7) && (inmv != 8) && (inmv != 9) && (inmv != 10) && (inmv != 11) && (inmv != 12) && (inmv != 13) && (inmv != 14) )
  149. {
  150.  
  151.  
  152. do {
  153. cout << "Incorrect input, please try again: ";
  154. cin >> inmv;
  155. }
  156.  
  157.  
  158.  
  159. while ((inmv != 0) && (inmv != 1) && (inmv != 2) && (inmv != 3) && (inmv != 4) && (inmv != 5) && (inmv != 6) && (inmv != 7) && (inmv != 8) && (inmv != 9) && (inmv != 10) && (inmv != 11) && (inmv != 12) && (inmv != 13) && (inmv != 14));
  160.  
  161. }
  162.  
  163.  
  164. if (inmv == 0){
  165. spelplan1[0] = 1;
  166. }
  167. if (inmv == 1){
  168. spelplan1[1] = 1;
  169. }
  170. if (inmv == 2){
  171. spelplan1[2] = 1;
  172. }
  173. if (inmv == 3){
  174. spelplan1[3] = 1;
  175. }
  176. if (inmv == 4){
  177. spelplan1[4] = 1;
  178. }
  179. if (inmv == 5){
  180. spelplan1[5] = 1;
  181. }
  182. if (inmv == 6){
  183. spelplan1[6] = 1;
  184. }
  185. if (inmv == 7){
  186. spelplan1[7] = 1;
  187. }
  188. if (inmv == 8){
  189. spelplan1[8] = 1;
  190. }
  191. if (inmv == 9){
  192. spelplan1[9] = 1;
  193. }
  194. if (inmv == 10){
  195. spelplan1[10] = 1;
  196. }
  197. if (inmv == 11){
  198. spelplan1[11] = 1;
  199. }
  200. if (inmv == 12){
  201. spelplan1[12] = 1;
  202. }
  203. if (inmv == 13){
  204. spelplan1[13] = 1;
  205. }
  206. if (inmv == 14){
  207. spelplan1[14] = 1;
  208. }
  209.  
  210. }
  211.  
  212. system("pause");
  213. system("cls");
  214.  
  215. cout << "You have chosen to place your ships in the following areas: " << endl;
  216.  
  217. if (spelplan1[0] == 1){
  218. cout << "0" << " ";
  219. }
  220. if (spelplan1[1] == 1){
  221. cout << "1" << " ";
  222. }
  223. if (spelplan1[2] == 1){
  224. cout << "2" << " ";
  225. }
  226. if (spelplan1[3] == 1){
  227. cout << "3" << " ";
  228. }
  229.  
  230. if (spelplan1[4] == 1){
  231. cout << "4" << " ";
  232. }
  233. if (spelplan1[5] == 1){
  234. cout << "5" << " ";
  235. }
  236. if (spelplan1[6] == 1){
  237. cout << "6" << " ";
  238. }
  239. if (spelplan1[7] == 1){
  240. cout << "7" << " ";
  241. }
  242. if (spelplan1[8] == 1){
  243. cout << "8" << " ";
  244. }if (spelplan1[9] == 1){
  245. cout << "9" << " ";
  246. }if (spelplan1[10] == 1){
  247. cout << "10" << " ";
  248. }if (spelplan1[11] == 1){
  249. cout << "11" << " ";
  250. }if (spelplan1[12] == 1){
  251. cout << "12" << " ";
  252. }if (spelplan1[13] == 1){
  253. cout << "13" << " ";
  254. }if (spelplan1[14] == 1){
  255. cout << "14" << " ";
  256. }
  257.  
  258.  
  259.  
  260.  
  261. }
  262.  
  263. void p2deployer(int p2field, int spelplan2[], string playername[], int x, int numberOfShips, int inmv){
  264.  
  265. cout << playername[1] << ", now it has become time to station your naval forces." << endl;
  266. for (x = 0; x < numberOfShips; x++){
  267.  
  268.  
  269. cout << "Avaliable areas: ";
  270. if (spelplan2[0] == 0){
  271. cout << "0" << " ";
  272. }
  273. if (spelplan2[1] == 0){
  274. cout << "1" << " ";
  275. }
  276. if (spelplan2[2] == 0){
  277. cout << "2" << " ";
  278. }
  279.  
  280. if (spelplan2[3] == 0){
  281. cout << "3" << " ";
  282. }
  283. if (spelplan2[4] == 0){
  284. cout << "4" << " ";
  285. }
  286. if (spelplan2[5] == 0){
  287. cout << "5" << " ";
  288. }
  289. if (spelplan2[6] == 0){
  290. cout << "6" << " ";
  291. }
  292. if (spelplan2[7] == 0){
  293. cout << "7" << " ";
  294. }
  295. if (spelplan2[8] == 0){
  296. cout << "8" << " ";
  297. }if (spelplan2[9] == 0){
  298. cout << "9" << " ";
  299. }if (spelplan2[10] == 0){
  300. cout << "10" << " ";
  301. }if (spelplan2[11] == 0){
  302. cout << "11" << " ";
  303. }if (spelplan2[12] == 0){
  304. cout << "12" << " ";
  305. }if (spelplan2[13] == 0){
  306. cout << "13" << " ";
  307. }if (spelplan2[14] == 0){
  308. cout << "14" << " ";
  309. }
  310.  
  311. cout << endl << "Please enter an area to occupy: ";
  312. cin >> inmv;
  313.  
  314.  
  315. if ((inmv != 0) && (inmv != 1) && (inmv != 2) && (inmv != 3) && (inmv != 4) && (inmv != 5) && (inmv != 6) && (inmv != 7) && (inmv != 8) && (inmv != 9) && (inmv != 10) && (inmv != 11) && (inmv != 12) && (inmv != 13) && (inmv != 14))
  316. {
  317.  
  318.  
  319. do {
  320. cout << "Incorrect input, please try again: ";
  321. cin >> inmv;
  322. }
  323.  
  324.  
  325.  
  326. while ((inmv != 0) && (inmv != 1) && (inmv != 2) && (inmv != 3) && (inmv != 4) && (inmv != 5) && (inmv != 6) && (inmv != 7) && (inmv != 8) && (inmv != 9) && (inmv != 10) && (inmv != 11) && (inmv != 12) && (inmv != 13) && (inmv != 14));
  327.  
  328. }
  329.  
  330.  
  331. if (inmv == 0){
  332. spelplan2[0] = 1;
  333. }
  334. if (inmv == 1){
  335. spelplan2[1] = 1;
  336. }
  337. if (inmv == 2){
  338. spelplan2[2] = 1;
  339. }
  340. if (inmv == 3){
  341. spelplan2[3] = 1;
  342. }
  343. if (inmv == 4){
  344. spelplan2[4] = 1;
  345. }
  346. if (inmv == 5){
  347. spelplan2[5] = 1;
  348. }
  349. if (inmv == 6){
  350. spelplan2[6] = 1;
  351. }
  352. if (inmv == 7){
  353. spelplan2[7] = 1;
  354. }
  355. if (inmv == 8){
  356. spelplan2[8] = 1;
  357. }
  358. if (inmv == 9){
  359. spelplan2[9] = 1;
  360. }
  361. if (inmv == 10){
  362. spelplan2[10] = 1;
  363. }
  364. if (inmv == 11){
  365. spelplan2[11] = 1;
  366. }
  367. if (inmv == 12){
  368. spelplan2[12] = 1;
  369. }
  370. if (inmv == 13){
  371. spelplan2[13] = 1;
  372. }
  373. if (inmv == 14){
  374. spelplan2[14] = 1;
  375. }
  376.  
  377. }
  378.  
  379.  
  380.  
  381. cout << "You have chosen to place your ships in the following areas: " << endl;
  382.  
  383. if (spelplan2[0] == 1){
  384. cout << "0" << " ";
  385. }
  386. if (spelplan2[1] == 1){
  387. cout << "1" << " ";
  388. }
  389. if (spelplan2[2] == 1){
  390. cout << "2" << " ";
  391. }
  392. if (spelplan2[3] == 1){
  393. cout << "3" << " ";
  394. }
  395.  
  396. if (spelplan2[4] == 1){
  397. cout << "4" << " ";
  398. }
  399. if (spelplan2[5] == 1){
  400. cout << "5" << " ";
  401. }
  402. if (spelplan2[6] == 1){
  403. cout << "6" << " ";
  404. }
  405. if (spelplan2[7] == 1){
  406. cout << "7" << " ";
  407. }
  408. if (spelplan2[8] == 1){
  409. cout << "8" << " ";
  410. }if (spelplan2[9] == 1){
  411. cout << "9" << " ";
  412. }if (spelplan2[10] == 1){
  413. cout << "10" << " ";
  414. }if (spelplan2[11] == 1){
  415. cout << "11" << " ";
  416. }if (spelplan2[12] == 1){
  417. cout << "12" << " ";
  418. }if (spelplan2[13] == 1){
  419. cout << "13" << " ";
  420. }if (spelplan2[14] == 1){
  421. cout << "14" << " ";
  422. }
  423.  
  424. }
  425.  
  426. void battle(int player1ships, int player2ships, string playername[], int bomb, int p1hits, int p2hits, int spelplan1[], int spelplan2[]){
  427.  
  428.  
  429. while (player1ships > 0 && player2ships > 0){
  430. cout << " [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] " << endl;
  431. cout << playername[0] << " , select an area to strike: ";
  432. cin >> bomb;
  433.  
  434.  
  435. do
  436. {
  437. cout << "incorrect input, try again:";
  438.  
  439. cin >> bomb;
  440.  
  441. }
  442.  
  443. while ((bomb != '0') && (bomb != '1') && (bomb != '2') && (bomb != '3') && (bomb != '4') && (bomb != '5') && (bomb != '6') && (bomb != '7') && (bomb != '8') && (bomb != '9') && (bomb != '10') && (bomb != '11') && (bomb != '12') && (bomb != '13') && (bomb != '14'));
  444.  
  445.  
  446.  
  447. if (spelplan2[bomb] == 1){
  448. cout << "BOOM! " << endl;
  449. spelplan2[bomb] = 0;
  450. player2ships--;
  451. p1hits++;
  452. cout << " Number of hits so far: " << p1hits << endl;
  453.  
  454.  
  455. if (player2ships == 0){
  456. cout << playername[0] << " wins!" << endl;
  457. }
  458.  
  459. }
  460.  
  461. else{ cout << "*splash*" << endl; }
  462.  
  463.  
  464.  
  465.  
  466.  
  467. system("pause");
  468. system("cls");
  469. if (player2ships > 0){
  470. cout << " [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] " << endl;
  471. cout << playername[1] << ",select an area to strike: ";
  472. cin >> bomb;
  473.  
  474.  
  475. do
  476. {
  477. cout << "incorrect input, try again:";
  478.  
  479. cin >> bomb;
  480.  
  481. }
  482.  
  483. while ((bomb != '0') && (bomb != '1') && (bomb != '2') && (bomb != '3') && (bomb != '4') && (bomb != '5') && (bomb != '6') && (bomb != '7') && (bomb != '8') && (bomb != '9') && (bomb != '10') && (bomb != '11') && (bomb != '12') && (bomb != '13') && (bomb != '14'));
  484.  
  485.  
  486.  
  487.  
  488. if (spelplan1[bomb] == 1){
  489. cout << "BOOM! " << endl;
  490.  
  491. spelplan1[bomb] = 0;
  492. player1ships--;
  493. p2hits++;
  494. cout << " Number of hits so far: " << p2hits << endl;
  495.  
  496. if (player1ships == 0){
  497. cout << playername[1] << " wins!" << endl;
  498. }
  499.  
  500.  
  501. }
  502.  
  503. else{ cout << "*splash*" << endl; }
  504. system("pause");
  505. system("cls");
  506.  
  507. }
  508.  
  509. }
  510. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement