Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Component;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.util.Random;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- public class XepSo22 extends JFrame implements ActionListener {
- private GridBagLayout gb;
- private GridBagConstraints gbc;
- private JButton[][] btMang;
- private String strTG;
- private int move = 0;
- public static void main(String[] args) {
- XepSo22 fr = new XepSo22("Xep So 22");
- fr.setSize(300, 200);
- fr.setVisible(true);
- }
- public XepSo22(String title) {
- super(title);
- this.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent we) {
- System.exit(0);
- }
- });
- gb = new GridBagLayout();
- this.setLayout(gb);
- btMang = new JButton[2][2];
- btMang[0][0] = new JButton();
- btMang[0][1] = new JButton();
- btMang[1][0] = new JButton();
- btMang[1][1] = new JButton();
- GanGiaTri();
- Them(btMang[0][0], 0, 0, 1, 1);
- Them(btMang[0][1], 1, 0, 1, 1);
- Them(btMang[1][0], 0, 1, 1, 1);
- Them(btMang[1][1], 1, 1, 1, 1);
- btMang[0][0].addActionListener(this);
- btMang[0][1].addActionListener(this);
- btMang[1][0].addActionListener(this);
- btMang[1][1].addActionListener(this);
- if (Win() == true) {
- JOptionPane.showMessageDialog(null, "Win roi ma");
- System.exit(0);
- }
- }
- public void GanGiaTri() {
- Random random = new Random();
- int[] mang = new int[4];
- int r1 = 0, r2 = 0, rtg;
- for (int i = 0; i < 4; i++) {
- mang[i] = i;
- }
- for (int i = 0; i < 9; i++) {
- random = new Random();
- r1 = random.nextInt(4);
- r2 = random.nextInt(4);
- if (r1 != r2) {
- rtg = mang[r1];
- mang[r1] = mang[r2];
- mang[r2] = rtg;
- }
- }
- btMang[0][0].setText(String.valueOf(mang[0]));
- btMang[0][1].setText(String.valueOf(mang[1]));
- btMang[1][0].setText(String.valueOf(mang[2]));
- btMang[1][1].setText(String.valueOf(mang[3]));
- }
- public void Them(Component comp, int x, int y, int w, int h) {
- gbc = new GridBagConstraints();
- gbc.gridx = x;
- gbc.gridy = y;
- gbc.gridwidth = w;
- gbc.gridheight = h;
- gb.setConstraints(comp, gbc);
- this.add(comp);
- }
- public void Doi(JButton btGiua1, JButton btGiua2) {
- strTG = btGiua1.getText();
- btGiua1.setText(btGiua2.getText());
- btGiua2.setText(strTG);
- }
- public boolean Win() {
- Boolean check = true;
- if (!btMang[0][0].getText().equals("0")
- || !btMang[0][1].getText().equals("1")
- || !btMang[1][0].getText().equals("2")
- || !btMang[1][1].getText().equals("3")) {
- check = false;
- }
- return check;
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- int vti = 0, vtj = 0;
- for (int i = 0; i < 2; i++) {
- for (int j = 0; j < 2; j++) {
- if (btMang[i][j].getText().equals("0")) {
- vti = i;
- vtj = j;
- }
- }
- }
- if (vti > 0) {
- if (e.getSource() == btMang[vti - 1][vtj]) {
- Doi(btMang[vti - 1][vtj], btMang[vti][vtj]);
- }
- }
- if (vti < 1) {
- if (e.getSource() == btMang[vti + 1][vtj]) {
- Doi(btMang[vti + 1][vtj], btMang[vti][vtj]);
- }
- }
- if (vtj > 0) {
- if (e.getSource() == btMang[vti][vtj - 1]) {
- Doi(btMang[vti][vtj - 1], btMang[vti][vtj]);
- }
- }
- if (vtj < 1) {
- if (e.getSource() == btMang[vti][vtj + 1]) {
- Doi(btMang[vti][vtj + 1], btMang[vti][vtj]);
- }
- }
- if (Win() == true) {
- JOptionPane.showMessageDialog(null, "Win roi ma");
- System.exit(0);
- }
- if (++move == 10) {
- JOptionPane.showMessageDialog(null, "Thua roi nhe");
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment