thieumao

Game xếp số đơn giản 2x2

May 9th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1. import java.awt.Component;
  2. import java.awt.GridBagConstraints;
  3. import java.awt.GridBagLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.WindowAdapter;
  7. import java.awt.event.WindowEvent;
  8. import java.util.Random;
  9.  
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JOptionPane;
  13.  
  14. public class XepSo22 extends JFrame implements ActionListener {
  15.     private GridBagLayout gb;
  16.     private GridBagConstraints gbc;
  17.     private JButton[][] btMang;
  18.     private String strTG;
  19.     private int move = 0;
  20.  
  21.     public static void main(String[] args) {
  22.         XepSo22 fr = new XepSo22("Xep So 22");
  23.         fr.setSize(300, 200);
  24.         fr.setVisible(true);
  25.     }
  26.  
  27.     public XepSo22(String title) {
  28.         super(title);
  29.         this.addWindowListener(new WindowAdapter() {
  30.             public void windowClosing(WindowEvent we) {
  31.                 System.exit(0);
  32.             }
  33.         });
  34.         gb = new GridBagLayout();
  35.         this.setLayout(gb);
  36.         btMang = new JButton[2][2];
  37.         btMang[0][0] = new JButton();
  38.         btMang[0][1] = new JButton();
  39.         btMang[1][0] = new JButton();
  40.         btMang[1][1] = new JButton();
  41.  
  42.         GanGiaTri();
  43.  
  44.         Them(btMang[0][0], 0, 0, 1, 1);
  45.         Them(btMang[0][1], 1, 0, 1, 1);
  46.         Them(btMang[1][0], 0, 1, 1, 1);
  47.         Them(btMang[1][1], 1, 1, 1, 1);
  48.         btMang[0][0].addActionListener(this);
  49.         btMang[0][1].addActionListener(this);
  50.         btMang[1][0].addActionListener(this);
  51.         btMang[1][1].addActionListener(this);
  52.         if (Win() == true) {
  53.             JOptionPane.showMessageDialog(null, "Win roi ma");
  54.             System.exit(0);
  55.         }
  56.     }
  57.  
  58.     public void GanGiaTri() {
  59.         Random random = new Random();
  60.         int[] mang = new int[4];
  61.         int r1 = 0, r2 = 0, rtg;
  62.         for (int i = 0; i < 4; i++) {
  63.             mang[i] = i;
  64.         }
  65.         for (int i = 0; i < 9; i++) {
  66.             random = new Random();
  67.             r1 = random.nextInt(4);
  68.             r2 = random.nextInt(4);
  69.             if (r1 != r2) {
  70.                 rtg = mang[r1];
  71.                 mang[r1] = mang[r2];
  72.                 mang[r2] = rtg;
  73.             }
  74.         }
  75.         btMang[0][0].setText(String.valueOf(mang[0]));
  76.         btMang[0][1].setText(String.valueOf(mang[1]));
  77.         btMang[1][0].setText(String.valueOf(mang[2]));
  78.         btMang[1][1].setText(String.valueOf(mang[3]));
  79.     }
  80.  
  81.     public void Them(Component comp, int x, int y, int w, int h) {
  82.         gbc = new GridBagConstraints();
  83.         gbc.gridx = x;
  84.         gbc.gridy = y;
  85.         gbc.gridwidth = w;
  86.         gbc.gridheight = h;
  87.         gb.setConstraints(comp, gbc);
  88.         this.add(comp);
  89.     }
  90.  
  91.     public void Doi(JButton btGiua1, JButton btGiua2) {
  92.         strTG = btGiua1.getText();
  93.         btGiua1.setText(btGiua2.getText());
  94.         btGiua2.setText(strTG);
  95.     }
  96.  
  97.     public boolean Win() {
  98.         Boolean check = true;
  99.         if (!btMang[0][0].getText().equals("0")
  100.                 || !btMang[0][1].getText().equals("1")
  101.                 || !btMang[1][0].getText().equals("2")
  102.                 || !btMang[1][1].getText().equals("3")) {
  103.             check = false;
  104.         }
  105.         return check;
  106.     }
  107.  
  108.     @Override
  109.     public void actionPerformed(ActionEvent e) {
  110.         int vti = 0, vtj = 0;
  111.         for (int i = 0; i < 2; i++) {
  112.             for (int j = 0; j < 2; j++) {
  113.                 if (btMang[i][j].getText().equals("0")) {
  114.                     vti = i;
  115.                     vtj = j;
  116.                 }
  117.             }
  118.         }
  119.  
  120.         if (vti > 0) {
  121.             if (e.getSource() == btMang[vti - 1][vtj]) {
  122.                 Doi(btMang[vti - 1][vtj], btMang[vti][vtj]);
  123.             }
  124.         }
  125.  
  126.         if (vti < 1) {
  127.             if (e.getSource() == btMang[vti + 1][vtj]) {
  128.                 Doi(btMang[vti + 1][vtj], btMang[vti][vtj]);
  129.             }
  130.         }
  131.  
  132.         if (vtj > 0) {
  133.             if (e.getSource() == btMang[vti][vtj - 1]) {
  134.                 Doi(btMang[vti][vtj - 1], btMang[vti][vtj]);
  135.             }
  136.         }
  137.  
  138.         if (vtj < 1) {
  139.             if (e.getSource() == btMang[vti][vtj + 1]) {
  140.                 Doi(btMang[vti][vtj + 1], btMang[vti][vtj]);
  141.             }
  142.         }
  143.  
  144.         if (Win() == true) {
  145.             JOptionPane.showMessageDialog(null, "Win roi ma");
  146.             System.exit(0);
  147.         }
  148.         if (++move == 10) {
  149.             JOptionPane.showMessageDialog(null, "Thua roi nhe");
  150.             System.exit(0);
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment