Now this is an assignment for me, but ive spent the last two weeks writing this code day and night, I just cant get it out of my head, I have to beat it lol. So anyway, two days ago I won and completed my program exactly the way I wanted it and I decided to try and use JFrameBuilder to build me a GUI to use my code. Well three sleepless night later its totally beaten me :( I would be so grateful if someone could take a look for me and tell me where im going wrong. I decided once the gui loaded up to use a start button to start running my code. Now I dont want it to do anything fancy, I just was trying to get it to display my code in the JTextArea but its absolutley defeated me. I think I have two problems, one when the code is run, I dont think its pausing to wait for the user input, like at the "Would you like to start a new Towers of Hanoi game? [Y/N]" prompt. And maybe im trying to execute my code the wrong way???

First i'll post my main GUI code then i'll post my working game code main im trying to integrate.

I'd be eternally grateful if anyone has any ideas.

package visualassignment2;

 
 import java.awt.*; 
 import java.awt.event.*; 
 import javax.swing.*;
 import boardage.*;
 import EasyIn.*;  
 import java.io.*;

  class Error extends Exception {
    public Error() {
        super("*******ERROR*******\n");
        }
}

class badInput extends Exception {
    public badInput() {
        super("Wrong Input type...  Please try again!\n");
        }
}
 
 public class Main extends JFrame
 { 
 	// Declared variables 
 	private JLabel jLabel1; 
        private JLabel jLabel3; 
 	private JTextField input; 
 	private JTextArea displayScreen; 
 	private JScrollPane jScrollPane1;
        private JProgressBar jProgressBar1;  	 
 	private JButton b1; 
 	private JButton b2; 
 	private JButton b3; 
 	private JButton b4; 
 	private JButton b5; 
 	private JButton b6; 
        private JButton b7;
 	private JPanel  contentPane;
        private board   bd;
 	// End of variables declaration 
  
        //Constructor
 	public Main() 
 	{ 
 		super(); 
 		initializeComponent();  
 		this.setVisible(true);
                bd = new board();
 	} 
  
 	
        //This method is called from within the constructor to initialize the form.
 	private void initializeComponent() 
 	{ 
 		jLabel1 = new JLabel(); 
                jLabel3 = new JLabel(); 
 		input = new JTextField(); 
 		displayScreen = new JTextArea(); 
 		jScrollPane1 = new JScrollPane();
                jProgressBar1 = new JProgressBar();  		 
 		b1 = new JButton();
                
 		b2 = new JButton(); 
 		b3 = new JButton(); 
 		b4 = new JButton(); 
 		b5 = new JButton(); 
 		b6 = new JButton(); 
                b7 = new JButton();
 		contentPane = (JPanel)this.getContentPane();          


  
 		// 
 		// jLabel1 
 		// setting image location
 		jLabel1.setIcon(new ImageIcon("K:\\Portable Start Menu\\Documents\\BInfoTech\\2nd Year\\Java\\visualassignment2\\build\\hanoititle.jpg")); 
 		//
                // jLabel3 
 		// Labels the input on the display
 		jLabel3.setText("Input"); 
                //
 		// jTextField1 
 		// This is where the user input goes
                input.setText("Type here");
 		input.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				jTextField1_actionPerformed(e); 
 			} 
  
 		}); 
 		input.addKeyListener(new KeyAdapter() { 
 			public void keyPressed(KeyEvent e) 
 			{ 
 				jTextField1_keyPressed(e); 
 			} 
  
 		}); 
 		// 
 		// jTextArea1 
 		//sets wether the main display for instructions and current gamestate is editable or not
 		displayScreen.setEditable(false);
                // Add display area with scroll bars                
 		// 
 		// jScrollPane1 
 		// scrolls textarea1 when needed
 		jScrollPane1.setViewportView(displayScreen); 
 		// jProgressBar1 
 		// sets the color of the progress bar
 		jProgressBar1.setForeground(new Color(246, 5, 21)); 
 		// 
 		
                // 
 		// jButton1 
 		// Move from peg1 to peg2
 		b1.setBackground(new Color(255, 255, 255)); 
 		b1.setForeground(new Color(0, 0, 0)); 
 		b1.setText("Peg 1 to 2"); 
 		b1.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				jButton1_actionPerformed(e);
 			} 
  
 		}); 
 		// 
 		// jButton2 
 		// Move from peg1 to peg3
 		b2.setBackground(new Color(255, 255, 255)); 
 		b2.setForeground(new Color(0, 0, 0)); 
 		b2.setText("Peg 1 to 3"); 
 		b2.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				jButton2_actionPerformed(e); 
 			} 
  
 		}); 
 		// 
 		// jButton3 
 		// Move from peg2 to peg1
 		b3.setBackground(new Color(255, 255, 255)); 
 		b3.setForeground(new Color(0, 0, 0)); 
 		b3.setText("Peg 2 to 1"); 
 		b3.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				jButton3_actionPerformed(e); 
 			} 
  
 		}); 
 		// 
 		// jButton4 
 		// Move from peg2 to peg3
 		b4.setBackground(new Color(255, 255, 255)); 
 		b4.setForeground(new Color(0, 0, 0)); 
 		b4.setText("Peg 2 to 3"); 
 		b4.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				jButton4_actionPerformed(e); 
 			} 
  
 		}); 
 		// 
 		// jButton5 
 		// Move from peg3 to peg1
 		b5.setBackground(new Color(255, 255, 255)); 
 		b5.setForeground(new Color(0, 0, 0)); 
 		b5.setText("Peg 3 to 1"); 
 		b5.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 			displayScreen.setText("dude, seriously");
	
                            jButton5_actionPerformed(e); 
 			} 
  
 		}); 
 		// 
 		// jButton6 
 		// Move from peg3 to peg2
 		b6.setBackground(new Color(255, 255, 255)); 
 		b6.setForeground(new Color(0, 0, 0)); 
 		b6.setText("Peg 3 to 2"); 
 		b6.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				jButton6_actionPerformed(e); 
 			} 
  
 		}); 
                // 
 		// jButton5 
 		// Start game button
 		b7.setText("START GAME"); 
 		b7.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				jButton7_actionPerformed(e); 
 			} 
  
 		}); 
 		// 
 		// contentPane 
 		// 
 		contentPane.setLayout(null); 
 		contentPane.setBackground(new Color(155, 155, 209)); 
 		addComponent(contentPane, jLabel1, -1,0,526,282);
                addComponent(contentPane, jLabel3, 11,475,65,18);
 		addComponent(contentPane, input, 9,452,228,22); 
 		addComponent(contentPane, jScrollPane1, 9,289,505,155);
                addComponent(contentPane, jProgressBar1, 11,280,503,9);  		
 		addComponent(contentPane, b1, 247,450,83,28); 
 		addComponent(contentPane, b2, 246,485,83,28); 
 		addComponent(contentPane, b3, 338,450,83,28); 
 		addComponent(contentPane, b4, 338,485,83,28); 
 		addComponent(contentPane, b5, 430,450,83,28); 
 		addComponent(contentPane, b6, 430,485,83,28);
                addComponent(contentPane, b7, 66,486,132,25); 
 		// 
 		// Main 
 		// 
 		this.setTitle("The Towers of Hanoi"); 
 		this.setLocation(new Point(112, 32)); 
 		this.setSize(new Dimension(538, 555)); 
 		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
 		this.setResizable(false); 
 	} 
    
 	 
           
            
 	/** Add Component Without a Layout Manager (Absolute Positioning) */ 
 	private void addComponent(Container container,Component c,int x,int y,int width,int height) 
 	{ 
 		c.setBounds(x,y,width,height); 
 		container.add(c); 
 	} 
  
 	// 
 	// TODO: Add any appropriate code in the following Event Handling Methods 
 	// 
 	private void jTextField1_actionPerformed(ActionEvent e) 
 	{ 
 		System.out.println("\njTextField1_actionPerformed(ActionEvent e) called."); 
 		// TODO: Add any handling code here 
  
 	} 
        
        	private void jTextField1_keyPressed(KeyEvent e) 
 	{ 
 		System.out.println("\njTextField1_keyPressed(KeyEvent e) called."); 
 		// TODO: Add any handling code here 
  
 	} 
                
 	private void jButton1_actionPerformed(ActionEvent e) 
 	{
  
 	} 
  
 	private void jButton2_actionPerformed(ActionEvent e) 
 	{ 
 		System.out.println("\njButton2_actionPerformed(ActionEvent e) called."); 
 		// TODO: Add any handling code here 
  
 	} 
  
 	private void jButton3_actionPerformed(ActionEvent e) 
 	{ 
 		System.out.println("\njButton3_actionPerformed(ActionEvent e) called."); 
 		// TODO: Add any handling code here 
  
 	} 
  
 	private void jButton4_actionPerformed(ActionEvent e) 
 	{ 
 		System.out.println("\njButton4_actionPerformed(ActionEvent e) called."); 
 		// TODO: Add any handling code here 
  
 	} 
  
 	private void jButton5_actionPerformed(ActionEvent e) 
 	{ 
 		System.out.println("\njButton5_actionPerformed(ActionEvent e) called."); 
 		// TODO: Add any handling code here 
  
 	} 
  
 	private void jButton6_actionPerformed(ActionEvent e) 
 	{ 
 		System.out.println("\njButton6_actionPerformed(ActionEvent e) called."); 
 		// TODO: Add any handling code here 
  
 	} 
 
         	private void jButton7_actionPerformed(ActionEvent e) 
 	{ 
 		 final boolean finished = false;
 		do  {            
            
                    displayScreen.setText("Would you like to start a new Towers of Hanoi game? [Y/N]");
            char yn = input.getText().charAt(0);          

            if (yn == 'Y'  || yn == 'y'){
                //new instance of the board every game
                board bd = new board();               
                displayScreen.setText("M)anual or A)uto mode?");
                char ma = input.getText().charAt(0);
                if(ma == 'm' || ma == 'M'){
                    int count = 0;
//                    String d = JOptionPane.showInputDialog("How many discs would you like to use?");
                    displayScreen.setText("How many discs would you like to use?");
                    // Parse input into integer
                    String d = input.getText();
                    int discNum = Integer.parseInt(d);
                    bd.board(discNum);
                    int optimum = (int)Math.pow(2,discNum)-1 ;
                    System.out.println("\n\n\n\n\n\nThe optimum number of moves to win in is "+optimum+"\n\n");
                    bd.pushPeg();
                    while(!bd.gameFINISH()){
                        //printing the current game state
                        bd.printBoard();                      

//                        System.out.println("So far you have made "+count+" moves.");
//                        System.out.println("Make your move!\nPlease choose peg to move from and then which peg to move to.");
//                        System.out.println("E.g 23 -- would move a disk from peg 2 to peg 3\n Your move.....\n\n");
                        String f = JOptionPane.showInputDialog("So far you have made "+count+" moves.\nPlease choose peg to move from and then which peg to move to.\nE.g 23 -- would move a disk from peg 2 to peg 3");
                        // Parse input into integer
                        int move = Integer.parseInt(f);
                        count++;                        
                        if (move==12){                                        
                            try{
                              bd.movePeg12();  
                            }catch (Exception ee){
                               displayScreen.setText("\n\n*** Bad Input, please try again! \n");                          
                            }                                                                               

                        } else if (move==13) {
                            try{
                              bd.movePeg13();  
                            }catch (Exception ee){
                               displayScreen.setText("\n\n*** Bad Input, please try again! \n"); 
                            }

                        }else if (move==21) {
                            try{
                              bd.movePeg21();  
                            }catch (Exception ee){
                               displayScreen.setText("\n\n*** Bad Input, please try again! \n"); 
                            }                                        
                        }else if (move==23) {
                            try{
                              bd.movePeg23();  
                            }catch (Exception ee){
                                displayScreen.setText("\n\n*** Bad Input, please try again! \n");
                            }                                        
                        }else if (move==31) {
                            try{
                              bd.movePeg31();  
                            }catch (Exception ee){
                                displayScreen.setText("\n\n*** Bad Input, please try again! \n");
                            }                                        
                        }else if (move==32) {
                            try{
                              bd.movePeg32();  
                            }catch (Exception ee){
                                displayScreen.setText("\n\n*** Bad Input, please try again! \n");
                            } 

                        } if (bd.gameFINISH()){
                            

                            

                            System.out.println("\n\n\n\n\n\n\n\n");
                            bd.printBoard();
                            System.out.println("Congratulations!!  You have completed the game!!\n\n");
                            System.out.println("The optimum number of moves was "+optimum); 
                            System.out.println("It took you "+count+" moves to finish.\n\n\n");

                        }

                    }
                } else if (ma == 'a' || ma == 'A') {
                    displayScreen.setText("How many discs would you like to use?");
                    // Parse input into integer
                    String q = input.getText();
                    int discNum = Integer.parseInt(q);                    
                    
                    bd.board(discNum);
                    bd.pushPeg();
                    try{

                    bd.initSolve(discNum);
                    
                    } catch (Exception ee){
                        displayScreen.setText("\n\n*** Bad Input, please try again! \n");
                        
                    }
                }              
             }
            else if (yn == 'n' || yn == 'N'){                      
                System.out.println("       Thankyou for Playing");
                System.out.println("^**____*.Have a Nice Day.*____**^");
                
            }                   
        } while (!finished);
 
  
 	} 
        	
        public static void main(String[] args){ new Main(); }   	 
 	
             }

        }
           
  }

Heres the code im unsucessfully trying to put in there :(

Any help would be greatly appreciated

package assignment2good;
import boardage.*;
import EasyIn.*;
import javax.swing.JOptionPane;
import javax.swing.JDialog;

// Operators exceeded exception class
class Error extends Exception {
    public Error() {
        super("*******ERROR*******\n");
        }
}

class badInput extends Exception {
    public badInput() {
        super("Wrong Input type...  Please try again!\n");
        }
}

public class Main {
    
     /** Creates a new instance of Main */
    public Main() {
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       boolean finished = false;
            
            char yn  = JOptionPane.showInputDialog("Would you like to start a new Towers of Hanoi game? [Y/N]").charAt(0); 
            if (yn == 'Y'  || yn == 'y'){
                //new instance of the board every game
                board bd = new board();               
                char ma  = JOptionPane.showInputDialog("M)anual or A)uto mode?").charAt(0);
                if(ma == 'm' || ma == 'M'){
                    int count = 0;
                    String d = JOptionPane.showInputDialog("How many discs would you like to use?");
                    // Parse input into integer
                    int discNum = Integer.parseInt(d);
                    bd.board(discNum);
                    int optimum = (int)Math.pow(2,discNum)-1 ;
                    System.out.println("\n\n\n\n\n\nThe optimum number of moves to win in is "+optimum+"\n\n");
                    bd.pushPeg();
                    while(!bd.gameFINISH()){
                        //printing the current game state
                        bd.printBoard();            


                        String f = JOptionPane.showInputDialog("So far you have made "+count+" moves.\nPlease choose peg to move from and then which peg to move to.\nE.g 23 -- would move a disk from peg 2 to peg 3");
                        // Parse input into integer
                        int move = Integer.parseInt(f);
                        count++;                        
                        if (move==12){                                        
                            try{
                              bd.movePeg12();  
                            }catch (Exception e){
                               System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage());                          
                            }                                                                               

                        } else if (move==13) {
                            try{
                              bd.movePeg13();  
                            }catch (Exception e){
                               System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage()); 
                            }

                        }else if (move==21) {
                            try{
                              bd.movePeg21();  
                            }catch (Exception e){
                               System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage()); 
                            }                                        
                        }else if (move==23) {
                            try{
                              bd.movePeg23();  
                            }catch (Exception e){
                                System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage());
                            }                                        
                        }else if (move==31) {
                            try{
                              bd.movePeg31();  
                            }catch (Exception e){
                                System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage());
                            }                                        
                        }else if (move==32) {
                            try{
                              bd.movePeg32();  
                            }catch (Exception e){
                                System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage());
                            } 

                        } if (bd.gameFINISH()){
                            

                            

                            System.out.println("\n\n\n\n\n\n\n\n");
                            bd.printBoard();
                            System.out.println("Congratulations!!  You have completed the game!!\n\n");
                            System.out.println("The optimum number of moves was "+optimum); 
                            System.out.println("It took you "+count+" moves to finish.\n\n\n");

                        }

                    }
                } else if (ma == 'a' || ma == 'A') {
                    String q = JOptionPane.showInputDialog("How many discs would you like to use?");
                    // Parse input into integer
                    int discNum = Integer.parseInt(q);
                    bd.board(discNum);
                    bd.pushPeg();
                    try{

                    bd.initSolve(discNum);
                    
                    } catch (Exception e){
                        System.out.println(e.getMessage());
                        
                    }
                }              
             }
            else if (yn == 'n' || yn == 'N'){                      
                System.out.println("       Thankyou for Playing");
                System.out.println("^**____*.Have a Nice Day.*____**^");
                finished = true;
            }                   
        } while (!finished);
    }
}

Recommended Answers

All 3 Replies

Yes, the code in your action listener will execute in it's entirety without pause for input. If you don't want to use modal dialogs to prompt for input, then you need to separate the action listener code into section to respond to each input action (which will occur when the user presses enter in the input box). Here is a partial example to demonstrate what I mean

private void inputActionPerformed(java.awt.event.ActionEvent evt) {
    char inputChar = input.getText().toLowerCase().charAt(0);
    if(Character.isLetter(inputChar)) {
        switch(inputChar) {
            case 'y':
                displayScreen.setText("M)anual or A)uto mode?");
                input.setText("");
                input.requestFocus();
                break;

            case 'm':
                displayScreen.setText("How many discs would you like to use?");
                input.setText("");
                input.requestFocus();
                useManual = true;
                break;
        }
    } else {
        if(useManual) {
            int numberDiscs = 0;
            try {
                numberDiscs = Integer.parseInt(input.getText());
            } catch(NumberFormatException nfe) {
                nfe.printStackTrace();
            }
            // ...
        }
    }
}

Because you lose the linear question/answer behavior of using dialogs like in your original code, you have to maintain the state of the question series yourself. The code above (with the 'useManual' flag) is not really a good way to maintain that state, but a more proper example at this point would probably just cause more confusion. To avoid having to set a bunch of flags to keep track of which question you are on, an array of question strings could be used in conjunction with an index value that you increment after each question. But that is only one of many possible ways to set it up.

