having problem in creating tree
hi to everyone
i want to create a tree with lots of children
i write this code:
- class Tree
- {
- friend class AIClass;
- int v;
- int **board;
- public:
- Tree()
- {
- v = 0;
- int n = 25;
- board=(int**) malloc((n)*sizeof(int*));
- for(int i=0;i<n;i++)
- {
- board[i]=(int*) malloc((n)*sizeof(int));
- for(int j=0;j<n;j++)
- board[i][j]=0;
- }
- }
- Tree(int n)
- {
- v = 0;
- board=(int**) malloc((n)*sizeof(int*));
- for(int i=0;i<n;i++)
- {
- board[i]=(int*) malloc((n)*sizeof(int));
- for(int j=0;j<n;j++)
- board[i][j]=0;
- }
- }
- void setxy(int x,int y,int value)
- {
- board[x][y] = value;
- }
- void setValue(int p)
- {
- v = p;
- }
- };
- {
- public:
- int predict( Tree *item,int depth,int alpha,int beta,bool Player);
- private:
- Tree *root;
- int board[25][25];
- int count;
- int k;
- };
and i wrote this:
- int AIClass::predict(Tree *item,int depth,int alpha,int beta,bool MaxPlayer)
- {
- for(int i=0;i<count;i++)
- {
- for(int j=0;j<count;j++)
- {
- if(item->board[i][j]==0) {
- temp = new Tree(count);
- for(int k=0;k<count;k++)
- for(int h=0;h<count;h++)
- {
- temp->setxy(k,h,item->board[k][h]);
- if((i==k)&&(j==h)) temp->setxy(k,h,2);
- }
- item->children->append(*temp);
- }
- }
- }
- for (int i = 0; i < item->children->size(); ++i) {
- int temp = predict(&(item->children->value(i)),depth-1,alpha,beta,!MaxPlayer);
- if(temp > alpha)
- alpha = temp;
- if(beta < alpha) break;
- }
- item->setValue(alpha);
- }
( it is a part of alpha beta pruning algorithm)
but when it is compiled,the children does’nt get its value,after calling predict function,and again the v value in Tree class is 0 and does’nt get updated value
and i get this warning message:(i think the problem is):
- ..\dooz\src\AIClass.cpp: In member function 'int AIClass::predict(Tree*, int, int, int, bool)':
- ..\dooz\src\AIClass.cpp:98: warning: taking address of temporary
what do you think?
2 replies
What line in your posted code does line AIClass.cpp:98 translate to?
And where does temp[m] come from in line 8 of AIClass::predict? Why do you redefine it to an int in line 21? And is temp[m] memory leaked (you deep-copy by value when appending * temp[m] to the list. Maybe you want a QList<Tree*> instead)?
Is children memory leaked? Where’s the dtor deleting children?
Why do you mix malloc and new?
You must log in to post a reply. Not a member yet? Register here!
