naufalphew

Miskot

Jan 11th, 2021
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.93 KB | None | 0 0
  1. package gim_1;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.GridLayout;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.util.Random;
  9.  
  10. import javax.swing.BorderFactory;
  11. import javax.swing.ImageIcon;
  12. import javax.swing.JButton;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JPanel;
  16.  
  17. public class Miskot implements ActionListener{
  18.    
  19.     JFrame fr;
  20.     JPanel jp;
  21.     JButton[][] button;
  22.     JLabel [][] label;
  23.     int baris,kolom;
  24.     int [][] grid;
  25.     int [] cek;
  26.    
  27.     public Miskot() {
  28.        
  29.         baris = 4;
  30.         kolom = 4;
  31.         grid = new int [baris][kolom];
  32.         cek = new int [16];
  33.         init();
  34.     }
  35.     public void init() {
  36.        
  37.         fr = new JFrame();
  38.         jp = new JPanel();
  39.        
  40.         jp.setLayout(new GridLayout(4,4));
  41.         button = new JButton[baris][kolom];
  42.         label  = new JLabel[baris][kolom];
  43.         do {
  44.             acak();
  45. //          System.out.print("tes");
  46.         }while(!isSolvable());
  47.        
  48.         for(int i=0;i<baris;i++)
  49.             for(int j=0;j<kolom;j++) {
  50.                
  51.                     button[i][j]= new JButton();
  52.                     String text = i + "," + j;
  53.                     button[i][j].setText(text);
  54.                     button[i][j].setFont(new Font ("SansSerif", Font.PLAIN,0));
  55.                     String ganti;
  56.                     int x = grid[i][j];
  57.                    
  58.                     if(x != 16)
  59.                     {
  60.                         ganti =  "images/" + x + ".png";
  61.                         label[i][j] = new JLabel(new ImageIcon(ganti),JLabel.CENTER);
  62.                     }
  63.                     else
  64.                     {
  65.                         ganti = "";
  66.                         label[i][j] = new JLabel();
  67.                     }
  68. //                  System.out.println(ganti);
  69.                     button[i][j].add(label[i][j]);
  70.                     button[i][j].setBorder(BorderFactory.createLineBorder(Color.WHITE));
  71.                     button[i][j].setBackground(Color.BLACK);
  72.                     this.button[i][j].addActionListener(this);
  73.                     jp.add(button[i][j]);
  74.             }
  75.         fr.add(jp);
  76.         fr.setVisible(true);
  77.         fr.setResizable(false);
  78.         fr.setSize(700,700);
  79.         fr.setLocationRelativeTo(null);
  80.         fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  81.     }
  82.  
  83.     public void acak() {
  84.        
  85.         Random rd = new Random();
  86.         int []arr = new int [16];
  87.        
  88.        
  89.         for(int i=0;i<16;i++)
  90.             arr[i] = i+1;
  91.          
  92.         for(int i=0;i<16;i++) {
  93.             int index = rd.nextInt(16);
  94.             int temp  = arr[i];
  95.             arr[i]    = arr[index];
  96.             arr[index]= temp;
  97.         }
  98.        
  99.         int count = 0;
  100.         for(int i=0;i<baris;i++) {
  101.             for(int j=0;j<kolom;j++) {
  102.                
  103.                 grid[i][j] = arr[count];
  104.                 cek[count] = arr[count];
  105.                 count = count + 1;
  106. //              System.out.print(grid[i][j] + "\t");
  107.             }
  108. //          System.out.println("");
  109.         }
  110.     }
  111.    
  112.     private boolean isSolvable() {
  113.         int countInversions = 0;
  114.        
  115.         for (int i = 0; i < 16; i++) {
  116.             if (cek[i]==16) continue;
  117.           for (int j = 0; j < i; j++) {
  118.             if(cek[j]==16) continue;
  119.             if (cek[j] > cek[i])
  120.               countInversions++;
  121.           }
  122.         }
  123.        
  124.         return countInversions % 2 == 0;
  125.       }
  126.    
  127.     Boolean wincon() {
  128.         int n = 1;
  129.         for(int i=0;i<baris;i++)
  130.             for(int j=0;j<kolom;j++) {
  131.                 if (grid[i][j]!=n && grid[i][j]!=16)
  132.                     return false;
  133.                 n = n+1;
  134.             }
  135.         return true;
  136.     }
  137.    
  138.     public void win() {
  139.             JFrame f = new JFrame();
  140.             JLabel l = new JLabel("You Win!",JLabel.CENTER);
  141.             l.setFont(new Font("SansSerif",Font.BOLD,30));
  142.             l.setForeground(Color.WHITE);
  143.             f.add(l);
  144.             f.setSize(300,300);
  145.             f.getContentPane().setBackground(new Color(5, 65, 90));
  146.             f.setVisible(true);
  147.             f.setLocationRelativeTo(null);
  148.             f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  149.     }
  150.     @Override
  151.     public void actionPerformed(ActionEvent e) {
  152.         // TODO Auto-generated method stub
  153.         Boolean con = wincon();
  154.         if(con==false) {
  155.             String s = e.getActionCommand().toString();
  156.             int r    = Integer.parseInt(s.split(",")[0]);
  157.             int c    = Integer.parseInt(s.split(",")[1]);
  158.            
  159.             if (grid[r][c] != 16) {
  160.                 if(r+1 < baris && grid[r+1][c]==16)
  161.                 {
  162.                     label[r][c].setIcon(new ImageIcon(""));
  163.                     String change = "images/" + grid[r][c] + ".png";
  164.                     label[r+1][c].setIcon(new ImageIcon(change));
  165.                    
  166.                     int temp = grid[r][c];
  167.                     grid[r][c]= grid[r+1][c];
  168.                     grid[r+1][c] = temp;
  169.                 }
  170.                 else if(r-1>=0 && grid[r-1][c]==16){
  171.                    
  172.                     label[r][c].setIcon(new ImageIcon(""));
  173.                     String change = "images/" + grid[r][c] + ".png";
  174.                     label[r-1][c].setIcon(new ImageIcon(change));
  175.                    
  176.                     int temp = grid[r][c];
  177.                     grid[r][c]= grid[r-1][c];
  178.                     grid[r-1][c] = temp;
  179.                 }
  180.                 else if(c+1<kolom && grid[r][c+1]==16) {
  181.                    
  182.                     label[r][c].setIcon(new ImageIcon(""));
  183.                     String change = "images/" +  grid[r][c] + ".png";
  184.                     label[r][c+1].setIcon(new ImageIcon(change));
  185.                    
  186.                     int temp = grid[r][c];
  187.                     grid[r][c]= grid[r][c+1];
  188.                     grid[r][c+1] = temp;
  189.                 }
  190.                 else if (c-1>=0 && grid[r][c-1]==16) {
  191.                    
  192.                     label[r][c].setIcon(new ImageIcon(""));
  193.                     String change = "images/" + grid[r][c] + ".png";
  194.                     label[r][c-1].setIcon(new ImageIcon(change));
  195.                    
  196.                     int temp = grid[r][c];
  197.                     grid[r][c]= grid[r][c-1];
  198.                     grid[r][c-1] = temp;
  199.                 }
  200.                
  201.             }
  202.             con = wincon();
  203.             if(con == true) {
  204.                 win();
  205.             }
  206.         }
  207.         }
  208.        
  209.    
  210. }
  211.  
Advertisement
Add Comment
Please, Sign In to add comment