The point is that you have to maintain the input state of the series yourself, because you have moved from a linear model to an event-based model with that GUI. The action listener needs to be able to tell which question it's collecting input for, store that input, and display the next question.

Ok cool, i'll post the whole code to the non GUI example at the end of this post, just in case someone wants to run it to see what is actually happening. My problem im very new to using GUI's and im not too sure how to let the GUI run my code, im to use to linear style coding. I have an idea, but minimum experience. Like I know everything runs off click or input events, but I just can't remember how to get it working how I want. I dont need input for 12, 32 etc, the buttons I made handle that, so the input for those will be deleted out.

Oh and ive taken out the EasyIn packagae now too, I realised I didnt need it anymore, it just helped with Input and output but remeber I changed my code to using JOptionPane.

Thankyou very much Ezzaral, im still alot in the dark but you've given me something to try working on. I just dont know where to put the code to print out my board state and messages. and then where do I put the input prompting stuff??? Im going to sit here until I do it, but it could take me all week. If anyone could help me out it would save me alot of RF screen radiation :)

Heres my working non GUI example,
MAIN CLASS

package assignment2good;
import boardage.*;
import javax.swing.JOptionPane;
import javax.swing.JDialog;

// Operators exceeded exception class
class Error extends Exception {
    public Error() {
        super("*******ERROR*******\n");
        }
}

class badInput extends Exception {
    public badInput() {
        super("Wrong Input type...  Please try again!\n");
        }
}

public class Main {
    
     /** Creates a new instance of Main */
    public Main() {
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       boolean finished = false;

       do  {
            
            
            char yn  = JOptionPane.showInputDialog("Would you like to start a new Towers of Hanoi game? [Y/N]").charAt(0); 
            if (yn == 'Y'  || yn == 'y'){
                //new instance of the board every game
                board bd = new board();               
                char ma  = JOptionPane.showInputDialog("M)anual or A)uto mode?").charAt(0);
                if(ma == 'm' || ma == 'M'){
                    int count = 0;
                    String d = JOptionPane.showInputDialog("How many discs would you like to use?");
                    // Parse input into integer
                    int discNum = Integer.parseInt(d);
                    bd.board(discNum);
                    int optimum = (int)Math.pow(2,discNum)-1 ;
                    System.out.println("\n\n\n\n\n\nThe optimum number of moves to win in is "+optimum+"\n\n");
                    bd.pushPeg();
                    while(!bd.gameFINISH()){
                        //printing the current game state
                        bd.printBoard();                      

//                        System.out.println("So far you have made "+count+" moves.");
//                        System.out.println("Make your move!\nPlease choose peg to move from and then which peg to move to.");
//                        System.out.println("E.g 23 -- would move a disk from peg 2 to peg 3\n Your move.....\n\n");
                        
                        String f = JOptionPane.showInputDialog("So far you have made "+count+" moves.\nPlease choose peg to move from and then which peg to move to.\nE.g 23 -- would move a disk from peg 2 to peg 3");
                        // Parse input into integer
                        int move = Integer.parseInt(f);
                        count++;                        
                        if (move==12){                                        
                            try{
                              bd.movePeg12();  
                            }catch (Exception e){
                               System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage());                          
                            }                                                                               

                        } else if (move==13) {
                            try{
                              bd.movePeg13();  
                            }catch (Exception e){
                               System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage()); 
                            }

                        }else if (move==21) {
                            try{
                              bd.movePeg21();  
                            }catch (Exception e){
                               System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage()); 
                            }                                        
                        }else if (move==23) {
                            try{
                              bd.movePeg23();  
                            }catch (Exception e){
                                System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage());
                            }                                        
                        }else if (move==31) {
                            try{
                              bd.movePeg31();  
                            }catch (Exception e){
                                System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage());
                            }                                        
                        }else if (move==32) {
                            try{
                              bd.movePeg32();  
                            }catch (Exception e){
                                System.out.println("\n\n*** Bad Input, please try again! \n" + e.getMessage());
                            } 

                        } if (bd.gameFINISH()){
                            

                            

                            System.out.println("\n\n\n\n\n\n\n\n");
                            bd.printBoard();
                            System.out.println("Congratulations!!  You have completed the game!!\n\n");
                            System.out.println("The optimum number of moves was "+optimum); 
                            System.out.println("It took you "+count+" moves to finish.\n\n\n");

                        }

                    }
                } else if (ma == 'a' || ma == 'A') {
                    String q = JOptionPane.showInputDialog("How many discs would you like to use?");
                    // Parse input into integer
                    int discNum = Integer.parseInt(q);
                    bd.board(discNum);
                    bd.pushPeg();
                    try{

                    bd.initSolve(discNum);
                    
                    } catch (Exception e){
                        System.out.println(e.getMessage());
                        
                    }
                }              
             }
            else if (yn == 'n' || yn == 'N'){                      
                System.out.println("       Thankyou for Playing");
                System.out.println("^**____*.Have a Nice Day.*____**^");
                finished = true;
            }                   
        } while (!finished);
    }
}

