Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Battleship_Try_and_Catch
  6. {
  7. class Program
  8. {
  9. //array declaration having farray = computer ships(pcboats) and zarray = user ships (userboats)
  10. public static int[,] farray = new int[12, 12]; //2D array
  11. public static int[,] zarray = new int[12, 12];
  12. public static int pc_score, user_score; //variable for the computer's and the player's score
  13.  
  14. static void Main(string[] args) //main program
  15. {
  16. pcboats();
  17. userboats(); //goes to the function userboats()
  18. display(); //displays the userboats()
  19. pc_shoot(); //goes to the function pc_shoot()
  20. display(); //displayes userboats() and pc_shoot()
  21.  
  22. for (int x = 0; x < 144; x++) //the grid is 12 x 12 giving a total of 144 hits, also the the max. no of hits
  23. {
  24. //how the program is displayed
  25. user_shoot(); //goes to the user_shoot() function
  26. display(); //shows the refreshed grid
  27. pc_shoot(); //goes to the pc_shoot() function
  28. display(); //shows the grid again
  29. if (pc_score == 15) //20 is the max. no. of hits since the ships add up to 20 squares
  30. {
  31. Console.WriteLine(" You LOSE the game ='[ "); //if the player loses the game it displays this message
  32. break; //stops the program
  33. }
  34. else if (user_score == 15) //if computer reaches a score of 20
  35. {
  36. Console.WriteLine(" You WIN =]"); //if the player wins the game is message is displayed
  37. break; //stops the program
  38. }
  39. }
  40. Console.ReadLine(); //waits for an input
  41.  
  42. }
  43.  
  44. static void display() //display program which will display the grids
  45. {
  46. Console.WriteLine("---------------------------------------------------------------------------"); //line above the grid
  47. Console.WriteLine(" A B C D E F G H I J K L" + " " + "A B C D E F G H I J K L"); //grid upper letters
  48.  
  49. //how the arrays are set up
  50. for (int x = 0; x < 12; x++) //horizontal axes
  51. {
  52. Console.Write(" " + x + "\t"); //fills it with array values
  53. for (int y = 0; y < 12; y++) //with each input in the x-axis, a y-axis column is generated
  54. {
  55. if (farray[y, x] != 0) //to display an empty grid
  56. {
  57. Console.Write("|" + farray[y, x]); //array values fillup
  58. }
  59. else
  60. {
  61. Console.Write("| "); //else draw the array with an empty space
  62. }
  63. }
  64. Console.Write("|" + "\t" + x + "\t"); //repetitive process for player's array
  65.  
  66. for (int z = 0; z < 12; z++)
  67. {
  68. if (zarray[z, x] != 0)
  69. {
  70. Console.Write("|" + zarray[z, x]);
  71. }
  72. else
  73. {
  74. Console.Write("| ");
  75. }
  76. }
  77. Console.Write("|" + " ");
  78. Console.WriteLine();
  79. }
  80. Console.WriteLine("\n\n\n\n\n\n"); //6 new lines
  81.  
  82. Console.WriteLine("---------------------------------------------------------------------------"); //this line is displayed under the grids
  83.  
  84. Console.ReadLine();
  85. }
  86.  
  87. static void pcboats() //placing pc ships
  88. {
  89. int position1, position2; //the x-axes and y-axes
  90.  
  91. //1 x 2 square ship
  92. for (int i = 0; i < 2; i++) //i < 2 since the ship is made up of 1 x 2 squares
  93. {
  94. position1 = random_number(12); //generate number from 0 to 11 both for x and for y
  95. position2 = random_number(12);
  96.  
  97. if (farray[position1, position2] == 0) //if the generated position is empty, fill it with 1
  98. { farray[position1, position2] = 1; }
  99.  
  100. else i--; //else make another position until an empty one is found
  101. }
  102.  
  103. for (int boat = 3; boat < 7; boat++)
  104. {
  105. position1 = random_number(12 - boat);
  106. position2 = random_number(12 - boat);
  107. int x1 = boat - 6;
  108. int x2 = boat - 5;
  109. int x3 = boat - 4;
  110. int x4 = boat - 3;
  111. int x5 = boat - 2;
  112. int x6 = boat - 1;
  113. if (x1 < 0)
  114. {
  115. x1 = 0;
  116. }
  117. if (x2 < 0)
  118. {
  119. x2 = 0;
  120. }
  121. if (x3 < 0)
  122. {
  123. x3 = 0;
  124. }
  125. if (x4 < 0)
  126. {
  127. x4 = 0;
  128. }
  129. if (x5 < 0)
  130. {
  131. x5 = 0;
  132. }
  133. if (x6 < 0)
  134. {
  135. x6 = 0;
  136. }
  137.  
  138. int direction = random_number(2);
  139. bool ready = false;
  140.  
  141. //horizontal position since direction is 0
  142.  
  143. if (direction == 0)
  144. {
  145.  
  146. do
  147. {
  148. if ((farray[position1 + x1, position2] == 0) && (farray[position1 + x2, position2] == 0) &&
  149. (farray[position1 + x3, position2] == 0)&&(farray[position1 + x4, position2] == 0)&&
  150. (farray[position1 + x5, position2] == 0)&&(farray[position1 + x6, position2] == 0))
  151. //position1 (horizontal) +1 each time up to 3 squares
  152. {
  153. for (int i = 0; i < boat; i++) //i < 3 since the ship is made up of 3 squares
  154. {
  155. farray[position1 + i, position2] = 1; //display the ships on the gris as the number 1
  156. }
  157. ready = true; //ship is complete mark
  158. }
  159.  
  160. else
  161. {
  162. position1 = random_number(12 - boat); //9+3=12
  163. position2 = random_number(12 - boat); //new position generator
  164. ready = false;
  165. }
  166. } while (ready == false);
  167.  
  168. }
  169.  
  170. if (direction == 1)
  171. {
  172.  
  173. do
  174. {
  175. if ((farray[position1, position2 + x1] == 0) && (farray[position1, position2 + x2] == 0) &&
  176. (farray[position1, position2 + x3] == 0)&&(farray[position1, position2 + x4] == 0) &&
  177. (farray[position1, position2 + x5] == 0)&&(farray[position1, position2 + x6] == 0))
  178. {
  179. for (int i = 0; i < boat; i++)
  180. {
  181. farray[position1, position2 + i] = 1;
  182. }
  183. ready = true;
  184. }
  185.  
  186. else
  187. {
  188. position1 = random_number(12 - boat);
  189. position2 = random_number(12 - boat);
  190. ready = false;
  191. }
  192.  
  193. } while (ready == false);
  194.  
  195. }
  196. }
  197.  
  198. }
  199.  
  200. static void userboats() //placing user ships
  201. {
  202. int direction = 0, position1 = 0, position2 = 0; //variables which will be inputted by the player using a keyboard
  203. bool flag1 = false, ready = false; //flag declatarion
  204.  
  205. //filling up 1 boat
  206. do
  207. {
  208. Console.WriteLine("Enter vertical position"); //asks plater vertical postition (y-axes)
  209. try
  210. {
  211. position1 = Convert.ToInt16(Console.ReadLine());//reads vertical and converts string input to intiger
  212. }
  213. catch
  214. {
  215. }
  216. Console.WriteLine("Enter horizontal position"); //asks player horiontal position (x-axes)
  217. try
  218. {
  219. position2 = Convert.ToInt16(Console.ReadLine());//reads horizontal and converts string input to intiger
  220. }
  221. catch
  222. {
  223. }
  224. if ((position1 > -1) && (position1 < 12) //range of the input
  225. && (position2 > -1) && (position2 < 12)
  226. && (zarray[position1, position2] == 0))
  227. {
  228. flag1 = true;
  229. zarray[position1, position2] = 1; //approves ship and displayes it
  230. }
  231. else
  232. Console.WriteLine("invalid inputs please enter data again"); //display if coordinates are not right
  233. }
  234. while (flag1 == false); //all the above happens
  235. flag1 = false;
  236. display(); //shows the grid with the ships on it
  237.  
  238. //filling up the second 1 square boat
  239. {
  240. Console.WriteLine("Enter vertical position"); //asks plater vertical postition (y-axes)
  241. try
  242. {
  243. position1 = Convert.ToInt16(Console.ReadLine());//reads vertical and converts string input to intiger
  244. }
  245. catch
  246. {
  247. }
  248. Console.WriteLine("Enter horizontal position"); //asks player horiontal position (x-axes)
  249. try
  250. {
  251. position2 = Convert.ToInt16(Console.ReadLine());//reads horizontal and converts string input to intiger
  252. }
  253. catch
  254. {
  255. }
  256. if ((position1 > -1) && (position1 < 12)
  257. && (position2 > -1) && (position2 < 12)
  258. && (zarray[position1, position2] == 0))
  259. {
  260. flag1 = true;
  261. zarray[position1, position2] = 1;
  262. }
  263. else
  264. Console.WriteLine("invalid inputs please enter data again");
  265. }
  266. while (flag1 == false) ;
  267. flag1 = false;
  268. display();
  269.  
  270. //fill up boat with 3
  271. ready = false;
  272. do
  273. {
  274. do
  275. {
  276. Console.WriteLine("Enter Direction 1 or 0");
  277. try
  278. {
  279. direction = Convert.ToInt16(Console.ReadLine());
  280. }
  281. catch
  282. {
  283. }
  284. Console.WriteLine("Enter vertical position"); //asks plater vertical postition (y-axes)
  285. try
  286. {
  287. position1 = Convert.ToInt16(Console.ReadLine());//reads vertical and converts string input to intiger
  288. }
  289. catch
  290. {
  291. }
  292. Console.WriteLine("Enter horizontal position"); //asks player horiontal position (x-axes)
  293. try
  294. {
  295. position2 = Convert.ToInt16(Console.ReadLine());//reads horizontal and converts string input to intiger
  296. }
  297. catch
  298. {
  299. }
  300.  
  301. if (((direction == 1)
  302. && (position1 > -1) && (position1 < 12)
  303. && (position2 > -1) && (position2 < 9))
  304. || ((direction == 0)
  305. && (position1 > -1) && (position1 < 9)
  306. && (position2 > -1) && (position2 < 12)))
  307. flag1 = true;
  308. else
  309. Console.WriteLine("invalid inputs please enter data again");
  310. }
  311. while (flag1 == false);
  312. if (direction == 0)
  313. {
  314. if ((zarray[position1, position2] == 0) && (zarray[position1 + 1, position2] == 0) &&
  315. (zarray[position1 + 2, position2] == 0)) //checking if the grid already contains a ship
  316. {
  317. for (int ii = 0; ii < 3; ii++)
  318. {
  319. zarray[position1 + ii, position2] = 1;
  320. }
  321. ready = true;
  322. }
  323. else Console.WriteLine("Position contains a ship"); //displays if there is another ship
  324. }
  325. if (direction == 1)
  326. {
  327. if ((zarray[position1, position2] == 0) && (zarray[position1, position2 + 1] == 0) &&
  328. (zarray[position1, position2 + 2] == 0))
  329. {
  330. for (int ii = 0; ii < 3; ii++)
  331. {
  332. zarray[position1, position2 + ii] = 1;
  333. }
  334. ready = true;
  335. }
  336. else Console.WriteLine("Position contains a ship");
  337. }
  338. }
  339. while (ready == false);
  340. display();
  341.  
  342. //fill up boat with 4
  343. ready = false;
  344. do
  345. {
  346. do
  347. {
  348. Console.WriteLine("Enter Direction 1 or 0");
  349. try
  350. {
  351. direction = Convert.ToInt16(Console.ReadLine());
  352. }
  353. catch
  354. {
  355. }
  356. Console.WriteLine("Enter vertical position"); //asks plater vertical postition (y-axes)
  357. try
  358. {
  359. position1 = Convert.ToInt16(Console.ReadLine());//reads vertical and converts string input to intiger
  360. }
  361. catch
  362. {
  363. }
  364. Console.WriteLine("Enter horizontal position"); //asks player horiontal position (x-axes)
  365. try
  366. {
  367. position2 = Convert.ToInt16(Console.ReadLine());//reads horizontal and converts string input to intiger
  368. }
  369. catch
  370. {
  371. }
  372. if (((direction == 1)
  373. && (position1 > -1) && (position1 < 12)
  374. && (position2 > -1) && (position2 < 9))
  375. || ((direction == 0)
  376. && (position1 > -1) && (position1 < 9)
  377. && (position2 > -1) && (position2 < 12)))
  378. flag1 = true;
  379. else
  380. Console.WriteLine("invalid inputs please enter data again");
  381. }
  382. while (flag1 == false);
  383. if (direction == 0)
  384. {
  385. if ((zarray[position1, position2] == 0) && (zarray[position1 + 1, position2] == 0) &&
  386. (zarray[position1 + 2, position2] == 0) && (zarray[position1 + 3, position2] == 0))
  387. {
  388. for (int ii = 0; ii < 4; ii++)
  389. {
  390. zarray[position1 + ii, position2] = 1;
  391. }
  392. ready = true;
  393. }
  394. else Console.WriteLine("Position contains a ship");
  395. }
  396. if (direction == 1)
  397. {
  398. if ((zarray[position1, position2] == 0) && (zarray[position1, position2 + 1] == 0) &&
  399. (zarray[position1, position2 + 2] == 0) && (zarray[position1, position2 + 3] == 0))
  400. {
  401. for (int ii = 0; ii < 4; ii++)
  402. {
  403. zarray[position1, position2 + ii] = 1;
  404. }
  405. ready = true;
  406. }
  407. else Console.WriteLine("Position contains a ship");
  408. }
  409. } while (ready == false);
  410. display();
  411.  
  412. //fill up boat with 5
  413.  
  414.  
  415. //fill up boat with 6
  416. ready = false;
  417. do
  418. {
  419. do
  420. {
  421. Console.WriteLine("Enter Direction 1 or 0");
  422. try
  423. {
  424. direction = Convert.ToInt16(Console.ReadLine());
  425. }
  426. catch
  427. {
  428. }
  429. Console.WriteLine("Enter vertical position"); //asks plater vertical postition (y-axes)
  430. try
  431. {
  432. position1 = Convert.ToInt16(Console.ReadLine());//reads vertical and converts string input to intiger
  433. }
  434. catch
  435. {
  436. }
  437. Console.WriteLine("Enter horizontal position"); //asks player horiontal position (x-axes)
  438. try
  439. {
  440. position2 = Convert.ToInt16(Console.ReadLine());//reads horizontal and converts string input to intiger
  441. }
  442. catch
  443. {
  444. }
  445. if (((direction == 1)
  446. && (position1 > -1) && (position1 < 12)
  447. && (position2 > -1) && (position2 < 9))
  448. || ((direction == 0)
  449. && (position1 > -1) && (position1 < 9)
  450. && (position2 > -1) && (position2 < 12)))
  451. flag1 = true;
  452. else
  453. Console.WriteLine("invalid inputs please enter data again");
  454. }
  455. while (flag1 == false);
  456. if (direction == 0)
  457. {
  458. if ((zarray[position1, position2] == 0) && (zarray[position1 + 1, position2] == 0) &&
  459. (zarray[position1 + 2, position2] == 0) && (zarray[position1 + 3, position2] == 0) &&
  460. (zarray[position1 + 4, position2] == 0) && (zarray[position1 + 5, position2] == 0))
  461. {
  462. for (int ii = 0; ii < 6; ii++)
  463. {
  464. zarray[position1 + ii, position2] = 1;
  465. }
  466. ready = true;
  467. }
  468. else Console.WriteLine("Position contains a ship");
  469. }
  470. if (direction == 1)
  471. {
  472. if ((zarray[position1, position2] == 0) && (zarray[position1, position2 + 1] == 0) &&
  473. (zarray[position1, position2 + 2] == 0) && (zarray[position1, position2 + 3] == 0) && (
  474. zarray[position1, position2 + 4] == 0) && (zarray[position1, position2 + 5] == 0))
  475. {
  476. for (int ii = 0; ii < 6; ii++)
  477. {
  478. zarray[position1, position2 + ii] = 1;
  479. }
  480. ready = true;
  481. }
  482. else Console.WriteLine("Position contains a ship");
  483. }
  484. } while (ready == false);
  485. }
  486.  
  487. static void pc_shoot() //computer shoot
  488. {
  489. int position1, position2;
  490. bool flag = false;
  491. do
  492. {
  493. position1 = random_number(12);
  494. position2 = random_number(12);
  495. if ((zarray[position1, position2] == 0) || (zarray[position1, position2] == 1))
  496. {
  497. if (zarray[position1, position2] == 0)
  498. {
  499. //9 is a miss
  500. zarray[position1, position2] = 9;
  501.  
  502. }
  503. else
  504. {
  505. //8 is a hit
  506. zarray[position1, position2] = 8;
  507. pc_score++; //increase the score everytime there is a hit
  508. }
  509. flag = true;
  510.  
  511. }
  512. }
  513. while (flag == false);
  514. }
  515.  
  516. static void user_shoot() //player shoot
  517. {
  518. int position1 = 0, position2 = 0;
  519. bool flag = false;
  520.  
  521. do
  522. {
  523. Console.WriteLine(" Enter your horizontal coordinate ");
  524. try
  525. {
  526. position1 = Convert.ToInt16(Console.ReadLine());//reads vertical and converts string input to intiger
  527. }
  528. catch
  529. {
  530. }
  531.  
  532. Console.WriteLine(" Enter your vertical coordinate");
  533. try
  534. {
  535. position2 = Convert.ToInt16(Console.ReadLine());//reads vertical and converts string input to intiger
  536. }
  537. catch
  538. {
  539. }
  540.  
  541. if ((farray[position1, position2] == 0) || (farray[position1, position2] == 1))
  542. {
  543. if (farray[position1, position2] == 0)
  544. {
  545. farray[position1, position2] = 9;
  546. }
  547. else
  548. {
  549. farray[position1, position2] = 8;
  550. user_score++;
  551. //increases the user's score by 1 if it is a hit
  552. }
  553. flag = true;
  554.  
  555. }
  556. }
  557. while (flag == false);
  558. }
  559.  
  560. static int random_number(int produce) //random number generator
  561. {
  562. int die;
  563. Random generator = new Random();
  564. die = (int)(generator.NextDouble() * produce);
  565. return (die);
  566. }
  567. }
  568. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement