Advertisement
Guest User

ddba

a guest
Nov 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.36 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. JOptionPane.showMessageDialog(null,"Querido usuario, se recomienda hacer una matriz de más de 10x10 para ver más acción..!!");
  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, no pude colocar todos los aviones estadounidenses\n"
  200. + "Le va a tocar repetir cuanto armamento quiere para satisfacerlo");
  201.  
  202.  
  203. }
  204.  
  205. for(int fila=0;fila<f;fila++)
  206. {
  207. for(int colu=0;colu<f;colu++)
  208. {
  209. System.out.print(USA[fila][colu]+" ");
  210. }
  211. System.out.print("\t");
  212. for(int colu=0;colu<f;colu++)
  213. {
  214. System.out.print(japan[fila][colu]+" ");
  215. }
  216. System.out.println();
  217.  
  218. }
  219. System.out.println();
  220. }
  221. }while (aviones>=2 && f<6);
  222.  
  223. do{
  224.  
  225. avionesjaponeses = Integer.parseInt(JOptionPane.showInputDialog("Cuantos aviones japoneses quiere..?"));
  226.  
  227. boolean av=true;
  228.  
  229. if(avionesjaponeses>=2 && f<6){
  230. JOptionPane.showMessageDialog(null, "Son muchos aviones para esta matriz");
  231. }
  232. else{
  233. for(int i=0; i<avionesjaponeses; i++)
  234. { //while
  235. a3 = (int)(Math.random()*(f-2)+1);
  236. a4 = (int)(Math.random()*(f-2)+1);
  237. if(japan[a3][a4]=='*'&&japan[a3-1][a4]=='*'&&japan[a3+1][a4]=='*'&&japan[a3][a4-1]=='*'&&japan[a3][a4+1]=='*')
  238. {
  239. System.out.println(a3+" "+a4);
  240.  
  241. japan[a3][a4]= 'a';
  242. japan[a3-1][a4]= 'A';
  243. japan[a3+1][a4]= 'A';
  244. japan[a3][a4-1]= 'A';
  245. japan[a3][a4+1]= 'A';
  246. }
  247. else
  248. av=false;
  249. }
  250. if(av==false)
  251. JOptionPane.showMessageDialog(null,"Lo siento, no pude colocar todos los aviones japoneses\n"
  252. + "Le va a tocar repetir cuanto armamento quiere para satisfacerlo");
  253.  
  254.  
  255. for(int fila=0;fila<f;fila++)
  256. {
  257. for(int colu=0;colu<f;colu++)
  258. {
  259. System.out.print(USA[fila][colu]+" ");
  260. }
  261. System.out.print("\t");
  262.  
  263. for(int colu=0;colu<f;colu++)
  264. {
  265. System.out.print(japan[fila][colu]+" ");
  266. }
  267. System.out.println();
  268. }
  269. System.out.println();
  270. }
  271. }while (avionesjaponeses>=2 && f<6);
  272.  
  273. }
  274. private void barcos()
  275. {
  276. do{
  277.  
  278. barcos = Integer.parseInt(JOptionPane.showInputDialog("Cuantos barcos estadounidenses quiere..?"));
  279. boolean bar = true;
  280.  
  281. if(barcos>=2 && f<6){
  282. JOptionPane.showMessageDialog(null, "Son muchos barcos para esta matriz");
  283. }
  284. else
  285. {
  286. for(int i=0; i<barcos; i++)
  287. { //while
  288. b1 = (int)(Math.random()*((f-3)/2)+1);
  289. b2 = (int)(Math.random()*(f-3)+1);
  290. 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]=='*')
  291. {
  292. //hay que validar para que no se repitan los aleatorios
  293. System.out.println(b1+" "+b2);
  294.  
  295. USA[b1][b2]= 'B';
  296. USA[b1][b2+1]= 'B';
  297. USA[b1][b2+2]= 'B';
  298. USA[b1+1][b2]= 'B';
  299. USA[b1+1][b2+1]= 'B';
  300. USA[b1+1][b2+2]= 'B';
  301. }
  302. else
  303. bar=false;
  304.  
  305. }
  306.  
  307. if(bar==false)
  308. {
  309. JOptionPane.showMessageDialog(null,"Lo siento, no pude colocar todos los barcos\n"
  310. + "Le va a tocar repetir cuanto armamento quiere para satisfacerlo");
  311.  
  312. }
  313.  
  314.  
  315. for(int fila=0;fila<f;fila++)
  316. {
  317. for(int colu=0;colu<f;colu++)
  318. {
  319. System.out.print(USA[fila][colu]+" ");
  320. }
  321. System.out.print("\t");
  322. for(int colu=0;colu<f;colu++)
  323. {
  324. System.out.print(japan[fila][colu]+" ");
  325. }
  326. System.out.println();
  327.  
  328. }
  329. System.out.println();
  330. }
  331. }while(barcos>=2 && f<6);
  332.  
  333.  
  334. }
  335. private void submarinos()
  336. {
  337. do{
  338.  
  339. submarinos = Integer.parseInt(JOptionPane.showInputDialog("Cuantos submarinos estadounidenses quiere..?"));
  340.  
  341. boolean suw=true;
  342. if(submarinos>=3 && f<6){
  343. JOptionPane.showMessageDialog(null, "Son muchos submarinos para esta matriz");
  344. }else{
  345. for(int i=0; i<submarinos; i++)
  346. { //while
  347. s1 = (int)(Math.random()*((f-2)/2)+1);
  348. s2 = (int)(Math.random()*(f-2));
  349. if(USA[s1][s2]=='*'&&USA[s1][s2+1]=='*'&&USA[s1+1][s2]=='*'&&USA[s1-1][s2]=='*')
  350. {
  351. System.out.println(s1+" "+s2); //coloco esta linea para saber donde seran las posiciones
  352.  
  353. USA[s1][s2]= 's';
  354. USA[s1][s2+1]= 'S';
  355. USA[s1+1][s2]= 'S';
  356. USA[s1-1][s2]= 'S';
  357. }
  358. else
  359. suw=false;
  360. }
  361. if(suw==false)
  362. JOptionPane.showMessageDialog(null,"Lo siento, no pude colocar todos los submarinos\n"
  363. + "Le va a tocar repetir cuanto armamento quiere para satisfacerlo");
  364.  
  365. for(int fila=0;fila<f;fila++)
  366. {
  367. for(int colu=0;colu<f;colu++)
  368. {
  369. System.out.print(USA[fila][colu]+" ");
  370. }
  371. System.out.print("\t");
  372. for(int colu=0;colu<f;colu++)
  373. {
  374. System.out.print(japan[fila][colu]+" ");
  375. }
  376. System.out.println();
  377. }
  378.  
  379. System.out.println();
  380. }
  381. }while(submarinos>=2 && f<6);
  382. }
  383. private void batalla()
  384. {
  385. JOptionPane.showMessageDialog(null,"Se han presentado los siguientes ataques, dele aceptar para verlos!!");
  386.  
  387. System.out.println();
  388. System.out.println("=====BATALLA=====");
  389.  
  390. disparos=(f*5);
  391.  
  392. for(int i=0;i<disparos;i++)
  393. {
  394.  
  395. x=(int)(Math.random()*f);
  396. y=(int)(Math.random()*f);
  397.  
  398. if(USA[x][y]=='A'&&USA[x][y]!='o')
  399. {
  400. USA[x][y]= 'x';
  401. contauAVx1=contauAVx1+40;
  402. }
  403. else
  404. {
  405. if(USA[x][y]=='a'&& USA[x][y]!='o')
  406. {
  407. USA[x][y]= 'X';
  408. contauAVx2=contauAVx2+200;
  409. }
  410. else
  411. {
  412. if(USA[x][y]=='*')
  413. {
  414. USA[x][y]= 'o';
  415. }
  416.  
  417. }
  418.  
  419. }
  420.  
  421. if(japan[x][y]=='A')
  422. {
  423. japan[x][y]='x';
  424. contajAVx1=contajAVx1+60;
  425. }
  426. else
  427. {
  428. if(japan[x][y]=='a')
  429. {
  430. japan[x][y]='X';
  431. contajAVx2=contajAVx2+300;
  432. }
  433. else
  434. {
  435. if(japan[x][y]=='*')
  436. {
  437. japan[x][y]='o';
  438. }
  439. }
  440.  
  441. }
  442.  
  443. //Para los barcos
  444. if(USA[x][y]=='B')
  445. {
  446. USA[x][y]= 'x';
  447. contaBx1=contaBx1+40;
  448. }
  449. else
  450. {
  451. if(USA[x][y]=='*'){
  452. USA[x][y]= 'o';
  453. }
  454.  
  455. }
  456. //Para los submarinos
  457. if(USA[x][y]=='S')
  458. {
  459. USA[x][y]= 'x';
  460. contaSx1=contaSx1+50;
  461. }
  462. else
  463. {
  464. if(USA[x][y]=='s')
  465. {
  466. USA[x][y]= 'X';
  467. contaSx2=contaSx2+200;
  468. }
  469. else
  470. {
  471. if(USA[x][y]=='*'){
  472. USA[x][y]= 'o';
  473. }
  474. }
  475.  
  476. }
  477.  
  478. }
  479.  
  480. for(int fila=0;fila<f;fila++)
  481. {
  482. for(int colu=0;colu<f;colu++)
  483. {
  484. System.out.print(USA[fila][colu]+" ");
  485. }
  486. System.out.print("\t");
  487.  
  488. for(int colu=0;colu<f;colu++)
  489. {
  490. System.out.print(japan[fila][colu]+" ");
  491. }
  492. System.out.println();
  493. }
  494. System.out.println();
  495.  
  496.  
  497. }
  498. private void informe()
  499. {
  500. int avionuder=0,avionuaver=0,avionjder=0,avionjaver=0,barcohun=0,barcoaver=0,submahun=0,submaaver=0;
  501.  
  502. System.out.println();
  503. System.out.println("=====INFORME=====");
  504.  
  505. //Aviones
  506.  
  507. System.out.println("AVIONES");
  508. System.out.println("\t Estados Unidos ");
  509.  
  510. for(int i=0; i<1; i++){
  511.  
  512. System.out.println("\t Derribados: "+avionuder);
  513. }
  514. for(int i=0; i<1; i++){
  515.  
  516.  
  517. System.out.println("\t Averiados: "+avionuaver);
  518. }
  519. System.out.println();
  520.  
  521. System.out.println("\t Japón ");
  522.  
  523. avionjder = avionesjaponeses;
  524. for(int i=0; i<1; i++){
  525. japan[x][y] = 'a';
  526. System.out.println("\t Derribados: "+avionjder);
  527. }
  528. avionjaver = avionesjaponeses + 'x';
  529. for(int i=0; i<1; i++){
  530. japan[x][y] = 'X';
  531. System.out.println("\t Averiados: "+avionjaver);
  532. }
  533. System.out.println();
  534.  
  535. //Barcos
  536. System.out.println("BARCOS");
  537. barcohun = barcos + 'x';
  538. for(int i=0; i<1; i++){
  539. USA[x][y] = 'b';
  540. System.out.println("\t Hundidos: "+barcohun);
  541. }
  542. barcoaver = barcos + 'x';
  543. for(int i=0; i<1; i++){
  544. USA[x][y] = 'B';
  545. System.out.println("\t Averiados: "+barcoaver);
  546. }
  547. System.out.println();
  548.  
  549. //Submarinos
  550. System.out.println("Submarinos");
  551.  
  552. for(int i=0; i<1; i++){
  553. USA[x][y] = 's';
  554. System.out.println("\t Hundidos: "+submahun);
  555. }
  556.  
  557. for(int i=0; i<1; i++){
  558. USA[x][y] = 'S';
  559. System.out.println("\t Averiados: "+submaaver);
  560. }
  561. System.out.println();
  562.  
  563. }
  564. private void resultado()
  565. {
  566. int puntosUSA,puntosJAPAN;
  567.  
  568. puntosUSA= contauAVx1+contauAVx2+contaBx1+contaSx1+contaSx2;
  569. puntosJAPAN=contajAVx1+contajAVx2;
  570.  
  571. System.out.println();
  572. System.out.println("=====RESULTADOS=====");
  573. System.out.println();
  574. System.out.println("Los puntos de Estados Unidos son: "+puntosJAPAN);
  575. System.out.println("Los puntos de Japón son: "+puntosUSA);
  576. System.out.println();
  577.  
  578. if(puntosUSA<puntosJAPAN)
  579. System.out.println("Estados Unidos ha ganado!!!!");
  580. else
  581. {
  582. if(puntosUSA>puntosJAPAN)
  583. System.out.println("Japón ha ganado!!!!");
  584. else
  585. System.out.println("¡Tenemos un empate!");
  586. }
  587.  
  588.  
  589.  
  590. }
  591.  
  592.  
  593. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement