Advertisement
Guest User

ohhoo

a guest
Nov 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.01 KB | None | 0 0
  1.  
  2. package pearlharbor;
  3. import javax.swing.JOptionPane;
  4. public class PearlHarbor
  5. {
  6. //variable de clase
  7. private int f;
  8. private int aviones;
  9. private int avionesjaponeses;
  10. private int barcos;
  11. private int contauAVx1 = 0;
  12. private int contauAVx2 = 0;
  13. private int contajAVx1 = 0;
  14. private int contajAVx2 = 0;
  15. private int contaBx1 = 0;
  16. private int contaSx1 = 0;
  17. private int contaSx2 = 0;
  18. private int disparos;
  19. private int x;
  20. private int y;
  21. private int submarinos;
  22. private char USA[][];
  23. private char japan[][];
  24. private int a1, a2, a3, a4, b1, b2, s1, s2;
  25. public static void main(String[]x)
  26. {
  27. PearlHarbor pearlHarbor = new PearlHarbor();
  28.  
  29. }
  30. public PearlHarbor()
  31. {
  32. this.todero();
  33.  
  34. }
  35. private void todero()
  36. {
  37. //Variable para la opcion del usuario
  38. int opcion;
  39. JOptionPane.showMessageDialog(null, "Bienvenido al juego: 'La batalla del Pearl Harbor'\n"+"¡Difrútalo!");
  40. do
  41. {
  42. //Listado y captura
  43.  
  44. opcion=Integer.parseInt(JOptionPane.showInputDialog("====OPCIONES==== \n"+
  45. "1. Configuración \n"+
  46. "2. Armamento \n"+
  47. "3. Simular Batalla \n"+
  48. "4. Ver informe \n"+
  49. "5. Resultados \n"+
  50. "0. SALIR \n"
  51. ));
  52.  
  53. switch(opcion)
  54. {
  55. case 1:
  56. this.configuracion();
  57. break;
  58. case 2:
  59. this.armamento();
  60. break;
  61. case 3:
  62. this.batalla();
  63. break;
  64. case 4:
  65. this.informe();
  66. break;
  67. case 5:
  68. this.resultado();
  69. break;
  70. case 0:
  71. JOptionPane.showMessageDialog(null,"Gracias, por participar");
  72. System.exit(0);
  73. break;
  74. default:
  75. JOptionPane.showMessageDialog(null,"ESA OPCIÓN NO EXISTE");
  76. break;
  77. }
  78. }
  79. while(opcion!=0);
  80.  
  81. }
  82. private void configuracion()
  83. {
  84.  
  85. do{
  86.  
  87. f=Integer.parseInt(JOptionPane.showInputDialog("Digite el número de filas"));
  88.  
  89. USA= new char[f][f];
  90. japan= new char[f][f];
  91.  
  92. if(f<5 || f>20){
  93. JOptionPane.showMessageDialog(null, "No puedo hacer este juego con estas dimensiones...");
  94. }
  95. else
  96. {
  97. System.out.print("Estados Unidos");
  98. for (int i = 0; i < f-7; i++) {
  99. System.out.print(" ");
  100. }
  101. System.out.print("\tJapón");
  102. System.out.println();
  103. for(int fila=0;fila<f;fila++)
  104. {
  105. for(int colu=0;colu<f;colu++)
  106. {
  107. USA[fila][colu]='*';
  108. japan[fila][colu]='*';
  109. }
  110.  
  111. }
  112.  
  113. for(int fila=0;fila<f;fila++)
  114. {
  115. for(int colu=0;colu<f;colu++)
  116. {
  117. System.out.print(USA[fila][colu]+" ");
  118. }
  119. System.out.print("\t");
  120.  
  121. for(int colu=0;colu<f;colu++)
  122. {
  123. System.out.print(japan[fila][colu]+" ");
  124. }
  125. System.out.println();
  126. }
  127. System.out.println();
  128.  
  129. }
  130.  
  131. }while (f<5 || f>20);
  132.  
  133. }
  134. private void armamento()
  135. {
  136. int opcion;
  137. do{
  138. opcion=Integer.parseInt(JOptionPane.showInputDialog("Elige que armamento configurar: \n"+
  139. "1. Aviones \n"+
  140. "2. Barcos \n"+
  141. "3. Submarinos \n"+
  142. "0. REGRESAR \n"
  143. ));
  144.  
  145. switch(opcion)
  146. {
  147. case 1:
  148. this.aviones();
  149. break;
  150. case 2:
  151. this.barcos();
  152. break;
  153. case 3:
  154. this.submarinos();
  155. break;
  156. case 0:
  157.  
  158. break;
  159. default:
  160. JOptionPane.showMessageDialog(null,"ESA OPCIÓN NO EXISTE");
  161. break;
  162. }
  163. }while(opcion!=0);
  164.  
  165. }
  166. private void aviones()
  167. {
  168. do{
  169.  
  170. aviones = Integer.parseInt(JOptionPane.showInputDialog("Cuantos aviones estadounidenses quiere..?"));
  171.  
  172. boolean ye=true;
  173.  
  174.  
  175. if(aviones>=2 && f<6){
  176. JOptionPane.showMessageDialog(null, "Son muchos aviones para esta matriz");
  177. }
  178. else{
  179. for(int i=0; i<aviones; i++)
  180. { //while
  181. a1 = (int)(int) (Math.random() * (f - 2)) + 1;
  182. a2 = (int)(int) (Math.random() * (f - 2)) + 1;
  183.  
  184. if(USA[a1][a2]=='*'&&USA[a1-1][a2]=='*'&&USA[a1+1][a2]=='*'&&USA[a1][a2-1]=='*'&&USA[a1][a2+1]=='*')
  185. {
  186. System.out.println(a1+" "+a2);
  187.  
  188. USA[a1][a2]= 'a';
  189. USA[a1-1][a2]= 'A';
  190. USA[a1+1][a2]= 'A';
  191. USA[a1][a2-1]= 'A';
  192. USA[a1][a2+1]= 'A';
  193. }
  194. else
  195. ye=false;
  196. }
  197. if(ye==false)
  198. {
  199. JOptionPane.showMessageDialog(null,"Lo siento, aleatoriamente no pude colocar todos los aviones estadounidenses.");
  200. }
  201.  
  202. for(int fila=0;fila<f;fila++)
  203. {
  204. for(int colu=0;colu<f;colu++)
  205. {
  206. System.out.print(USA[fila][colu]+" ");
  207. }
  208. System.out.print("\t");
  209. for(int colu=0;colu<f;colu++)
  210. {
  211. System.out.print(japan[fila][colu]+" ");
  212. }
  213. System.out.println();
  214.  
  215. }
  216. System.out.println();
  217. }
  218. }while (aviones>=2 && f<6);
  219.  
  220. do{
  221.  
  222. avionesjaponeses = Integer.parseInt(JOptionPane.showInputDialog("Cuantos aviones japoneses quiere..?"));
  223.  
  224. boolean av=true;
  225.  
  226. if(avionesjaponeses>=2 && f<6){
  227. JOptionPane.showMessageDialog(null, "Son muchos aviones para esta matriz");
  228. }
  229. else{
  230. for(int i=0; i<avionesjaponeses; i++)
  231. { //while
  232. a3 = (int)(Math.random()*(f-2)+1);
  233. a4 = (int)(Math.random()*(f-2)+1);
  234. if(japan[a3][a4]=='*'&&japan[a3-1][a4]=='*'&&japan[a3+1][a4]=='*'&&japan[a3][a4-1]=='*'&&japan[a3][a4+1]=='*')
  235. {
  236. System.out.println(a3+" "+a4);
  237.  
  238. japan[a3][a4]= 'a';
  239. japan[a3-1][a4]= 'A';
  240. japan[a3+1][a4]= 'A';
  241. japan[a3][a4-1]= 'A';
  242. japan[a3][a4+1]= 'A';
  243. }
  244. else
  245. av=false;
  246. }
  247. if(av==false)
  248. JOptionPane.showMessageDialog(null,"Lo siento, aleatoriamente no pude colocar todos los aviones japoneses.");
  249.  
  250. for(int fila=0;fila<f;fila++)
  251. {
  252. for(int colu=0;colu<f;colu++)
  253. {
  254. System.out.print(USA[fila][colu]+" ");
  255. }
  256. System.out.print("\t");
  257.  
  258. for(int colu=0;colu<f;colu++)
  259. {
  260. System.out.print(japan[fila][colu]+" ");
  261. }
  262. System.out.println();
  263. }
  264. System.out.println();
  265. }
  266. }while (avionesjaponeses>=2 && f<6);
  267.  
  268. }
  269. private void barcos()
  270. {
  271. do{
  272.  
  273. barcos = Integer.parseInt(JOptionPane.showInputDialog("Cuantos barcos estadounidenses quiere..?"));
  274. int si = 0;
  275.  
  276. if(barcos>=2 && f<6){
  277. JOptionPane.showMessageDialog(null, "Son muchos barcos para esta matriz");
  278. }
  279. else
  280. {
  281. for(int i=0; i<barcos; i++)
  282. { //while
  283. b1 = (int)(int) (Math.random() * (f-3)+1);
  284. b2 = (int)(int) (Math.random() * (f-3)+1);
  285. if(USA[b1][b2]=='*'&&USA[b1][b2+1]=='*'&&USA[b1][b2+2]=='*'&&USA[b1-1][b2]=='*'&&USA[b1-1][b2+1]=='*'&&USA[b1-1][b2+2]=='*')
  286. {
  287. //hay que validar para que no se repitan los aleatorios
  288. System.out.println(b1+" "+b2);
  289.  
  290. USA[b1][b2]= 'b';
  291. USA[b1][b2+1]= 'B';
  292. USA[b1][b2+2]= 'B';
  293. USA[b1+1][b2]= 'B';
  294. USA[b1+1][b2+1]= 'B';
  295. USA[b1+1][b2+2]= 'B';
  296. }
  297. else
  298. si=5;
  299.  
  300. }
  301.  
  302. if(si==5)
  303. {
  304. JOptionPane.showMessageDialog(null,"Lo siento, aleatoriamente no pude colocar todos los barcos");
  305. }
  306.  
  307.  
  308. for(int fila=0;fila<f;fila++)
  309. {
  310. for(int colu=0;colu<f;colu++)
  311. {
  312. System.out.print(USA[fila][colu]+" ");
  313. }
  314. System.out.print("\t");
  315. for(int colu=0;colu<f;colu++)
  316. {
  317. System.out.print(japan[fila][colu]+" ");
  318. }
  319. System.out.println();
  320.  
  321. }
  322. System.out.println();
  323. }
  324. }while(barcos>=2 && f<6);
  325.  
  326.  
  327. }
  328. private void submarinos()
  329. {
  330. do{
  331.  
  332. submarinos = Integer.parseInt(JOptionPane.showInputDialog("Cuantos submarinos estadounidenses quiere..?"));
  333.  
  334. boolean suw=true;
  335. if(submarinos>=3 && f<6){
  336. JOptionPane.showMessageDialog(null, "Son muchos submarinos para esta matriz");
  337. }else{
  338. for(int i=0; i<submarinos; i++)
  339. { //while
  340. s1 = (int)(Math.random()*((f-2)/2)+1);
  341. s2 = (int)(Math.random()*(f-2));
  342. if(USA[s1][s2]=='*'&&USA[s1][s2+1]=='*'&&USA[s1+1][s2]=='*'&&USA[s1-1][s2]=='*')
  343. {
  344. System.out.println(s1+" "+s2); //coloco esta linea para saber donde seran las posiciones
  345.  
  346. USA[s1][s2]= 's';
  347. USA[s1][s2+1]= 'S';
  348. USA[s1+1][s2]= 'S';
  349. USA[s1-1][s2]= 'S';
  350. }
  351. else
  352. suw=false;
  353. }
  354. if(suw==false)
  355. JOptionPane.showMessageDialog(null,"Lo siento, aleatoriamente no pude colocar todos los submarinos.");
  356. for(int fila=0;fila<f;fila++)
  357. {
  358. for(int colu=0;colu<f;colu++)
  359. {
  360. System.out.print(USA[fila][colu]+" ");
  361. }
  362. System.out.print("\t");
  363. for(int colu=0;colu<f;colu++)
  364. {
  365. System.out.print(japan[fila][colu]+" ");
  366. }
  367. System.out.println();
  368. }
  369.  
  370. System.out.println();
  371. }
  372. }while(submarinos>=2 && f<6);
  373. }
  374. private void batalla()
  375. {
  376.  
  377.  
  378. System.out.println();
  379. System.out.println("=====BATALLA=====");
  380.  
  381. disparos=(f*f);
  382.  
  383. for(int i=0;i<disparos;i++)
  384. {
  385.  
  386. x=(int)(Math.random()*f);
  387. y=(int)(Math.random()*f);
  388.  
  389. if(USA[x][y]=='A'&&USA[x][y]!='o')
  390. {
  391. USA[x][y]= 'x';
  392. contauAVx1=contauAVx1+40;
  393. }
  394. else
  395. {
  396. if(USA[x][y]=='a'&& USA[x][y]!='o')
  397. {
  398. USA[x][y]= 'X';
  399. contauAVx2=contauAVx2+200;
  400. }
  401. else
  402. {
  403. if(USA[x][y]=='*')
  404. {
  405. USA[x][y]= 'o';
  406. }
  407.  
  408. }
  409.  
  410. }
  411.  
  412. if(japan[x][y]=='A')
  413. {
  414. japan[x][y]='x';
  415. contajAVx1=contajAVx1+60;
  416. }
  417. else
  418. {
  419. if(japan[x][y]=='a')
  420. {
  421. japan[x][y]='X';
  422. contajAVx2=contajAVx2+300;
  423. }
  424. else
  425. {
  426. if(japan[x][y]=='*')
  427. {
  428. japan[x][y]='o';
  429. }
  430. }
  431.  
  432. }
  433.  
  434. //Para los barcos
  435. if(USA[x][y]=='B')
  436. {
  437. USA[x][y]= 'x';
  438. contaBx1=contaBx1+40;
  439. }
  440. else
  441. {
  442. if(USA[x][y]=='*'){
  443. USA[x][y]= 'o';
  444. }
  445.  
  446. }
  447. //Para los submarinos
  448. if(USA[x][y]=='S')
  449. {
  450. USA[x][y]= 'x';
  451. contaSx1=contaSx1+50;
  452. }
  453. else
  454. {
  455. if(USA[x][y]=='s')
  456. {
  457. USA[x][y]= 'X';
  458. contaSx2=contaSx2+200;
  459. }
  460. else
  461. {
  462. if(USA[x][y]=='*'){
  463. USA[x][y]= 'o';
  464. }
  465. }
  466.  
  467. }
  468.  
  469. }
  470.  
  471.  
  472. for(int fila=0;fila<f;fila++)
  473. {
  474. for(int colu=0;colu<f;colu++)
  475. {
  476. System.out.print(USA[fila][colu]+" ");
  477. }
  478. System.out.print("\t");
  479.  
  480. for(int colu=0;colu<f;colu++)
  481. {
  482. System.out.print(japan[fila][colu]+" ");
  483. }
  484. System.out.println();
  485. }
  486. System.out.println();
  487.  
  488.  
  489. }
  490. private void informe()
  491. {
  492. int avionuder=0,avionuaver=0,avionjder=0,avionjaver=0,barcohun=0,barcoaver=0,submahun=0,submaaver=0;
  493.  
  494. System.out.println();
  495. System.out.println("=====INFORME=====");
  496.  
  497. //Aviones
  498.  
  499. System.out.println("AVIONES");
  500. System.out.println("\t Estados Unidos ");
  501.  
  502. avionuder = aviones + 'X';
  503. for(int i=0; i<aviones; i++){
  504. USA[x][y] = 'a';
  505. System.out.println("\t Derribados: "+avionuder);
  506. }
  507. avionuaver = aviones + 'x';
  508. for(int i=0; i<aviones; i++){
  509. USA[x][y] = 'A';
  510. System.out.println("\t Averiados: "+avionuaver);
  511. }
  512. System.out.println();
  513.  
  514. System.out.println("\t Japón ");
  515.  
  516. avionjder = avionesjaponeses + 'X';
  517. for(int i=0; i<avionesjaponeses; i++){
  518. japan[x][y] = 'a';
  519. System.out.println("\t Derribados: "+avionjder);
  520. }
  521. avionjaver = avionesjaponeses + 'x';
  522. for(int i=0; i<avionesjaponeses; i++){
  523. japan[x][y] = 'X';
  524. System.out.println("\t Averiados: "+avionjaver);
  525. }
  526. System.out.println();
  527.  
  528. //Barcos
  529. System.out.println("BARCOS");
  530. barcohun = barcos + 'x';
  531. for(int i=0; i<barcos; i++){
  532. USA[x][y] = 'b';
  533. System.out.println("\t Hundidos: "+barcohun);
  534. }
  535. barcoaver = barcos + 'x';
  536. for(int i=0; i<barcos; i++){
  537. USA[x][y] = 'B';
  538. System.out.println("\t Averiados: "+barcoaver);
  539. }
  540. System.out.println();
  541.  
  542. //Submarinos
  543. System.out.println("Submarinos");
  544. submahun = submarinos + 'X';
  545. for(int i=0; i<submarinos; i++){
  546. USA[x][y] = 's';
  547. System.out.println("\t Hundidos: "+submahun);
  548. }
  549. submaaver = submarinos + 'x';
  550. for(int i=0; i<submarinos; i++){
  551. USA[x][y] = 'S';
  552. System.out.println("\t Averiados: "+submaaver);
  553. }
  554. System.out.println();
  555.  
  556. }
  557. private void resultado()
  558. {
  559. int puntosUSA,puntosJAPAN;
  560.  
  561. puntosUSA= contauAVx1+contauAVx2+contaBx1+contaSx1+contaSx2;
  562. puntosJAPAN=contajAVx1+contajAVx2;
  563.  
  564. System.out.println();
  565. System.out.println("=====RESULTADOS=====");
  566. System.out.println();
  567. System.out.println("Los puntos de Estados Unidos son: "+puntosJAPAN);
  568. System.out.println("Los puntos de Japón son: "+puntosUSA);
  569. System.out.println();
  570.  
  571. if(puntosUSA<puntosJAPAN)
  572. System.out.println("Estados Unidos ha ganado!!!!");
  573. else
  574. {
  575. if(puntosUSA>puntosJAPAN)
  576. System.out.println("Japón ha ganado!!!!");
  577. else
  578. System.out.println("¡Tenemos un empate!");
  579. }
  580.  
  581.  
  582.  
  583. }
  584.  
  585.  
  586. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement