Guest User

Untitled

a guest
Apr 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. //Battleships - BY SHREYA JADHAV
  2. #include<iostream.h>
  3. #include<conio.h>
  4. #include<stdlib.h>
  5. #include<process.h>
  6. #include<dos.h>
  7.  
  8. char grid[5][5];
  9. char comp[5][5];
  10. char attack[5][5];
  11. char compattack[5][5];
  12. int i,j,z,y;
  13. void main()
  14. {
  15. void shiplocation(); //Player's ship location
  16. void playerattack(); //Player attack's ship's base
  17. void computerattack(); //Computer attack's player's base
  18. void computerlocation(); //Computer's ship location
  19. int winner(); //To determine the winner
  20. void displaycomp(); //To display the computer's grid
  21. void displayplayer(); //To display the player's grid
  22. void intro(); //To display the initial introductory screen
  23. char choice;
  24. clrscr();
  25.  
  26. intro();
  27. clrscr();
  28. cout<<"\n\n\n\n\t R U L E S : \n"
  29. <<"\t1.The aim of the game is to locate the enemy's ship on the grid"
  30. <<"\n\t2.There is one ship on each base."
  31. <<"\n\t3.You will place 1 ship (1x3) on your grid \n\t for the enemy to attempt to attack"
  32. <<"\n\t4.There is 1 ship on the enemy's grid, with size (1x3)"
  33. <<"\n\t5.You have 7 tries to locate the ship"
  34. <<"\n\t6.A hit displays a 'X' and a miss displays @"
  35. <<"\n\n\t Do you want to play (y/n) "; cin>>choice;
  36. if(choice!='y')
  37. exit(0);
  38. clrscr();
  39. shiplocation();
  40. computerlocation();
  41. for (int I=0;I<7;I++)
  42. {
  43. cout<<"\n ATTEMPT NUMBER : "<<I+1<<" out of 7";
  44. playerattack();
  45. computerattack();
  46. clrscr();
  47. displayplayer();
  48. displaycomp();
  49. int ans= winner();
  50. if (ans==5)
  51. {
  52. clrscr();
  53. cout<<"\n\n\n\n\n\n\n\t\t\t\t COMPUTER HAS WON !!!!! ";
  54. delay(2000);
  55. exit(0);
  56. }
  57. else if (ans==10)
  58. {
  59. clrscr();
  60. cout<<"\n\n\n\n\n\n\n\t\t\t\t PLAYER HAS WON !!!!! ";
  61. delay(2000);
  62. exit(0);
  63. }
  64. else if (ans==0)
  65. {
  66. continue;
  67. }
  68. }
  69. clrscr();
  70. cout<<"\n\n\n\n\n\n\t\t\t\t NO ONE HAS WON !! ";
  71. delay(2000);
  72. exit(0);
  73. getch();
  74. }
  75.  
  76. void intro()
  77. {
  78. clrscr();
  79. char intro[50] = {" B A T T L E S H I P S "};
  80. char instruction[50] = {" Press enter to continue "};
  81.  
  82. cout<<"\n\n\n\n\n\n\n\n\t\t\t";
  83. for (i=0;intro[i]!='\0';i++)
  84. {
  85. cout<<intro[i];
  86. delay(80);
  87. }
  88.  
  89. cout<<"\n\n\t\t\t";
  90. for (i=0;instruction[i]!='\0';i++)
  91. {
  92. cout<<instruction[i];
  93. delay(80);
  94. }
  95. getch();
  96. }
  97.  
  98.  
  99. void shiplocation()
  100. {
  101. int row,col;
  102. cout<<"\n Enter the coordinates of where you want to locate your ship : \n";
  103. for (i=0;i<3;i++)
  104. {
  105. cout<<"\n coordinate "<<i+1;
  106. cout<<"\n row : ";cin>>row;
  107. cout<<"\n col : ";cin>>col;
  108. grid[row-1][col-1]='S';
  109. }
  110. clrscr();
  111.  
  112. for (i=0;i<5;i++)
  113. {
  114. for (j=0;j<5;j++)
  115. {
  116. if (grid[i][j]!='S')
  117. grid[i][j]='*';
  118. }
  119. }
  120. cout<<"\n\tP L A Y E R 'S B O A R D \n\n";
  121.  
  122. for (i=1;i<6;i++)
  123. cout<<"\t"<<i;
  124.  
  125. cout<<"\n";
  126.  
  127. for (i=0;i<5;i++)
  128. {
  129. cout<<i+1;
  130. for (j=0;j<5;j++)
  131. cout<<"\t"<<grid[i][j];
  132. cout<<"\n";
  133. }
  134. }
  135. void computerlocation()
  136. {
  137. randomize();
  138. int randrow,randcol,a;
  139. randrow=random(5);
  140. randcol=random(5);
  141. a=random(2);
  142.  
  143. if (a==0) //along the row
  144. {
  145. for (i=0;i<5;i++)
  146. {
  147. if (i==randrow)
  148. {
  149. if (randcol<=3)
  150. {
  151. for (j=randcol,z=0;j<randcol+4,z<3;j++,z++)
  152. comp[randrow][j]='S';
  153. }
  154. else if (randcol>3)
  155. {
  156. for (j=randcol,z=0;(j<=0 && z<3);j--,z++)
  157. comp[randrow][j]='S';
  158. }
  159. }
  160. }
  161. }
  162.  
  163. else if (a==1)
  164. {
  165. for (j=0;j<5;j++)
  166. {
  167. if (j==randcol)
  168. {
  169. if (randrow<=3)
  170. {
  171. for (i=randrow,z=0;i<randrow+4,z<3;i++,z++)
  172. comp[i][randcol]='S';
  173. }
  174. else if (randrow>3)
  175. {
  176. for (i=randrow,z=0;(i<=0 && z<3);i++,z++)
  177. comp[i][randcol]='S';
  178. }
  179. }
  180. }
  181. }
  182.  
  183. cout<<"\n C O M P U T E R 'S B O A R D \n\n";
  184. for (i=0;i<5;i++)
  185. {
  186. for (j=0;j<5;j++)
  187. {
  188. if (comp[i][j]!='S')
  189. comp[i][j]='O';
  190. }
  191. }
  192.  
  193. for (i=1;i<6;i++)
  194. cout<<"\t"<<i;
  195.  
  196. cout<<"\n";
  197.  
  198. for (i=0;i<5;i++)
  199. {
  200. cout<<i+1;
  201. for (j=0;j<5;j++)
  202. cout<<"\t"<<"*";
  203. cout<<"\n";
  204. }
  205. /*Display the ship's location .. (for programmer purposes)
  206. cout<<"\n";
  207. for (i=0;i<5;i++)
  208. {
  209. cout<<i+1;
  210. for (j=0;j<5;j++)
  211. cout<<"\t"<<comp[i][j];
  212. cout<<"\n";
  213. }
  214. */
  215.  
  216.  
  217. }
  218.  
  219. void playerattack()
  220. {
  221. int row,col;
  222. cout<<"\n Enter the coordinates of the ship's base you want to attack : \n";
  223. cout<<"Row = "; cin>>row;
  224. cout<<"\nCol = ";cin>>col;
  225.  
  226. if (row>5||col>5)
  227. {
  228. cout<<"\n Wrong choice !!!! You miss a turn :( ";
  229. cout<<"\n Press enter \n";
  230. getch();
  231. }
  232.  
  233. if (comp[row-1][col-1]=='S')
  234. comp[row-1][col-1]='X';
  235. else
  236. comp[row-1][col-1]='@';
  237. }
  238.  
  239. void computerattack()
  240. {
  241. cout<<"\n Computer Attacking . . .";
  242. delay(800);
  243. int row,col;
  244. row=random(5);
  245. col=random(5);
  246.  
  247. if (grid[row][col]=='S')
  248. grid[row][col]='X';
  249. else
  250. grid[row][col]='@';
  251. }
  252.  
  253. void displaycomp()
  254. {
  255. cout<<"\n C O M P U T E R 'S B O A R D \n\n";
  256.  
  257.  
  258. for (i=1;i<6;i++)
  259. cout<<"\t"<<i;
  260.  
  261. cout<<"\n";
  262.  
  263. for (i=0;i<5;i++)
  264. {
  265. cout<<i+1;
  266. for (j=0;j<5;j++)
  267. {
  268. if (comp[i][j]=='X')
  269. cout<<"\t"<<"X";
  270. else if (comp[i][j]=='@')
  271. cout<<"\t"<<"@";
  272. else
  273. cout<<"\t"<<"*";
  274. }
  275. cout<<"\n";
  276. }
  277. }
  278.  
  279. void displayplayer()
  280. {
  281. cout<<"\n\tP L A Y E R 'S B O A R D \n\n";
  282.  
  283.  
  284. for (i=1;i<6;i++)
  285. cout<<"\t"<<i;
  286.  
  287. cout<<"\n";
  288.  
  289. for (i=0;i<5;i++)
  290. {
  291. cout<<i+1;
  292. for (j=0;j<5;j++)
  293. cout<<"\t"<<grid[i][j];
  294. cout<<"\n";
  295. }
  296. }
  297.  
  298. int winner()
  299. {
  300. int flag1=0;
  301. int flag2=0;
  302. int ans=0;
  303. for (i=0;i<5;i++)
  304. {
  305. for (j=0;j<5;j++)
  306. {
  307. if (grid[i][j]=='S')
  308. flag1=1; //comp wins if flag1 is 0
  309. else if (comp[i][j]=='S')
  310. flag2=1; //player wins if flag2 is 0
  311. }
  312. }
  313.  
  314. if (flag1==0)
  315. ans=5;
  316. else if (flag2==0)
  317. ans=10;
  318. return(ans);
  319. }
Add Comment
Please, Sign In to add comment