Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Fifteen Puzzle

fifteen puzzle help

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 hasere

hasere

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 10 October 2015 - 08:04 AM

Guys I am Working on a fifteen puzzle project make a slide effect so if you take the number and drag it, it goes to the place you dragged it.

Heres My Code:

 

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.text.*;

public class FiffteenPuzzle extends JPanel {
    public static JMenuItem newItem, beginnerItem, intermediateItem, customItem, bestTimesItem, exitItem;
    public static JMenuItem aboutItem;
    public static javax.swing.Timer t;
    JButton[][] jb = new JButton[4][4];
    static int minute, second;
    static JFrame jf;

    public FiffteenPuzzle() {
        setLayout(new GridLayout(4, 4));

        int i, j, k;
        init(true);
        for (i = 0; i < jb.length; i++)
            for (j = 0; j < jb[i].length; j++)
                jb[i][j].addActionListener(
                    new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            String command = e.getActionCommand();
                            String[] indices = command.split(" ");
                            int i = Integer.parseInt(indices[0]);
                            int j = Integer.parseInt(indices[1]);
                            int[] r = {i - 1, i, i + 1, i};
                            int[] c = {j, j + 1, j, j - 1};
                            for (int k = 0; k < 4; k++) {
                                if (r[k] >= 0 && r[k] < 4 && c[k] >= 0 && c[k] < 4) {
                                    JButton button = jb[r[k]][c[k]];
                                    if (button.getText().equals("")) {
                                        button.setText(jb[i][j].getText());
                                        jb[i][j].setText("");
                                    }
                                }

                            }
                        }
                    });

        newItem.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    init(false);
                }
            });
    }
    public void init(boolean firstTime) {
        ArrayList a = new ArrayList();
        int i, j, k;
        for (i = 1; i <= 15; i++)
            a.add(new Integer(i));
        Collections.shuffle(a);
        a.add(new Integer(0));

        k = 0;
        for (i = 0; i < jb.length; i++)
            for (j = 0; j < jb[i].length; j++) {
                Integer val = (Integer)a.get(k++);
                if (firstTime) {
                    jb[i][j] = new JButton(val.intValue() + "");
                    jb[i][j].setActionCommand(i + " " + j);
                    add(jb[i][j]);
                }
                else
                    jb[i][j].setText(val.intValue() + "");
            }
            jb[3][3].setText("");
            minute = 0;
            second = 0;
    }

    public static void main(String[] args) {
        jf = new JFrame();
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JMenuBar jmb = new JMenuBar();
        jf.setJMenuBar(jmb);

        JMenu gameMenu = new JMenu("Game");
        jmb.add(gameMenu);
        JMenu helpMenu = new JMenu("Help");
        jmb.add(helpMenu);


        newItem = new JMenuItem("New");
        gameMenu.add(newItem);

        gameMenu.addSeparator();

        beginnerItem = new JMenuItem("Beginner");
        gameMenu.add(beginnerItem);
        intermediateItem = new JMenuItem("Intermediate");
        gameMenu.add(intermediateItem);
        customItem = new JMenuItem("Custom...");
        gameMenu.add(customItem);

        gameMenu.addSeparator();

        bestTimesItem = new JMenuItem("Best Time");
        gameMenu.add(bestTimesItem);

        gameMenu.addSeparator();

        exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(new ExitListener());
        gameMenu.add(exitItem);

        aboutItem = new JMenuItem("About");
        helpMenu.add(aboutItem);

        FiffteenPuzzle fp = new FiffteenPuzzle();
        jf.setContentPane(fp);

        final DecimalFormat dc = new DecimalFormat("00");

        t = new javax.swing.Timer(
            1000,
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jf.setTitle(dc.format(minute) + ":" + dc.format(second));
                    second++;
                    if (second >= 60) {
                        second %= 60;
                        minute++;
                    }
                }
            });
        jf.pack();
        jf.setVisible(true);
        Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
        int w=(int)d.getWidth();
        int h=(int)d.getHeight();
        jf.setLocation((int)(w/2-jf.getWidth()/2),(int)(h/2-jf.getHeight()/2));
        t.start();
    }

    public static class ExitListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
}
 
Thanks.

Edited by dargueta, 21 October 2015 - 07:11 PM.
Added code tags.


#2 hasere

hasere

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 10 October 2015 - 10:10 AM

sorry how to make the

slide effect