BOARD CLASS

public class board{
    
    /** Creates a new instance of board */
    public board() {
        super();        
    } 
    peg peg1 = new peg();    
    peg peg2 = new peg();
    peg peg3 = new peg();
    public static int discNUM = 0;
    public static boolean gameFinish = false;
    
    public void board(int discNum){ 
        discNUM = discNum; 
    } 

    
    
    public void pushPeg(){        
        for(int i = discNUM; i > 0; i--) {
            peg1.push(i);
            
            
        }
    }    
    
    
    public void printBoard(){
       
        System.out.print("\n\n\n\n\n\n\n\n|");
        peg1.print();        
        System.out.print("|");
        peg2.print();
        System.out.print("|");
        peg3.print();
        System.out.print("\n\n\n\n\n\n\n");
        if(peg3.topNum() == 1 && peg2.topNum() == peg1.topNum()){ 
            gameFinish = true; 
        } 
        //do some debugging 
        System.out.println(peg1.topNum() + "" + peg2.topNum() + "" + peg3.topNum());            
    }
    
    public void movePeg12() throws Exception {
        int a =0;
        a = peg2.topNum();
        int b = peg1.pop();
        if (b >a){
            peg1.push(b);
            System.out.print("**12Illegal Move**\nCannot stack a larger number onto a smaller one!");
            throw new Exception ("**!12Illegal Move**\nCannot stack a larger number onto a smaller one!");
            
            }else{
                peg2.push(b);
                }        
    }
    public void movePeg13() throws Exception{
        int a =0;
        a = peg3.topNum();
        int b = peg1.pop();
        if (b >a){
            peg1.push(b);
            System.out.print("**Illegal Move**\nCannot stack a larger number onto a smaller one!");
            throw new Exception ("**Illegal Move**\nCannot stack a larger number onto a smaller one!");
            
            }else{
                peg3.push(b);
                }        
    }        

