Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 19th, 2007, 4:17 PM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
more problems with copying from classes

alright i switched it a bit since i know im only going to be having 4 chidlren i just made 4 things. But now i am having the same problem with these as i was with the int [][] array, the only difference is this time, it really doesnt make sense cause they are all seperate, so why would the upChild point to the same place as the leftChild, when there is already 2 different spots. So now the GridNode class has 5 sections,

state | leftChild | upChild | rightChild | downChild

I need to find a way to create all the other nodes, o and now i am storing the nodes in a linked list, so then to do through the tree all i do is go through the linked list and then to find one of the children to one of them all i do is call that persons get whatever child i want method, well except for the copying problems that exist with those as well.

import java.io.*;
public class GridNode
{
  private int[][] state;
  private GridNode leftChild;
  private GridNode upChild;
  private GridNode rightChild;
  private GridNode downChild;
  
  // set state to newState and firstChild to newFirstChild and nextSibling to newNextSibling
   public GridNode(int[][] newState, GridNode newLeftChild, GridNode newUpChild, GridNode newRightChild, GridNode newDownChild)
   {
      setState(newState);
      setLeftChild(newLeftChild);
      setUpChild(newUpChild);
      setRightChild(newRightChild);
      setDownChild(newDownChild);
   } // end GridNode constructor
   
   // copy one state to another
   public static int[][] copy(int[][] a)
   {
      int[][] b = new int[a[0].length][a.length];
      for (int i = 0; i < a.length; i++)
          for (int j = 0; j < a[0].length; j++)
              b[i][j] = a[i][j];
     
      return b;
} // end copy
   
   public static GridNode copy(GridNode a)
   {
      GridNode b = a;
      return b;
   } // end copy
   
   // get current state
   public int[][] getState()
   {
      return state;
   } // end getState
   
   public void setState(int[][] newState)
   {
      state = newState;
   } // end setState
   
   // set leftChild
   public void setLeftChild(GridNode newLeftChild)
   {
      leftChild = newLeftChild;
   } // end setLeftChild
   
   // set UpChild
   public void setUpChild(GridNode newUpChild)
   {
      upChild = newUpChild;
   } // end setUpChild
   
   // set right child
   public void setRightChild(GridNode newRightChild)
   {
      rightChild = newRightChild;
   } // end setRightChild
   
   // set down child
   public void setDownChild(GridNode newDownChild)
   {
       downChild = newDownChild;
   } // end setDownChild;
   
   // get leftChild
   public GridNode getLeftChild()
   {
      return leftChild;
   } // end getLeftCHild
   
   // getUpChild
   public GridNode getUpChild()
   {
       return upChild;
   } // end getUpChild
  
   // get right child
   public GridNode getRightChild()
   {
      return rightChild;
   } // getRightChild;
   
   public GridNode getDownChild()
   {
      return downChild;
   } // end getDownChild
   
   // print the current state
   public void printNode()
   {
      for(int row = 0; row < 3; row++)
      {
        for(int col = 0; col < 3; col++)
          System.out.print(state[row][col] + "   ");
        
        System.out.println();
      } // end for
    } // end printNode
   
   // check to see if current state is equal to the goal state
   public boolean isEqual(int[][] state, int[][] goalState)
   {
      for (int row = 0; row < 3; row++)
      {
         for (int col = 0; col < 3; col++)
         {
            if (state[row][col] == goalState[row][col])
               continue;
            
            else
             return false;
         } // end for
      }  // end for
      return true;
   } // end isEqual
   
   // calculates h1(n)
   public int findHone(int[][] state, int[][] goalState)
   {
      int hOne = 0;
      for (int row = 0; row < 3; row++)
      {
         for (int col = 0; col < 3; col++)
         {
            if (state[row][col] != goalState[row][col] && state[row][col] != 0)
               hOne++;
            
            else
             continue;
         } // end for
      }  // end for
      return hOne;
   } // end findHone
   
   // find f(n) = g(n) + h(n)
   public int findF(int g, int h)
   {
      return g + h;
   } // end findF
   
   // check to see if it can move left
   public boolean canMoveLeft(int[][] state)
   {
      int tempPos = -1;
      for (int row = 0; row < 3; row++)
      {
         for (int col = 0; col < 3; col++)
         {
             if (state[row][col] == 0)
                tempPos = col;
         } // end for
      } // end for
      
      if (tempPos == 0)
        return false;
      else
       return true;
   } // end canMoveLeft
   
   // check to see if it can move up
   public boolean canMoveUp(int[][] state)
   {
      int tempPos = -1;
      for (int row = 0; row < 3; row++)
      {
         for (int col = 0; col < 3; col++)
         {
             if (state[row][col] == 0)
                tempPos = row;
         } // end for
      } // end for
      
      if (tempPos == 0)
        return false;
      else
       return true;
   } // end canMoveUp
   
   // check to see if it can move right
   public boolean canMoveRight(int[][] state)
   {
      int tempPos = -1;
      for (int row = 0; row < 3; row++)
      {
         for (int col = 0; col < 3; col++)
         {
             if (state[row][col] == 0)
                tempPos = col;
         } // end for
      } // end for
      
      if (tempPos == 2)
        return false;
      else
       return true;
   } // end canMoveRight
   
