Results 1 to 3 of 3
  1. #1
    Harlow777 is offline Member
    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    Default 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 in
    Java 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;		
    	}
    }
    
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Peg Puzzle Solver GUI

    P[] being global
    Because it is static, there is only one copy of the array being used. If the logic expects changes to be backed out when the recursive method returns, the array wouldn't be restored to the state it was in when the method was called. Any changes made in the method are kept.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,691
    Rep Power
    5

    Default 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

  1. StringIndexOutOfBoundsException- Crossword Solver App.
    By numbskull20 in forum Advanced Java
    Replies: 3
    Last Post: 12-01-2010, 01:33 PM
  2. The Maze solver Problem
    By robbins in forum Advanced Java
    Replies: 17
    Last Post: 11-23-2010, 07:14 PM
  3. Sudoku solver with threads
    By judas in forum New To Java
    Replies: 8
    Last Post: 05-27-2010, 03:07 PM
  4. How to best design a calculus solver
    By xcallmejudasx in forum New To Java
    Replies: 4
    Last Post: 06-25-2009, 05:27 AM
  5. Rubiks Cube Solver
    By sufs2000 in forum Advanced Java
    Replies: 0
    Last Post: 06-03-2008, 03:20 PM

Posting Permissions

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