    public void movePeg21() throws Exception{
        int a =0;
        a = peg1.topNum();
        int b = peg2.pop();
        if (b >a){
            peg2.push(b);
            System.out.print("**Illegal Move**\nCannot stack a larger number onto a smaller one!");
            throw new Exception ("**Illegal Move**\nCannot stack a larger number onto a smaller one!");
            
            }else{
                peg1.push(b);
                }              
    }
    public void movePeg23() throws Exception{
        int a =0;
        a = peg3.topNum();
        int b = peg2.pop();
        if (b > a){
            peg2.push(b);
            System.out.print("**Illegal Move**\nCannot stack a larger number onto a smaller one!");
            throw new Exception ("**Illegal Move**\nCannot stack a larger number onto a smaller one!");
            
            }else{
                peg3.push(b);
                }              
    }
    public void movePeg31() throws Exception{
        int a =0;
        a = peg1.topNum();
        int b = peg3.pop();
        if (b > a){
            peg3.push(b);
            System.out.print("**Illegal Move**\nCannot stack a larger number onto a smaller one!");
            throw new Exception ("**Illegal Move**\nCannot stack a larger number onto a smaller one!");
            
            }else{
                peg1.push(b);
                }              
    }
    public void movePeg32() throws Exception{
        int a =0;
        a = peg2.topNum();
        int b = peg3.pop();
        if (b > a ){
            peg3.push(b);
            System.out.print("**Illegal Move**\nCannot stack a larger number onto a smaller one!");
            throw new Exception ("**Illegal Move**\nCannot stack a larger number onto a smaller one!");
            
            }else{
                peg2.push(b);
                }              
    }
    
//   
    
    public boolean gameFINISH(){
        int a = peg1.topNum();
        int b = peg2.topNum();
        int c = peg3.topNum();
        if (a==b && c == 1){
            
            return true;
        } 
           return false; 
    }      
    
    public void initSolve(int discNo){
        solve(discNo, peg1, peg3, peg2);
    }
    private void solve(int pegHeight, boardage.peg peg1, boardage.peg peg2, boardage.peg peg3) {
        try{
             int count = 0;
        
                if(pegHeight == 1) {
                    // Push off one onto another
                    peg2.push(peg1.pop());  printBoard();count++;
                    // we're done at this point 
                }else {
                    // Move discs
                    solve(pegHeight -1, peg1, peg3, peg2);
                    peg2.push(peg1.pop()); printBoard();
                    solve(pegHeight -1, peg3, peg2, peg1);
            }
        }catch (Exception e) {
              System.out.println(e + "*** Error!! ");
        }
    }
}

PEG CLASS

package boardage;
import linkage.*;

// Operators exceeded exception class
class UnderflowException extends Exception {
    public UnderflowException() {
        super("*******ERROR*******\n       Excessive operators\n....Please Try Aagain....");
        }
}

public class peg extends llist{
    private int top;
    
    public peg() {
        super();
        top = 0;
        
    }
    public int countPeg = 0;

    public int topNum(){
        return getTopInt();
    }
    
    public boolean pegEmpty(){
        return empty();
    } 
    public void push(int n) {
        addItem(n);
        countPeg++;
        top = n;
    }
    
    public int pop() throws Exception {
        try {
            countPeg--;
            return removeItem();
        }
        catch (EmptyListException e) {
            countPeg++;
            throw new UnderflowException();
        }
    }
    
    public void print() {
        printList();
    }   

    public int countPeg(){ 
        return countPeg; 
    }  
        
}

UNDERFLOW EXCEPTION CLASS

package boardage;

/**
 *
 * @author itmpm
 */
public class UnderflowException extends Exception {

    public UnderflowException() {
        super("Stack underflow");
    }
}

LLIST CLASS

package linkage;

/**
 *
 * @author dammit11
 */
public class llist {
  protected llnode head; // the first item in the list


      public llist() { 
        head=null;
      }



    public void addItem(int newVal) {
       llnode newNode= new llnode(newVal,head);
       head = newNode;
    }

    public boolean empty() {
        return head == null;
    }
    
    public int removeItem() throws EmptyListException {
        if (empty())
            throw new EmptyListException();
        else {
            int returnVal = head.dataVal();
            head = head.nextNode();
            return returnVal;
        }   
    }
//    public void printList() {
//        if (head==null)
//          System.out.print("");
//        else {
//          llnode runner=head;
//
//          while (runner!=null) {
//              
//            System.out.print(runner.dataVal() + " ");
//            
//            runner=runner.nextNode();
//          }
//        }
//        System.out.println();
//    }
    public void printList() { 
    printNode(head); 
    System.out.println(); 
} 

public void printNode(llnode node) { 
  if (node != null) { 
    printNode(node.nextNode()); 
    System.out.print(node.dataVal() + " "); 
  } 
}
public int getTopInt(){ 
        if(head == null){ 
            return (boardage.board.discNUM + 1); 
        }else{ 
            return head.dataVal(); 
        } 
    }
    public static String backwards(llnode head) {
        if (head == null)
            return null;
        if (head.next == null)            
            return Integer.toString(head.data);
        else
            return backwards(head.next) + "" + Integer.toString(head.data);
    }    
        
}

LLNODE CLASS

package linkage;


public class llnode {
    public int data;
    public llnode next;
    public llnode() {
        next=null;
    }
    public llnode(int val, llnode oldHead) {
        data=val;
        next=oldHead;
    }
    public int dataVal() {
        return data;
    }
    public llnode nextNode() {
        return next;
    }    
}

EMPTYLIST EXCEPTION CLASS

package linkage;

/**
 *
 * @author itmpm
 */
public class EmptyListException extends Exception {

    public EmptyListException() {
        super("List is empty");
    }
}

Thanks Ezz. I used what you said, worked on it all day and night and came up with a working solution.

Thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.