Tuesday, 4 February 2014

Simulation Of Tower Of Hanoi in java.

The Tower of Hanoi (also called the Tower of Brahma or Lucas' Tower, and sometimes pluralised) is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
  1. Only one disk can be moved at a time.
  2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
  3. No disk may be placed on top of a smaller disk.
With three disks, the puzzle can be solved in seven moves. The minimum number of moves required to solve a Tower of Hanoi puzzle is 2n - 1, where n is the number of disks.

We Implemented TOH using applets in java.

//CODE:


/*

 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

//package netbeans;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author Vijay S Y
 */

public class TOH extends Applet implements ItemListener ,ActionListener,Runnable {
boolean first;
Choice from ,to; 
    int[] compArr=new int[]{0,2,0,1,2,1,0,2,1,0,1,2,0,2};
Button submit,reset,plycom;
String clr[]=new String[]{"RED","GREEN","BLUE"};
int f,t,cf,ct;
Thread circleThread;
int tos[]=new int[]{2,-1,-1};
int [][]stack=new int [][]{{2,1,0},
                            {3,3,3},
                            {3,3,3}};

boolean isvalid(){
  print();
  if(ct==t&&cf==f) return true;
   if(tos[t]==-1||stack[f][tos[f]]<stack[t][tos[f]]){  
       tos[t]++;
       stack[t][tos[t]]=stack[f][tos[f]];
       tos[f]--;
        ct=t;
        cf=f;
       return true;
   }

   return false;
}

void print(){
    for(int i=0;i<3;i++)
        System.out.print(tos[i] + "\t");
    System.out.print("\n");
}



@Override
    public void init() {
        setLayout(new FlowLayout(FlowLayout.CENTER));          
           first=false;
           f=t=0;
           ct=cf=5;      
        from =new Choice();
        to= new Choice();
        submit =new Button("SUBMIT");
        reset=new Button("RESET");
        plycom=new Button("COMPUTER_TERN");
       
        from.add("From_A"); to.add("To_A");
        from.add("From_B"); to.add("To_B");
        from.add("From_C");to.add("To_C");
       
        add(from);
        add(to);
        add(submit);
        add(reset);
        add(plycom);
        
        submit.addActionListener((ActionListener) this);
        reset.addActionListener((ActionListener) this);
        plycom.addActionListener((ActionListener) this);
       
        from.addItemListener((ItemListener) this);
        to.addItemListener((ItemListener) this);
       
       
               
    }
   
@Override
    public void itemStateChanged(ItemEvent ie){  
    f = from.getSelectedIndex();
    t=to.getSelectedIndex();
    }
  
    void reset(){
           f=t=0;
                ct=cf=5;
                tos[0]=2;
                tos[1]=-1;
                tos[2]=-1;
                //tos[3]=-1;
               
                stack[0][0]=2;  stack[1][0]=-1;  stack[2][0]=-1;
                stack[0][1]=1;  stack[1][1]=-1;  stack[2][1]=-1;
                stack[0][2]=0;  stack[1][2]=-1;  stack[2][2]=-1;
         
    }
   
@Override
@SuppressWarnings("empty-statement")
    public void actionPerformed(ActionEvent ae){
   
        String str=ae.getActionCommand();
        switch (str) {
            case "SUBMIT":
                repaint();
                break;
            case "RESET":
               reset();
                repaint();
                break;
            case "COMPUTER_TERN":
                System.out.println("in com");
                comp();
               
                break;
                 default:
                break;
        }
       
    }
  
       
       void comp(){
            f=t=0;
                cf=ct=5;
                //*****************************************************************
                tos[0]=2;
                tos[1]=-1;
                tos[2]=-1;
                // tos[3]=-1;
                stack[0][0]=2;  stack[1][0]=-1;  stack[2][0]=-1;
                stack[0][1]=1;  stack[1][1]=-1;  stack[2][1]=-1;
                stack[0][2]=0;  stack[1][2]=-1;  stack[2][2]=-1;
                // stack[0][3]=0;   stack[0][3]=-1;  stack[0][3]=-1;
           circleThread = new Thread((Runnable) this);
         circleThread.start();
             
       }
       
@Override
       public void run(){
        for(int i=0;i<14;i+=2){
            f=compArr[i];
            t=compArr[i+1];
             repaint();
             try {
                  Thread.sleep(1000);
               }
               catch (InterruptedException evt) {}
        }        
          reset();
          repaint();
       }
      
@Override
    public void paint(Graphics g){                       
       //******************************************BASE
           g.fillRoundRect(100, 550-100, 1020 ,20,20,20);
          //*******************************************C1        
          g.fillRoundRect(300, 190, 20,370-100,20,20);
          //********************************************C2
          g.fillRoundRect(600, 190, 20,370-100,20,20);
          //********************************************C3       
          g.fillRoundRect(900, 190, 20,370-100,20,20);
       //*************************************************************************************
      
          if(!isvalid()&&first){
              g.drawString("Invalid Move",450,150);
              first=true;
          }
       //*********************************************************************************************
        for(int i=0;i<=tos[0];i++){
            switch(stack[0][i]){
                case 2:g.setColor(Color.GREEN);
                       break;
                 case 0:g.setColor(Color.BLUE);
                        break;
                 case 1:g.setColor(Color.RED);
                       break;
                     // default:g.setColor(Color.yellow);
            }
          g.fillRoundRect(240-(50*stack[0][i]),530-100-20*i,(100*stack[0][i])+130, 20,20, 20);
        }
      //***************************************************************************************************
        for(int i=0;i<=tos[1];i++){
            switch(stack[1][i]){
                case 2:g.setColor(Color.GREEN);
                       break;
                 case 0:g.setColor(Color.BLUE);
                        break;
                 case 1:g.setColor(Color.RED);
                       break;
                 //default:g.setColor(Color.yellow);
            }
          g.fillRoundRect(540-(50*stack[1][i]),530-100-20*i,(100*stack[1][i])+130, 20,20, 20);
        }
       //*********************************************************************************************
        for(int i=0;i<=tos[2];i++){
            switch(stack[2][i]){
                case 2:g.setColor(Color.GREEN);
                       break;
                 case 0:g.setColor(Color.BLUE);
                        break;
                 case 1:g.setColor(Color.RED);
                       break;
                     // default:g.setColor(Color.yellow);
            }
          g.fillRoundRect(840-(50*stack[2][i]),530-100-20*i,(100*stack[2][i])+130, 20,20,20);
        }
       //***************************************************************************************************
      
       
        
    }
        }
 



//THML FILE

No comments:

Post a Comment