Advertisement
Guest User

PearlHarbor

a guest
Nov 19th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.12 KB | None | 0 0
  1. package pearl.harbor;
  2. import javax.swing.JOptionPane;
  3. public class PearlHarbor
  4. {
  5. //variable de clase
  6. private int f;
  7. private char USA[][];
  8. char japan[][];
  9.  
  10. public static void main(String[]x)
  11. {
  12. PearlHarbor pearlHarbor = new PearlHarbor();
  13.  
  14. }
  15. public PearlHarbor()
  16. {
  17. this.todero();
  18.  
  19. }
  20. private void todero()
  21. {
  22. //Variable para la opcion del usuario
  23. int opcion;
  24. JOptionPane.showMessageDialog(null, "Bienvenido al juego: 'La batalla del Pearl Harbor'\n"+"¡Difrútalo!");
  25. do
  26. {
  27. //Listado y captura
  28.  
  29. opcion=Integer.parseInt(JOptionPane.showInputDialog("====OPCIONES==== \n"+
  30. "1. Configuración \n"+
  31. "2. Armamento \n"+
  32. "3. Simular Batalla \n"+
  33. "4. Ver informe \n"+
  34. "5. Resultados \n"+
  35. "0. SALIR \n"
  36. ));
  37.  
  38. switch(opcion)
  39. {
  40. case 1:
  41. this.configuracion();
  42. break;
  43. case 2:
  44. this.armamento();
  45. break;
  46. case 3:
  47. this.batalla();
  48. break;
  49. case 4:
  50. this.informe();
  51. break;
  52. case 5:
  53. this.resultado();
  54. break;
  55. case 0:
  56. JOptionPane.showMessageDialog(null,"Gracias, por participar");
  57. System.exit(0);
  58. break;
  59. default:
  60. JOptionPane.showMessageDialog(null,"ESA OPCIÓN NO EXISTE");
  61. break;
  62. }
  63. }
  64. while(opcion!=0);
  65.  
  66. }
  67. private void configuracion()
  68. {
  69.  
  70. do{
  71.  
  72. f=Integer.parseInt(JOptionPane.showInputDialog("Digite el número de filas"));
  73.  
  74. USA= new char[f][f];
  75. japan= new char[f][f];
  76.  
  77. if(f<5 || f>20){
  78. JOptionPane.showMessageDialog(null, "No puedo hacer este juego con estas dimensiones...");
  79. }
  80. else
  81. {
  82. System.out.print("Estados Unidos");
  83. for (int i = 0; i < f-7; i++) {
  84. System.out.print(" ");
  85. }
  86. System.out.print("\t \t Japón");
  87. System.out.println();
  88. for(int fila=0;fila<f;fila++)
  89. {
  90. for(int colu=0;colu<f;colu++)
  91. {
  92. USA[fila][colu]='*';
  93. japan[fila][colu]='*';
  94. }
  95.  
  96. }
  97.  
  98. for(int fila=0;fila<f;fila++)
  99. {
  100. for(int colu=0;colu<f;colu++)
  101. {
  102. System.out.print(USA[fila][colu]+" ");
  103. }
  104. System.out.print("\t");
  105.  
  106. for(int colu=0;colu<f;colu++)
  107. {
  108. System.out.print(japan[fila][colu]+" ");
  109. }
  110. System.out.println();
  111. }
  112. System.out.println();
  113.  
  114. }
  115.  
  116. }while (f<5 || f>20);
  117.  
  118. }
  119. private void armamento()
  120. {
  121. int opcion;
  122. do{
  123. opcion=Integer.parseInt(JOptionPane.showInputDialog("Elige que armamento configurar: \n"+
  124. "1. Aviones \n"+
  125. "2. Barcos \n"+
  126. "3. Submarinos \n"+
  127. "0. REGRESAR \n"
  128. ));
  129.  
  130. switch(opcion)
  131. {
  132. case 1:
  133. this.aviones();
  134. break;
  135. case 2:
  136. this.barcos();
  137. break;
  138. case 3:
  139. this.submarinos();
  140. break;
  141. case 0:
  142.  
  143. break;
  144. default:
  145. JOptionPane.showMessageDialog(null,"ESA OPCIÓN NO EXISTE");
  146. break;
  147. }
  148. }while(opcion!=0);
  149.  
  150. }
  151. private void aviones()
  152. {
  153. //AVIONES
  154.  
  155. int avionesu,avionesj,a, b,c,d;
  156.  
  157. avionesu = Integer.parseInt(JOptionPane.showInputDialog("Cuantos aviones estadounidenses quiere..?"));
  158. for(int i=0; i<avionesu; i++)
  159. {
  160.  
  161. do{
  162. a = (int)(Math.random()*(f-1)+1);
  163. b = (int)(Math.random()*(f-1)+1);
  164. //hay que validar para que no se repitan los aleatorios (Esto aun no lo he hecho)
  165. //que los aleatorios ademas esten en el rango donde pueda generar aviones (ya está hecho)
  166.  
  167. System.out.println(a+" "+b); //coloco esta linea para saber donde seran las posiciones
  168.  
  169. USA[a][b]= '1';
  170. USA[a-1][b]= '2';
  171. USA[a+1][b]= '3';
  172. USA[a][b-1]= '4';
  173. USA[a][b+1]= '5';
  174.  
  175. //los numeros al final los quitaré, son para guiarnos en donde aparecen.
  176.  
  177. }while(USA[a][b]=='*'&&USA[a-1][b]=='*'&&USA[a+1][b]=='*'&&USA[a][b-1]=='*'&&USA[a][b+1]=='*');
  178.  
  179. }
  180.  
  181. avionesj = Integer.parseInt(JOptionPane.showInputDialog("Cuantos aviones japoneses quiere..?"));
  182. for(int i=0; i<avionesj; i++)
  183. {
  184. do{
  185. c = (int)(Math.random()*(f-1)+1);
  186. d = (int)(Math.random()*(f-1)+1);
  187. //hay que validar para que no se repitan los aleatorios
  188. //que los aleatorios ademas esten en el rango donde pueda generar aviones
  189.  
  190. System.out.println(c+" "+d); //coloco esta linea para saber donde seran las posiciones
  191.  
  192. japan[c][d]= '1';
  193. japan[c-1][d]= '2';
  194. japan[c+1][d]= '3';
  195. japan[c][d-1]= '4';
  196. japan[c][d+1]= '5';
  197.  
  198. }while(japan[c][d]=='*'&&japan[c-1][d]=='*'&&japan[c+1][d]=='*'&&japan[c][d-1]=='*'&&japan[c][d+1]=='*');
  199.  
  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.  
  210. for(int colu=0;colu<f;colu++)
  211. {
  212. System.out.print(japan[fila][colu]+" ");
  213. }
  214. System.out.println();
  215. }
  216. System.out.println();
  217.  
  218.  
  219. }
  220. private void barcos()
  221. {
  222. //BARCOS
  223.  
  224. int barcos;
  225. barcos = Integer.parseInt(JOptionPane.showInputDialog("Cuantos barcos quiere..?"));
  226. int ab1, ab2;
  227.  
  228. for(int i=0; i<barcos; i++)
  229. {
  230.  
  231. do{
  232. ab1 = (int)(Math.random()*(f/2)); //variar esto para empezar a variar desde que punto salen los objetos
  233. ab2 = (int)(Math.random()*f);
  234.  
  235. //hay que validar para que no se repitan los aleatorios
  236.  
  237. System.out.println(ab1+" "+ab2);
  238.  
  239. USA[ab1][ab2]= '1';
  240. USA[ab1][ab2+1]= '2';
  241. USA[ab1][ab2+2]= '3';
  242. USA[ab1+1][ab2]= '4';
  243. USA[ab1+1][ab2+1]= '5';
  244. USA[ab1+1][ab2+2]= '6';
  245. }while(USA[ab1][ab2]=='*'&&USA[ab1][ab2+1]=='*'&&USA[ab1][ab2+2]=='*'&&USA[ab1-1][ab2]=='*'&&USA[ab1-1][ab2+1]=='*'&&USA[ab1-1][ab2+2]=='*');
  246.  
  247. }
  248.  
  249. for(int fila=0;fila<f;fila++)
  250. {
  251. for(int colu=0;colu<f;colu++)
  252. {
  253. System.out.print(USA[fila][colu]+" ");
  254. }
  255. System.out.print("\t");
  256.  
  257. for(int colu=0;colu<f;colu++)
  258. {
  259. System.out.print(japan[fila][colu]+" ");
  260. }
  261. System.out.println();
  262. }
  263. System.out.println();
  264.  
  265. }
  266. private void submarinos()
  267. {
  268. //SUBMARINOS
  269. int submarinos;
  270. submarinos = Integer.parseInt(JOptionPane.showInputDialog("Cuantos submarinos quiere..?"));
  271. int s1, s2;
  272. for(int i=0; i<submarinos; i++)
  273. {
  274.  
  275. do{
  276. s1 = (int)(Math.random()*(f-1)+1);
  277. s2 = (int)(Math.random()*(f-1));
  278. //hay que validar para que no se repitan los aleatorios
  279. //que los aleatorios ademas esten en el rango donde pueda generar aviones
  280.  
  281. System.out.println(s1+" "+s2); //coloco esta linea para saber donde seran las posiciones
  282.  
  283. USA[s1][s2]= '1';
  284. USA[s1][s2+1]= '2';
  285. USA[s1+1][s2]= '3';
  286. USA[s1-1][s2]= '4';
  287.  
  288. }while(USA[s1][s2]=='*'&&USA[s1][s2+1]=='*'&&USA[s1+1][s2]=='*'&&USA[s1-1][s2]=='*');
  289.  
  290. }
  291. for(int fila=0;fila<f;fila++)
  292. {
  293. for(int colu=0;colu<f;colu++)
  294. {
  295. System.out.print(USA[fila][colu]+" ");
  296. }
  297. System.out.print("\t");
  298.  
  299. for(int colu=0;colu<f;colu++)
  300. {
  301. System.out.print(japan[fila][colu]+" ");
  302. }
  303. System.out.println();
  304. }
  305. System.out.println();
  306.  
  307. }
  308. private void batalla()
  309. {
  310. int disparos,x,y;
  311.  
  312. /*El numero de disparos varia de acuerdo al tamaño de la matriz
  313. para matriz 5x5=25
  314. matriz 6x6=30
  315. 7x7=35
  316.  
  317. va a ir aumentando de 5 en 5
  318. */
  319.  
  320. disparos=f*5;
  321.  
  322. for(int i=0;i<disparos;i++)
  323. {
  324. x=(int)(Math.random()*f);
  325. y=(int)(Math.random()*f);
  326.  
  327. //falta validar para que no se repitan los numeros aleatorios.
  328.  
  329. if(USA[x][y]=='2'||USA[x][y]=='3'||USA[x][y]=='4'||USA[x][y]=='5')
  330. {
  331. USA[x][y]= 'x';
  332. }
  333. else
  334. {
  335. if(USA[x][y]=='1')
  336. {
  337. USA[x][y]= 'X';
  338. }
  339. else
  340. {
  341. USA[x][y]= 'o';
  342. }
  343.  
  344. }
  345.  
  346. if(japan[x][y]=='2'||japan[x][y]=='3'||japan[x][y]=='4'||japan[x][y]=='5')
  347. {
  348. japan[x][y]='x';
  349. }
  350. else
  351. {
  352. if(japan[x][y]=='1')
  353. {
  354. japan[x][y]='X';
  355. }
  356. else
  357. japan[x][y]='o';
  358. }
  359.  
  360. }
  361.  
  362. for(int fila=0;fila<f;fila++)
  363. {
  364. for(int colu=0;colu<f;colu++)
  365. {
  366. System.out.print(USA[fila][colu]+" ");
  367. }
  368. System.out.print("\t");
  369.  
  370. for(int colu=0;colu<f;colu++)
  371. {
  372. System.out.print(japan[fila][colu]+" ");
  373. }
  374. System.out.println();
  375. }
  376. System.out.println();
  377.  
  378.  
  379. }
  380. private void informe()
  381. {
  382.  
  383. }
  384. private void resultado()
  385. {
  386.  
  387. }
  388.  
  389.  
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement