Oceano Azul
Enviado por manuellinux64 • 20 de Mayo de 2014 • 3.689 Palabras (15 Páginas) • 216 Visitas
Para desarrollar nuestro juego vamos a crear dos clases, una llamada Game que extiende de un JFrame y es la que contendra los botones del juego. La otra clase se llamara Robot y esta clase contendra todos los metodos que realizara que realizara la computadora, es decir sera el usuario PC.
A continuacion explicare un poco los metodos de las clases:
Clase Game
view plainprint?
1. import java.awt.BorderLayout;
2. import java.awt.Dimension;
3. import java.awt.FlowLayout;
4. import java.awt.Font;
5. import java.awt.GridBagConstraints;
6. import java.awt.GridBagLayout;
7. import java.awt.Insets;
8. import java.awt.Toolkit;
9. import java.awt.event.ActionEvent;
10. import java.awt.event.ActionListener;
11. import javax.swing.ButtonGroup;
12. import javax.swing.JButton;
13. import javax.swing.JFrame;
14. import javax.swing.JOptionPane;
15. import javax.swing.JPanel;
16. import javax.swing.JRadioButton;
17.
18. /**
19. *
20. * @author jzavaleta
21. */
22. public class Game extends JFrame {
23.
24. private JButton[] buttonGrid = new JButton[9];
25. private JRadioButton radioButtonHuman = new JRadioButton("Human VS Human", false);
26. private JRadioButton radioButtonRobot = new JRadioButton("Human VS Robot", true);
27. private ButtonGroup buttonGroupOptions = new ButtonGroup();
28. private JPanel optionsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
29. private JPanel gamePanel = new JPanel(new GridBagLayout());
30. private String player = "X";
31. private boolean robotActivado = true;
32. private Robot robot = new Robot();
33. private int moves = 0;
34.
35. public Game() {
36. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
37. getContentPane().setLayout(new BorderLayout());
38.
39. getContentPane().add(optionsPanel, BorderLayout.NORTH);
40. buttonGroupOptions.add(radioButtonHuman);
41. buttonGroupOptions.add(radioButtonRobot);
42. optionsPanel.add(radioButtonHuman);
43. optionsPanel.add(radioButtonRobot);
44. setListeners();
45.
46. getContentPane().add(gamePanel, BorderLayout.CENTER);
47. setButtons();
48.
49. pack();
50. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
51. int posX = (screenSize.width - getWidth()) / 2;
52. int posY = (screenSize.height - getHeight()) / 2;
53. setLocation(posX, posY);
54. System.out.println("THE GAME HAS BEGIN!!!");
55. System.out.println("Human VS Robot");
56. }
57.
58. private void reset() {
59. for (JButton b : buttonGrid) {
60. b.setEnabled(true);
61. b.setText("");
62. }
63. player = "X";
64. robot = new Robot();
65. moves = 0;
66. System.out.println("THE GAME HAS BEGIN AGAIN!!!");
67. if (robotActivado) {
68. System.out.println("Human VS Robot");
69. } else {
70. System.out.println("Human VS Human");
71. }
72. }
73.
74. private void setListeners() {
75. radioButtonHuman.addActionListener(new ActionListener() {
76.
77. public void actionPerformed(ActionEvent e) {
78. if (robotActivado) {
79. int resp = JOptionPane.showConfirmDialog(null, "El juego se va a reiniciar\n¿Desea continuar?", "", JOptionPane.OK_CANCEL_OPTION);
80. if (resp == JOptionPane.OK_OPTION) {
81. robotActivado = false;
82. reset();
83. } else {
84. radioButtonHuman.setSelected(false);
85. radioButtonRobot.setSelected(true);
86. }
87. }
88. }
89. });
90. radioButtonRobot.addActionListener(new ActionListener() {
91.
92. public void actionPerformed(ActionEvent e) {
93. if (!robotActivado) {
94. int resp = JOptionPane.showConfirmDialog(null, "El juego se va a reiniciar\n¿Desea continuar?", "", JOptionPane.OK_CANCEL_OPTION);
95. if (resp == JOptionPane.OK_OPTION) {
96. robotActivado = true;
97. reset();
98. } else {
99. radioButtonHuman.setSelected(true);
100. radioButtonRobot.setSelected(false);
101. }
102. }
103. }
104. });
105. }
106.
107. private void setButtons() {
108. int index = 0;
109. for (int row = 0; row < 3; row++) {
110. for (int col = 0; col < 3; col++) {
111. GridBagConstraints gbc = new GridBagConstraints();
112. gbc.fill = GridBagConstraints.NONE;
113. gbc.gridheight = 1;
114. gbc.gridwidth = 1;
115. gbc.gridx = col;
116. gbc.gridy = row;
117. gbc.insets = new Insets(2, 2, 2, 2);
118. gbc.ipadx = 0;
119. gbc.ipady = 0;
120. buttonGrid[index] = new JButton();
121. buttonGrid[index].setPreferredSize(new Dimension(40, 40));
122. buttonGrid[index].setFont(new Font("Calibri", Font.BOLD, 10));
123. buttonGrid[index].addActionListener(buttonListener(index));
124.
...