4 Replies - 9351 Views - Last Post: 03 June 2010 - 03:32 AM Rate Topic: -----

#1 very nice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 11-December 08

Code of hanoi tower in java

Posted 02 June 2010 - 02:11 PM

Hi evrey one
i have problem when i Write a program to solve the Hanoi towers problem using uninformed search techniques: BFS, DFS and IDS

the error is Exception in thread "main" java.lang.NullPointerException

my code:(static 3 dics)


package towerofhanoi;
import java.util.*;
import java.lang.Object.*;
/**
 *
 *
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Hanoi HT=new Hanoi();
        
        HT.initial();
       // System.out.println(HT.tower1.toString());
         
         
      
   /* HT.tower1.addFirst(1);
    HT.tower1.addLast(2);
    HT.tower1.addLast(3);


       HT.counter[0]=3;
       HT.counter[1]=0;
       HT.counter[2]=0;

       HT.goal[0]=0;
       HT.goal[1]=0;
       HT.goal[2]=3;*/
        //draw graph
        BinaryTree tree= new BinaryTree( );
       Queue q=new LinkedList();
       Stack s=new Stack();

        tree.SetTree(HT.counter);
        System.out.println(BFS(tree,q));
        System.out.println(DFS(tree,s));
        //System.out.println(IDS(tree));

    }

    public static String BFS(BinaryTree state, Queue fring)
    {
       
      Queue cloused =new LinkedList();
      Node node=new Node();
      Hanoi HT=new Hanoi();
      
        fring.add(state.root);
        if(fring.isEmpty()==true)
            return "failuer";
     while(!fring.isEmpty())
     {
         node= (Node)fring.remove();
         //node.setData(first);
         if(node.getData() == HT.goal)
             return "success";
         else if (cloused.contains(node)==false)
         { cloused.add(node);
            fring.addAll((Expand(node)));
          }

}
        return "failuer";
    }

public static Queue Expand(Node state)
{  Node node=new Node();
  Queue q=new LinkedList();
 Hanoi HT=new Hanoi();
 String[] moves=Actions(state.getData());
 
 for(String move: moves)
 {
     if(move.equals("MoveAtoB"))
   {  node=HT.moveAtoB();
      q.add(node);
   }
   else if(move.equals("MoveAtoC"))
    { node=HT.moveAtoC();
      q.add(node);
   }
   else if(move.equals("MoveBtoA"))
   { node=HT.moveBtoA();// 
     q.add(node);
   }
   else if(move.equals("MoveBtoC"))
   { node=HT.moveBtoC();// 
     q.add(node);
   }
   else if(move.equals("MoveCtoA"))
   { node=HT.moveCtoA();// 
     q.add(node);
   }
   else if(move.equals("MoveCtoB"))
   { node=HT.moveCtoB();// 
     q.add(node);
   }
 }
 return q;

}


  public static String[] Actions(int [] index)
  {
      String [] str=new String[6];

      if (index[0]==3)
      { str[0]="MoveAtoB";
          str[1]="MoveAtoC";
          return str;}

      else if (index[0]==2 & index[1]==1)
      { str[0]="MoveAtoB";
          str[1]="MoveBtoA";
          str[2]="MoveAtoC";
          str[3]="MoveBtoC";

          return str;}
      else if (index[0]==2 & index[2]==1)
      { str[0]="MoveAtoC";
          str[1]="MoveAtoB";
          str[2]="MoveCtoA";
          str[3]="MoveCtoB";

          return str;}
      else if (index[0]==1 & index[1]==1)
      { str[0]="MoveAtoC";
          str[1]="MoveAtoB";
          str[2]="MoveBtoA";
          str[3]="MoveBtoC";
          str[4]="MoveCtoA";
          str[5]="MoveCtoB";
          return str;}

      else if (index[0]==1 & index[2]==2) 
      { str[0]="MoveAtoC";
          str[1]="MoveAtoB";
          str[2]="MoveCtoA";
          str[3]="MoveCtoB";
          return str;}

      else if (index[0]==1 & index[1]==2)
      { str[0]="MoveAtoB";
          str[1]="MoveAtoC";
          str[2]="MoveBtoA";
          str[3]="MoveBtoC";
          return str;}

      else if (index[1]==2 & index[2]==1)
      { str[0]="MoveBtoC";
          str[1]="MoveBtoA";
          str[2]="MoveCtoA";
          str[3]="MoveCtoB";
          return str;}

      else if (index[1]==1 & index[2]==2)
      { str[0]="MoveBtoC";
          str[1]="MoveBtoA";
          str[2]="MoveCtoA";
          str[3]="MoveCtoB";
          return str;}

      else if ( index[1]==3)
      { str[0]="MoveBtoA";
          str[1]="MoveBtoC";
         return str;}
      
      return str;

  }

public static String DFS(BinaryTree state,Stack fring)
{
  Queue cloused =new LinkedList();
      Node node=new Node();
      Hanoi HT=new Hanoi();

        fring.push(state.root);
        if(fring.isEmpty()==true)
            return "failuer";
     while(!fring.isEmpty())
     {
         node= (Node)fring.pop();
         //node.setData(first);
         if(node.getData() == HT.goal)
             return "success";
         else if (cloused.contains(node)==false)
         { cloused.add(node);
            fring.push(((Expand(node))));
          }

}
        return "failuer";
    }

package towerofhanoi;
import java.util.*;

/**
 *
 * 
 */
public class Hanoi {

    //state representaion
    public Stack TowerA= new Stack();
    public Stack TowerB= new Stack();
    public Stack TowerC= new Stack();



    public int[] counter = new int[3];
public int[] goal = new int [3];

   LinkedList tower1 =new LinkedList();
   LinkedList tower2 =new LinkedList();
   LinkedList tower3 =new LinkedList();

   //initial state
    public void initial(){

    tower1.addFirst(1);
    tower1.addLast(2);
    tower1.addLast(3);


       counter[0]=3;
       counter[1]=0;
       counter[2]=0;

       goal[0]=0;
       goal[1]=0;
       goal[2]=3;


    }

   

    //successor function
    public Node moveAtoB ()
    {Node node=new Node();
     int y;
   
    if(tower1.isEmpty())
       initial();

    else
   {   y = Integer.parseInt(tower1.removeFirst().toString());

        if(tower2.isEmpty())
        {tower2.addFirst(y);
      System.out.println("Move disc " + y +" from " + "A" + " to " + "B");
      counter[0]-=1;
      counter[1]+=1;
        }
        else
    if (Integer.parseInt(tower2.getFirst().toString())<y)
        
        tower1.addFirst(y);
    else
    {tower2.addFirst(tower1.removeFirst());
        System.out.println("Move disc " + y +" from " + "A" + " to " + "B");
      counter[0]-=1;
      counter[1]+=1; }

     node.setData(counter);
}
     return node;
    }

    
    public Node moveAtoC ()
    {Node node=new Node();
     int y;

     if(tower1.isEmpty())
       initial();

    else
   {
     y=Integer.parseInt(tower1.removeFirst().toString());
        if(tower3.isEmpty())
        { tower3.addLast(tower1.removeFirst());
      System.out.println("Move disc " + y +" from " + "A" + " to " + "C");
      counter[0]-=1;
      counter[2]+=1;}
        else
    if (Integer.parseInt(tower3.getFirst().toString())<y)
       tower1.addFirst(y);
    else
    {tower3.addFirst(tower1.removeFirst());
        System.out.println("Move disc " + y +" from " + "A" + " to " + "C");
      counter[0]-=1;
      counter[2]+=1;}

     node.setData(counter);
    }
     return node;
    }
    public Node moveBtoA ()
    {   Node node=new Node();
        int y=Integer.parseInt(tower2.removeFirst().toString());
        if(tower1.isEmpty())
        { tower1.addLast(tower2.removeFirst());
      System.out.println("Move disc " + y +" from " + "B" + " to " + "A");
      counter[1]-=1;
      counter[0]+=1;}
        else
    if (Integer.parseInt(tower1.getFirst().toString())<y)
       tower2.addFirst(y);
    else
    {tower1.addFirst(tower2.removeFirst());
        System.out.println("Move disc " + y +" from " + "B" + " to " + "A");
      counter[1]-=1;
      counter[0]+=1;}

    node.setData(counter);
     return node;
    }

    public Node moveBtoC ()
    {   Node node=new Node();
        int y=Integer.parseInt(tower2.removeFirst().toString());
        if(tower3.isEmpty())
        { tower3.addLast(tower2.removeFirst());
      System.out.println("Move disc " + y +" from " + "B" + " to " + "C");
      counter[1]-=1;
      counter[2]+=1;}
        else
    if (Integer.parseInt(tower3.getFirst().toString())<y)
       tower2.addFirst(y);
    else
    {tower3.addFirst(tower2.removeFirst());
        System.out.println("Move disc " + y +" from " + "B" + " to " + "C");
      counter[1]-=1;
      counter[2]+=1;}

    node.setData(counter);
     return node;
    }

    public Node moveCtoA ()
    {   Node node=new Node();
        int y=Integer.parseInt(tower3.removeFirst().toString());
        if(tower1.isEmpty())
        { tower1.addLast(tower3.removeFirst());
      System.out.println("Move disc " + y +" from " + "C" + " to " + "A");
      counter[2]-=1;
      counter[0]+=1;}
        else
    if (Integer.parseInt(tower1.getFirst().toString())<y)
       tower3.addFirst(y);
    else
    {tower1.addFirst(tower3.removeFirst());
        System.out.println("Move disc " + y +" from " + "C" + " to " + "A");
    counter[2]-=1;
    counter[0]+=1;}

    node.setData(counter);
     return node;
    }
    
    public Node moveCtoB ()
    {   Node node=new Node();
        int y=Integer.parseInt(tower3.removeFirst().toString());
        if(tower2.isEmpty())
        { tower2.addLast(tower3.removeFirst());
      System.out.println("Move disc " + y +" from " + "C" + " to " + "B");
      counter[2]-=1;
      counter[1]+=1;}
        else
    if (Integer.parseInt(tower2.getFirst().toString())<y)
       tower3.addFirst(y);
    else
    {tower2.addFirst(tower3.removeFirst());
        System.out.println("Move disc " + y +" from " + "C" + " to " + "B");
    counter[2]-=1;
    counter[1]+=1;}

      node.setData(counter);
     return node;
    }


}


package towerofhanoi;

/**
 *
 * 
 */
public class Node {
private int[] data;
private Node left;
private Node right;

public Node ()
{data=null;}
public Node (int[] newData)
{this(newData,null,null);}

public Node (int[] newData,Node leftChild, Node rightChild )
{ data=newData;
 leftChild=left;
 right=rightChild;
}
public void setData(int[] newData)
{data = newData;}
public int[] getData()
{return data;}

public void setleftChild(Node newleft)
{left = newleft;}
public void setrightChild(Node newleft)
{right = newleft;}
public Node getleftChild()
{return left;}
public Node getrightChild()
{return right;}



}



Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Code of hanoi tower in java

#2 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: Code of hanoi tower in java

Posted 02 June 2010 - 02:42 PM

Well your getting an java.lang.NullPointerException exception because somewhere you have an Object that is pointing to null.

What line is the error at?

Looking at the code also, this problem is best solved using Recursion, I seriously do advise you to have a look at the your code again

Its actually difficult to solve a problem such as Towers of Hanoi
without the use of recursion.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12229
  • View blog
  • Posts: 45,302
  • Joined: 27-December 08

Re: Code of hanoi tower in java

Posted 02 June 2010 - 02:43 PM

Moved out of Java Programmers to Java Help. Please reserve Java Programmers for topical discussion rather than Help/Homework/Academic Questions. :)

Also, where are you getting the error? Can you show us the Stack trace, including pointing out the offending line?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Code of hanoi tower in java

Posted 02 June 2010 - 04:00 PM

Hanoi Tower are usely used to show recursion.
You need to be masochist to try to do it without recursion, it implies a lot of dirty hardcoding in your code.
Anyhow the BinaryTree class is missing from your posting. Even if I have a good idea of what that class should do I don't think it worthes investigating into your problem until you post that class.

Post it and I'll see what I can do.
Was This Post Helpful? 0
  • +
  • -

#5 very nice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 11-December 08

Re: Code of hanoi tower in java

Posted 03 June 2010 - 03:32 AM

BinaryTree Class


package towerofhanoi;

/**
 *
 * 
 */
public class BinaryTree {

    public Node root;
    BinaryTree left;
    BinaryTree right;
    public BinaryTree()
    {root=null;}
    public BinaryTree(int[] rootData, BinaryTree left,BinaryTree right)
    {privateSetTree(rootData,left,right);}
    public void SetTree(int[] rootData)
    {root= new Node(rootData);
    }
    public void setTree(int[] rootData, BinaryTree leftTree, BinaryTree rightTree)
    {privateSetTree(rootData,leftTree,rightTree);
    }
public BinaryTree getleft()
{return left;}
  private void privateSetTree(int[] rootData, BinaryTree leftTree, BinaryTree rightTree)
  {root= new Node(rootData);
   if(leftTree !=null)
       root.setleftChild(leftTree.root);
   if(rightTree !=null)
       root.setrightChild(rightTree.root);

  }
  }



Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

This post has been edited by PsychoCoder: 03 June 2010 - 08:28 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1