Guest User

Untitled

a guest
Apr 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. public class Schiffeversenken {
  2.  
  3. static int[][] wasser = new int[20][20];
  4.  
  5. public static void main(String[] args) {
  6. wasser = setshiponwater(2, 4, wasser, 10);
  7. wasser = setshiponwater(3, 3, wasser, 11);
  8. wasser = setshiponwater(4, 2, wasser, 12);
  9. wasser = setshiponwater(5, 1, wasser, 13);
  10.  
  11. for (int i = 0; i < wasser.length; i++) {
  12. IO.writeLn();
  13. for (int j = 0; j < wasser[0].length; j++) {
  14.  
  15. IO.write(wasser[i][j], 3);
  16. }
  17. }
  18. IO.writeLn();
  19. IO.writeLn();
  20. IO.writeLn("Wieviel Munition fuehren sie mit sich?");
  21. int treffer = 30;
  22. int maxschuesse = IO.readCorrectInt();
  23. int schuesse = 0;
  24. int a1 = 0;
  25. int a2 = 0;
  26. int a3;
  27. int c = 1;
  28. int c1 = 1;
  29. int c2 = 1;
  30. int c3 = 1;
  31. int statm = 0;
  32. int statm1 = 0;
  33. int statm2 = 0;
  34. int statm3 = 0;
  35. while (schuesse <= maxschuesse) {
  36. IO.writeLn();
  37. a1 = IO.readCorrectInt() - 1;
  38. if (a1 > 19 && a1 >= 0) {
  39. IO
  40. .writeLn("Sie haben verlohren wegen eines Verstosses gegen Java");
  41. break;
  42. }
  43. IO.flush();
  44. a2 = IO.readCorrectInt() - 1;
  45. if (a1 > 19 && a1 >= 0) {
  46. IO
  47. .writeLn("Sie haben verlohren wegen eines Verstosses gegen Java");
  48. break;
  49. }
  50. IO.flush();
  51. a3 = checkiftreffer(a1, a2);
  52. switch (a3) {
  53. case 0:
  54. IO.writeLn();
  55. schuesse++;
  56. IO.writeLn("HIT WATER...");
  57. wasser[a1][a2] = 3;
  58. break;
  59. case 1:
  60. IO.writeLn();
  61. IO.writeLn("HIT SHIP...");
  62. schuesse++;
  63. treffer--;
  64. wasser[a1][a2] = 2;
  65. break;
  66. case 2:
  67. IO.writeLn();
  68. schuesse++;
  69. IO.writeLn("Already shot on the ship at this field");
  70. break;
  71. case 3:
  72. IO.writeLn();
  73. schuesse++;
  74. IO.writeLn("Already shot into the water at this place");
  75. break;
  76. case 10:
  77. IO.writeLn();
  78. schuesse++;
  79. treffer--;
  80. wasser[a1][a2] = 2;
  81. if (c < 2) {
  82. c++;
  83. IO.writeLn("HIT SHIP...");
  84. } else {
  85. c = 0;
  86. IO.writeLn("Ship with length 2 sunk.");
  87. statm++;
  88. }
  89.  
  90. break;
  91. case 11:
  92. IO.writeLn();
  93. schuesse++;
  94. treffer--;
  95. wasser[a1][a2] = 2;
  96. if (c1 < 3) {
  97. c1++;
  98. IO.writeLn();
  99. IO.writeLn("HIT SHIP...");
  100. } else {
  101. c1 = 0;
  102. IO.writeLn("Ship with length 3 sunk.");
  103. statm1++;
  104. }
  105. break;
  106. case 12:
  107. IO.writeLn();
  108. schuesse++;
  109. treffer--;
  110. wasser[a1][a2] = 2;
  111. if (c2 < 4) {
  112. c2++;
  113. IO.writeLn();
  114. IO.writeLn("HIT SHIP...");
  115. } else {
  116. c2 = 0;
  117. IO.writeLn("Ship with length 4 sunk.");
  118. statm2++;
  119. }
  120. break;
  121. case 13:
  122. IO.writeLn();
  123. schuesse++;
  124. treffer--;
  125. wasser[a1][a2] = 2;
  126. if (c3 < 5) {
  127. c3++;
  128. IO.writeLn();
  129. IO.writeLn("HIT SHIP...");
  130. } else {
  131. c3 = 0;
  132. IO.writeLn("Ship with length 5 sunk.");
  133. statm3++;
  134. }
  135. break;
  136.  
  137. }
  138. if (treffer == 0) {
  139. IO.writeLn("Fleet has sunk. The player needed " + schuesse
  140. + " shots.");
  141. break;
  142. }
  143. }
  144. IO.write(" ", 3);
  145. for (char b = 65; b < 85; b++) {
  146. IO.write(" " + b, 3);
  147. }
  148. for (int i = 1; i < wasser.length + 1; i++) {
  149. IO.writeLn();
  150. IO.write(i, 3);
  151. for (int j = 0; j < wasser[0].length; j++) {
  152. int abc = wasser[i - 1][j];
  153. if (abc == 0) {
  154. IO.write(" -", 3);
  155. } else {
  156. if (abc == 3) {
  157. IO.write(" W", 3);
  158. } else {
  159. if (abc == 2) {
  160. IO.write(" S", 3);
  161. } else {
  162. IO.write(" -", 3);
  163. }
  164. }
  165. }
  166. }
  167. }
  168. IO.writeLn();
  169. IO.writeLn("Game Statistic:");
  170. int n = 4 - statm;
  171. int n1 = 3 - statm1;
  172. int n2 = 2 - statm2;
  173. int n3 = 1 - statm3;
  174. IO.writeLn("Ships with len 2 (4): " + statm + " sunk, " + n
  175. + " remaining");
  176. IO.writeLn("Ships with len 3 (3): " + statm1 + " sunk, " + n1
  177. + " remaining");
  178. IO.writeLn("Ships with len 4 (2): " + statm2 + " sunk, " + n2
  179. + " remaining");
  180. IO.writeLn("Ships with len 5 (1): " + statm3 + " sunk, " + n3
  181. + " remaining");
  182. IO.writeLn(" Shots: " + schuesse);
  183. }
  184.  
  185. static int checkiftreffer(int a1, int a2) {
  186.  
  187. int a3 = wasser[a1][a2];
  188. return a3;
  189. }
  190.  
  191. static int[][] setshiponwater(int a1, int a2, int[][] wasser, int q) {
  192. int i = 0;
  193. while (i < a2) {
  194. int dirrandi = (int) (Math.random() * 4);
  195. int xrandi = (int) (Math.random() * 19);
  196. int yrandi = (int) (Math.random() * 19);
  197.  
  198. i++;
  199. for (int j = 0; j < a1; j++) {
  200.  
  201. switch (dirrandi) {
  202.  
  203. case 1:
  204. if ((yrandi + a1) > 19
  205. && checkship(wasser, yrandi, xrandi, a1, 1, 0) == 0) {
  206.  
  207. yrandi = (int) (Math.random() * 19);
  208. j = 0;
  209.  
  210. }
  211. wasser[xrandi][yrandi] = q;
  212.  
  213. yrandi++;
  214.  
  215. break;
  216. case 2:
  217. if ((xrandi + a1) > 19
  218. && checkship(wasser, yrandi, xrandi, a1, 2, 0) == 0) {
  219.  
  220. xrandi = (int) (Math.random() * 19);
  221. j = 0;
  222.  
  223. }
  224. wasser[xrandi][yrandi] = q;
  225.  
  226. xrandi++;
  227.  
  228. break;
  229. case 3:
  230. if ((yrandi - a1 < 0)
  231. && checkship(wasser, yrandi, xrandi, a1, 3, 0) == 0) {
  232.  
  233. yrandi = (int) (Math.random() * 19);
  234. j = 0;
  235.  
  236. }
  237. wasser[xrandi][yrandi] = q;
  238.  
  239. yrandi--;
  240.  
  241. break;
  242. case 4:
  243. if ((xrandi - a1 < 0)
  244. && checkship(wasser, yrandi, xrandi, a1, 4, 0) == 0) {
  245.  
  246. xrandi = (int) (Math.random() * 20);
  247. j = 0;
  248.  
  249. }
  250. wasser[xrandi][yrandi] = q;
  251.  
  252. xrandi--;
  253.  
  254. }
  255.  
  256. }
  257. }
  258.  
  259. return wasser;
  260. }
  261.  
  262. static int checkship(int[][] wasser, int yrandi, int xrandi, int a1, int b,
  263. int k) {
  264.  
  265. int yrandi1 = yrandi;
  266. int xrandi1 = xrandi;
  267. switch (b) {
  268.  
  269. case 1:
  270. if (yrandi1 + a1 < 20) {
  271. for (int j = 0; j < a1; j++) {
  272.  
  273. if (wasser[xrandi1][yrandi1] == 0) {
  274. yrandi1++;
  275. k = 0;
  276.  
  277. } else {
  278. k = 1;
  279. break;
  280. }
  281. }
  282. } else {
  283. k = 1;
  284. }
  285. break;
  286. case 2:
  287. if (xrandi1 + a1 < 20) {
  288. for (int j = 0; j < a1; j++) {
  289.  
  290. if (wasser[xrandi1][yrandi1] == 0) {
  291. xrandi1++;
  292. k = 0;
  293. } else {
  294. k = 1;
  295. break;
  296. }
  297. }
  298. } else {
  299. k = 1;
  300. }
  301. break;
  302. case 3:
  303. if (yrandi1 - a1 < 0) {
  304. for (int j = 0; j < a1; j++) {
  305.  
  306. if (wasser[xrandi1][yrandi1] == 0) {
  307. xrandi1--;
  308. k = 0;
  309. } else {
  310. k = 1;
  311. break;
  312. }
  313. }
  314. } else {
  315. k = 1;
  316. }
  317. break;
  318. case 4:
  319. if (xrandi1 - a1 < 0) {
  320. for (int j = 0; j < a1; j++) {
  321.  
  322. if (wasser[xrandi1][yrandi1] == 0) {
  323. xrandi1--;
  324. k = 0;
  325. } else {
  326. k = 1;
  327. break;
  328. }
  329. }
  330. } else {
  331. k = 1;
  332. }
  333. }
  334. if (k == 0) {
  335. return 0;
  336.  
  337. } else {
  338. return 1;
  339. }
  340. }
  341. }
Add Comment
Please, Sign In to add comment