Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gim_1;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Random;
- import javax.swing.BorderFactory;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- public class Miskot implements ActionListener{
- JFrame fr;
- JPanel jp;
- JButton[][] button;
- JLabel [][] label;
- int baris,kolom;
- int [][] grid;
- int [] cek;
- public Miskot() {
- baris = 4;
- kolom = 4;
- grid = new int [baris][kolom];
- cek = new int [16];
- init();
- }
- public void init() {
- fr = new JFrame();
- jp = new JPanel();
- jp.setLayout(new GridLayout(4,4));
- button = new JButton[baris][kolom];
- label = new JLabel[baris][kolom];
- do {
- acak();
- // System.out.print("tes");
- }while(!isSolvable());
- for(int i=0;i<baris;i++)
- for(int j=0;j<kolom;j++) {
- button[i][j]= new JButton();
- String text = i + "," + j;
- button[i][j].setText(text);
- button[i][j].setFont(new Font ("SansSerif", Font.PLAIN,0));
- String ganti;
- int x = grid[i][j];
- if(x != 16)
- {
- ganti = "images/" + x + ".png";
- label[i][j] = new JLabel(new ImageIcon(ganti),JLabel.CENTER);
- }
- else
- {
- ganti = "";
- label[i][j] = new JLabel();
- }
- // System.out.println(ganti);
- button[i][j].add(label[i][j]);
- button[i][j].setBorder(BorderFactory.createLineBorder(Color.WHITE));
- button[i][j].setBackground(Color.BLACK);
- this.button[i][j].addActionListener(this);
- jp.add(button[i][j]);
- }
- fr.add(jp);
- fr.setVisible(true);
- fr.setResizable(false);
- fr.setSize(700,700);
- fr.setLocationRelativeTo(null);
- fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public void acak() {
- Random rd = new Random();
- int []arr = new int [16];
- for(int i=0;i<16;i++)
- arr[i] = i+1;
- for(int i=0;i<16;i++) {
- int index = rd.nextInt(16);
- int temp = arr[i];
- arr[i] = arr[index];
- arr[index]= temp;
- }
- int count = 0;
- for(int i=0;i<baris;i++) {
- for(int j=0;j<kolom;j++) {
- grid[i][j] = arr[count];
- cek[count] = arr[count];
- count = count + 1;
- // System.out.print(grid[i][j] + "\t");
- }
- // System.out.println("");
- }
- }
- private boolean isSolvable() {
- int countInversions = 0;
- for (int i = 0; i < 16; i++) {
- if (cek[i]==16) continue;
- for (int j = 0; j < i; j++) {
- if(cek[j]==16) continue;
- if (cek[j] > cek[i])
- countInversions++;
- }
- }
- return countInversions % 2 == 0;
- }
- Boolean wincon() {
- int n = 1;
- for(int i=0;i<baris;i++)
- for(int j=0;j<kolom;j++) {
- if (grid[i][j]!=n && grid[i][j]!=16)
- return false;
- n = n+1;
- }
- return true;
- }
- public void win() {
- JFrame f = new JFrame();
- JLabel l = new JLabel("You Win!",JLabel.CENTER);
- l.setFont(new Font("SansSerif",Font.BOLD,30));
- l.setForeground(Color.WHITE);
- f.add(l);
- f.setSize(300,300);
- f.getContentPane().setBackground(new Color(5, 65, 90));
- f.setVisible(true);
- f.setLocationRelativeTo(null);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- Boolean con = wincon();
- if(con==false) {
- String s = e.getActionCommand().toString();
- int r = Integer.parseInt(s.split(",")[0]);
- int c = Integer.parseInt(s.split(",")[1]);
- if (grid[r][c] != 16) {
- if(r+1 < baris && grid[r+1][c]==16)
- {
- label[r][c].setIcon(new ImageIcon(""));
- String change = "images/" + grid[r][c] + ".png";
- label[r+1][c].setIcon(new ImageIcon(change));
- int temp = grid[r][c];
- grid[r][c]= grid[r+1][c];
- grid[r+1][c] = temp;
- }
- else if(r-1>=0 && grid[r-1][c]==16){
- label[r][c].setIcon(new ImageIcon(""));
- String change = "images/" + grid[r][c] + ".png";
- label[r-1][c].setIcon(new ImageIcon(change));
- int temp = grid[r][c];
- grid[r][c]= grid[r-1][c];
- grid[r-1][c] = temp;
- }
- else if(c+1<kolom && grid[r][c+1]==16) {
- label[r][c].setIcon(new ImageIcon(""));
- String change = "images/" + grid[r][c] + ".png";
- label[r][c+1].setIcon(new ImageIcon(change));
- int temp = grid[r][c];
- grid[r][c]= grid[r][c+1];
- grid[r][c+1] = temp;
- }
- else if (c-1>=0 && grid[r][c-1]==16) {
- label[r][c].setIcon(new ImageIcon(""));
- String change = "images/" + grid[r][c] + ".png";
- label[r][c-1].setIcon(new ImageIcon(change));
- int temp = grid[r][c];
- grid[r][c]= grid[r][c-1];
- grid[r][c-1] = temp;
- }
- }
- con = wincon();
- if(con == true) {
- win();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment