Results 1 to 8 of 8
  1. #1
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default [Help] 8-Square Puzzle Loop Game

    The 8-puzzle is a sliding puzzle that consists of 8 squares labeled 1 to 8 on a 3x3 grid, with one empty space. The picture below demonstrates some example configurations, as the pieces are slid around. Each move slides one neighboring square into the empty space. The goal of the puzzle is to reach the configuration on the right after a series of moves. The numbers must be in order when completed.



    For this project, my "grid" will simply be an array printed on the console. The blank space is a char space. The game works by issuing an input box for the player to type in the number of the square they wish to move. But somewhere along the line, the loop is breaking, or the game seems to think it's solved.

    And there are supposed to be certain squares that can't be switched, yet this code allows it. For example, in the first run of the program, you shouldn't be able to enter "7", because 7 can't be slid into the blank because it does not surround the blank. And I don't know how to make the game know when it's solved; when it's in the final, complete configuration. Can someone please tell me what's wrong with it?


    Java Code:
    import javax.swing.JOptionPane;
    public class Puzzle {
        
    	
    	static char[][] puzzle = {
    			{'3', '8', '2'},
    			{' ', '6', '5',},
    			{'4', '1', '7'}
    	};
    	
    	public static void main(String[] args) {
    		int n;
    		int c;
    		char a;
    		int nn;
    		int cc;
    		char temp;
    		String input;
    		System.out.println(puzzle[0][0] + " " + puzzle[0][1] + " " + puzzle[0][2]);
    		System.out.println(puzzle[1][0] + " " + puzzle[1][1] + " " + puzzle[1][2]);
    		System.out.println(puzzle[2][0] + " " + puzzle[2][1] + " " + puzzle[2][2]);
    		System.out.println("");
    		while(puzzle[0][0] != '1' && puzzle[0][1] != '2' && puzzle[0][2] != '3' && puzzle[1][0] != '4' && puzzle[1][1] != '5' && puzzle[1][2] != '6' && puzzle[2][0] != '7' && puzzle[2][1] != '8' && puzzle[2][2] != ' ') {
    			input = JOptionPane.showInputDialog(null, "Enter number to switch:");
    			a = (char)input.charAt(0);
    			n = findBlankn();
    			c = findBlankc();
    			nn = findVariablenn(a);
    			cc = findVariablecc(a);
    			if (n - 1 == nn || n + 1 == nn || c - 1 == cc || c + 1 == cc) { //This should sort out only the blank square's surrounding squares
    
    				temp = puzzle[n][c];
    				puzzle[n][c] = puzzle[nn][cc];
    				puzzle[nn][cc] = temp;
    			}else
    				System.out.println("Quit entering the WRONG NUMBERS!!!"); //error message if an input that does not neighbor the blank square is entered
    
    			System.out.println(puzzle[0][0] + " " + puzzle[0][1] + " " + puzzle[0][2]);
    			System.out.println(puzzle[1][0] + " " + puzzle[1][1] + " " + puzzle[1][2]);
    			System.out.println(puzzle[2][0] + " " + puzzle[2][1] + " " + puzzle[2][2]);
    			System.out.println("");
    		}
    		System.out.println(puzzle[0][0] + " " + puzzle[0][1] + " " + puzzle[0][2]);
    		System.out.println(puzzle[1][0] + " " + puzzle[1][1] + " " + puzzle[1][2]);
    		System.out.println(puzzle[2][0] + " " + puzzle[2][1] + " " + puzzle[2][2]);
    		System.out.println("");
    	}
    	public static int findBlankn() {
    		for (int i = 0; i < 3; i ++)
    			for (int j = 0; j < 3; j ++)
    				if (puzzle[i][j] == ' ')
    					return i;
    		return -1;
    	}
    	public static int findBlankc() {
    		for (int i = 0; i < 3; i ++)
    			for (int j = 0; j < 3; j ++)
    				if (puzzle[i][j] == ' ')
    					return j;
    		return -1;
    	}
    	public static int findVariablenn(char a) {
    		for (int i = 0; i < 3; i ++)
    			for (int j = 0; j < 3; j ++)
    				if (puzzle[i][j] == a)
    					return i;
    		return -1;
    	}
    	public static int findVariablecc(char a) {
    		for (int i = 0; i < 3; i ++)
    			for (int j = 0; j < 3; j ++)
    				if (puzzle[i][j] == a)
    					return j;
    		return -1;
    	}
    
    }
    Last edited by SapphireSpark; 12-03-2008 at 08:58 PM. Reason: add picture

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    To check if it's solved just create a checkSolved() method and have it check if [0][0] = 1, [0][1] = 2, [0][2] = 3, etc. If checkSolved() != true then just call the switch method(by the way you might want to break you main into 2 method, displayGrid() and swapPlaces().) Personally I think it's easier to work with method calls then following all sorts of loops to do something.

  3. #3
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    Ok, I'll start on the checkSolved method, but I don't get what you mean by splitting the main method. I'm still not very good with methods, which is why I have so many loops. If I can get this game to work, I'd love some tips on shortening the loops into methods.

    EDIT: judas, would this work? Where should I put it?

    Java Code:
        public static boolean checkSolved(){
            if (puzzle[0][0] == '1' && puzzle[0][1] == '2' && puzzle[0][2] == '3' 
                    && puzzle[1][0] == '4' && puzzle[1][1] == '5' && puzzle[1][2] == '6' 
                    && puzzle[2][0] == '7' && puzzle[2][1] == '8' && puzzle[2][2] == ' '){
                return true;
                
            }
            else {return false;}
        }
    Last edited by SapphireSpark; 12-03-2008 at 09:11 PM.

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    that while loop you have does the same thing the checkSolved method would do. So your saying while solved is false ask user for input. So instead of having the while loop you would do
    Java Code:
    checkSolved(puzzle){
       check each position
       if (not solved){
          ask user for swap info
           checkSolved();
       }
       else{
           puzzle solved. display results
       }
    }

  5. #5
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    You mean, like this?

    Java Code:
    for(search through every element in the array){
    
         if (puzzle[0][0] != '1' || puzzle[0][1] != '2' || puzzle[0][2] != '3' 
                    || puzzle[1][0] != '4' || puzzle[1][1] != '5' || puzzle[1][2] != '6' 
                    || puzzle[2][0] != '7' || puzzle[2][1] != '8' || puzzle[2][2] != ' '){
                     input = JOptionPane.showInputDialog(null, "Enter number to switch:");
                     checkSolved(); //[B]like this?[/B]
          }
           else{
                   System.out.println("Puzzle Solved!");
                   System.out.println(puzzle[0][0] + " " + puzzle[0][1] + " " + puzzle[0][2]);
    		System.out.println(puzzle[1][0] + " " + puzzle[1][1] + " " + puzzle[1][2]);
    		System.out.println(puzzle[2][0] + " " + puzzle[2][1] + " " + puzzle[2][2]);
    		System.out.println("");
    
                   }
    
    }
    Last edited by SapphireSpark; 12-04-2008 at 02:13 AM. Reason: code

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    ya that's pretty much it. It always checks if it's correct after the user swaps something and if not it just hops back up a step and asks again.

  7. #7
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    Ok, but where do I call it from? The main method? At the beginning?

  8. #8
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    ya you main method could be as simple as
    main(String[] args){
    declare variables

    display();
    checkSolved();
    display();


    }

Similar Threads

  1. change the square to triangle java
    By anotsu in forum New To Java
    Replies: 3
    Last Post: 07-09-2009, 11:17 AM
  2. Creating a New Method for Square Root Loop
    By SapphireSpark in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 01:21 PM
  3. SWT OpenGL snippet: draw a square
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 09:29 PM
  4. Java Drawing PUZZLE
    By Cyorxamp in forum AWT / Swing
    Replies: 3
    Last Post: 06-09-2008, 10:35 AM
  5. Problem using buttons to creat a magic square game
    By goldman in forum New To Java
    Replies: 5
    Last Post: 05-05-2008, 04:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •