Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Source code for "Towers of Hanoi"?

Options
  • 12-12-2007 5:01pm
    #1
    Registered Users Posts: 7,155 ✭✭✭


    Hey,

    I'm doing a project on reinforcement learning and i think that the towers of hanoi puzzle to get it going. Only problem is i cant find a decent source code for it! Did anyone ever write this program or know where i could get it from? Its only an internal college project and i will give references to the author of the code. Thanks in advance

    EDIT: Forgot to say its java i'm looking for


Comments

  • Registered Users Posts: 901 ✭✭✭EL_Loco


    looks more like a request for a java project. have you to write it? or are you just looking to show the application because it shows something you're trying to prove for your reinforcement learning.

    http://www.cut-the-knot.org/recurrence/hanoi.shtml


  • Registered Users Posts: 7,155 ✭✭✭witnessmenow


    No i dont have to write it. I have a source code here, but the authors email address no longer exists so i'm a bit weary of using it without permission.

    I think you need to buy that applet off them!

    I will be writing code to run through the program, learning from its mistakes or whatever and eventually knowing whats the best way to solve it with the least moves. Thats my project , the game/puzzle can be anything but the towers of hanoi seems like a good one seeing as it has very defined states , which is essential for the application of reinforcement learning!


  • Registered Users Posts: 1,712 ✭✭✭neil_hosey


    then surely your able to write the towers of hanoi?


  • Registered Users Posts: 545 ✭✭✭ravydavygravy


    I agree. Anyway, you'll find that it'll be easier to link up your reinforement learning harness to the hanoi program if you write it in such a way that you decide how the program gets called, and how it runs... (For example, how did you plan on getting your project to run an applet over and over? Wouldn't it be easier to call a small text based program?)

    Many algorithms for implementing hanoi exist (recursive and non-recursive) - heres a page that descibes one algorithm - http://yupana.autonoma.edu.co/publicaciones/yupana/003/hanoi/hanoi_eng.html - it should be pretty simple to impement from this...


  • Registered Users Posts: 304 ✭✭PhantomBeaker


    Having helped a friend with hooking some premade tower of hanoi stuff into a test harness, I can say that the simulation is really simple, but trying to use premade code can be an absolute pain.

    In the end I subclassed a stack class for him. All I did was one thing, I overrode push() so that it would only work if I was trying to push a smaller block than was on the top, otherwise it threw an exception. All he had to do was create three of them and he had his simulation. Given that he was getting the machine to learn how to solve it, that's all he needed.

    The moral of the story? Sometimes getting Other People's Code to work with your code can be a royal pain.

    Aoife


  • Advertisement
  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    Hey,

    I'm doing a project on reinforcement learning and i think that the towers of hanoi puzzle to get it going. Only problem is i cant find a decent source code for it! Did anyone ever write this program or know where i could get it from? Its only an internal college project and i will give references to the author of the code. Thanks in advance

    EDIT: Forgot to say its java i'm looking for
    Depends on your experience in coding and what sort of experience your target audience are? Would they get more use or help from the recursive or non-recursive solution??

    Find the algorithm and implement in your chosen language (ten seconds in google and you'll find pseudo-code to both), you're writing in Java, so there's no problem in implementing it. Write it yourself in Java, it should take no more than half hour if you understand the problem.


  • Registered Users Posts: 7,155 ✭✭✭witnessmenow


    Thanks for the suggestions, in the end i wrote my own

    here it is if anyone is interested or wants to use it
    import javax.swing.JOptionPane;
    
    //Brian Lough
    //17/12/07
    //ecenuig@gmail.com
    public class Tower 
    {
    	public static void main( String[] args ) 
    	{
    		int towerSelect = 0,
    			destination = 0,
    			tempBlock = 0,
    			tempLocation = 0,
    		    numBlocks = 0,
    		    testCompleted = 0,
    			completedCheck =0;
    		String tempStr;
    		Boolean check = false;
    		Boolean towerCheck = false;
    		Boolean completed = false;
    		Boolean nonZero = false;
    		Boolean moving = false;
    		
    		while (check == false)
    		{
    			//Selecting the number of blocks to play with
    			tempStr = JOptionPane.showInputDialog("Please enter number of blocks (3-8)");
    			numBlocks = Integer.parseInt(tempStr);
    			
    			if ((numBlocks>=3)&&(numBlocks<=8))
    			{
    				check = true;	
    			}
    			else
    			{
    				JOptionPane.showMessageDialog(null, "Incorrect selection");
    			}
    		}
    		//making an array using the previous selection
    		int[][] game = new int[numBlocks][3] ;
    		
    		//Setting up first tower 
    		for ( int i = 0; i < numBlocks; i++ )
    		{
    			game[i][0]= (i + 1);
    			completedCheck = completedCheck + (i+1);
    		}
    		
    		while(completed == false)
    		{
    			//Printing the tower
    			for ( int i = 0; i < numBlocks; i++ )
    			{
    				for ( int j = 0; j < 3; j++ )
    				{
    					System.out.print(" ");
    					System.out.print( game[i][j] );
    					System.out.print(" ");
    				}
    					System.out.println();
    			}
    			System.out.println("---------");
    			
    			//checking for completion
    			testCompleted=0;
    			for ( int i = 0; i < numBlocks; i++ )
    			{
    				testCompleted = game[i][2]+ testCompleted;
    			}
    			if (testCompleted==completedCheck)
    			{
    				JOptionPane.showMessageDialog(null, "Congrats you have won!!");
    				completed=true;
    			}
    			
    			if (completed==false)
    			{
    				moving = false;
    				while(moving == false)
    				{
    					towerCheck=false;
    					while(towerCheck == false)
    					{
    						tempStr = JOptionPane.showInputDialog("Please select tower (1,2 or 3)");
    						towerSelect = Integer.parseInt(tempStr);
    						if ((towerSelect>=1)&&(towerSelect<=3))
    						{
    							for ( int i = numBlocks; i > 0; i-- )
    							{
    								if (game[i-1][(towerSelect-1)] != 0)
    								{
    									
    									tempLocation = i-1;
    									tempBlock = (game[i-1][(towerSelect-1)]);
    									towerCheck = true;
    								}
    							}
    							if (towerCheck==false)
    							{
    								JOptionPane.showMessageDialog(null, "there is no blocks on this tower");
    							}
    						}
    						else
    						{
    							JOptionPane.showMessageDialog(null, "Invalid selection");
    						}
    					}
    					tempStr = JOptionPane.showInputDialog("Please select destination (1,2 or 3)");
    					destination = Integer.parseInt(tempStr);
    					if ((destination>=1)&&(destination<=3))
    					{
    						if (towerSelect!=destination)
    						{
    							nonZero=false;
    							for ( int i = 0; i < numBlocks; i++ )
    							{
    								if (game[i][(destination-1)] != 0)
    								{
    									nonZero=true;
    									if ((game[i][(destination-1)])>tempBlock)
    									{
    										(game[i-1][(destination-1)])=tempBlock;
    										(game[tempLocation][towerSelect-1])=0;
    										moving=true;
    									}
    									else
    									{
    										JOptionPane.showMessageDialog(null, "Selected block is bigger than destination block");
    									}
    									i=numBlocks;
    								}
    								
    							}
    							if (nonZero == false)
    							{
    								game[numBlocks-1][destination-1]=tempBlock;
    								game[tempLocation][towerSelect-1]=0;
    								moving=true;
    							}
    						}
    						else
    						{
    							//Exiting the loop if destination and select are the same
    							moving=true;
    						}
    										
    					}
    				}
    			}
    		}
    			
    		
    		
    	System.exit(0);	
    	}
    	
    	
    }
    


Advertisement