![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 15
Rep Power: 0
![]() |
Strange Problem with List
I am creating a program that is generating a decision tree for a Tic Tac Toe game. I'm using and n-ary tree with nodes. Each node has a parent and a list of children defined as: List<Node> children = new ArrayList<Node>();
I know that there is nothing wrong with my tree, node, and board class. Board is just a class I made up to store the tic tac toe boards, its just a double array or strings. My problem comes in where I am generating the children for a parent. Here is the code I am using: public static void buildChildren(Node parent)
{
int i,j,k=0,l;
Node temp = new Node();
Board tempBoard = new Board();
Iterator<Node> chIter;
for(i=0; i < 3; i++)
{
for(j=0; j < 3; j++)
{
tempBoard.copyBoard((Board)parent.getData());
if(tempBoard.get(i,j).equals("_"))
{
tempBoard.fill(i,j); temp = new Node(tempBoard);
temp.setParent(parent);
parent.addChild(temp);
//System.out.println(dTree.toString());
}
}
}
}The problem is in the list that contains all the children. This list is filled with replicas of the last iteration. For example the first child should have X at [0,0], second child X at [0,1] etc. until the last child has X at [2,2]. But instead the list is full of the correct number of children but they are all the last one (X at [2,2]) in this case. Further testing revealed that at each iteration the list of children is filled with copies of the lastest iteration. So that at the end the list is filled with copies of the last iteration. I have tried everything and every sort of testing I can think of and I can't figure this one out. If anyone can PLEASE help it would be greatly appreciated!!! |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Apr 2005
Posts: 15
Rep Power: 0
![]() |
Re: Strange Problem with List
I realized now that the issue is a memory problem. Instead of creating the actual Nodes and Boards I'm just creating references to other Nodes and Boards so when those change they all change. Does anyone know a solution for this?
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Linked List problem - Deleting a node | Brent | C++ | 9 | Dec 16th, 2007 3:39 PM |
| strange problem | brad sue | C++ | 3 | Sep 1st, 2006 8:50 AM |
| Linked List problem | Berto | C | 3 | Apr 4th, 2005 6:24 AM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 4:12 PM |
| string problem when passing in linked list | quantz | C++ | 0 | Feb 27th, 2005 10:11 AM |