Results 1 to 3 of 3
Thread: Peg Puzzle Solver GUI
- 11-11-2013, 09:39 AM #1
Member
- Join Date
- Nov 2013
- Posts
- 1
- Rep Power
- 0
Peg Puzzle Solver GUI
So for my algorithms class I am developing a program that solves the triangular peg puzzle game and shows the solution through the use of checkboxes.
1 I am kind of a novice when it comes to Java because I havent used it in 4 years
2 Im using checkboxes because I had little to no experience with SWT and eclipse gui interface system and I had no other ideas
So I currently have the code to the point where I'm creating the checkboxes and then recursively solving the puzzle by making a move and then checking to see if that move leads to a solved state. The problem I'm having is when the program displays the checkboxes they seem to be moving towards a solution and then just start showing all possible moves(when i have the solve function inside of the constructBx function). I know the recursive calling of the constructBx function probably has something to do with it but I can't figure out how.
The only reason I had the solve function inside the function creating the boxes is because thats the only way i could figure out how to setSelection for the checkboxes based on the outcome of the solve function. If the solve function is seperate I can't access the checkboxes to setSelection. Basically I iterate through the integer array P[] and set each checkbox to true if P[checkbox ID] is 1.
So I don't know if P[] being global is the issue(i know its probably frowned upon but I'm just trying to get it done at this point) or if the checkboxes are the issue(since I don't fully understand how they are being initialized here) but the problems I want to fix are obviously getting it to show the correct sequence of moves, and also to get it to change the checkbox selection states in the same window based on when I click a button or something as opposed to having to close the window to get a new set of checkboxes.
I will post the code as soon as I get a tip on how to do that, cause i've seen people get mad in other forums when they don't know how to post code properly and just copy paste it inJava Code:import org.eclipse.swt.events.FocusEvent; import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class SWTButtonRev2 { static int[] P = {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; static int[][] moves= new int[][] { {0,1,3,},{0,2,5},{1,4,8},{1,3,6},{2,4,7},{2,5,9}, {3,1,0},{3,4,5},{3,7,12,},{3,6,10},{4,8,13},{4,7,11}, {5,2,0},{5,9,14},{5,8,12},{5,4,3},{6,3,1},{6,7,8}, {7,4,2},{7,8,9},{8,4,1},{8,7,6},{9,5,2},{9,8,7},{10,6,3}, {10,11,12},{11,7,4},{11,12,13},{12,7,3},{12,8,5}, {12,13,14},{12,11,10},{13,12,11},{13,8,4},{14,9,5},{14,13,12} }; public static void main (String [] args) { int i = 0,j = 0,k = 0; constructBx(); } public static boolean constructBx(){ Display display = new Display (); Shell shell = new Shell(display); int xaxis; int count = 1; int n, i, j, k, t; //check box Button[] checkBx = new Button[15]; //top box checkBx[0] = new Button(shell, SWT.CHECK); if(P[0] == 1){ checkBx[0].setSelection(true); } checkBx[0].setLocation(100,25); checkBx[0].pack(); //row 2 for(t = 0; t<2; t++){ xaxis = 50*t + 75; checkBx[t] = new Button(shell, SWT.CHECK); if(P[count] == 1){ checkBx[t].setSelection(true); } checkBx[t].setLocation(xaxis,50); checkBx[t].pack(); count++; } //row 3 for(t = 0; t<3; t++){ xaxis = 50*t + 50; checkBx[t] = new Button(shell, SWT.CHECK); if(P[count] == 1){ checkBx[t].setSelection(true); } checkBx[t].setLocation(xaxis,75); checkBx[t].pack(); count ++; } //row 4 for(t = 0; t<4; t++){ xaxis = 50*t + 25; checkBx[t] = new Button(shell, SWT.CHECK); if(P[count] == 1){ checkBx[t].setSelection(true); } checkBx[t].setLocation(xaxis,100); checkBx[t].pack(); count++; } //row 5 for(t = 0; t<5; t++){ xaxis = 50*t + 0; checkBx[t] = new Button(shell, SWT.CHECK); if(P[count] == 1){ checkBx[t].setSelection(true); } checkBx[t].setLocation(xaxis,125); checkBx[t].pack(); count++; } shell.setSize(240,200); shell.open (); while (!shell.isDisposed ()) { if (!display.readAndDispatch ()) display.sleep (); } display.dispose (); if(solved()) return true; for (n=0; n<=35; n++){ i = moves[n][0]; j = moves[n][1]; k = moves[n][2]; if(make_move(i,j,k)){ if(constructBx()){ System.out.println("Move " + i + " to " + k + " over " + j); //change selection state of check boxes P[i] = 0; P[j] = 0; P[k] = 1; for(int b = 0; b<15; b++){ if(P[b] == 1) checkBx[b].setSelection(true); } return true; } else undo_move(i,j,k); } } return false; } public static int pegcount(int[] P){ int count = 0; for(int i = 0; i<15; i++){ if(P[i] == 1) count++; } return count; } public static boolean solved(){ if(pegcount(P) == 1) return true; else return false; } public static boolean peg(int i){ if(P[i] == 1) return true; else return false; } public static boolean hole(int i){ if(P[i] == 0) return true; else return false; } public static boolean make_move(int i, int j, int k){ if(peg(i) && peg(j) && hole(k)){ P[i] = 0; P[j] = 0; P[k] = 1; return true; } else return false; } public static void undo_move(int i, int j, int k){ if(hole(i) && hole(j) && peg(k)){ P[i] = 1; P[j] = 1; P[k] = 0; } } }
- 11-11-2013, 03:39 PM #2
Re: Peg Puzzle Solver GUI
P[] being globalIf you don't understand my response, don't ignore it, ask a question.
- 11-11-2013, 04:48 PM #3
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Peg Puzzle Solver GUI
This would be a good moment to go learn about classes and objects in stead of making everything static. When you know and understand how to use Java as it was intended (object oriented), problems like this tend to disappear.
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
Similar Threads
-
StringIndexOutOfBoundsException- Crossword Solver App.
By numbskull20 in forum Advanced JavaReplies: 3Last Post: 12-01-2010, 02:33 PM -
The Maze solver Problem
By robbins in forum Advanced JavaReplies: 17Last Post: 11-23-2010, 08:14 PM -
Sudoku solver with threads
By judas in forum New To JavaReplies: 8Last Post: 05-27-2010, 04:07 PM -
How to best design a calculus solver
By xcallmejudasx in forum New To JavaReplies: 4Last Post: 06-25-2009, 06:27 AM -
Rubiks Cube Solver
By sufs2000 in forum Advanced JavaReplies: 0Last Post: 06-03-2008, 04:20 PM
Bookmarks