This thread reminds me of this one (on theprogrammingsite) . . . http://board.theprogrammingsite.com/...er=asc&start=0
I get one warning when I compile your code, on this line:
Code:
start->nBoard[3][3] = NULL;
Code:
107 C:\dwk\c\ttttemp.cpp [Warning] converting to non-pointer type `int' from NULL
I'm guessing you want to set each element in start->nBoard[][] to be set to zero. In that case, try memset:
Code:
memset(start->nBoard, 0, 3*3*sizeof(int));
Erhm, what's this for?
Code:
//Create First Level
int cDepth=1;
addNodes(start, cDepth);
copyParentBoard(start);
cDepth++;
If you want the variable to retain its value between function calls, pass it to the function or make the variable static.
Here's what's causing this segmentation fault. displayPly() calls displayBoard() with an nBoard that has not been initialized.
Code:
//Display The Board Function
void displayBoard(int board[3][3])
{
//Display The Board Function
char piece[]="_XO";
cout<<"\n\n";
cout<<"\t 1 2 3\n";
cout<<"\t _ _ _\n";
for(int c=0; c<3; c++)
{
cout<<"\t";
cout<<(c+1)<<" ";
for(int r=0; r<3; r++)
{
cout<<"|";
cout<<piece[(board[c][r])];
cout<<"|";
}
cout<<"\n";
}
}
// ...
void displayPly(node *parent)
{
cout<<"Parent Node Information:\n";
cout<<"Depth: "<<parent->depth<<"\n";
displayBoard(parent->nBoard);
cout<<"\n\n";
cout<<"Children Nodes:\n";
for(int dn=0; dn<9; dn++)
{
cout<<"Child Node "<<dn<<":\n";
cout<<"Depth: ";
cout<<parent->link[dn]->depth;
displayBoard(parent->link[dn]->nBoard);
cin.ignore();
}
}
In addNodes(), you need to have the memset call I mentioned above.
Code:
void addNodes(node *parent, int cDepth)
{
//Add the 9 Nodes to the parent
node *nNode[9];
for(int n=0; n<9; n++)
{
nNode[n] = new node;
nNode[n]->depth = cDepth;
nNode[n]->parent = parent;
parent->link[n] = nNode[n];
}
}
Something like
Code:
memset(nNode[n]->nBoard, 0, 3*3*sizeof(int));
(This segfaults because you try to access an element in piece[] that is far beyond its end -- or even before the beginning because it is a signed int.)
I'm just curious as to why you have this
in your structure when you have a global variable that is much the same thing.
Code:
int board[3][3] = {{EMPTY, EMPTY, EMPTY}, {EMPTY, EMPTY, EMPTY}, {EMPTY, EMPTY, EMPTY}};
That could be initialized with memset BTW or like this:
Code:
int board[3][3] = {{0}};
This works because any initializers left out are automatically set to zero; since you want them to all be zero, you can leave (almost) all of the initalizers out.
Actually, global variables are automatically initialized to zero so you could just do this.