Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Problem in implementation of Fifteen Puzzle with 2D Arrays

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Problem in implementation of Fifteen Puzzle with 2D Arrays

    ok, so I'm realllyyyyy stuck at the moment. I started coding something, but I've realised that I've done it all wrong and now I literally have no idea where to start.
    I'm not asking anyone to write a program for me, etc. But I do need someone to kick start me and give me some sort of idea on where to even begin.
    Here's the website for the actual assignment: Java Programming (CITS1200)

    Here's what *little* code I have (Some of it not even related to how the assignment is supposed to be done (Mainly supplying it so that if anyone does decide to help, they may be able to use what I have there and give me some sort of idea what my constructor should look like or something?):
    public class FifteenPuzzle  {
     
            //Instance Variables
            public SimpleCanvas sc;
            public int x;
            public int y;
            public int[][] initialGrid;
     
            //Constructor
            public FifteenPuzzle(int[][] initialGrid){
                sc = new SimpleCanvas ();
     
                //Colour
                java.awt.Color tBlue = new java.awt.Color(0,0,255);
                sc.setForegroundColour(tBlue);
                for (int i=0; i < 400;i++) sc.drawLine(0,i,400,i);
     
                // Lines
                java.awt.Color black = new java.awt.Color(0,0,0);
                sc.setForegroundColour(black);
                sc.drawLine(100,0,100,400);
                sc.drawLine(200,0,200,400);
                sc.drawLine(300,0,300,400);
                sc.drawLine(0,100,400,100);
                sc.drawLine(0,200,400,200);
                sc.drawLine(0,300,400,300); 
                //Lines
     
                //Input
                java.awt.Color white = new java.awt.Color(255,255,255);
                sc.setForegroundColour(white);
                for (int j=0; j < x*100;j++) sc.drawLine(j,(y-1)*100,j,y*100);
     
            }
     
            //Methods
            public void moveTile (int x, int y){
            }
     
            private void createTile (int x){ 
                sc.drawLine(xPos,yPos,xPos+100,yPos); 
                sc.drawLine(xPos+100,yPos,xPos+100,yPos+100); 
                sc.drawLine(xPos+100,yPos+100,xPos,yPos+100); 
                sc.drawLine(xPos,yPos+100,xPos,yPos); 
    } 
     
     
     
            //public int [][] getCurrentGrid(){
            //}
     
     
        }
    I'm even having trouble understanding what exactly the assignment means, in particular the constructor and the moveTile method.

    Any help is very much appreciated.

    Need help ASAP. Been struggling with it for a few days now, and have only just realised how wrong I was and its due tomorrow (Yay for deadlines)

    Regards, Tom.

    P.S Heres the code for simpleCanvas if you need it:
    /**
     * This is a stripped-down version of the Canvas class from the 
     * BlueJ team, retaining only the most fundamental features.
     * 
     * @author BlueJ team with modifications by Gordon Royle 
     * @version July 2003
     */
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class SimpleCanvas
    {
        private JFrame frame;
        private CanvasPane canvas;
        private Graphics2D graphic;
        private Image canvasImage;
        private boolean autoRepaint;
     
     
        /**
         * Creates and displays a SimpleCanvas of the specified size
         * with a white background. The client specifies whether repainting
         * after a drawing command should be manual or automatic.
         * 
         * @param   title title for the window
         * @param   width the desired width of the SimpleCanvas 
         * @param   height the desired height of the SimpleCanvas
         * @param   autoRepaint true for automatic repainting
         * 
         */
     
        public SimpleCanvas(String title, int width, int height, boolean autoRepaint) {
            frame = new JFrame();
            canvas = new CanvasPane();
            frame.setContentPane(canvas);
            frame.setTitle(title);
            canvas.setPreferredSize(new Dimension(width,height));
            frame.pack();
            Dimension size = canvas.getSize();
            canvasImage = canvas.createImage(size.width,size.height);
            graphic = (Graphics2D) canvasImage.getGraphics();
            graphic.setColor(Color.white);
            graphic.fillRect(0,0,size.width,size.height);
            graphic.setColor(Color.black);
            frame.setVisible(true);
            frame.setVisible(true);
    	//        frame.show();
            frame.setVisible(true);
            this.autoRepaint = autoRepaint;
        }
     
       /**
         * Creates and displays a SimpleCanvas with a white background and
         * with automatic repainting after drawing commands.
         * 
         * @param   title title for the window
         * @param   width the desired width of the SimpleCanvas 
         * @param   height the desired height of the SimpleCanvas
         * 
         */
     
        public SimpleCanvas(String title, int width, int height) {
            this(title,width,height,true);
        }
     
        /**
         * Creates and displays a SimpleCanvas of size 400x400 with the
         * default title "SimpleCanvas" and with automatic repainting 
         * enabled.
         * 
         * @param   title title for the window
         * @param   width the desired width of the SimpleCanvas 
         * @param   height the desired height of the SimpleCanvas
         * 
         */
     
        public SimpleCanvas() {
            this("SimpleCanvas",400,400);
     
     
            }
     
        /** 
         * Draws a line on the SimpleCanvas between two points.
         * 
         * @param   x1 x-coordinate of the first point
         * @param   y1 y-coordinate of the first point
         * @param   x2 x-coordinate of the second point
         * @param   y2 y-coordinate of the second point
         *
         */
     
        public void drawLine(int x1, int y1, int x2, int y2) {
            graphic.drawLine(x1,y1,x2,y2);
            if (autoRepaint) canvas.repaint();
        }
     
        /** 
         * Changes the colour for subsequent 
         * drawing on this SimpleCanvas.
         * 
         * @param   newColour the new drawing colour
         * 
         */
     
        public void setForegroundColour(Color newColour) {
            graphic.setColor(newColour);
        }
     
        /**
         * Gets the colour currently used for 
         * drawing on this SimpleCanvas.
         * 
         */
     
        public Color getForegroundColour() {
            return graphic.getColor();
        }
     
        /**
         * Changes the font for subsequent String
         * drawing on this SimpleCanvas.
         *
         * @param   newFont the new Font
         * 
         */
     
        public void setFont(Font newFont) {
            graphic.setFont(newFont);
        }
     
        /**
         * Gets the font currently used for
         * String drawing on this Canvas
         */
     
        public Font getFont() {
            return graphic.getFont();
        }
     
        /**
         * Draws the specified String at the specified
         * location on this SimpleCanvas
         */
     
        public void drawString(String text, int x, int y) {
            graphic.drawString(text, x, y);
            if (autoRepaint) canvas.repaint();
        }
     
        /**
         * Sets the repaint mode to either manual or automatic.
         * 
         * @param   autoRepaint automatic repainting if this is true
         * 
         */
     
        public void setAutoRepaint(boolean autoRepaint) {
            this.autoRepaint = autoRepaint;
        }
     
     
        /**
         * If this SimpleCanvas does not automatically repaint 
         * after each drawing command, then this method can be
         * used to cause a manual repaint.
         */
     
        public void repaint() {
            canvas.repaint();
        }
     
        /**
         * Causes execution to pause for the specified amount of time.
         * This is usually used to produce animations in an easy
         * manner, by repeatedly drawing, pausing, and then redrawing
         * an object.
         */
     
        public void wait(int millis) {
            try {
                Thread.sleep(millis);
            } catch (InterruptedException ie) {
                System.out.println("Interrruption in SimpleCanvas: "+ie);
            }
        }
     
        public void addMouseListener(MouseListener ml) {
            canvas.addMouseListener(ml);
        }
     
        class CanvasPane extends JPanel {
            public void paint(Graphics g) {
                g.drawImage(canvasImage,0,0,null);
            }
        }
     
    }
    Last edited by bruint; May 2nd, 2009 at 12:54 AM.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Fifteen Puzzle - 2D Arrays, etc.

    Hi Tom.
    Your constructor should be relitvely simple, all it needs to do it check that a correct 4x4 grid with numbers 0-15 ar present within it an no others. If that is no the case thow an IllegalArgumentException.
    public FifteenPuzzle(int[][] initialGrid){
            if(initialGrid.length == 4 && initialGrid[0].length == 4){
                boolean[] n = new boolean[16];
                for(int i = 0; i < 4; i++){
                    for(int j = 0; j < 4; j++){
                        if(initialGrid[i][j] >= 0 && initialGrid[i][j] <= 15){
                            if(!n[initialGrid[i][j]]){
                                n[initialGrid[i][j]] = true;
                            }else
                                throw new IllegalArgumentException("Same number enetered twice");
                        }else
                            throw new IllegalArgumentException("Number out of range");
                    }
                }
            }else
                throw new IllegalArgumentException("Invalid grid size");
     
            /*******************************
             * Load grid into main grid?
             * 
             * Start the game?
             */
        }
    Just an example. It looks pretty messy but hey, it's an idea you can do what you want with it.

    Chris

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    bruint (May 2nd, 2009)

  4. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Fifteen Puzzle - 2D Arrays, etc.

    Thanks a ton mate. Honestly, I owe you like a virtual beer or something.

    I'll most likely be back here in a couple of minutes when I run into my next problem.

  5. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Fifteen Puzzle - 2D Arrays, etc.

    Glad I could help get you started!

    Chris

  6. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Thumbs up Re: Fifteen Puzzle - 2D Arrays, etc.

    Quote Originally Posted by bruint View Post
    Thanks a ton mate. Honestly, I owe you like a virtual beer or something.
    Here you go guys.. I'll get the round in.

    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Fifteen Puzzle - 2D Arrays, etc.

    Cheers, but I'm under age ....But that looks so refreshing, might have to go the pub now lol!

    Chris

  8. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Fifteen Puzzle - 2D Arrays, etc.

    Quote Originally Posted by Freaky Chris View Post
    Cheers, but I'm under age ....But that looks so refreshing, might have to go the pub now lol!

    Chris
    Yours is a larger shandy Chris..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  9. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Fifteen Puzzle - 2D Arrays, etc.

    Uncool,
    I'd much prefer a Magners with ice

  10. #9
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Fifteen Puzzle - 2D Arrays, etc.

    *sigh* Alas, I'm underage also. I was working on the basis that my brother would buy the virtual beers for me.

Similar Threads

  1. Elegant and short way to implement my program on 2d Array
    By TheForumLord in forum Collections and Generics
    Replies: 1
    Last Post: December 8th, 2008, 04:56 PM
  2. Java error in using Arrays
    By AnithaBabu1 in forum Collections and Generics
    Replies: 4
    Last Post: November 4th, 2008, 07:50 AM