# 8-Square puzzle loop

• 12-03-2008, 09:32 PM
SapphireSpark
[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.

http://fc72.deviantart.com/fs38/f/20...pfruitloop.jpg

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?

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;         } }```
• 12-03-2008, 09:56 PM
xcallmejudasx
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.
• 12-03-2008, 10:00 PM
SapphireSpark
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?

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;}     }```
• 12-03-2008, 10:11 PM
xcallmejudasx
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
Code:

```checkSolved(puzzle){   check each position   if (not solved){       ask user for swap info       checkSolved();   }   else{       puzzle solved. display results   } }```
• 12-04-2008, 03:12 AM
SapphireSpark
You mean, like this?

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("");               } }```
• 12-04-2008, 04:43 PM
xcallmejudasx
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.
• 12-04-2008, 06:23 PM
SapphireSpark
Ok, but where do I call it from? The main method? At the beginning?
• 12-04-2008, 08:21 PM
xcallmejudasx
ya you main method could be as simple as
main(String[] args){
declare variables

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

}