   // check to see if it can move down
   public boolean canMoveDown(int[][] state)
   {
      int tempPos = -1;
      for (int row = 0; row < 3; row++)
      {
         for (int col = 0; col < 3; col++)
         {
             if (state[row][col] == 0)
                tempPos = row;
         } // end for
      } // end for
      
      if (tempPos == 2)
        return false;
      else
       return true;
   } // end canMoveDown
   
    // move it left, the old one
   public void moveLeft(int[][] state)
   {
      for (int row = 0; row < 3; row++)
      {
         for (int col = 0; col < 3; col++)
         {
             if (state[row][col] == 0)
             {
                  state[row][col] = state[row][col-1];
                  state[row][col-1] = 0;
                  //printNode();
                  return;
             } // end if
         } // end for
      } // end for
   } // end moveLeft
   
   
   // move it Up
   public void moveUp(int[][] state)
   {
      for (int row = 0; row < 3; row++)
      {
         for (int col = 0; col < 3; col++)
         {
             if (state[row][col] == 0)
             {
                  state[row][col] = state[row-1][col];
                  state[row-1][col] = 0;
                 // printNode(state);
                  return;
             } // end if
         } // end for
      } // end for
   } // end moveUp
   
    // move it right
   public void moveRight(int[][] state)
   {
      for (int row = 0; row < 3; row++)
      {
         for (int col = 0; col < 3; col++)
         {
             if (state[row][col] == 0)
             {
                   state[row][col] = state[row][col+1];
                   state[row][col+1] = 0;
                  // printNode(state);
                   return;
             } // end if
         } // end for
      } // end for
   } // end moveRight
   
    // move it down
   public void moveDown(int[][] state)
   {
      //int tempPos = -1;
      for (int row = 0; row < 3; row++)
      {
         for (int col = 0; col < 3; col++)
         {
             if (state[row][col] == 0)
             {
                 state[row][col] = state[row+1][col];
                 state[row+1][col] = 0;
                // printNode(state);
                 return;
             } // end if
         } // end for
      } // end for
   } // end moveDown
   
} // end GridNode

import java.util.*;
public class NewMain
{
   private static int level;
   private static List tree = new LinkedList();
   
   private static int[][] goalState = {{7, 6, 4},
                        {8, 5, 2},
                        {1, 0, 3}
                       };
   
   
   // insert root and check if its goal
   static void insertRoot(GridNode root)
   {
     tree.add(0, root);
   } // end insertRoot
   
   // find smallest f(n)
   static int findF(List tree)
   {
      GridNode temp;
      int lowestF = 0;
      int tempLowestF = 0;
      int i = 0;
      while (i < tree.size())
      {
         temp = (GridNode) tree.get(i);
         tempLowestF = temp.findF(level, temp.findHone(temp.getState(), goalState)); 
          
         if (tempLowestF < lowestF)
           lowestF = tempLowestF;
         i++;
      }
      
      return lowestF;
   } // end findF
   
   // insert children
   static void insertChild(GridNode parent, GridNode child)
   {
      if (parent.canMoveLeft(parent.getState()))
      {
         child.setState(parent.copy(parent.getState()));
         child.moveLeft(child.getState());
         tree.add(child);
         parent.setLeftChild(parent.copy(child));
         System.out.println("The child is");
         child.printNode();
         System.out.println();
         System.out.println("the parent is");
         parent.printNode();
         level++;
      }  // end if
      
      
      if (parent.canMoveUp(parent.getState()))
      {
         child.setState(parent.copy(parent.getState()));
         child.moveUp(child.getState());
         tree.add(child);
         parent.setUpChild(child);
         System.out.println("The child is");
         child.printNode();
         System.out.println();
         System.out.println("the parent is");
         parent.printNode();
         level++;
      } // end else if
      
      else if (parent.canMoveRight(parent.getState()))
      {
         child.setState(parent.copy(parent.getState()));
         child.moveRight(child.getState());
         tree.add(child);
         parent.setRightChild(child);
          System.out.println("The child is");
         child.printNode();
         System.out.println();
         System.out.println("the parent is");
         parent.printNode();
         level++;
      } // end else if
      
      else if (parent.canMoveDown(parent.getState()))
      {
         child.setState(parent.copy(parent.getState()));
         child.moveDown(child.getState());
         tree.add(child);
         parent.setDownChild(child);
         System.out.println("The child is");
         child.printNode();
         System.out.println();
         System.out.println("the parent is");
         parent.printNode();
         level++;
      } // end else if
   } // end insertChild
   
   public static void main(String[] args)
   {
       int[][] initialState = {{7, 6, 4},
                             {8, 5, 2},
                             {1, 0, 3}
                            };
      
      GridNode parent = new GridNode(initialState, null, null, null, null);
      GridNode child = new GridNode(null, null, null, null, null);
      insertChild(parent, child);
      parent.getLeftChild().printNode();
      //insertChild(parent.getLeftChild(), child);
       //System.out.println(findF(tree));
      // insertChild(parent, child);
       //insertSibling(parent, child);
   } // end main
 
} // end NewMain
cwl157 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Could some please explain classes to me... TCStyle C++ 10 Feb 20th, 2006 3:51 PM
What is Lightweight C++? difference in inner classes between C++ and Java? jonyzz C++ 4 Sep 2nd, 2005 9:56 AM
List of classes Dark_Shinobi C++ 8 May 23rd, 2005 3:50 PM
Classes Sane Python 8 Apr 29th, 2005 12:50 PM
C++ Classes - Quick question(s) Delete C++ 8 Apr 7th, 2005 7:38 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:33 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC