Results 1 to 8 of 8
Thread: 8-Square puzzle loop
- 12-03-2008, 09:32 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 38
- Rep Power
- 0
[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 09:58 PM. Reason: add picture
- 12-03-2008, 09:56 PM #2
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 #3
Member
- Join Date
- Oct 2008
- Posts
- 38
- Rep Power
- 0
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 10:11 PM.
- 12-03-2008, 10:11 PM #4
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 } }
- 12-04-2008, 03:12 AM #5
Member
- Join Date
- Oct 2008
- Posts
- 38
- Rep Power
- 0
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 03:13 AM. Reason: code
- 12-04-2008, 04:43 PM #6
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 #7
Member
- Join Date
- Oct 2008
- Posts
- 38
- Rep Power
- 0
Ok, but where do I call it from? The main method? At the beginning?
- 12-04-2008, 08:21 PM #8
Similar Threads
-
change the square to triangle java
By anotsu in forum New To JavaReplies: 3Last Post: 07-09-2009, 12:17 PM -
Creating a New Method for Square Root Loop
By SapphireSpark in forum New To JavaReplies: 14Last Post: 02-25-2009, 02:21 PM -
SWT OpenGL snippet: draw a square
By Java Tip in forum SWT TipsReplies: 0Last Post: 06-28-2008, 10:29 PM -
Java Drawing PUZZLE
By Cyorxamp in forum AWT / SwingReplies: 3Last Post: 06-09-2008, 11:35 AM -
Problem using buttons to creat a magic square game
By goldman in forum New To JavaReplies: 5Last Post: 05-05-2008, 05:04 AM
Bookmarks