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 8 of 8

Thread: 15 square puzzle

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 15 square puzzle

    I was wondering if I could get a little help on the classic 15 puzzle game. I have attached the code I have so far and everything functions correctly. The only things I am having problems with is that I need to implement allowing for once the empty location is found allowing for two "buttons" being tiles to be moved but the logic is not really making sense to me. I have it correct for moving them one time. As well once the puzzle is solved I have a message pop up that says the puzzle is solved but then once I go to shuffle the board again sometimes that message will appear again then the board will shuffle, and for the life of me I cannot figure out why.

    Thanks in advance

    /*
     * Fifteen Square Game
     * This program creates a gui version of the classical game fifteen square
     * Each of the different pieces are created as a button 
     * The 16th location is in the array for moving around the different pieces but the button is not there
     * The gui has an information button to display how many games or played moves made and games won
     * 
     */
     
     
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;				//imports the needed things from the java library 
    import javax.swing.*;
     
     
    @SuppressWarnings("serial")
    public class fifteenSquare extends JFrame implements ActionListener 
    {
    	int val = 0; //variable for board generation
        int w = 0; //variable for number of wins
        int p = 0; //variable for number of games played
        int m = 0; //variable for number of moves
        boolean solved = false; //boolean for once board is solved
    	private JPanel frame; //name for the JPanel
        private final static int X = 75; //constant for button generation
        private int[][] locations = new int[4][4]; //array for making the buttons
        @SuppressWarnings("rawtypes") //removes errors 
    	private Vector buttons = new Vector(); //makes a vector for buttons
        public fifteenSquare() //constructor 
        {
            super("Fifteen puzzle"); //Title of the gui
            setBounds(250, 150, X * 4 + 8, X * 4 + 50); //sets the bounds of the buttons 
            setDefaultCloseOperation(EXIT_ON_CLOSE); //allows program to terminate when x'ed out
            setResizable(false); //the window cannot be resized
     
     
            JMenuBar menuBar = new JMenuBar(); //creates menu bar
            JMenu game = new JMenu("Game"); //makes the game button on menu bar
            JMenuItem gameNew = new JMenuItem("Shuffle"); //makes shuffle button
            JMenuItem hint = new JMenuItem("Hint"); //makes hint button
            JMenuItem save = new JMenuItem("Save"); //makes save button
            JMenuItem load = new JMenuItem("Load"); //makes load button
            JMenuItem info = new JMenuItem("Info"); //makes info button
            JMenuItem help = new JMenuItem("Help"); //makes help button
            JMenuItem exit = new JMenuItem("Exit"); //makes exit button
     
            gameNew.addActionListener(new ActionListener() 
            {
                public void actionPerformed(ActionEvent e) 
                {
                	int version = new Random().nextInt(100); //generates a random int from 0-100
                    Integer resI = version; //makes Integer resI and makes it equal to version
                     shuffle(resI);  //calls shuffle method
     
     
                }
            });
     
            info.addActionListener(new ActionListener() //action listener for information button
            {
     
            	public void actionPerformed(ActionEvent e)
            	{
                    //displays a message box displaying moves games played and games won
            		JOptionPane.showMessageDialog(null, "Moves: " + m + "\n Games Played: " + p + "\nGames Won: " + w);		
            	}
     
     
     
            });
     
            exit.addActionListener(new ActionListener() //action listener for exit button
            {
     
            	public void actionPerformed(ActionEvent e)
            	{
                    //terminates the program
            		System.exit(0);		
            	}
     
     
     
            });
     
            game.add(gameNew);
            game.add(hint);
            game.add(save);
            game.add(load);
            game.add(info);			//adds the different buttons to the menu bar
            game.add(help);
            game.add(exit);
     
            hint.setEnabled(false);
            save.setEnabled(false); //makes these hint save load and help button inactive
            load.setEnabled(false);
            help.setEnabled(false);
     
            menuBar.add(game); //adds game button to menu bar
            setJMenuBar(menuBar); //makes a menu bar
            frame = new JPanel(); //makes a panel
            frame.setLayout(null); //sets the frame to have no layout manager
            add(frame); //adds bFrame to panel
            initiateMatrix(); //calls initiate Matrix method
            drawButtons(); //calls drawButtons method
     
            } //end constructor
     
        @SuppressWarnings("unchecked")
    	public void initiateMatrix() //method to put values in the array
        {
            int value = 1; //makes int value and assings it to value of 1
            for (int j = 0; j < 4; j++) //loop for putting in values
            {
                for (int i = 0; i < 4; i++) 
                {
                    if (value != 16) //checks values to keep in the 15 
                        locations[i][j] = value++; //plugs in values in the array
                    else
                        locations[i][j] = 0; //makes the location[4][4] = 0 
                }
            }
            value = 1; //reassigns value to 1
            for (int i = 0; i < 4; i++) //loop for putting the buttons on the panel
            {
                for (int j = 0; j < 4; j++) //checks the value contained
                {
                    if (value != 0) //checks the value if its not zero so the 16 button is not made
                    {
                        JButton b = new JButton(Integer.toString(value++)); //makes new button
                        b.addActionListener(this); //adds listener for the button
                        buttons.add(b); //adds number to the button
                        frame.add(b);  //adds button to the panel
                    }
                }
            }
        }
        public void drawButtons() //method to make the buttons
        {
            for (int i = 0; i < 4; i++) //loop to go through all the first locations
            {
                for (int j = 0; j < 4; j++) //loop to go into the second location
                {
                    int val = locations[i][j]; //assigns vals the value contained in the array
                    if (val != 0) //makes sure the value is not zero
                    {
                        ((Component) buttons.get(val - 1)).setBounds(X * i, X * j, X, X); //makes the button based on the bounds
                    }
                }
            }
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public static void main(String[] args) //main method
        {
        	JFrame frame = new fifteenSquare(); //makes new JFrame
        	frame.setVisible(true); //makes the frame visible
     
     
        }
        //////////////////////////////////////////////////////////////////////////////////////////////////
        @Override
        public void actionPerformed(ActionEvent e) //method for each action listener
        {
            JButton b = (JButton) e.getSource(); //checks what button is pressed
            moveButton(b.getText()); //calls moveButton method and passed the button that is pressed
     
     
        }
        private void moveButton(String b) //moveButton method
        {
            int val = new Integer(b); //makes int val and assigns it a new Integer
            int direction = findBlank(val); //assigns direction the value found from the findBlank method
            if (direction != 0) //checks if direction is not equal to zero
            {
                doMoving(val, direction); //calls the doMoving method and passes val and direction
     
            }
        }
        private int findBlank(int val) //method findBlank
        {
            int ii = 0; //creates variable ii and assigns it 0
            int jj = 0; //creates variable jj and assigns it 0 
            for (int i = 0; i < 4; i++) //loop to go through each location in the array and get the value
            {
                for (int j = 0; j < 4; j++) 
                {
                    if (locations[i][j] == val) 
                    {
                        ii = i; //the value of each is then assigned to ii and jj
                        jj = j;
                    }
                }
            }
            //makes the different variables called leftcell rightcell topcell and bottomcell
            //checks if jj and ii is equal to 0 or 3 is so it assigns the values to the array
            int leftCell = (jj == 0) ? -1 : locations[ii][jj - 1]; 
            int rightCell = (jj == 3) ? -1 : locations[ii][jj + 1];
            int topCell = (ii == 0) ? -1 : locations[ii - 1][jj];
            int bottomCell = (ii == 3) ? -1 : locations[ii + 1][jj];
            int result = 0; //creates result and assings it zero
            //finds which cell is equal to zero so it can return that result
            if (leftCell == 0) 
                result = 1; //sets result to 1
            if (rightCell == 0)
                result = 2; //sets result to 2
            if (topCell == 0)
                result = 3; //sets result to 3
            if (bottomCell == 0)
                result = 4; //sets result to 4
            return result; //returns result being where the empty slot is
        }
        private void doMoving(int val, int direction) //doMoving method 
        {
            int ii = 0; //creates ii and assigns it 0
            int jj = 0; //creates jj and assigns it 0
            for (int i = 0; i < 4; i++)  //loop to go through each location in the array
            {
                for (int j = 0; j < 4; j++)  
                {
                    if (locations[i][j] == val) //checks if location in array is equal to val
                    {
                        ii = i; //if true ii = i and jj = j
                        jj = j;
                    }
                }
            }
            locations[ii][jj] = 0; //makes the location of ii and jj = 0 
            switch (direction) //switch statement for the different cases
            {
                case 1:jj--;break; //checks which case it is given then 
                case 2:jj++;break; //either increases are decreases jj or ii  
                case 3:ii--;break; //then breaks out
                case 4:ii++;break;
            }
            locations[ii][jj] = val; //makes that location of jj and ii equal to val
            JButton b = (JButton) buttons.elementAt(val - 1); //puts that but in right location
            b.setBounds(X * ii, X * jj, X, X); //sets the bounds of the button using the constant
     
     
            if(p >= 1) //checks if board has been shuffled once
            {
            	m++; //increments moves
            	solved(); //calls the isSolved method
            	if(solved == true) //if the board has been solved
                {
                	JOptionPane.showMessageDialog(null, "You have solved the puzzle"); //puts up window that you have won
                	solved = false;
                	w++; //increments w being number of wins
                }	
     
     
     
            }
     
     
        }
        private void solved() //isSolved method
    	{
    		if(p >= 1) //makes sure the board has been shuffled once so when the board is generated it does not fall into this
    		{
    			if(locations[0][0] == 1 && 
    			   locations[0][1] == 5 &&	
    			   locations[0][2] == 9 &&
    			   locations[0][3] == 13 &&
    			   locations[1][0] == 2 &&
    			   locations[1][1] == 6 &&
    			   locations[1][2] == 10 &&  //checks the values in to array to when should be there for a win
    			   locations[1][3] == 14 &&
    			   locations[2][0] == 3 &&
    			   locations[2][1] == 7 &&
    			   locations[2][2] == 11 &&
    			   locations[2][3] == 15 &&
    			   locations[3][0] == 4 &&
    			   locations[3][1] == 8 &&
    			   locations[3][2] == 12 &&
    			   locations[3][3] == 0 )
    			{			
    			solved = true; //assigns solved to true
    			}
    		}
    		else
    		{
    			solved = false; //if not solved assigns solved to false
    		}
     
     
    	}
     
    	private void shuffle(int version) //shuffle method
        {
     
    		initiateMatrix(); //calls initiateMatrix Method
            drawButtons(); //calls drawButtons method
            Random r = new Random(); //creates a new random called r
            for (int i = 0; i < 1000; i++) // has to allow for the board needing to be changed many times
            {
                Integer random = r.nextInt(15) + 1; //creates random between 0-16
                moveButton(random.toString()); //calls moveButton method passing it random as a string
            }
            p++; //increments p being number of plays 
            m = 0; //resets move to zero so the number of moves made when board is created is not counted
        }
     
     
     
     
     
    }
    Attached Files Attached Files


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: 15 square puzzle

    once the puzzle is solved I have a message pop up that says the puzzle is solved but then once I go to shuffle the board again sometimes that message will appear again then the board will shuffle
    Sounds like a logic problem. Are there variables with incorrect values causing it to happen? Have you tried debugging by adding some println statements that print out the values of the variables that control the logice flow for what is happening?

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 15 square puzzle

    Quote Originally Posted by Norm View Post
    Sounds like a logic problem. Are there variables with incorrect values causing it to happen? Have you tried debugging by adding some println statements that print out the values of the variables that control the logice flow for what is happening?
    I have tried debugging in eclipse with some breakpoints it just does not make sense because it occurs once like the puzzle has been solved for like the third time.

  4. #4
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: 15 square puzzle

    once like the puzzle has been solved for like the third time.
    Debugging takes patience. Sometimes printlns are better because they can be faster than the interactive debug. Especially if the error doesn't happen every time.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 15 square puzzle

    Quote Originally Posted by Norm View Post
    Debugging takes patience. Sometimes printlns are better because they can be faster than the interactive debug. Especially if the error doesn't happen every time.
    Okay thanks for the help. I will give it a try. Then onto figuring out how to allow the buttons to move twice

  6. #6
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: 15 square puzzle

    For quicker testing, change the shuffle method to create a position that is one move from a win.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 15 square puzzle

    Quote Originally Posted by Norm View Post
    For quicker testing, change the shuffle method to create a position that is one move from a win.
    I believe I have solved the problem. I just added another variable and has it set back to zero aside form using my variable for the number of plays because I think it would go back and check the condition of the loop and it would be true for the that p value and fall back in.

  8. #8
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 15 square puzzle

    I am really struggling with the whole idea of letting the board do double and triple moves. I have figured out the logic on how to determine if the blank cell is two or three locations away but I cannot put it together on how to pass it to my method that actually does the moving. Any ideas to push me in the right direction would be great.

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;				//imports the needed things from the java library 
    import javax.swing.*;
     
     
     
     
    @SuppressWarnings("serial")
    public class fifteenSquare extends JFrame implements ActionListener 
    {
     
    	int val = 0; //variable for board generation
        int z = 0; //variable to use as a place holder
    	int w = 0; //variable for number of wins
        int p = 0; //variable for number of games played
        int m = 0; //variable for number of moves
        boolean solved = false; //boolean for once board is solved
    	private JPanel frame; //name for the JPanel
        private final static int X = 75; //constant for button generation
        private int[][] locations = new int[4][4]; //array for making the buttons
        @SuppressWarnings("rawtypes") //removes errors 
    	private Vector buttons = new Vector(); //makes a vector for buttons
        public fifteenSquare() //constructor 
        {
            super("Fifteen puzzle"); //Title of the gui
            setBounds(250, 150, X * 4 + 8, X * 4 + 50); //sets the bounds of the buttons 
            setDefaultCloseOperation(EXIT_ON_CLOSE); //allows program to terminate when x'ed out
            setResizable(false); //the window cannot be resized
     
     
            JMenuBar menuBar = new JMenuBar(); //creates menu bar
            JMenu game = new JMenu("Game"); //makes the game button on menu bar
            JMenuItem gameNew = new JMenuItem("Shuffle"); //makes shuffle button
            JMenuItem hint = new JMenuItem("Hint"); //makes hint button
            JMenuItem save = new JMenuItem("Save"); //makes save button
            JMenuItem load = new JMenuItem("Load"); //makes load button
            JMenuItem info = new JMenuItem("Info"); //makes info button
            JMenuItem help = new JMenuItem("Help"); //makes help button
            JMenuItem exit = new JMenuItem("Exit"); //makes exit button
     
            gameNew.addActionListener(new ActionListener() 
            {
                public void actionPerformed(ActionEvent e) 
                {
                	int version = new Random().nextInt(100); //generates a random int from 0-100
                    Integer resI = version; //makes Integer resI and makes it equal to version
                     shuffle(resI);  //calls shuffle method
     
     
                }
            });
     
            info.addActionListener(new ActionListener() //action listener for information button
            {
     
            	public void actionPerformed(ActionEvent e)
            	{
                    //displays a message box displaying moves games played and games won
            		JOptionPane.showMessageDialog(null, "Moves: " + m + "\n Games Played: " + p + "\nGames Won: " + w);		
            	}
     
     
     
            });
     
            exit.addActionListener(new ActionListener() //action listener for exit button
            {
     
            	public void actionPerformed(ActionEvent e)
            	{
                    //terminates the program
            		System.exit(0);		
            	}
     
     
     
            });
     
            help.addActionListener(new ActionListener() //action listener for help button
            {
     
            	public void actionPerformed(ActionEvent e) 
            	{
                    //displays message box with a basic explanation of how to play
            		JOptionPane.showMessageDialog(null,"To play the game click on the tiles.\nThey will move into the empty location." +
                    "\nThe goal is to get the tiles in order from 1-15.\nMatching the board before it is shuffled.");	
            	}
     
     
     
            });
     
            game.add(gameNew);
            game.add(hint);
            game.add(save);
            game.add(load);
            game.add(info);			//adds the different buttons to the menu bar
            game.add(help);
            game.add(exit);
     
            hint.setEnabled(false);
            save.setEnabled(false); //makes these hint save load and help button inactive
            load.setEnabled(false);
     
     
            menuBar.add(game); //adds game button to menu bar
            setJMenuBar(menuBar); //makes a menu bar
            frame = new JPanel(); //makes a panel
            frame.setLayout(null); //sets the frame to have no layout manager
            add(frame); //adds bFrame to panel
            initiateLocations(); //calls initiate locations method
            drawButtons(); //calls drawButtons method
     
            } //end constructor
     
        @SuppressWarnings("unchecked")
    	public void initiateLocations() //method to put values in the array
        {
            int value = 1; //makes int value and assigns it to value of 1
            for (int j = 0; j < 4; j++) //loop for putting in values
            {
                for (int i = 0; i < 4; i++) 
                {
                    if (value != 16) //checks values to keep in the 15 
                        locations[i][j] = value++; //plugs in values in the array
                    else
                        locations[i][j] = 0; //makes the location[4][4] = 0 
                }
            }
            value = 1; //reassigns value to 1
            for (int i = 0; i < 4; i++) //loop for putting the buttons on the panel
            {
                for (int j = 0; j < 4; j++) //checks the value contained
                {
                    if (value != 0) //checks the value if its not zero so the 16 button is not made
                    {
                        JButton b = new JButton(Integer.toString(value++)); //makes new button
                        b.addActionListener(this); //adds listener for the button
                        buttons.add(b); //adds number to the button
                        frame.add(b);  //adds button to the panel
                    }
                }
            }
        }
     
        public void drawButtons() //method to make the buttons
        {
            for (int i = 0; i < 4; i++) //loop to go through all the first locations
            {
                for (int j = 0; j < 4; j++) //loop to go into the second location
                {
                    int val = locations[i][j]; //assigns vals the value contained in the array
                    if (val != 0) //makes sure the value is not zero
                    {
                        ((Component) buttons.get(val - 1)).setBounds(X * i, X * j, X, X); //makes the button based on the bounds
                    }
                }
            }
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public static void main(String[] args) //main method
        {
        	JFrame frame = new fifteenSquare(); //makes new JFrame
        	frame.setVisible(true); //makes the frame visible
     
     
        }
        //////////////////////////////////////////////////////////////////////////////////////////////////
        @Override
        public void actionPerformed(ActionEvent e) //method for each action listener
        {
            JButton b = (JButton) e.getSource(); //checks what button is pressed
            moveButton(b.getText()); //calls moveButton method and passed the button that is pressed
     
     
       }
     
        private void moveButton(String b) //moveButton method
        {
            int val = new Integer(b); //makes int val and assigns it a new Integer
            int direction = findBlank(val); //assigns direction the value found from the findBlank method
            if (direction != 0) //checks if direction is not equal to zero
            {
                if(direction == 1 ^ direction == 2 ^ direction == 3 ^ direction == 4)
                {
                	doMoving(val, direction); //calls the doMoving method and passes val and direction
                }
            	if(direction == 5 ^ direction == 6 ^ direction == 7 ^ direction == 8)
            	{
     
            		doMoving(val, direction); //calls the doMoving method and passes val and direction
                    findBlank(val);
                    doMoving(val,direction);
            	}
            	if(direction == 9 ^ direction == 10 ^ direction == 11 ^ direction == 12)
            	{
            		doMoving(val, direction); //calls the doMoving method and passes val and direction
            	}
     
     
     
            }
     
            if(solved == true) //if the board has been solved
            {
        		solved = false;
        		JOptionPane.showMessageDialog(null, "You have solved the puzzle"); //puts up window that you have won
            	w++; //increments w being number of wins
     
            }	
        }
        private int findBlank(int val) //method findBlank
        {
            int ii = 0; //creates variable ii and assigns it 0
            int jj = 0; //creates variable jj and assigns it 0 
            for (int i = 0; i < 4; i++) //loop to go through each location in the array and get the value
            {
                for (int j = 0; j < 4; j++) 
                {
                    if (locations[i][j] == val) 
                    {
                        ii = i; //the value of each is then assigned to ii and jj
                        jj = j;
                    }
                }
            }
            //makes the different variables called leftcell rightcell topcell and bottomcell
            //checks if jj and ii is equal to 0 or 3 is so it assigns the values to the array
     
            int topCell = (jj == 0) ? -1 : locations[ii][jj - 1]; 
            int bottomCell = (jj == 3) ? -1 : locations[ii][jj + 1];
            int leftCell = (ii == 0) ? -1 : locations[ii - 1][jj];
            int rightCell = (ii == 3) ? -1 : locations[ii + 1][jj];
     
            int topCell2 = (jj == 0 ^ jj == 1) ? -2 : locations[ii][jj - 2]; 
            int bottomCell2 = (jj == 3 ^ jj == 2) ? -2 : locations[ii][jj + 2];
            int leftCell2 = (ii == 0 ^ ii == 1) ? -2 : locations[ii - 2][jj];
            int rightCell2 = (ii == 3 ^ ii == 2) ? -2 : locations[ii + 2][jj];
     
            int topCell3 = (jj == 0 ^ jj == 1 ^ jj == 2) ? -1 : locations[ii][jj - 3]; 
            int bottomCell3 = (jj == 3 ^ jj == 2 ^ jj == 1) ? -1 : locations[ii][jj + 3];
            int leftCell3 = (ii == 0 ^ ii == 1 ^ ii == 2) ? -1 : locations[ii - 3][jj];
            int rightCell3 = (ii == 3 ^ ii == 2 ^ ii == 1) ? -1 : locations[ii + 3][jj];
     
            int result = 0; //creates result and assigns it zero
     
            //finds which cell is equal to zero so it can return that result
            if (topCell == 0) 
                result = 1; //sets result to 1
            if (bottomCell == 0)
                result = 2; //sets result to 2
            if (leftCell == 0)
                result = 3; //sets result to 3
            if (rightCell == 0)
                result = 4; //sets result to 4
            if (topCell2 == 0) 
                result = 5; //sets result to 1
            if (bottomCell2 == 0)
                result = 6; //sets result to 2
            if (leftCell2 == 0)
                result = 7; //sets result to 3
            if (rightCell2 == 0)
                result = 8; //sets result to 4
            if (topCell3 == 0) 
                result = 9; //sets result to 1
            if (bottomCell3 == 0)
                result = 10; //sets result to 2
            if (leftCell3 == 0)
                result = 11; //sets result to 3
            if (rightCell3 == 0)
                result = 12; //sets result to 4
     
            return result; //returns result being where the empty slot is
     
     
     
        }
        private void doMoving(int val, int direction) //doMoving method 
        {
            int ii = 0; //creates ii and assigns it 0
            int jj = 0; //creates jj and assigns it 0
            for (int i = 0; i < 4; i++)  //loop to go through each location in the array
            {
                for (int j = 0; j < 4; j++)  
                {
                    if (locations[i][j] == val) //checks if location in array is equal to val
                    {
                        ii = i; //if true ii = i and jj = j
                        jj = j;
                    }
                }
            }
            locations[ii][jj] = 0; //makes the location of ii and jj = 0 
     
            if(direction == 1 ^ direction == 2 ^ direction == 3 ^ direction == 4)
            {
            	switch (direction) //switch statement for the different cases
                {
                    case 1:jj--;break; //checks which case it is given then 
                    case 2:jj++;break; //either increases are decreases jj or ii  
                    case 3:ii--;break; //then breaks out
                    case 4:ii++;break;    
                }
                locations[ii][jj] = val; //makes that location of jj and ii equal to val
                JButton b = (JButton) buttons.elementAt(val - 1); //puts that but in right location
                b.setBounds(X * ii, X * jj, X, X); //sets the bounds of the button using the constant
     
            }
     
     
            if(z >= 1) //checks if board has been shuffled once
            {
            	m++; //increments moves
            	solved(); //calls the solved method
            }
     
     
        }
     
        private void solved() //isSolved method
    	{
    		if(z == 1) //makes sure the board has been shuffled once so when the board is generated it does not fall into this
    		{
    			if(locations[0][0] == 1 && 
    			   locations[0][1] == 5 &&	
    			   locations[0][2] == 9 &&
    			   locations[0][3] == 13 &&
    			   locations[1][0] == 2 &&
    			   locations[1][1] == 6 &&
    			   locations[1][2] == 10 &&  //checks the values in to array to when should be there for a win
    			   locations[1][3] == 14 &&
    			   locations[2][0] == 3 &&
    			   locations[2][1] == 7 &&
    			   locations[2][2] == 11 &&
    			   locations[2][3] == 15 &&
    			   locations[3][0] == 4 &&
    			   locations[3][1] == 8 &&
    			   locations[3][2] == 12 &&
    			   locations[3][3] == 0 )
    			{			
    			solved = true; //assigns solved to true
    			z=0; //sets z to zero 
    			}
    		}
    		else
    		{
    			solved = false; //if not solved assigns solved to false
    		}
     
     
    	}
     
    	private void shuffle(int version) //shuffle method
        {
     
    		initiateLocations(); //calls initiateLocations Method
            drawButtons(); //calls drawButtons method
            Random r = new Random(); //creates a new random called r
            for (int i = 0; i < 1000; i++) // has to allow for the board needing to be changed many times
            {
                Integer random = r.nextInt(15) + 1; //creates random between 0-16
                moveButton(random.toString()); //calls moveButton method passing it random as a string
            }
            z++; //increments z 
            p++; //increments p being number of plays 
            m = 0; //resets move to zero so the number of moves made when board is created is not counted
     
        }
     
    }

Similar Threads

  1. Puzzle Game. Need some help
    By clydefrog in forum Java Theory & Questions
    Replies: 10
    Last Post: March 12th, 2012, 03:14 PM
  2. [SOLVED] Method help, one thing then another - puzzle
    By Scotty in forum Java Theory & Questions
    Replies: 3
    Last Post: April 28th, 2011, 01:52 PM
  3. Search Word puzzle game
    By lew1s in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 9th, 2011, 04:23 AM
  4. JavaScript pic puzzle
    By fr334a11 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 25th, 2011, 08:29 AM
  5. [SOLVED] Problem in implementation of Fifteen Puzzle with 2D Arrays
    By bruint in forum Collections and Generics
    Replies: 8
    Last Post: May 3rd, 2009, 10:37 PM